AttributeError: module ‘tensorflow’ has no attribute ‘Session’. Did you mean: ‘version’?
1.原因
安装的是tensorflow2.X版本,而tf.Session()是tensorflow1.X中的代码。
2.解决方法
将代码:
import tensorflow as tf
hello = tf.constant(‘TensorFlow!’)
sess = tf.Session()
print(sess.run(hello))
修改为如下代码:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello=tf.constant(‘Tensorflow!’)
sess=tf.compat.v1.Session()
print(sess.run(hello))
主要变动
增加tf.compat.v1.disable_eager_execution()
将
sess = tf.Session()
变为
sess=tf.compat.v1.Session()
成了!