【Linux】Tensorflow2.0 报错 AttributeError module tensorflow has no attribute Session 的解决办法

文章目录

  • 问题描述
  • 解决办法
  • 参考链接

问题描述

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
Traceback (most recent call last):
  File "", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'Session'
>>> print sess.run(hello)
  File "", 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函数对照表

你可能感兴趣的:(Linux,Python,linux,tensorflow)