检查输入时出错:预期 dense_8_input 有 2 个维度

Error when checking input: expected dense_8_input to have 2 dimensions

我正在尝试 运行 一个简单的 MNIST 分类网络:

network = models.Sequential()
network.add(layers.Dense(512, activation = 'relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))

当我尝试适应时:

network.fit(train_images, train_labels, epochs =5, batch_size = 128)

我收到这个错误:

Error when checking input: expected dense_8_input to have 2 dimensions, but got array with shape (60000, 28, 28)

我在做什么?

您的模型期望每个输入样本的形状为 (784,)(即 input_shape=(28 * 28,))。但是,如错误所示,输入数组当前的形状为 (num_samples, 28, 28)。所以你需要重塑它:

import numpy as np

train_images = np.reshape(train_images, (-1, 28*28))