update_op返回变量在TensorFlow中的精度度量是什么意思?

update_op返回变量在TensorFlow中的精度度量是什么意思?,tensorflow,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(r

我有以下代码:

>>> 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\u op
变量是什么?

来自文档:

返回:

  • 精度
    :值为的标量浮点64张量
    true\u正值
    除以
    true\u正值之和
    假阳性
  • update\u op
    :递增
    true\u正值的操作
    假阳性
    变量,且其值匹配 精确性
从文件:

返回:

  • 精度
    :值为的标量浮点64张量
    true\u正值
    除以
    true\u正值之和
    假阳性
  • update\u op
    :递增
    true\u正值的操作
    假阳性
    变量,且其值匹配 精确性

下面的示例详细说明了这两个从
tf.metrics.precision\u at_k
返回的函数是如何工作的

rel=tf.placeholder(tf.int64[1,3])
rec=tf.常数([[7,5,10,6,3,1,8,12,31,88]],tf.int64)
精度,更新=tf.metrics.precision(相对,记录,10)
sess=tf.Session()
sess.run(tf.local\u variables\u initializer())
stream_vars=[i代表tf.local_variables()中的i]
#获取局部变量真\正和假\正
打印(sess.run(precision,{rel:[[1,5,10]]}))#nan
#tf.metrics.precision保持两个变量为真正
#和假阳性,每个都从零开始。
#所以这一步的输出是“nan”
打印(sess.run(update_op,{rel:[[1,5,10]}))#0.2
#调用update_op时,它会更新true_正值
#以及使用标签和预测的假阳性。
打印(sess.run(stream_vars))35;[2.0,8.0]
#获得真阳性率和假阳性率
打印(sess.run(精度,{rel:[[1,10,15]]}))#0.2
#因此,调用精度将使用真阳性和假阳性,并输出0.2
打印(sess.run(update_op,{rel:[1,10,15]}))#0.15
#update_op将值更新为新的计算值0.15。
打印(sess.run(stream_vars))35;[3.0,17.0]

您在每批中运行
update\u op
,并且仅当您想要检查精度时,您才评估
precision

下面的示例详细说明了这两个从
tf.metrics.precision\u在k
返回的结果

rel=tf.placeholder(tf.int64[1,3])
rec=tf.常数([[7,5,10,6,3,1,8,12,31,88]],tf.int64)
精度,更新=tf.metrics.precision(相对,记录,10)
sess=tf.Session()
sess.run(tf.local\u variables\u initializer())
stream_vars=[i代表tf.local_variables()中的i]
#获取局部变量真\正和假\正
打印(sess.run(precision,{rel:[[1,5,10]]}))#nan
#tf.metrics.precision保持两个变量为真正
#和假阳性,每个都从零开始。
#所以这一步的输出是“nan”
打印(sess.run(update_op,{rel:[[1,5,10]}))#0.2
#调用update_op时,它会更新true_正值
#以及使用标签和预测的假阳性。
打印(sess.run(stream_vars))35;[2.0,8.0]
#获得真阳性率和假阳性率
打印(sess.run(精度,{rel:[[1,10,15]]}))#0.2
#因此,调用精度将使用真阳性和假阳性,并输出0.2
打印(sess.run(update_op,{rel:[1,10,15]}))#0.15
#update_op将值更新为新的计算值0.15。
打印(sess.run(stream_vars))35;[3.0,17.0]
您在每批中运行
update\u op
,并且仅当您想要检查精度时,才评估
精度

回答注释

import tensorflow as tf

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


k = 10
precision, update_op = tf.metrics.precision_at_k(rel, rec, k)
tmp_rank = tf.nn.top_k(rec, k)

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("[TMP_RANK]: ", sess.run(tmp_rank))
print("[PRECSION_1]: ",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("[UPDATE_OP_1]:",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("[STREAM_VARS_1]:",sess.run(stream_vars)) #[2.0, 8.0]
# Get true positive rate and false positive rate

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

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

print("[STREAM_VARS_2]:",sess.run(stream_vars)) #[3.0, 17.0]
输出为:

[TMP_RANK]:  TopKV2(values=array([[88, 31, 12, 10,  8,  7,  6,  5,  3,  1]]), indices=array([[9, 8, 7, 2, 6, 0, 3, 1, 4, 5]], dtype=int32))
[PRECSION_1]:  nan
[UPDATE_OP_1]: 0.2
[STREAM_VARS_1]: [2.0, 8.0]
[PRECISION_1]: 0.2
[UPDATE_OP_2]: 0.15
[STREAM_VARS_2]: [3.0, 17.0]
注意TMP_等级的指数 Rec代表概率,因此应将rel的值与Rec的指数进行比较。

回答评论

import tensorflow as tf

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


k = 10
precision, update_op = tf.metrics.precision_at_k(rel, rec, k)
tmp_rank = tf.nn.top_k(rec, k)

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("[TMP_RANK]: ", sess.run(tmp_rank))
print("[PRECSION_1]: ",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("[UPDATE_OP_1]:",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("[STREAM_VARS_1]:",sess.run(stream_vars)) #[2.0, 8.0]
# Get true positive rate and false positive rate

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

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

print("[STREAM_VARS_2]:",sess.run(stream_vars)) #[3.0, 17.0]
输出为:

[TMP_RANK]:  TopKV2(values=array([[88, 31, 12, 10,  8,  7,  6,  5,  3,  1]]), indices=array([[9, 8, 7, 2, 6, 0, 3, 1, 4, 5]], dtype=int32))
[PRECSION_1]:  nan
[UPDATE_OP_1]: 0.2
[STREAM_VARS_1]: [2.0, 8.0]
[PRECISION_1]: 0.2
[UPDATE_OP_2]: 0.15
[STREAM_VARS_2]: [3.0, 17.0]
注意TMP_等级的指数
Rec代表概率,因此应将rel的值与Rec的指数进行比较。

当然,但这意味着什么?我搞不懂!正如doc所解释的,场景背后有两个变量,用于计算精度。Tensorflow提供了一个op来增加它们(对流数据有用)。你能详细说明你的问题吗?当然,但这是什么意思?我搞不懂!正如doc所解释的,场景背后有两个变量,用于计算精度。Tensorflow提供了一个op来增加它们(对流数据有用)。你能详细说明你的问题吗?update_op变量是如何计算的?我的意思是,为什么一开始是0.2,后来是0.15?因为输入是不同的。要计算的公式是tp/(tp+fp),我已经添加了代码来获得局部变量tp和fp。但是tp一开始不是=3吗?我的意思是,你为标签(1、5、10)传递的值都包含在建议中。你能帮我做一下上述评论吗?是的,这似乎是一个错误。如何计算update_op变量?我的意思是,为什么一开始是0.2,后来是0.15?因为输入是不同的。要计算的公式是tp/(tp+fp),我已经添加了代码来获得局部变量tp和fp。但是tp一开始不是=3吗?我的意思是,你为标签(1、5、10)传递的值都包含在建议中。你能帮我做一下上述评论吗?是的,这似乎是一个bug。