Kar*_*rus 79 machine-learning neural-network deep-learning keras tensorflow
我试图了解该Flatten
功能在Keras中的作用.下面是我的代码,这是一个简单的双层网络.它接收形状(3,2)的二维数据,并输出形状(1,4)的1维数据:
model = Sequential()
model.add(Dense(16, input_shape=(3, 2)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(4))
model.compile(loss='mean_squared_error', optimizer='SGD')
x = np.array([[[1, 2], [3, 4], [5, 6]]])
y = model.predict(x)
print y.shape
Run Code Online (Sandbox Code Playgroud)
这打印出y
形状(1,4).但是,如果我删除该Flatten
行,则打印出y
具有形状(1,3,4)的行.
我不明白这一点.根据我对神经网络的理解,该model.add(Dense(16, input_shape=(3, 2)))
功能正在创建一个隐藏的全连接层,具有16个节点.这些节点中的每一个都连接到3x2输入元件中的每一个.因此,该第一层输出处的16个节点已经"平坦".因此,第一层的输出形状应为(1,16).然后,第二层将其作为输入,并输出形状(1,4)的数据.
因此,如果第一层的输出已经是"平坦的"和形状(1,16),为什么我需要进一步压平它?
谢谢!
Mar*_*jko 93
如果您阅读Dense
此处的文档,您将看到:
Dense(16, input_shape=(5,3))
Run Code Online (Sandbox Code Playgroud)
将导致Dense
具有3个输入和16个输出的网络,这些输入将独立应用于5个步骤中的每个步骤.因此,如果D(x)
将3维向量转换为16维向量,您将从图层输出的内容将是一系列向量:[D(x[0,:], D(x[1,:],..., D(x[4,:]]
具有形状(5, 16)
.为了获得您指定的行为,您可以先Flatten
输入15维向量,然后应用Dense
:
model = Sequential()
model.add(Flatten(input_shape=(3, 2)))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dense(4))
model.compile(loss='mean_squared_error', optimizer='SGD')
Run Code Online (Sandbox Code Playgroud)
编辑: 有些人努力理解 - 在这里你有一个解释图像:
pro*_*sti 18
short read:
Flattening a tensor means to remove all of the dimensions except for one. This is exactly what the Flatten layer do.
long read:
If we take the original model (with the Flatten layer) created in consideration we can get the following model summary:
Layer (type) Output Shape Param #
=================================================================
D16 (Dense) (None, 3, 16) 48
_________________________________________________________________
A (Activation) (None, 3, 16) 0
_________________________________________________________________
F (Flatten) (None, 48) 0
_________________________________________________________________
D4 (Dense) (None, 4) 196
=================================================================
Total params: 244
Trainable params: 244
Non-trainable params: 0
Run Code Online (Sandbox Code Playgroud)
For this summary the next image will hopefully provide little more sense on the input and output sizes for each layer.
The output shape for the Flatten layer as you can read is (None, 48)
. Here is the tip. You should read it (1, 48)
or (2, 48)
or ... or (16, 48)
... or (32, 48)
, ...
In fact, None
on that position means any batch size. For the inputs to recall, the first dimension means the batch size and the second means the number of input features.
The role of the Flatten layer in Keras is super simple:
A flatten operation on a tensor reshapes the tensor to have the shape that is equal to the number of elements contained in tensor non including the batch dimension.
Note: I used the model.summary()
method to provide the output shape and parameter details.
扁平化是将数据转换为一维数组,以便输入到下一层。我们展平卷积层的输出以创建单个长特征向量。在某些架构中,例如 CNN,如果图像是 1D 形式而不是 2D 形式,那么神经网络可以更好地处理图像。
归档时间: |
|
查看次数: |
60764 次 |
最近记录: |