python - 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")
解决方案
您使用了错误的损失,sparse_categorical_crossentropy
需要整数标签,而不是 one-hot 编码标签,因此您可以只使用categorical_crossentropy
as loss,这需要 one-hot 编码标签。
推荐阅读
- arrays - Google Apps:Array.map() - 如何仅向一个索引抛出错误
- javascript - 如何使用变量值来寻址数据对象键
- html - 为什么属性 align-content:flex-end; 当没有多行项目时对显示项目有影响吗?
- c - 如何在要使用 Linux 内核编译的 Linux 源代码中包含用户级 C 程序?
- postgresql - 使用 args [] 调用查询,在 go-sqlmock 中不期望用于复合 SQL 查询
- saml-2.0 - oracle apex 18 saml 身份验证与外部身份提供者
- python - 围绕谷歌云数据存储嵌套事务的意外行为
- c# - EF Core 3:SaveChanges() 时拥有的属性插入失败
- javascript - Express Js Firebase SignOut() 立即向客户端 Fetch Post 请求返回状态 200
- swift - strokeThrough 属性与 iOS 13 不兼容