Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

python - ImportError: cannot import name np_utils

I'm trying to run the following example from keras

but I get this error:

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  

I'm using Ubuntu and I installed keras with:

sudo pip install keras 

This question was already asked but there was no answer: Keras: Cannot Import Name np_utils

question from:https://stackoverflow.com/questions/45149341/importerror-cannot-import-name-np-utils

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

np_utils is a separate package (and a keras dependency - which doesn't get install with it). Can be installed using pip:

pip install np_utils

using - Keras==2.0.6


Suggestion: For some odd (and still unknown) reasons, even after installing the import

from keras.utils.np_utils import to_categorical

didn't work - I had to restart the notebook (first restart even didn't work), and once it worked, I got stuck again for same import call (gave exception for no module named tensorflow) - as in utils there's another import from . import conv_utils, which required the tensorflow.

I did try installing tensorflow using pip install tensorflow gave:

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

even this gist didn't work for me.


Finally, I installed Anaconda - which have all the scientific packages (numpy, scipy, scikit-learn,..) pre-installed. Installed keras:

conda install keras

Best thing was, it even installed tensorflow as its dependency.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...