1

My model is like

print('Build main model...')
model = Sequential()
model.add(Merge([left, right], mode='sum'))
model.add(Dense(14, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])            

when I use model.evaluate([xtest1, xtest2], y_test), I get an accuracy of 90% but when I use model.predict_classes([x_test1, x_xtest2]), I get totally wrong class labels, going by which my accuracy drops significantly. What is the difference in model.evaluate and model.predict_classes schema? Where am I making the mistake?

desertnaut
  • 57,590
  • 26
  • 140
  • 166

1 Answers1

4

Since you ask for loss='binary_cross_entropy' and metric=['accuracy'] in your model compilation, Keras infers that you are interested in the binary accuracy, and this is what it returns in model.evaluate(); in fact, since you have 14 classes, you are actually interested in the categorical accuracy, which is the one reported via model.predict_classes().

So, you should change the loss function in your model compilation to categorical_crossentropy:

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 

If, for whatever reason, you want to stick with loss='binary_crossentropy' (admittedly it would be a very unusual choice) , you should change the model compilation to clarify that you want the categorical accuracy as follows:

from keras.metrics import categorical_accuracy
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[categorical_accuracy])

In either of these cases, you will find that the accuracies reported by model.evaluate() and model.predict_classes() are the same, as they should be.

For a more detailed explanation and an example using the MNIST data, see my answer here.

desertnaut
  • 57,590
  • 26
  • 140
  • 166