ERROR/SQL
[python]AttributeError: 'Tensor' object has no attribute 'numpy'
onlyun
2022. 6. 17. 15:32
파이썬에서
TensorFlow 2 하다가...
a = tf.constant(10)
b = tf.constant(20)
c = a + b
d = (a+b).numpy() #<-여기서 에러떴음
print(type(c))
print(c)
print(type(d), d)
d_numpy_to_tensor = tf.convert_to_tensor(d)
print(type(d_numpy_to_tensor))
print(d_numpy_to_tensor)
AttributeError Traceback (most recent call last) Input In [44], in <cell line: 5>() 2 b = tf.constant(20) 4 c = a + b ----> 5 d = (a+b).numpy() 7 print(type(c)) 8 print(c) File ~\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:446, in Tensor.__getattr__(self, name) 437 if name in {"T", "astype", "ravel", "transpose", "reshape", "clip", "size", 438 "tolist", "data"}: 439 # TODO(wangpeng): Export the enable_numpy_behavior knob 440 raise AttributeError( 441 f"{type(self).__name__} object has no attribute '{name}'. " + """ 442 If you are looking for numpy-related methods, please run the following: 443 from tensorflow.python.ops.numpy_ops import np_config 444 np_config.enable_numpy_behavior() 445 """) --> 446 self.__getattribute__(name) AttributeError: 'Tensor' object has no attribute 'numpy' |
더보기
AttributeError Traceback (most recent call last)
Input In [44], in <cell line: 5>()
2 b = tf.constant(20)
4 c = a + b
----> 5 d = (a+b).numpy()
7 print(type(c))
8 print(c)
File ~\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:446, in Tensor.__getattr__(self, name)
437 if name in {"T", "astype", "ravel", "transpose", "reshape", "clip", "size",
438 "tolist", "data"}:
439 # TODO(wangpeng): Export the enable_numpy_behavior knob
440 raise AttributeError(
441 f"{type(self).__name__} object has no attribute '{name}'. " + """
442 If you are looking for numpy-related methods, please run the following:
443 from tensorflow.python.ops.numpy_ops import np_config
444 np_config.enable_numpy_behavior()
445 """)
--> 446 self.__getattribute__(name)
AttributeError: 'Tensor' object has no attribute 'numpy'
->
문서 내에서
#Tensorflow 2 설치 후 Tensorflow 2 사용
import tensorflow.compat.v1 as tf1
tf1.disable_v2_behavior() #tensorflow version 2를 사용 안 하겠다.
를 수행? 실행시켰기 때문에 이 문서 내에서는 더이상 Tensorflow2의 것들을 사용할 수 없음.
-> 새로운 문서에서
Tensorfow2를 import하고 안의 속성들을 사용하면 됨.