*AttributeError: module ‘tensorflow’ has no attribute 'Session
*’)
原代码
`import tensorflow as tf
sess= tf.Session()
a = tf.constant(1)
b = tf.constant(2)
print(sess.run(a+b))`
这是一个最简单的tensorflow程序,运行之后
sess= tf.Session()
AttributeError: module 'tensorflow' has no attribute 'Session'
出现了这个结果,解决办法如下
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
#保证sess.run()能够正常运行
sess = tf.compat.v1.Session()
a = tf.constant(1)
b = tf.constant(2)
print(sess.run(a+b))
在第二行加了一行代码
tf.compat.v1.disable_eager_execution()
然后将sess= tf.Session()
改为了
sess = tf.compat.v1.Session()
之后程序就可以正常运行
2021-08-14 20:58:18.317322: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264] 0
2021-08-14 20:58:18.317617: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0: N
2021-08-14 20:58:18.318185: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1418] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2816 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce GTX 950M, pci bus id: 0000:0a:00.0, compute capability: 5.0)
3