导入错误:无法从 'keras.utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py) 导入名称 'to_categorical'

Jay*_*neM 9 python-import keras tensorflow

我刚刚从一个导入中得到以下错误,该导入在几个小时前可以正常工作,但一直在下降。

---------------------------------------------------------------------------
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.
---------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

Keras 是否忙于更新以弃用此方法?

在此处输入图片说明

小智 25

用这个

from tensorflow.keras.utils import to_categorical
Run Code Online (Sandbox Code Playgroud)

代替

from keras.utils import to_categorical
Run Code Online (Sandbox Code Playgroud)

  • 您可以添加一些信息或有关此更改的文档中的任何链接吗?Keras 的作者在他的书中使用了 *from keras.utils import to_categorical*。 (2认同)