【问题标题】:I'm new at ML and I am trying to build a CNN on the fashion mnist dataset and i keep getting this error我是 ML 的新手,我正在尝试在时尚 mnist 数据集上构建 CNN,但我不断收到此错误
【发布时间】:2020-10-23 08:06:20
【问题描述】:

我正确地遵循了教程,所以我不知道我做错了什么。 这是我的代码:

model = Sequential([
                    Conv2D(32, (3,3), padding='same', activation='relu', input_shape=(28,28,1)),
                    MaxPooling2D((2,2)),
                    Conv2D(64,(3,3), activation='relu'),
                    MaxPooling2D((2,2)),
                    Flatten(),
                    Dense(128, activation='relu'),
                    Dense(10)
])

#Compile the model
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])

#Train the model
history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))


ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 28, 28]

【问题讨论】:

标签: tensorflow keras google-colaboratory conv-neural-network


【解决方案1】:

您的 train_images 数组似乎具有这种格式(n_images、height、width)。为了应用卷积操作,你必须明确一个特征暗淡。在黑白图像的情况下,通道为 1。因此您可以简单地解决修改图像的问题,在拟合之前添加通道尺寸

train_images = train_images[:,:,:,None]
test_images = test_images[:,:,:,None]

模型的输入形状已正确定义input_shape=(28,28,1)

我还建议你应用 softmax 作为输出层的最后一次激活,因为你是一个多分类问题

【讨论】:

    【解决方案2】:
    mnist = tf.keras.datasets.fashion_mnist
    (training_images, training_labels), (test_images, test_labels) = mnist.load_data()
    training_images = training_images.reshape(training_images.shape[0],28,28,1)
    test_images = test_images.reshape(test_images.shape[0],28,28,1)
    

    加载训练和测试图像后,您需要调整训练和测试图像的大小

    【讨论】:

      猜你喜欢
      • 2019-09-08
      • 2023-04-02
      • 2021-02-16
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      相关资源
      最近更新 更多