【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 "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'Session'
>>> print sess.run(hello)
  File "<stdin>", 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函數對照表

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章