gpt4 book ai didi

python - Keras 数字数据集 : expected conv2d_input to have 4 dimensions, 中出现错误,但获得形状为 (60000, 28, 28) 的数组

转载 作者:行者123 更新时间:2023-12-01 00:26:10 24 4
gpt4 key购买 nike

Keras 数字数据集中出现错误:ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 (60000, 28, 28) 的数组。我不确定发生了什么,为什么这样做

我需要 4 个维度?我确信它们是 28x28 的图像。我该怎么办?

   (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images/255
test_images = test_images/255

classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

model = keras.Sequential([
tf.keras.layers.Conv2D(56, (3, 3), activation='relu', input_shape=(28, 28)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(56, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(784, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=5)

test_loss, test_acc = model.evaluate(test_images, test_labels)

最佳答案

谢谢@bit01指出我的错误,我已经相应地更新了我的答案 -

您无法在无深度或 channel 图像上使用 2D 方法。您的图像不包含任何 channel ,具有 28 X 28 形状,Conv2D 是为具有 grey_scale(256,256,1) 或 RGB(256,256,3) 等 channel 的图像或具有 alpha channel (256,256,4) 的 png 图像定义的。它不适用于没有任何深度的 2D 矩阵。

所以你有两种方法来解决这个问题:

  1. 将卷积层和最大池层从 2D 方法切换为 1D 方法。

像这样 -

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images/255
test_images = test_images/255

classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

model = keras.Sequential([
tf.keras.layers.Conv1D(56, 3, activation='relu', input_shape=(28, 28)),
tf.keras.layers.MaxPooling1D(2, 2),
tf.keras.layers.Conv1D(56, 3, activation='relu'),
tf.keras.layers.MaxPooling1D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(784, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=5)

test_loss, test_acc = model.evaluate(test_images, test_labels)
  • 向您的数据集添加一个额外维度(使其为 28 X 28 -> 28 X 28 X1)。
  • 像这样 -

    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

    train_images = np.expand_dims(train_images, axis=-1) # (60000, 28, 28, 1)
    test_images = np.expand_dims(test_images, axis=-1) # (10000, 28, 28, 1)

    train_images = train_images/255
    test_images = test_images/255

    #train_images.shape, test_images.shape
    classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']


    input_layer = keras.layers.Input(shape=(28, 28, 1), name='image_input')
    conv1 = keras.layers.Conv2D(56, (3,3), activation='relu')(input_layer)
    pool1 = keras.layers.MaxPooling2D(2, 2)(conv1)
    conv2 = keras.layers.Conv2D(56, (3,3), activation='relu')(pool1)
    pool2 = keras.layers.MaxPooling2D(2, 2)(conv2)
    flatten = keras.layers.Flatten()(pool2)
    dense1 = keras.layers.Dense(784, activation='relu')(flatten)
    output_layer = keras.layers.Dense(10, activation='softmax')(dense1)

    model = keras.models.Model(inputs=input_layer, outputs=output_layer, name='my_model')
    model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
    model.fit(train_images, train_labels, epochs=5)

    test_loss, test_acc = model.evaluate(test_images, test_labels)

    关于python - Keras 数字数据集 : expected conv2d_input to have 4 dimensions, 中出现错误,但获得形状为 (60000, 28, 28) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58576446/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com