Python 3.x 张量流稀疏分类精度度量计算

Python 3.x 张量流稀疏分类精度度量计算,python-3.x,tensorflow,Python 3.x,Tensorflow,我试图使用TensorFlow的“SparseCategoricaccuracy”[TensorFlow 2.0]计算精度,但对计算值和我手动计算的值存在混淆- m = tf.keras.metrics.SparseCategoricalAccuracy() m.update_state( y_true = [[2], [1], [3]], y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0], [0.02, 0.5, 0.8]], sa

我试图使用TensorFlow的“SparseCategoricaccuracy”[TensorFlow 2.0]计算精度,但对计算值和我手动计算的值存在混淆-

m = tf.keras.metrics.SparseCategoricalAccuracy()

m.update_state(
    y_true = [[2], [1], [3]],
    y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0], [0.02, 0.5, 0.8]],
    sample_weight = [1, 1, 1]
    # or-
    # sample_weight = None
    )

m.result().numpy()
# 0.33333334
在这里,准确率不是应该是66.67%而不是33.33%,因为“y_pred”中的第一个和第三个预测与y_true匹配吗

再说一遍

m.reset_states()                                                       

y_pred = [[0.1, 0.4, 0.8], [0.05, 0.95, 0], [0.2, 0.1, 0.7]]           
m.update_state(y_true=y_true, y_pred=y_pred, sample_weight=None)

m.result().numpy()                                                     
# 0.6666667


y_true                                                                 
# [[2], [1], [3]]

y_pred                                                                 
# [[0.1, 0.4, 0.8], [0.05, 0.95, 0], [0.2, 0.1, 0.7]]
这里,准确率应为33.33%,因为只有一个“y_pred”,即第三个预测与“y_true”匹配

我做错了什么


谢谢?

您的标签需要零索引。然后你会得到正确的准确度

m = tf.keras.metrics.SparseCategoricalAccuracy()

m.update_state(
    # We have changed y_true = [[2], [1], [3]] to the following
    y_true = [[1], [0], [2]],
    y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0], [0.02, 0.5, 0.8]],
    sample_weight = [1, 1, 1]
    # or-
    # sample_weight = None
    )

m.result().numpy()