簡體   English   中英

AttributeError: 模塊“tensorflow”在 Keras 中沒有屬性“name_scope”

[英]AttributeError: module 'tensorflow' has no attribute 'name_scope' with Keras

我正在嘗試運行一個腳本,但我已經在導入中掙扎了。 這個進口

from keras.preprocessing.image import save_img

引發以下錯誤:

AttributeError: module 'tensorflow' has no attribute 'name_scope'.

我正在使用以下軟件包。

Keras                     2.2.2,                     
Keras-Applications        1.0.4,                   
Keras-Preprocessing       1.0.2,                   
tensorflow                1.9.0,                     
tensorflow-gpu            1.9.0                

我無法用相同版本的keras和tensorflow重現,重新安裝keras和tensorflow,可能會解決問題,請使用以下命令:

pip install --upgrade pip setuptools wheel
pip install -I tensorflow
pip install -I keras

注意-I參數代表忽略已安裝的包。

對於每個使用 Tensorflow 2.0 並以同樣的錯誤偶然發現這個問題的人,就像我一樣:我通過將導入從keras.xxxtensorflow.keras.xxx解決了這個問題

當我在執行時停止我的 IDE 時,我也遇到了同樣的問題。 重新啟動我的 IDE 對我有用。 只需保存您的程序並重新啟動 IDE。 希望它也適用於你。

正如Andriy Ivaneyko上面提到的,重新安裝tensorflow幫助。 我不知道為什么,但是安裝tensorflow-serving-api會在此過程中的某個地方破壞某些東西。 我們通過運行解決了這個問題:

pip install --force-reinstall tensorflow

請注意,這適用於tensorflowtensorflow-gpu安裝。 具體來說,上述命令將在您專門使用tensorlfow-gpu情況下解決此問題。 如果尚未安裝, tensorflow-serving-api會安裝常規的tensorflow

我遇到了同樣的錯誤,重新安裝 tensorflow 沒有任何區別,這讓我有些頭疼。

最終我注意到我的 IDE 自動完成在我的代碼中添加了以下行:

from tensorflow_core.python.keras.callbacks import EarlyStopping

似乎直接引用tensorflow_core.python會破壞tensorflow

用普通的 tensorflow 導入替換它解決了這個問題!

from tensorflow.keras.callbacks import EarlyStopping

我的 IDE 為我提供了兩種不同的導入路徑

keras

或者

tensorflow_core.python.keras

在我的示例中,我可以像這樣導入:

from keras.layers import Dense, Dropout, LSTM, Input, Activation
from keras import optimizers, Model

或者像這樣:

from tensorflow_core.python.keras import Input, Model, optimizers
from tensorflow_core.python.keras.layers import LSTM, Dropout, Dense

在我的情況下,將 tensorflow_core.python.keras 和普通 keras 混合導致了問題。 在我直接從 keras 和 keras.layers 導入所有內容后,它對我有用。

我的 tensorflow 版本是 2.1,我發現我的 tensorflow-estimator 版本是 2.2

我的解決方法是將估算器降級到相同版本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM