问题描述
y_true和y_pred是形状为[batch_size,11]的张量,其中可能的标签表示为0和1,例如:
每个示例,batch_size = 4
put_along_axis
-
“准确度”,我的意思是如果正确的标签是[class1,class2,class3,...,class11]。
y_true=[[0,1,1],[0,0]] y_pred= [[0,0],[1,0]]
-
批处理中每个元素的准确度平均值,例如:
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
我如何通过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>
我希望我做对了。您的两个第一对我来说都一样。