Keras 输入形状抛出预期值错误 4d 但得到一个形状为 (60000, 28,28) 的数组
Keras input shape throws value error expected 4d but got an array with shape (60000, 28,28)
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
x_train.shape #Shape is (60000, 28, 28)
然后模型确保输入形状为 28,28,1,因为 60k 是样本。
model2 = tf.keras.Sequential()
# Must define the input shape in the first layer of the neural network
model2.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1)))
model2.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model2.add(tf.keras.layers.Dropout(0.3))
model2.add(tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))
model2.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model2.add(tf.keras.layers.Dropout(0.3))
model2.add(tf.keras.layers.Flatten())
model2.add(tf.keras.layers.Dense(256, activation='relu'))
model2.add(tf.keras.layers.Dropout(0.5))
model2.add(tf.keras.layers.Dense(10, activation='softmax'))
model2.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model2.fit(x_train,
y_train,
batch_size=64,
epochs=25,)
我收到错误:
ValueError:检查输入时出错:预期 conv2d_19_input 有 4 个维度,但得到形状为 (60000, 28, 28)
的数组
就像每次我试图理解输入形状时,我都会变得更加困惑。就像我现在对 conv2d 和 dense 的输入形状感到困惑一样。
无论如何,为什么这是错误的?
是的,这是正确的,参数 input_shape
准备取 3 个值。但是函数 Conv2D
需要一个 4D 数组作为输入,覆盖:
- 样本数
- 通道数
- 图像宽度
- 图像高度
而函数load_data()
是由宽度、高度和样本数组成的3D数组。
您可以通过简单的重塑来解决问题:
train_X = train_X.reshape(-1, 28,28, 1)
test_X = test_X.reshape(-1, 28,28, 1)
来自 keras 文档的更好定义:
Input shape:
4D tensor with shape: (batch, channels, rows, cols) if data_format is "channels_first" or 4D tensor with shape: (batch, rows, cols, channels) if data_format is "channels_last".
您缺少通道维度(值为一),可以通过重塑数组轻松更正:
x_train = x_train.reshape((-1, 28, 28, 1))
x_test = x_test.reshape((-1, 28, 28, 1))
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
x_train.shape #Shape is (60000, 28, 28)
然后模型确保输入形状为 28,28,1,因为 60k 是样本。
model2 = tf.keras.Sequential()
# Must define the input shape in the first layer of the neural network
model2.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1)))
model2.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model2.add(tf.keras.layers.Dropout(0.3))
model2.add(tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))
model2.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model2.add(tf.keras.layers.Dropout(0.3))
model2.add(tf.keras.layers.Flatten())
model2.add(tf.keras.layers.Dense(256, activation='relu'))
model2.add(tf.keras.layers.Dropout(0.5))
model2.add(tf.keras.layers.Dense(10, activation='softmax'))
model2.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model2.fit(x_train,
y_train,
batch_size=64,
epochs=25,)
我收到错误: ValueError:检查输入时出错:预期 conv2d_19_input 有 4 个维度,但得到形状为 (60000, 28, 28)
的数组就像每次我试图理解输入形状时,我都会变得更加困惑。就像我现在对 conv2d 和 dense 的输入形状感到困惑一样。 无论如何,为什么这是错误的?
是的,这是正确的,参数 input_shape
准备取 3 个值。但是函数 Conv2D
需要一个 4D 数组作为输入,覆盖:
- 样本数
- 通道数
- 图像宽度
- 图像高度
而函数load_data()
是由宽度、高度和样本数组成的3D数组。
您可以通过简单的重塑来解决问题:
train_X = train_X.reshape(-1, 28,28, 1)
test_X = test_X.reshape(-1, 28,28, 1)
来自 keras 文档的更好定义:
Input shape: 4D tensor with shape: (batch, channels, rows, cols) if data_format is "channels_first" or 4D tensor with shape: (batch, rows, cols, channels) if data_format is "channels_last".
您缺少通道维度(值为一),可以通过重塑数组轻松更正:
x_train = x_train.reshape((-1, 28, 28, 1))
x_test = x_test.reshape((-1, 28, 28, 1))