TensorFlow: Is there a metric to calculate and update top k accuracy?

為{幸葍}努か 提交于 2019-12-04 07:32:42

You could replace your use of tf.contrib.metrics.streaming_accuracy by the lower-level tf.metrics.mean, which is by the way ultimately used by streaming_accuracy -- you will find a similarity in their respective documentations.

E.g. (not tested)

tf.metrics.mean(tf.nn.in_top_k(predictions=predictions, targets=labels, k=5))

For top-k accuracy per batch, this also works.

k_val=3
accs = []
for each_bach in range(batch_size):
    acc = tf.keras.metrics.top_k_categorical_accuracy(y_true=tf_class1[each_bach], y_pred=tf_class2[each_bach], k=k_val)
    accs.append(acc)

acc_data_per_batch = tf.reduce_mean(accs)

tf.keras.metrics.top_k_categorical_accuracy returns K.mean( nn.in_top_k(y_pred, math_ops.argmax(y_true, axis=-1), k), axis=-1) per batch

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