【发布时间】:2020-01-19 08:58:49
【问题描述】:
我正在运行 tf2.0,根本无法打印混淆矩阵值。问题描述如下。
@tf.function
def test_step(self, x , y):
predictions = model(x, training=False)
loss = self.loss(y, predictions)
y, predictions = tf.reshape(y,[-1,]), tf.reshape(predictions, [-1,])
# Cast into class labels
predictions = math_ops.cast(predictions > 0.5, predictions.dtype)
....
self.test_conf_matrix = tf.math.confusion_matrix(y, predictions, num_classes=2) <--- important line!
到目前为止一切顺利,混淆矩阵将被正确计算。
但根本不可能像这样打印出来:
print(str(self.test_conf_matrix.numpy()))
我得到的错误是:
AttributeError: 'Tensor' object has no attribute 'numpy'
但是既然 tf2 和 eagerExecution 这应该是这样做的,对吧?见:TF2.0 Tutorial
【问题讨论】:
-
math_ops是什么。你从哪里导入的? -
它来自'from tensorflow.python.ops import math_ops'。因为例如tfa 的 F1-Score 实现不适用于概率。所以我看了一下 tensorflows 的准确性实现,这就是他们将概率转换为类标签的方式。
标签: python tensorflow machine-learning deep-learning