gpt4 book ai didi

python - Keras 顺序密集输入层 - 和 MNIST : why do images need to be reshaped?

转载 作者:太空宇宙 更新时间:2023-11-03 11:15:38 25 4
gpt4 key购买 nike

我问这个是因为我觉得我缺少一些基本的东西。

现在大多数人都知道 MNIST 图像是 28X28 像素。 keras documentation告诉我有关 Dense 的信息:

Input shape nD tensor with shape: (batch_size, ..., input_dim). The most common situation would be a 2D input with shape (batch_size, input_dim).

所以像我这样的新手会假设图像可以作为 28*28 矩阵输入到模型中。然而,我找到的每个教程都经过各种体操,将图像转换为一个 784 长的特征。

有时是

num_pixels = X_train.shape[1] * X_train.shape[2]
model.add(Dense(num_pixels, input_dim=num_pixels, activation='...'))

num_pixels = np.prod(X_train.shape[1:])
model.add(Dense(512, activation='...', input_shape=(num_pixels,)))

model.add(Dense(units=10, input_dim=28*28, activation='...'))
history = model.fit(X_train.reshape((-1,28*28)), ...)

甚至:

model = Sequential([Dense(32, input_shape=(784,)), ...),])

所以我的问题很简单 - 为什么?难道 Dense 不能按原样接受图像,或者在必要时“在幕后”处理它吗?如果像我怀疑的那样必须进行此处理,那么这些方法(或其他方法)中的任何一种在本质上是否更可取?

最佳答案

应 OP(即 Original Poster)的要求,我将在评论中提及我给出的答案并详细说明。

Can't Dense just accept an image as-is or, if necessary, just process it "behind the scenes", as it were?

根本不!那是因为 currently the Dense layer is applied on the last axis .因此,如果您为其提供形状为 (height, width)(height, width, channels) 的图像,Dense 层将仅应用于最后一个轴(即宽度或 channel )。然而,当图像被扁平化时,Dense 层中的所有单元将应用于整个图像,并且每个单元连接到具有不同权重的所有像素。为了进一步阐明这一点,请考虑以下模型:

model = models.Sequential()
model.add(layers.Dense(10, input_shape=(28*28,)))
model.summary()

模型总结:

Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense) (None, 10) 7850
=================================================================
Total params: 7,850
Trainable params: 7,850
Non-trainable params: 0
_________________________________________________________________

如您所见,Dense 层中有 7850 个参数:每个单元连接到所有像素(28*28*10 + 10 个偏置参数 = 7850)。现在考虑这个模型:

model = models.Sequential()
model.add(layers.Dense(10, input_shape=(28,28)))
model.summary()

模型总结:

_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_3 (Dense) (None, 28, 10) 290
=================================================================
Total params: 290
Trainable params: 290
Non-trainable params: 0
_________________________________________________________________

在这种情况下,Dense 层中只有 290 个参数。这里 Dense 层中的每个单元也连接到所有像素,但不同之处在于权重在第一个轴上共享(28*10 + 10 偏置参数 = 290)。与之前在整个图像中提取特征的模型相比,就好像是从图像的每一行中提取特征一样。因此,这(即权重共享)可能对您的应用有用,也可能没有用。

关于python - Keras 顺序密集输入层 - 和 MNIST : why do images need to be reshaped?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184142/

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