上記のエラーを生じる短いスクリプト例
前提
python3.x
tensorflow2.x
スクリプト
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)
print(c)
print(d)
print(e)
print("print numpy.array")
use_numpy_method = True
if use_numpy_method:
print(e.numpy())
else:
print(e.eval(session=tf.compat.v1.Session()))
生じたエラー
AttributeError: 'Tensor' object has no attribute 'numpy'
対処方法1
e.numpy()
を次のように置き換えると動作する。
e.eval(session=tf.compat.v1.Session())