Tensorflow多标签准确性度量

问题描述

我正在处理11种可能的标签标签问题。

y_true和y_pred是形状为[batch_size,11]的张量,其中可能的标签表示为0和1,例如:

每个示例,batch_size = 4

put_along_axis

我一直在搜索,但是我有很多问题,我想知道3种准确性:

  1. “准确度”,我的意思是如果正确的标签是[class1,class2,class3,...,class11]。

      y_true=[[0,1,1],[0,0]]
    
       y_pred= [[0,0],[1,0]]
    
  2. 批处理中每个元素的准确度平均值,例如:

    y_true_batch_element_1 = [0,1]

    y_pred_batch_element_1 = [0,0]

    acc_element_1 = 10/11

然后:

 y_true_class1=[0,0]  
 y_pred_class1=[0,1]
 acc_class1=2/4
  1. 所有类都必须正确预测,在这种情况下,我们将收到:

    mean(acc_element_1,acc_element_2,...,acc_element_n) (element2是唯一一个完全 对)`

我如何通过TensorFlow获得这些精度中的每一个

解决方法

这是您拥有的:

import tensorflow as tf

y_true = tf.convert_to_tensor([[0,1,1],[0,0]])

y_pred = tf.convert_to_tensor([[0,0],[1,0]])

这是您的第一个和第二个指标:

tf.divide(tf.reduce_sum(
    tf.cast(
        tf.equal(y_true,y_pred),tf.int32),axis=1),tf.shape(y_true)[1])
<tf.Tensor: shape=(4,),dtype=float64,numpy=array([0.90909091,1.,0.90909091,0.81818182])>

这是您的第三个指标:

tf.reduce_mean(tf.cast(tf.reduce_all(tf.equal(y_true,tf.float32),axis=0)
<tf.Tensor: shape=(),dtype=float32,numpy=0.25>

我希望我做对了。您的两个第一对我来说都一样。