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值取樣於,
其中:
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) ,
其中:
參數: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值取樣於,
其中: