問題描述
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'Session'
>>> print sess.run(hello)
File "<stdin>", line 1
print sess.run(hello)
^
SyntaxError: invalid syntax
原因:tensorflow 2.0版本與 1.0版本不兼容。
解決辦法
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant('Hello, TensorFlow!')
sess = tf.compat.v1.Session()
print(sess.run(hello))
參考鏈接
Tensorflow 2.0 - AttributeError: module ‘tensorflow’ has no attribute ‘Session’
兩個版本tensorflow函數對照表