python - 导入错误 : cannot import name np_utils

标签 python keras

我正在尝试运行以下 example来自 keras

但我收到此错误:

ImportError
Traceback (most recent call last)
<ipython-input-58-50de27eea0f8> in <module>()   
      8 import numpy as np  
      9 import matplotlib.pyplot as plt  
---> 10 from keras.models import Sequential  
     11 from keras.layers import Dense, LSTM  
     12   

/usr/local/lib/python2.7/dist-packages/keras/__init__.py in <module>()  
      1 from __future__ import absolute_import  
      2   
----> 3 from . import utils  
      4 from . import activations  
      5 from . import applications  

/usr/local/lib/python2.7/dist-packages/keras/utils/__init__.py in <module>()  
      1 from __future__ import absolute_import  
----> 2 from . import np_utils  
      3 from . import generic_utils  
      4 from . import data_utils  
      5 from . import io_utils  

ImportError: cannot import name np_utils  

我使用的是 Ubuntu,并安装了 keras:

sudo pip install keras 

这个问题已经被问过了,但没有答案: Keras: Cannot Import Name np_utils

最佳答案

np_utils 是一个单独的包(和一个 keras 依赖项 - 不会随它一起安装)。可以使用 pip 安装:

pip install np_utils

使用 - Keras==2.0.6


建议: 由于一些奇怪的(仍然未知的)原因,即使在安装了导入之后

from keras.utils.np_utils import to_categorical

没有工作 - 我不得不重新启动笔记本(第一次重新启动甚至没有工作),一旦它工作,我再次卡住了相同的导入调用(为 no module named tensorflow) - 在 utils 中还有另一个 import from 。 import conv_utils,需要用到tensorflow。

我确实尝试使用 pip install tensorflow 安装 tensorflow:

Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow

即使是这个 gist对我不起作用。


最后,我installed Anaconda - 预装了所有科学软件包(numpy、scipy、scikit-learn、..)。已安装的 keras:

conda install keras

最好的是,它甚至安装了 tensorflow 作为其依赖项。

关于python - 导入错误 : cannot import name np_utils,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45149341/

相关文章:

python - 使用查找数据框替换数据框中的值

python - Cherrypy 中的路由 href

python - 如何从 Keras 中的自定义损失函数中获取结果?

python - 如何在linux上的python中安装模块autopy?

python - 在 python 中使用 super 对撞机

python - 检查目标 : sparse_categorical_crossentropy output shape 时出错

python - Predict_classes() 的函数式 API Keras 替代解决方案

python - 使用ELMo配合Keras,如何正确输入训练集和标签?

python - Keras/Theano 输出

python - 如何检测鼠标点击 pyside 绘制的椭圆?