如何使用tf.metrics.accuracy? [英] How to use tf.metrics.accuracy?
问题描述
我想使用 tf.metrics.accuracy
跟踪我的预测的准确性,但是我不确定如何使用函数返回的update_op(下面的 acc_update_op
) :
accuracy, acc_update_op = tf.metrics.accuracy(labels, predictions)
我当时想将其添加到 tf.GraphKeys.UPDATE_OPS
会很有意义,但是我不确定如何执行此操作。
I was thinking that adding it to tf.GraphKeys.UPDATE_OPS
would make sense, but I am not sure how to do this.
推荐答案
tf.metrics.accuracy
是许多流式度量TensorFlow操作之一(其中另一个是 tf.metrics.recall
)。创建后,将创建两个变量( count
和 total
)以累积一个最终结果的所有传入结果。返回的第一个值是计算 count / total
的张量。返回的第二个操作是有状态函数,它将更新这些变量。当评估多批数据上的分类器性能时,流式度量功能非常有用。使用的一个简短示例:
tf.metrics.accuracy
is one of the many streamed metric TensorFlow operations (another one of which is tf.metrics.recall
). Upon creation, two variables (count
and total
) are created in order to accumulate all incoming results for one final outcome. The first returned value is a tensor for the calculation count / total
. The second op returned is a stateful function which updates these variables. Streamed metric functions are useful when evaluating the performance of a classifier over multiple batches of data. A quick example of use:
# building phase
with tf.name_scope("streaming"):
accuracy, acc_update_op = tf.metrics.accuracy(labels, predictions)
test_fetches = {
'accuracy': accuracy,
'acc_op': acc_update_op
}
# when testing the classifier
with tf.name_scope("streaming"):
# clear counters for a fresh evaluation
sess.run(tf.local_variables_initializer())
for _i in range(n_batches_in_test):
fd = get_test_batch()
outputs = sess.run(test_fetches, feed_dict=fd)
print("Accuracy:", outputs['accuracy'])
我当时想将其添加到
tf.GraphKeys.UPDATE_OPS
会很有意义,但是我不确定如何做到这一点。
I was thinking that adding it to
tf.GraphKeys.UPDATE_OPS
would make sense, but I am not sure how to do this.
除非您仅将UPDATE_OPS集合用于测试目的,否则这不是一个好主意s。通常,集合在训练阶段已经具有某些控制操作(例如,移动批次归一化参数),这些控制操作不打算与验证阶段一起运行。最好将它们保留在新集合中,或者将这些操作手动添加到提取字典中。
That would not be a good idea unless you are only using the UPDATE_OPS collection for testing purposes. Usually, the collection will already have certain control operations for the training phase (such as moving batch normalization parameters) that are not meant to be run alongside the validation phase. It may be best to either keep them in a new collection or add these operations to the fetch dictionary manually.
这篇关于如何使用tf.metrics.accuracy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!