Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name np_utils

Tags:

python

keras

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

like image 736
Elizabeth Fons Avatar asked Jul 17 '17 16:07

Elizabeth Fons


People also ask

What is Np_utils in keras?

np_utils. to_categorical is used to convert array of labeled data(from 0 to nb_classes - 1 ) to one-hot vector.

What is To_categorical in keras?

to_categorical functionConverts a class vector (integers) to binary class matrix. E.g. for use with categorical_crossentropy . Arguments. y: Array-like with class values to be converted into a matrix (integers from 0 to num_classes - 1 ).


2 Answers

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.

like image 119
Nabeel Ahmed Avatar answered Sep 24 '22 21:09

Nabeel Ahmed


For keras > 2.0, please use from keras.utils import to_categorical instead.

Example of usage will be to_categorical(y, num_classes=None)

like image 31
Mimi Cheng Avatar answered Sep 21 '22 21:09

Mimi Cheng