函数的增益值
torch.nn.init.calculate_gain(nonlinearity, param=None)
提供了对非线性函数增益值的计算。
增益值gain是一个比例值,来调控输入数量级和输出数量级之间的关系。
fan_in和fan_out
pytorch计算fan_in和fan_out的源码
def _calculate_fan_in_and_fan_out(tensor):
dimensions = tensor.ndimension()
if dimensions < 2:
raise ValueError("Fan in and fan out can not be computed
for tensor with fewer than 2 dimensions")
if dimensions == 2: # Linear
fan_in = tensor.size(1)
fan_out = tensor.size(0)
else:
num_input_fmaps = tensor.size(1)
num_output_fmaps = tensor.size(0)
receptive_field_size = 1
if tensor.dim() > 2:
receptive_field_size = tensor[0][0].numel()
fan_in = num_input_fmaps * receptive_field_size
fan_out = num_output_fmaps * receptive_field_size
return fan_in, fan_out
对于全连接层,fan_in是输入维度,fan_out是输出维度;对于卷积层,设其维度为
[
C
o
u
t
,
C
i
n
,
H
,
W
]
[C_{out},C_{in},H,W]
[Cout,Cin,H,W],其中
H
×
W
H\times W
H×W为kernel规模。则fan_in是
H
×
W
×
C
i
n
H\times W\times C_{in}
H×W×Cin,fan_out是
H
×
W
×
C
o
u
t
H\times W\times C_{out}
H×W×Cout。
举例: 设输入的数都是出于同一数量级。输入维度较小时,
x
=
[
1
,
1
]
T
x=[1,1]^{T}
x=[1,1]T,此时方差较大,正态分布生成的参数初始值都比较大,
w
=
[
0.5
,
0.5
]
T
w=[0.5,0.5]^{T}
w=[0.5,0.5]T,得到的值
w
T
x
=
1
w^{T}x=1
wTx=1。当输入维度较大时,
x
=
[
1
,
1
,
1
,
1
]
T
x=[1,1,1,1]^{T}
x=[1,1,1,1]T,此时方差较小,正态分布生成的参数初始值都比较小,
w
=
[
0.25
,
0.25
,
0.25
,
0.25
]
T
w=[0.25,0.25,0.25,0.25]^{T}
w=[0.25,0.25,0.25,0.25]T,得到的值
w
T
x
=
1
w^{T}x=1
wTx=1,数量级是不变的。
xavier分布
xavier分布解析:https://prateekvjoshi.com/2016/03/29/understanding-xavier-initialization-in-deep-neural-networks/
假设使用的是sigmoid函数。当权重值(值指的是绝对值)过小,输入值每经过网络层,方差都会减少,每一层的加权和很小,在sigmoid函数0附件的区域相当于线性函数,失去了DNN的非线性性。
当权重的值过大,输入值经过每一层后方差会迅速上升,每层的输出值将会很大,此时每层的梯度将会趋近于0.
xavier初始化可以使得输入值
x
x
x方差经过网络层后的输出值
y
y
y方差不变。
(1)xavier的均匀分布
torch.nn.init.xavier_uniform_(tensor, gain=1)
填充一个tensor使用
U
(
−
a
,
a
)
\mathcal{U}(-a,a)
U(−a,a),
a
=
g
a
i
n
×
6
f
a
n
_
i
n
+
f
a
n
_
o
u
t
a=gain\times \sqrt{\frac{6}{fan\_in+fan\_out}}
a=gain×fan_in+fan_out6
也称为Glorot initialization。
>>> w = torch.empty(3, 5)
>>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu'))
(2)xavier正态分布
torch.nn.init.xavier_normal_(tensor, gain=1)
填充一个tensor使用
N
(
0
,
s
t
d
)
\mathcal{N}(0,std)
N(0,std),
s
t
d
=
g
a
i
n
×
2
f
a
n
_
i
n
+
f
a
n
_
o
u
t
std=gain\times \sqrt{\frac{2}{fan\_in+fan\_out}}
std=gain×fan_in+fan_out2
也称为Glorot initialization。
kaiming分布
Xavier在tanh中表现的很好,但在Relu激活函数中表现的很差,所何凯明提出了针对于relu的初始化方法。pytorch默认使用kaiming正态分布初始化卷积层参数。
(1)kaiming均匀分布
torch.nn.init.kaiming_uniform_
(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')
使用均匀分布
U
(
−
b
o
u
n
d
,
b
o
u
n
d
)
\mathcal{U}(-bound,bound)
U(−bound,bound)
b
o
u
n
d
=
6
(
1
+
a
2
)
×
f
a
n
_
i
n
bound=\sqrt{\frac{6}{(1+a^{2})\times fan\_in}}
bound=(1+a2)×fan_in6
也被称为 He initialization。
- a – the negative slope of the rectifier used after this layer (0 for ReLU by default).激活函数的负斜率,
- mode – either ‘fan_in’ (default) or ‘fan_out’. Choosing fan_in
preserves the magnitude of the variance of the weights in the forward
pass. Choosing fan_out preserves the magnitudes in the backwards
pass.默认为fan_in模式,fan_in可以保持前向传播的权重方差的数量级,fan_out可以保持反向传播的权重方差的数量级。
>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu')
(2)kaiming正态分布
torch.nn.init.kaiming_normal_
(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')
使用正态分布
N
(
0
,
s
t
d
)
\mathcal{N}(0,std)
N(0,std)
s
t
d
=
2
(
1
+
a
2
)
×
f
a
n
_
i
n
std=\sqrt{\frac{2}{(1+a^{2})\times fan\_in}}
std=(1+a2)×fan_in2
也被称为 He initialization。
>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')