在TensorFlow中,update_op返回变量在precision_at_k度量中的含义是什么?

Dim*_*los 0 tensorflow

我有以下代码:

>>> 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
Run Code Online (Sandbox Code Playgroud)

因此,精度计算如下:

# of relevant items recommended to the user / # of recommendations
Run Code Online (Sandbox Code Playgroud)

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

vij*_*y m 6

下面是详细说明两者如何从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]
Run Code Online (Sandbox Code Playgroud)

您可以update_op在每个批次中运行,并且仅在您要检查评估的精度时运行precision.