【问题标题】:ImportError: cannot import name 'to_categorical' from 'keras.utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py)ImportError:无法从“keras.utils”(/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py)导入名称“to_categorical”
【发布时间】:2021-05-26 11:22:19
【问题描述】:

我刚刚收到以下错误,导入数小时前可以正常工作的导入下降。

---------------------------------------------------------------------------
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.
---------------------------------------------------------------------------

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

【问题讨论】:

    标签: tensorflow keras python-import


    【解决方案1】:

    使用这个

    from tensorflow.keras.utils import to_categorical
    

    而不是

    从 keras.utils 导入 to_categorical

    【讨论】:

    • 您能否添加一些信息或文档中有关此更改的任何链接。 Keras 的作者在他的书中使用了 from keras.utils import to_categorical
    • from tensorflow.keras.utils import to_categorical 它对我有用
    • @shy-tan 这可能是因为从 TensorFlow 1.x 迁移。到 2.x。 Keras 被捆绑到 tensorflow 中。你的书很可能使用的是 TensorFlow 1
    猜你喜欢
    • 2021-12-07
    • 2021-02-27
    • 2023-02-02
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    相关资源
    最近更新 更多