tensorflow - 在 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
函数返回的变量是什么?
解决方案
下面是详细说明两者如何从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
。
推荐阅读
- javascript - 使用 jquery 或 javascript 获取 Viddler 视频 END 的方法有哪些?
- python - 如何仅从字典中获取平均身高
- javascript - 为什么 addEventListener 不能使用 id 属性
- kubernetes - 如何使用更多副本更新 K8s 部署
- reactjs - 试图通过反应设置 nodemailer 得到 ERR_TOO_MANY_REDIRECTS 和 createError.js:16 Uncaught (in promise) 错误:网络错误
- javascript - NodeJS“fork”子进程正在执行,但在退出时没有给父进程任何指示
- javascript - 使用innerHTML在javascript中添加选项元素的问题
- mongodb - 将 Qlik Sense 连接到 MongoDB 时出现问题
- grails - RabbitMQ 在我的队列中写入错误的字符
- c++ - Visual Studio 不会将更改 VC++ 保存到资源文件