在 Tensorflow 中使用 Fashion MNIST 数据集。
当构建如下模型时,为什么展平层只有(28*28=724)而不是724x60000? 60000 是数据集中的图像数量?
模型中每张图像在什么时候使用?
#flatten the images into a 1D array
model = tf.keras.models.Sequential([
tf.keras. layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras. layers.Dense(10, activation=tf.nn.softmax)
])
# Get the input shape of the Flatten layer
input_shape = model.layers[0].input_shape
print("Input shape:", input_shape)
# Get the output shape of the Flatten layer
output_shape = model.layers[0].output_shape
print("Output shape:", output_shape)
正如评论中所述,该模型适合批量图像,这是数据集中的一小部分。如果您查看结果,您会发现:
#flatten the images into a 1D array
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras. layers.Dense(10, activation=tf.nn.softmax)
])
# Get the input shape of the Flatten layer
input_shape = model.layers[0].input_shape
print("Input shape:", input_shape)
>>> Input shape: (None, 28, 28)
# Get the output shape of the Flatten layer
output_shape = model.layers[0].output_shape
print("Output shape:", output_shape)
>>> Output shape: (None, 784)
您可以看到第一个维度是
None
,因为您的模型还不知道批量大小。