好吧,我试图通过 keras 框架将 maxpooling 用作第一层。我正在研究用于数字识别的 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")
答案 0 :(得分:1)
您使用了错误的丢失,sparse_categorical_crossentropy
需要整数标签,而不是一键编码的标签,因此您可以将categorical_crossentropy
用作丢失,它需要一键编码的标签。