Programming/Tips
[tensorflow] AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’ 해결 방법
a voyager
2020. 10. 30. 21:11
728x90
반응형
증상
텐서플로우가 2.0으로 업데이트 되면서 tf.placeholder를 사용할 수 없게 되었다. 다음과 같이 실행한다면 placeholder라는 속성을 찾을 수 없다는 오류를 낸다.
import tensorflow as tf
X = tf.placeholder("float")
이를 해결 할 수 있는 두 가지 방법을 알아보도록 하자.
Solution 1
: tensorflow의 버전 업데이트에 따른 변화를 적용하는 것이다. 업데이트에 대한 자세한 내용은 여기를 참고하기 바란다.
#tensorflow 1.x
self._states = tf.placeholder(shape=[None, self._num_states], dtype=tf.float32)
#tensorflow 2.x
self._states = tf.Variable(tf.ones(shape=[None, self._num_states]), dtype=tf.float32)
Solution 2
: compatibility mode를 적용해 1.x 버전의 기능을 그대로 사용하는 것이다.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
x = tf.placeholder(shape=[None, 2], dtype=tf.float32)
위와 같이 tf.disable_v2_behavior()를 실행해서 tf.placeholder를 그대로 사용할 수 있다.
728x90
반응형