728x90

※ 필자는 파이썬도,,, 텐서플로우도,,, 처음 공부하는 사람으로 내용에 틀린게 있을 수도 있습니당....

    - 참고서적 : 텐서플로로 배우는 딥러닝 / 저자: 솔라리스 

 

 

텐서플로우를 공부하면서 간단한 첫 코드를 실행시켜보았다! 

import tensorflow as tf

node1=tf.constant(3.0, dtype=tf.float32)
node2=tf.constant(4.0)
print(node1,node2)

sess = tf.Session()
print(sess.run([node1,node2]))

하지만 위 코드를 실행하자 아래와 같은 오류가 발생했다....ㅎ

AttributeError Traceback (most recent call last) <ipython-input-3-4a1f6c3445dc> in <module>() ----> 1 sess=tf.Session() 2 print(sess.run([node1,node2])) AttributeError: module 'tensorflow' has no attribute 'Session'

 

ㅎ...Session이란걸 모르겠다는 텐서플로우,,,,

 

해결방법을 찾아보고 코드를 아래와 같이 고쳐 다시 실행해보았다.

sess = tf.compat.vi.Session()
print(sess.run([node1,node2]))

하지만 또 다른 오류가 발생,,,

RuntimeError : The Session graph is empty. Add operations to the graph before calling run().

 

 

좀 더 구글링해본 결과

 

tf.Session()과 고친 코드의 경우 텐서플로우 버전 1.x.x에서 사용하는 표현방식이라 발생한 오류였던것,,, 

(본인의 텐서플로우 버전은 2.0.0)

 

 

우선 텐서플로우 버전을 확인해보는 코드는 아래와 같다.

import tensorflow as tf
tf.__version__

텐서플로우 버전 2.0.0에서는 Session을 정의하고 run 해주는 과정이 생략된다.

고로 아래와 같이 코드를 작성하면 됩니당~

import tensorflow as tf
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0)

tf.print(node1,node2)

 

 

+ Recent posts