如何计算喀拉拉邦的top5准确性? [英] How to calculate top5 accuracy in keras?

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

问题描述

我想在imagenet2012数据集中计算top5,但我不知道如何在keras中进行. 拟合函数只能计算出前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])

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

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