Python tensorflow中不同元素数量的标签和预测的精度和召回评估指标
我在Tensorflow中将精度和召回率注册为评估指标时遇到问题。我的标签和预测没有相同数量的元素,所以我不能使用已经内置的函数。我有计算精度和召回率的功能,但我似乎无法获得精度和召回率。我能从标签、预测和前面提到的计算精度和召回率的功能中得到什么?谢谢以下是一个简单的示例,说明如何构建自己的度量标准。我将演示Python tensorflow中不同元素数量的标签和预测的精度和召回评估指标,python,tensorflow,Python,Tensorflow,我在Tensorflow中将精度和召回率注册为评估指标时遇到问题。我的标签和预测没有相同数量的元素,所以我不能使用已经内置的函数。我有计算精度和召回率的功能,但我似乎无法获得精度和召回率。我能从标签、预测和前面提到的计算精度和召回率的功能中得到什么?谢谢以下是一个简单的示例,说明如何构建自己的度量标准。我将演示的意思是,您也应该能够适应上述情况 def mean_metrics(values): """ For mean, there are two variables that are
的意思是,您也应该能够适应上述情况
def mean_metrics(values):
""" For mean, there are two variables that are
required to hold the sum and the total number of variables"""
# total sum
total = tf.Variable(initial_value=0., dtype=tf.float32, name='total')
# total count
count = tf.Variable(initial_value=0., dtype=tf.float32, name='count')
# Update total op by updating total with the sum of the values
update_total_op = tf.assign_add(total, tf.cast(tf.reduce_sum(values), tf.float32))
# Update count op by updating the total size of the values
update_count_op = tf.assign_add(count, tf.cast(tf.size(tf.squeeze(values)), tf.float32))
# Mean
mean = tf.div(total, count, 'value')
# Mean update op
update_op = tf.div(update_total_op, update_count_op, 'value')
return mean, update_op
测试上述代码:
tf.reset_default_graph()
values = tf.placeholder(tf.float32, shape=[None])
mean, mean_op = mean_metrics(values)
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run([mean, mean_op], {values:[1.,2.,3.]}))
print(sess.run([mean, mean_op], {values:[4.,5.,6.]}))
#output
#[nan, 2.0]
#[2.0, 3.5]