如何计算keras中的top5准确度? [英] How to calculate top5 accuracy in keras?

查看:35
本文介绍了如何计算keras中的top5准确度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在imagenet2012数据集中计算top5,但是我不知道如何在keras中计算.fit函数只能计算top 1的准确率.

I want to calculate top5 in imagenet2012 dataset, but i don't know how to do it in keras. fit function just can calculate top 1 accuracy.

推荐答案

如果您刚好在 topK 之后,您可以随时直接调用 tensorflow(您不说您使用的是哪个后端).

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))

如果您需要准确度指标,可以将其添加到您的模型中'top_k_categorical_accuracy'.>

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 - 
...

该指标的默认 k 为 5,但如果您想将其更改为 3,您可以像这样设置模型:

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])

这篇关于如何计算keras中的top5准确度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆