密集层没有给出预期的输出形状
Posted
技术标签:
【中文标题】密集层没有给出预期的输出形状【英文标题】:Dense layer does not give expected Output shape 【发布时间】:2022-01-12 20:35:33 【问题描述】:我正在尝试复制模型架构。在原始模型架构中,应用最后一个 Dense 层后,输出形状为 (None, 3),参数为 300。如图
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_Dense1 (Dense) (None, 100) 128100
dense_Dense2 (Dense) (None, 3) 300
但是当我应用密集输出形状时,我得到的是 (None, 3) 和 303 个参数。如下图
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_35 (Dense) (None, 100) 128100
dense_36 (Dense) (None, 3) 303
这是我为此部分编写的代码:
x = GlobalAveragePooling2D()(x)
x = Dense(100, activation="relu")(x)
prediction = Dense(3, activation='softmax')(x)
【问题讨论】:
【参考方案1】:您尝试复制的架构是否可能不使用偏见?试试not using bias:
Dense(3, activation='softmax', use_bias=False)
Model: "sequential_5"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
global_average_pooling2d_3 ( (None, 8) 0
_________________________________________________________________
dense_9 (Dense) (None, 100) 900
_________________________________________________________________
dense_10 (Dense) (None, 3) 300
=================================================================
Total params: 1,200
Trainable params: 1,200
Non-trainable params: 0
_________________________________________________________________
【讨论】:
以上是关于密集层没有给出预期的输出形状的主要内容,如果未能解决你的问题,请参考以下文章
Tensorflow ValueError:层“顺序”的输入0与层不兼容:预期形状=(无,20,20,3),找到形状=(无,20,3)