4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

'Tensor' object has no attribute 'numpy' を生じる短いスクリプト例

Posted at

上記のエラーを生じる短いスクリプト例

前提

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())

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?