gpt4 book ai didi

python - Keras 密集层输出形状

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

我无法理解获取第一个隐藏层的输出形状背后的逻辑。我举了一些随意的例子如下;

示例 1:

model.add(Dense(units=4,activation='linear',input_shape=(784,)))  
model.add(Dense(units=10,activation='softmax'))
model.summary()

Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_7 (Dense) (None, 4) 3140
_________________________________________________________________
dense_8 (Dense) (None, 10) 50
=================================================================
Total params: 3,190
Trainable params: 3,190
Non-trainable params: 0

示例 2:
model.add(Dense(units=4,activation='linear',input_shape=(784,1)))   
model.add(Dense(units=10,activation='softmax'))
model.summary()
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_11 (Dense) (None, 784, 4) 8
_________________________________________________________________
dense_12 (Dense) (None, 784, 10) 50
=================================================================
Total params: 58
Trainable params: 58
Non-trainable params: 0

示例 3:
model.add(Dense(units=4,activation='linear',input_shape=(32,28)))    
model.add(Dense(units=10,activation='softmax'))
model.summary()
Model: "sequential_8"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_15 (Dense) (None, 32, 4) 116
_________________________________________________________________
dense_16 (Dense) (None, 32, 10) 50
=================================================================
Total params: 166
Trainable params: 166
Non-trainable params: 0

示例 4:
model.add(Dense(units=4,activation='linear',input_shape=(32,28,1)))    
model.add(Dense(units=10,activation='softmax'))
model.summary()
Model: "sequential_9"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_17 (Dense) (None, 32, 28, 4) 8
_________________________________________________________________
dense_18 (Dense) (None, 32, 28, 10) 50
=================================================================
Total params: 58
Trainable params: 58
Non-trainable params: 0

请帮助我理解逻辑。

另外,我认为排名 input_shape=(784,)input_shape=(784,1)是一样的那为什么他们的 Output Shape不同的?

最佳答案

根据 Keras 的官方文档,对于 Dense 层,当您输入为 input_shape=(input_units,) 时模态作为形状的输入数组 (*, input_units)并输出形状为 (*, output_units) 的数组[在你的情况下 input_shape=(784,)被视为 input shape=(*, 784)和输出是 output_shape=(*,4) ]

通常对于 (batch_size, ..., input_dim) 的输入维度,模态给出大小 (batch_size, ..., units) 的输出.

所以当你输入 input_shape=(784,)模态作为形状的输入数组 (*, 784) , 其中 *是批量大小和 784作为 input_dim,输出形状为 (*, 4) .

当输入为 (784,1) ,模态将其视为 (*, 784, 1)哪里*是批量大小,784...1是 input_dim => (batch_size, ..., input_dim)并输出为 (*, 784, 4) => (batch_size, ..., units) .
input_shape=(32,28)=>(*,32,28) 也是如此, 给出输出 (*,32,4)和输入 input_shape=(32,28,1)=>(*,32,28,1)又在哪里*是批量大小,32,28...1是 input_dim => (batch_size, ..., input_dim)
None 是什么意思,请查看 What is the meaning of the "None" in model.summary of KERAS?

关于python - Keras 密集层输出形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61560888/

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