Oli*_*Yew 5 python keras google-colaboratory
所以我一直在使用 Google Colab 制作笔记本,突然间我收到以下错误。
---------------------------------------------------------------------------
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.
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为它工作得很好,当我重新开始我的会话时,这发生了。我也尝试使用另一个 google 帐户(以防我的帐户设置有问题),但我仍然遇到相同的错误。
这是我用来导入函数的。
from keras.utils import to_categorical
Run Code Online (Sandbox Code Playgroud)
我想知道是否有任何变化,以及是否有其他人遇到同样的问题。谢谢。
小智 9
从 TF 2.0 开始,它已随 TensorFlow 一起移动。请使用这种方式:
from tensorflow.keras.utils import to_categorical
to_categorical([0, 1, 2, 3], num_classes=4)
Run Code Online (Sandbox Code Playgroud)
结果会像
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1995 次 |
最近记录: |