Hello Guys, How are you all? Hope You all Are Fine. Today When I run my project its fails with the exception ImportError: cannot import name ‘to_categorical’ from ‘keras.utils’ (/usr/local/lib/python3.7/dist-packages/keras/utils/init.py) in Python. So Here I am Explain to you all the possible solutions here.
Without Wasting your time, Lets start This Article to Solve This Error.
- How ImportError: cannot import name 'to_categorical' from 'keras.utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/init.py) Occurs ?
- How to solve ImportError: cannot import name 'to_categorical' from 'keras.utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/init.py) ?
- Solution 1
- Summery
How ImportError: cannot import name ‘to_categorical’ from ‘keras.utils’ (/usr/local/lib/python3.7/dist-packages/keras/utils/init.py) Occurs ?
Today I have just got below error from an import that use to work fine few hours ago going downward.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-33-4c52b67c20bf> in <module>()
1 import keras
----> 2 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.
---------------------------------------------------------------------------
How to solve ImportError: cannot import name ‘to_categorical’ from ‘keras.utils’ (/usr/local/lib/python3.7/dist-packages/keras/utils/init.py) ?
Question: How to solve ImportError: cannot import name ‘to_categorical’ from ‘keras.utils’ (/usr/local/lib/python3.7/dist-packages/keras/utils/init.py) ?
Answer: if you need to use to_categorical then import it from tensorflow.keras.utils not from the keras.utils.
Solution 1
First of all Remove below line from your code
from keras.utils import to_categorical
use this
from tensorflow.keras.utils import to_categorical
instead of
from keras.utils import to_categorical
Summery
It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also Read
This helped me. Thanks.
This helped me too. Tks a lot
helpful , thanks
thanks my man !