首页 > 解决方案 > Tensorflow 指标和 tensorflow-addons 指标的一致性问题

问题描述

这是计算精度的示例,在 tensorflow.keras.metrics 中实现的召回率和在 tensorflow_addons.metrics 中实现的 F1_score。结果显然不一致!

from tensorflow.keras.metrics import Precision, Recall
from tensorflow_addons.metrics import F1Score

metric = F1Score(num_classes=3, threshold=0.5, average='weighted')
y_true = np.array([[1, 1, 1],
                    [1, 0, 0],
                    [1, 1, 0]], np.int32)
y_pred = np.array([[0.2, 0.6, 0.7],
                   [0.2, 0.6, 0.6],
                    [0.6, 0.8, 0.0]], np.float32)
metric.update_state(y_true, y_pred)
result = metric.result()
result.numpy()

**0.62777776**

p= Precision(thresholds=0.5)
p.update_state(y_true, y_pred)
result = p.result()
result.numpy()

**0.6666667**

r= Recall(thresholds=0.5)
r.update_state(y_true, y_pred)
result = r.result()
result.numpy()

**0.6666667**

f1 = 2*(p*r)/(p+r) = 0.6666667 #  0.62777776

标签: pythontensorflowkerasdeep-learning

解决方案


推荐阅读