首页 > 解决方案 > ValueError:检查目标时出错:预期dense_8的形状为(1,),但数组的形状为(10,)

问题描述

好吧,我正在尝试将maxpooling作为keras框架的第一层。我在MNIST著名的数字识别数据集上工作,但输入和输出维度存在问题。

这是我的模型摘要:

    Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_5 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 12, 12, 64)        0         
_________________________________________________________________
dropout_6 (Dropout)          (None, 12, 12, 64)        0         
_________________________________________________________________
flatten_3 (Flatten)          (None, 9216)              0         
_________________________________________________________________
dense_7 (Dense)              (None, 128)               1179776   
_________________________________________________________________
dropout_7 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_8 (Dense)              (None, 10)                1290      
=================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0

我在最后一步得到了这个错误:

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

我正在尝试执行分类任务。

这些是我的程序的主要部分:

from keras.utils import to_categorical
num_class = 10

y_train = to_categorical(y_train, num_class)
y_test = to_categorical(y_test, num_class)
#
#Model created
from keras.models import Sequential
#Layers added
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D

model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3),
     activation = 'relu', #A “relu” activation stands for “Rectified Linear Units”, which takes the max of a value or zero
     input_shape=(img_rows, img_cols, 1)))
#
model.add(Conv2D(64, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
#
model.add(Dropout(0.25))
#
model.add(Flatten())
#
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_class, activation='softmax'))
#“softmax” activation is used 
#when we’d like to classify the data into a number of pre-decided classes
model.compile(loss = 'sparse_categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
#
batch_size = 128
epochs = 10

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save("test_model.h5")

标签: pythonkerasneural-networkdeep-learning

解决方案


您使用了错误的损失,sparse_categorical_crossentropy需要整数标签,而不是 one-hot 编码标签,因此您可以只使用categorical_crossentropyas loss,这需要 one-hot 编码标签。


推荐阅读