作者:尛丶俊_188 | 来源:互联网 | 2023-08-31 08:21
1.导入常用初始化方法fromtorch.nn.initimportxavier_uniform_,xavier_normal_fromtorch.nn.initimportkai
1. 导入常用初始化方法
from torch.nn.init import xavier_uniform_, xavier_normal_
from torch.nn.init import kaiming_uniform_, kaiming_normal_
2. 各种初始化方法分析
xavier_uniform_
(tensor, gain=1.0)
Note: 以均匀分布的值初始化输入tensor. 方法根据《Understanding the difficulty of training deep feedforward neural networks – Glorot, X. & Bengio, Y. (2010)》论文实现。最终得到的Tesor值取样于U(−a,a) ,
其中:
\
参数:
gain: 缩放因素(optional)
xavier_normal_
(tensor, gain=1.0)
Note: 以正太分布的值初始化输入tensor. 方法根据《Understanding the difficulty of training deep feedforward neural networks – Glorot, X. & Bengio, Y. (2010)》论文实现。最终得到的Tesor值取样于
,
其中: ![《【Pytorch】tensor初始化方法》](https://img7.php1.cn/3cdc5/f3f0/ae9/aa3a46b6b1ff70a5.gif)
kaiming_uniform_
(tensor, a=0, mode=’fan_in’, nOnlinearity=’leaky_relu’)
Note: 以均匀分布的值初始化输入tensor. 方法根据《Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification – He, K. et al. (2015)》论文实现。最终得到的Tesor值取样于U(−bound,bound) ,
其中: ![《【Pytorch】tensor初始化方法》](https://img7.php1.cn/3cdc5/f3f0/ae9/b502217a018441e2.gif)
参数:a:
mode: “fan_in” 或 “fan_out”. 选择“fan_in” 在前向传播中保存权重方差的幅度, ”fan_out” 在后向传播中保存幅度。
nonlinearity: 非线性函数。推荐”relu” or “leaky_relu”.
kaiming_normal_
(tensor, a=0, mode=’fan_in’, nOnlinearity=’leaky_relu’)
Note: 以正太分布的值初始化输入tensor. 方法根据《Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification – He, K. et al. (2015)》论文实现。最终得到的Tesor值取样于
,
其中: ![《【Pytorch】tensor初始化方法》](https://img7.php1.cn/3cdc5/f3f0/ae9/1f5048f81e7eee0c.png)