环境
- tensoflow >= 1.14
制造错误:
import tensorflow as tf
m = tf.keras.metrics.Accuracy()
m.update_state([1, 2, 3, 4], [1, 2, 3, 4])
print('Final result: ', m.result().numpy())
错误:
AttributeError: 'Tensor' object has no attribute 'numpy'
解决方式:(版本1.14后才有的特性)
import tensorflow as tf
tf.enable_eager_execution() # 关键
m = tf.keras.metrics.Accuracy()
m.update_state([1, 2, 3, 4], [1, 2, 3, 4])
print('Final result: ', m.result().numpy())