ImportError: cannot import name 'to_categorical' from 'keras.utils' ??

455 views
Skip to first unread message

Arnaud Le Bourdonnec

unread,
Feb 13, 2022, 3:45:34 AM2/13/22
to Keras-users
Hello,

I am starting to use keras based on François Chollet's book, but from the second chapter onwards, I am faced with the following import error:

ImportError: cannot import name 'to_categorical' from 'keras.utils' (/opt/anaconda3/lib/python3.9/site-packages/keras/utils/__init__.py)

I have tried to reinstall Anaconda, Tensorflow and keras..., but nothing works!
Can anyone give me a clue as to what the problem is and how I can fix it?

Thank you

Arnaud Le Bourdonnec

unread,
Feb 13, 2022, 4:07:11 AM2/13/22
to Keras-users
I found an answer that seems to work:

Instead of :
from keras.utils import to_categorical
I write :
from keras.utils.np_utils import to_categorical

Thanks
Reply all
Reply to author
Forward
0 new messages