AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘解决方案

解决AttributeError: module ‘tensorflow’ has no attribute ‘Session’


问题描述:

TensorFlow版本是2.8.0,执行如下代码:

sess = tf.Session()

报错如下:

AttributeError: module 'tensorflow' has no attribute 'Session'

原因分析:

TensorFlow 2.0以上的版本已经移除 Session 模块了。


解决方案:

tf.compat.v1.Session() 替换 tf.Session() 即可。

# sess = tf.Session()
sess = tf.compat.v1.Session()

如果代码多处因版本问题出现报错,可改换低版本的TensorFlow,用pip安装即可。

pip uninstall tensorflow
pip install tensorflow==1.14

猜你喜欢

转载自blog.csdn.net/qq_39691492/article/details/123095541