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

对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)

你可能感兴趣的:(机器学习)