Cannot import to_categorical from keras in Google Colab

Viewed 7421

So i have been working on a notebook on Google Colab, and all of a sudden i get the following error.

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-bd6ec74ccf2e> in <module>()
----> 1 from keras.utils import to_categorical

ImportError: cannot import name 'to_categorical' from 'keras.utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py)

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.

It's very strange since it was working just fine, and when i restarted my session, this happened. I tried with another google account too (in case there might be something wrong with my account's setup), but i still got the same error.

This is what i use to import the function.

from keras.utils import to_categorical 

I'm wondering if anything change, and if anyone else experiences the same issue. Thanks.

3 Answers

from TF 2.0, it's been moved with tensorflow. please use this way:

from tensorflow.keras.utils import to_categorical
to_categorical([0, 1, 2, 3], num_classes=4)

result will be like

array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]], dtype=float32)
import tensorflow as tf

y_train_one_hot = tf.keras.utils.to_categorical(y_train)
from keras.utils import to_categorical

remove this line from your code and use this

from tensorflow.keras.utils import to_categorical

this will solve your problem

Related