我有28x28张图片的数据集。数据点数组x
具有形状(10000, 28, 28)
,标签数组y
具有形状(10000,)
。
以下代码:
x = x.reshape(-1, 28, 28, 1)
model = Sequential([
Conv2D(8, kernel_size=(3, 3), padding="same", activation=tf.nn.relu, input_shape=(28, 28, 1)),
Dense(64, activation=tf.nn.relu),
Dense(64, activation=tf.nn.relu),
Dense(10, activation=tf.nn.softmax)
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x, y, epochs=5) #error
提供:
ValueError: Error when checking target: expected dense_3 to have 4 dimensions, but got array with shape (10000, 1)
model.summary()
输出:
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 28, 28, 8) 80
_________________________________________________________________
dense_1 (Dense) (None, 28, 28, 64) 576
_________________________________________________________________
dense_2 (Dense) (None, 28, 28, 64) 4160
_________________________________________________________________
dense_3 (Dense) (None, 28, 28, 10) 650
=================================================================
Total params: 5,466
Trainable params: 5,466
Non-trainable params: 0
_________________________________________________________________
你的输出是三维的,而你的目标是一维的。很可能在
Con2D
层之后缺少一个Flatten
层,这将把卷积的输出减少到一个维度:然后,尺寸正确:
您忘记添加
Flatten()
层(keras.layers.Flatten()
):相关问题 更多 >
编程相关推荐