如何解决“ ValueError:检查目标时出错:预期density_20具有形状(24,)但具有形状(1,)的数组”?

时间:2019-06-06 13:48:35

标签: python-3.x keras deep-learning conv-neural-network

我正在使用深度学习进行手语解释,为此,我正在构建CNN,却遇到类似这样的错误,

  

ValueError:检查目标时出错:预期density_20具有   形状(24,),但数组的形状为(1,)

我的神经网络的结构:

Layer (type)                 Output Shape              Param #   

conv2d_62 (Conv2D)           (None, 64, 64, 64)        1088      
_________________________________________________________________
conv2d_63 (Conv2D)           (None, 32, 32, 64)        65600     
_________________________________________________________________
dropout_31 (Dropout)         (None, 32, 32, 64)        0         
_________________________________________________________________
conv2d_64 (Conv2D)           (None, 32, 32, 128)       131200    
_________________________________________________________________
conv2d_65 (Conv2D)           (None, 16, 16, 128)       262272    
_________________________________________________________________
dropout_32 (Dropout)         (None, 16, 16, 128)       0         
_________________________________________________________________
conv2d_66 (Conv2D)           (None, 16, 16, 256)       524544    
_________________________________________________________________
conv2d_67 (Conv2D)           (None, 8, 8, 256)         1048832   
_________________________________________________________________
flatten_11 (Flatten)         (None, 16384)             0         
_________________________________________________________________
dropout_33 (Dropout)         (None, 16384)             0         
_________________________________________________________________
dense_19 (Dense)             (None, 512)               8389120   
_________________________________________________________________
dense_20 (Dense)             (None, 24)                12312

代码:

    model = Sequential()

    model.add(Conv2D(64, kernel_size=4, strides=1, activation='relu', input_shape = (64,64,1),padding = 'same'))
    model.add(Conv2D(64, kernel_size=4, strides=2, activation='relu',padding = 'same'))
    model.add(Dropout(0.2))

    model.add(Conv2D(128, kernel_size=4, strides=1, activation='relu',padding = 'same'))
    model.add(Conv2D(128, kernel_size=4, strides=2, activation='relu',padding = 'same'))
    model.add(Dropout(0.2))

    model.add(Conv2D(256, kernel_size=4, strides=1, activation='relu',padding = 'same'))
    model.add(Conv2D(256, kernel_size=4, strides=2, activation='relu',padding = 'same'))

    model.add(Flatten())
    model.add(Dropout(0.3))
    model.add(Dense(512, activation='relu'))
    model.add(Dense(24, activation='softmax'))

    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

    print(model.summary())

    model.fit(x_train, y_train, validation_data=(x_test, y_test),batch_size=64,epochs=8)

使用的数组尺寸:
x_train:(3977,64,64,1)
y_train:(3977,1)
x_test:(995、64、64、1)
y_test:(995,1)

1 个答案:

答案 0 :(得分:1)

您的最后一层输出形状需要与标签的矢量形状匹配
因此,您需要对y_train进行one_hot编码才能适合您的网络。

您可以这样做:

from keras.utils import to_categorical
y_train = to_categorical(y_train, 24)

这会将您的每个标签编码为大小为24(或您需要的大小)的矢量,在相应标签的位置填充0和1。

要了解更多信息:
https://keras.io/utils/