Keras错误:ValueError: expected dense_2 to have shape (10,) but got array with shape (1,)

本文详细介绍了在使用Keras框架训练深度学习模型时,如何正确地应用categorical_crossentropy作为损失函数。文章强调了在使用此类损失函数时,目标变量必须进行one-hot编码的重要性,并提供了具体的操作示例,包括使用Keras和TensorFlow的one-hot编码方法。同时,文章还解析了不进行one-hot编码时可能遇到的错误及其原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对keras模型进行编译,当loss是categorical_crossentropy的时候,

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

需要将label标签转换成one_hot编码然后进行fit,以mnist为例,在keras下可以作如下操作:

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

或者使用tensorflow的接口

y_train = tf.one_hot(y_train, num_classes)

否则就会出现以下错误:

ValueError: Error when checking target: expected dense_2 to have shape (10,) but got array with shape (1,)

根据错误提示可知输入数据的维度出错了。

One Hot编码

大家都熟知MINIST的标签值是0-9,共10类,每张图对应一个标签,one hot编码就是先形成和classes长度一致的数组,然后将标签对应那位数值为1,其他值为0,比如一张图像原始对应的label为3,经过one hot编码后变为

[0,0,0,1,0,0,0,0,0,0]
       ^
    这个是对应的3
         

下面的例子是在python控制台里执行的结果,y为编码转化后的结果,y_train为元素标签值,显示了前两组数据

y[:2]
Out[7]: 
array([[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)

y_train[:2]
Out[8]: array([5, 0], dtype=uint8)

参考链接
[1] https://keras.io/losses/

官方文档是给出了详细解释:

Note: when using the categorical_crossentropy loss, your targets should be in categorical format (e.g. if you have 10 classes, the target for each sample should be a 10-dimensional vector that is all-zeros except for a 1 at the index corresponding to the class of the sample). In order to convert integer targets into categorical targets, you can use the Keras utility to_categorical:

from keras.utils.np_utils import to_categorical

categorical_labels = to_categorical(int_labels, num_classes=None)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值