首页 > 解决方案 > 在 TensorFlow 中,update_op 返回变量在precision_at_k 度量中是什么意思?

问题描述

我有以下代码:

>>> rel = tf.constant([[1, 5, 10]], tf.int64) # Relevant items for a user
>>> rec = tf.constant([[7, 5, 10, 6, 3, 1, 8, 12, 31, 88]], tf.int64) # Recommendations
>>>
>>> metric = tf.metrics.precision_at_k(rel, rec, 10)
>>>
>>> sess = tf.Session()
>>> sess.run(tf.local_variables_initializer())
>>> precision, update = sess.run(metric)
>>> precision
0.2
>>> update
0.2

因此,精度计算如下:

# of relevant items recommended to the user / # of recommendations

我的问题是,update_op函数返回的变量是什么?

标签: tensorflow

解决方案


下面是详细说明两者如何从tf.metrics.precision_at_k工作中返回的示例。

rel = tf.placeholder(tf.int64, [1,3])
rec = tf.constant([[7, 5, 10, 6, 3, 1, 8, 12, 31, 88]], tf.int64) 
precision, update_op = tf.metrics.precision_at_k(rel, rec, 10)

sess = tf.Session()
sess.run(tf.local_variables_initializer())

stream_vars = [i for i in tf.local_variables()]
#Get the local variables true_positive and false_positive

print(sess.run(precision, {rel:[[1,5,10]]})) # nan
#tf.metrics.precision maintains two variables true_positives 
#and  false_positives, each starts at zero.
#so the output at this step is 'nan'

print(sess.run(update_op, {rel:[[1,5,10]]})) #0.2
#when the update_op is called, it updates true_positives 
#and false_positives using labels and predictions.

print(sess.run(stream_vars)) #[2.0, 8.0]
# Get true positive rate and false positive rate

print(sess.run(precision,{rel:[[1,10,15]]})) # 0.2
#So calling precision will use true_positives and false_positives and outputs 0.2

print(sess.run(update_op,{rel:[[1,10,15]]})) #0.15
#the update_op updates the values to the new calculated value 0.15.

print(sess.run(stream_vars)) #[3.0, 17.0]

update_op在每批中运行,并且仅在您想要检查您评估的精度时运行precision


推荐阅读