sess = tf.Session() module 'tensorflow' has no attribute 'Session'
时间: 2023-10-29 07:07:34 浏览: 21
这个错误是因为 TensorFlow 2.0 版本中不再使用 `tf.Session()`,而是使用 Eager Execution 模式,这样可以更加方便地进行开发和调试。如果你想要使用 TensorFlow 1.x 的 Session 模式,可以将 TensorFlow 的版本降到 1.x。如果你想在 TensorFlow 2.0 中使用类似于 Session 的模式,可以使用 `tf.compat.v1.Session()`。 但是建议使用 TensorFlow 2.0 的 Eager Execution 模式,因为它更加简洁易懂并且可以提高代码的效率。
相关问题
sess = tf.Session() AttributeError: module 'tensorflow' has no attribute 'Session'
在TensorFlow 2.0版本中,tf.Session()被删除了,所以在使用时会出现"AttributeError: module 'tensorflow' has no attribute 'Session'"的错误。你可以将tf.Session()更改为tf.compat.v1.Session(),这样就可以在TensorFlow 2.0版本中使用Session属性了。
源代码的修改后版本如下:
import tensorflow as tf
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
a = tf.constant(2)
b = tf.constant(3)
with tf.compat.v1.Session() as sess:
print("a:%i" % sess.run(a),"b:%i" % sess.run(b))
print("Addition with constants: %i" % sess.run(a+b))
print("Multiplication with constant:%i" % sess.run(a*b))
sess = tf.Session() module 'tensorflow' has no attribute 'Session'
在 TensorFlow 2.0 及以上版本中,`tf.Session()` 被弃用了,取而代之的是使用 `tf.compat.v1.Session()`。如果您正在使用 TensorFlow 2.0 及以上版本,请改为使用 `tf.compat.v1.Session()`。如果您在使用 TensorFlow 1.x 版本,则可以继续使用 `tf.Session()`。