Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: Cannot Import Name np_utils [duplicate]

Tags:

python

keras

I'm using Python 2.7 and a Jupyter notebook to do some basic machine learning. I'm following along with this tutorial:

http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/

I'm simply trying to import different things from Keras so I can run the tutorial. Specifically, I do this:

from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

and it gets stuck at the first import, giving me a traceback of this:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-28-aae66d0fdaee> in <module>()
----> 1 from keras.models import Sequential
      2 from keras.layers import Dense
      3 from keras.wrappers.scikit_learn import KerasRegressor
      4 from sklearn.model_selection import cross_val_score
      5 from sklearn.model_selection import KFold

/Users/newscred/anaconda/lib/python2.7/site-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

/Users/newscred/anaconda/lib/python2.7/site-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've Googled around but can't seem to find out why I'm running into this problem / how to fix. Any ideas?

Thanks!

like image 354
anon_swe Avatar asked Jul 12 '17 14:07

anon_swe


2 Answers

That tutorial was written on June 9th, 2016. Keras 2 was released in March 2017. Try installing the old version, using pip install keras==1.2.2.

like image 69
Valentin Brasso Avatar answered Oct 07 '22 17:10

Valentin Brasso


Hi this is how it worked for me.(I am using conda) i created a virtualenv first with conda and then install tensorflow,theano and also future. make sure numpy is updated as well...

steps to follow

conda install numpy

conda install future

conda install -c anaconda theano

conda install keras

now once all this is done. you can open jupyter from the same virtualenv or spyder(i was using spyder) or ipython notebook. this will definitely work.

like image 23
dineep thomas Avatar answered Oct 07 '22 16:10

dineep thomas