bn层需要初始化的参数主要是scale和bias,bias一般初始化为0,scale有两种初始化方式
- 初始化为1
- 初始化为0:这里假定bn层不存在,一般用在有残差的结构:resblock,因为有chortcut,所以这种初始化方式才可行
# Zero-initialize the last BN in each residual branch,
# so that the residual branch starts with zeros, and each residual block behaves like an identity.
# This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
if zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck):
nn.init.constant_(m.bn3.weight, 0)
elif isinstance(m, BasicBlock):
nn.init.constant_(m.bn2.weight, 0)
2. fc层的参数初始化
- weight:正态分布来初始化,均值为0,方差可以调整
- bias:0.01或者为0,一般为0
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01) #均值为0, 方差为0.01
nn.init.zeros_(m.bias)
3. conv层的参数初始化
- bias:0.01或者为0,一般为0
- weight:
(1)Xavier 初始化:Xavier 参数初始化、深度学习参数初始化(一)Xavier初始化 含代码
(2)He初始化:深度学习参数初始化(二)Kaiming初始化 含代码
在He初始化中可以选择mode为:fan_in或者fan_out,有博主总结:
- 如果权重是通过线性层(卷积或全连接)隐性确定的,则需设置mode=fan_in;
- 如果通过创建随机矩阵显式创建权重,则应进行设置mode=‘fan_out’。
我看了下各大主流的模型如mobilenet_v2,resnet都采用的是mode=‘fan_out’



