程序報錯:AttributeError: module 'tensorflow' has no attribute 'xxx' 解決辦法

執行程序遇到報錯:AttributeError: module ‘tensorflow’ has no attribute ‘xxx’ 解決辦法

報錯例如:
AttributeError: module ‘tensorflow’ has no attribute ‘reset_default_graph’
AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’
AttributeError: module ‘tensorflow’ has no attribute ‘sub’
。。。等等
在這裏插入圖片描述
報錯原因:
Tensorflow1.x版本和2.x版本部分方法名不兼容。
一定是你安裝使用的是Tensorflow2.0及以上版本,而在程序中使用了Tensorflow1.x版本的方法或者情況相反。

解決辦法(一):

1.如果你導入Tensorflow模塊的代碼爲:

import tensorflow 

替換爲:

import tensorflow.compat.v1

2.如果你導入Tensorflow模塊的代碼爲:

import tensorflow as tf

則替換爲:

import tensorflow.compat.v1 as tf

解決辦法(二):

如果上面的辦法(一)還是沒有解決問題的話,有以下兩個硬核方案可選擇:

1、卸載當前高版本的Tensorflow,安裝1.x版本的Tensorflow。

2、將代碼中不兼容的方法命,更改爲對應Tensorflow版本的方法名。

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