Python AttributeError: 模块“tensorflow”在 Keras 中没有属性“name_scope”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51724309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: module 'tensorflow' has no attribute 'name_scope' with Keras
提问by Henrik
I am trying to run a script, but I struggle already at the imports. This import
我正在尝试运行一个脚本,但我已经在导入中挣扎了。这个进口
from keras.preprocessing.image import save_img
raises the following error:
引发以下错误:
AttributeError: module 'tensorflow' has no attribute 'name_scope'.
I am using the following packages.
我正在使用以下软件包。
Keras 2.2.2,
Keras-Applications 1.0.4,
Keras-Preprocessing 1.0.2,
tensorflow 1.9.0,
tensorflow-gpu 1.9.0
回答by Andriy Ivaneyko
I was unable to reproduce with the same versions of the keras and tensorflow, reinstalling keras and tensorflow, may solve the issue, please use commands below:
我无法用相同版本的keras和tensorflow重现,重新安装keras和tensorflow,可能会解决问题,请使用以下命令:
pip install --upgrade pip setuptools wheel
pip install -I tensorflow
pip install -I keras
NOTE: The -I
parameter stands for ignore installed package.
注意:该-I
参数代表忽略已安装的包。
回答by zeyger
For everyone who use Tensorflow 2.0 and stumble upon this question with the same error, like I did: I solved it by changing the imports from keras.xxx
to tensorflow.keras.xxx
对于每个使用 Tensorflow 2.0 并以同样的错误偶然发现这个问题的人,就像我所做的一样:我通过将导入从 更改keras.xxx
为tensorflow.keras.xxx
回答by Hemant Jaiman
I also encountered this same problem when I stopped my IDE while executing. Restarting my IDE works for me. Just save your program and restart IDE. Hope it will work for you as well.
当我在执行时停止我的 IDE 时,我也遇到了同样的问题。重新启动我的 IDE 对我有用。只需保存您的程序并重新启动IDE。希望它也适用于您。
回答by Grant Beyleveld
As Andriy Ivaneykomentioned above, reinstalling tensorflow
helps. I'm not sure why, but installing tensorflow-serving-api
breaks something somewhere along the way. We solved this by running:
正如Andriy Ivaneyko上面提到的,重新安装有tensorflow
帮助。我不知道为什么,但是安装过程中的tensorflow-serving-api
某个地方会破坏一些东西。我们通过运行解决了这个问题:
pip install --force-reinstall tensorflow
Note that this applies to both tensorflow
and tensorflow-gpu
installations. Specifically, the above command will fix this problem in situations where you're specifically using tensorlfow-gpu
. tensorflow-serving-api
installs regular tensorflow
if it's not already installed.
请注意,这适用于tensorflow
和tensorflow-gpu
安装。具体来说,上述命令将在您专门使用tensorlfow-gpu
. 如果尚未tensorflow-serving-api
安装,则定期tensorflow
安装。
回答by James McGuigan
I encountered this same bug and reinstalling tensorflow made no difference, and this caused me some headscratching.
我遇到了同样的错误,重新安装 tensorflow 没有任何区别,这让我有些头疼。
Eventually I noticed that my IDE autocomplete had added the following line to my code:
最终我注意到我的 IDE 自动完成在我的代码中添加了以下行:
from tensorflow_core.python.keras.callbacks import EarlyStopping
It seems that directly referencing tensorflow_core.python
will break tensorflow
.
似乎直接引用tensorflow_core.python
会中断tensorflow
。
Replacing this with the normal tensorflow import solved the problem!
用普通的 tensorflow 导入替换它解决了这个问题!
from tensorflow.keras.callbacks import EarlyStopping
回答by Codev
My IDE offered me two different import paths
我的 IDE 为我提供了两种不同的导入路径
keras
or
或者
tensorflow_core.python.keras
In my example I could either import like this:
在我的示例中,我可以像这样导入:
from keras.layers import Dense, Dropout, LSTM, Input, Activation
from keras import optimizers, Model
or like that:
或者像这样:
from tensorflow_core.python.keras import Input, Model, optimizers
from tensorflow_core.python.keras.layers import LSTM, Dropout, Dense
Mixing up tensorflow_core.python.keras and plain keras led to the problem in my case. After I imported everything directly from keras and keras.layers, it worked for me.
在我的情况下,将 tensorflow_core.python.keras 和普通 keras 混合导致了问题。在我直接从 keras 和 keras.layers 导入所有内容后,它对我有用。