If you are just after the topK you could always call tensorflow directly (you don't say which backend you are using).
from keras import backend as K
import tensorflow as tf
top_values, top_indices = K.get_session().run(tf.nn.top_k(_pred_test, k=5))
If you want an accuracy metric you can add it to your model 'top_k_categorical_accuracy'.
model.compile('adam', 'categorical_crossentropy', ['accuracy', 'top_k_categorical_accuracy'])
history = model.fit(X_train, y_train, nb_epoch=3, validation_split=0.2)
Train on 31367 samples, validate on 7842 samples
Epoch 1/3
31367/31367 [==============================] - 6s - loss: 0.0818 - acc: 0.9765 - top_k_categorical_accuracy: 0.9996 -
...
The default k
for this metric is 5 but if you wanted to change that to say 3 you would set up your model like this:
top3_acc = functools.partial(keras.metrics.top_k_categorical_accuracy, k=3)
top3_acc.__name__ = 'top3_acc'
model.compile('adam', 'categorical_crossentropy', ['accuracy', top3_acc])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…