Tensorflow가 1.x 버전에서 2.x 버전으로 업그레이드 됨에 따라 개발형태가 매우 달라졌습니다.
그래서 AttributeError: module 'tensorflow' has no attribute 'placeholder' 와 같은 오류를 발견하게 됩니다.
tf.placeholder & tf.Session
텐서플로우가 2.x로 업데이트되면서 tf.placeholder와 tf.Session을 사용할 수 없게 되었습니다.
저는 tf.placeholder 코드를 사용하다가 AttributeError: module 'tensorflow' has no attribute 'placeholder' 오류를 얻었습니다.
해결방법
2가지의 해결방법이 있습니다. 텐서플로우 1.x 버전을 불러와서 tf.placeholder를 그대로 사용하는 방법과 텐서플로우 2.x버전에 맞는 코드로 작성하는 방법입니다. (앞으로 2.x 버전을 자주 사용하게 될 것 같아서 2.x를 익히는 것이 더 좋을 것 같습니다!)
방법 1. Tensorflow 1.x
위의 2줄의 코드를 작성한 후, tf.placeholder를 사용하면 잘 작동됩니다.
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
x = tf.placeholder(tf.float32, shape=[None, 28, 28, 1])
방법 2. Tensorflow 2.x
기존처럼 tensorflow를 import하고, 아래와 같은 형식으로 코드를 작성합니다.
import tensorflow as tf
tf.Variable(tf.ones(shape=[None, 28, 28, 1]), dtype=tf.float32)
위 두가지 방법 중 한 가지 방법을 사용해서 코드를 작성하면 오류가 해결됩니다!
Tensorflow 업데이트가 되면서 어떻게 바뀌었는지 좀 더 자세히 알고 싶으면 아래의 링크를 통해서 확인하시면 됩니다.
www.tensorflow.org/guide/migrate
텐서플로 1 코드를 텐서플로 2로 바꾸기 | TensorFlow Core
Note: 이 문서는 텐서플로 커뮤니티에서 번역했습니다. 커뮤니티 번역 활동의 특성상 정확한 번역과 최신 내용을 반영하기 위해 노력함에도 불구하고 공식 영문 문서의 내용과 일치하지 않을 수
www.tensorflow.org
'Python' 카테고리의 다른 글
Section 5 과제 수행과정 (1) | 2021.05.06 |
---|---|
[Error 해결] ValueError: Iterable over raw text documents expected, string object received. (0) | 2021.05.04 |
[Error해결] ValueError: No gradients provided for any variable (0) | 2021.04.23 |
[Error해결]ResourceExhaustedError: OOM (0) | 2021.04.23 |
Matplotlib 한글 폰트 깨짐 해결 (0) | 2021.02.25 |
댓글