1. 程式人生 > 其它 >AttributeError: module ‘tensorflow‘ has no attribute ‘InteractiveSession‘或 ‘placeholder‘的解決

AttributeError: module ‘tensorflow‘ has no attribute ‘InteractiveSession‘或 ‘placeholder‘的解決

技術標籤:python

問題提示:

AttributeError: module ‘tensorflow’ has no attribute ‘InteractiveSession’
或者:
AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’

解決:

如果安裝的是高版本的tf(tf2.x),跑比較舊的程式碼容易出現這種報錯。解決方案:註釋tf的引用,換為相容模式。

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

替換掉 import tensorflow as tf 即可。

雖然提示 no module named compat ,仍然執行成功了。
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述