Python 无法从Google Colab中的keras导入到_Category
所以我一直在谷歌Colab上做一个笔记本,突然我发现了以下错误Python 无法从Google Colab中的keras导入到_Category,python,keras,google-colaboratory,Python,Keras,Google Colaboratory,所以我一直在谷歌Colab上做一个笔记本,突然我发现了以下错误 --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-1-bd6ec74ccf2e> in <module>() ---->
---------------------------------------------------------------------------
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.
我想知道是否有什么变化,是否还有其他人经历过同样的问题。
谢谢。从TF2.0开始,它已经用tensorflow移动了。请用这种方式:
from tensorflow.keras.utils import to_categorical
to_categorical([0, 1, 2, 3], num_classes=4)
结果会是这样的
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]], dtype=float32)
从代码中删除这一行并使用
from tensorflow.keras.utils import to_categorical
这会解决你的问题
import tensorflow as tf
y_train_one_hot = tf.keras.utils.to_categorical(y_train)
from keras.utils import to_categorical
from tensorflow.keras.utils import to_categorical