Getting different results from Keras model.evaluate and model.predict

橙三吉。 提交于 2021-01-23 05:01:24

问题


I have trained a model to predict topic categories using word2vec and an lstm model using keras and got about 98% accuracy during training, I saved the model then loaded it into another file for trying on the test set, I used model.evaluate and model.predict and the results were very different.

I'm using keras with tensorflow as a backend, the model summary is:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (None, 22)                19624     
_________________________________________________________________
dropout_1 (Dropout)          (None, 22)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 40)                920       
_________________________________________________________________
activation_1 (Activation)    (None, 40)                0         
=================================================================
Total params: 20,544
Trainable params: 20,544
Non-trainable params: 0
_________________________________________________________________
None

The code:

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.load_weights(os.path.join('model', 'lstm_model_weights.hdf5'))
score, acc = model.evaluate(x_test, y_test, batch_size=batch_size)

print()
print('Score: %1.4f' % score)
print('Evaluation Accuracy: %1.2f%%' % (acc*100))

predicted = model.predict(x_test, batch_size=batch_size)
acc2 = np.count_nonzero(predicted.argmax(1) == y_test.argmax(1))/y_test.shape[0]
print('Prediction Accuracy: %1.2f%%' % (acc2*100))

The output of this code is

39680/40171 [============================>.] - ETA: 0s  
Score: 0.1192
Evaluation Accuracy: 97.50%
Prediction Accuracy: 9.03%

Can anyone tell me what did I miss?

来源:https://stackoverflow.com/questions/57212021/getting-different-results-from-keras-model-evaluate-and-model-predict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!