轉載請注明出處:
http://www.cnblogs.com/darkknightzh/p/8297793.html
參考網址:
http://pytorch.org/docs/0.3.0/nn.html?highlight=kaiming#torch.nn.init.kaiming_normal
https://github.com/prlz77/ResNeXt.pytorch/blob/master/models/model.py
https://github.com/facebookresearch/ResNeXt/blob/master/models/resnext.lua
https://github.com/bamos/densenet.pytorch/blob/master/densenet.py
https://github.com/szagoruyko/wide-residual-networks/blob/master/models/utils.lua
說明:暫時就這么多吧,錯誤之處請見諒。前兩個初始化的方法見pytorch官方文檔
1. xavier初始化
torch.nn.init.xavier_uniform(tensor, gain=1)
對於輸入的tensor或者變量,通過論文Understanding the difficulty of training deep feedforward neural networks” - Glorot, X. & Bengio, Y. (2010)的方法初始化數據。初始化服從均勻分布$U(-a,a)$,其中$a=gain\times \sqrt{2/(fan\_in+fan\_out)}\times \sqrt{3}$,該初始化方法也稱Glorot initialisation。
參數:
tensor:n維的 torch.Tensor 或者 autograd.Variable類型的數據
a:可選擇的縮放參數
例如:
w = torch.Tensor(3, 5) nn.init.xavier_uniform(w, gain=nn.init.calculate_gain('relu'))
torch.nn.init.xavier_normal(tensor, gain=1)
對於輸入的tensor或者變量,通過論文Understanding the difficulty of training deep feedforward neural networks” - Glorot, X. & Bengio, Y. (2010)的方法初始化數據。初始化服從高斯分布$N(0,std)$,其中$std=gain\times \sqrt{2/(fan\_in+fan\_out)}$,該初始化方法也稱Glorot initialisation。
參數:
tensor:n維的 torch.Tensor 或者 autograd.Variable類型的數據
a:可選擇的縮放參數
例如:
w = torch.Tensor(3, 5)
nn.init.xavier_normal(w)
2. kaiming初始化
torch.nn.init.kaiming_uniform(tensor, a=0, mode='fan_in')
對於輸入的tensor或者變量,通過論文“Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification” - He, K. et al. (2015)的方法初始化數據。初始化服從均勻分布$U(-bound,bound)$,其中$bound=\sqrt{2/((1+{{a}^{2}})\times fan\_in)}\times \sqrt{3}$,該初始化方法也稱He initialisation。
參數:
tensor:n維的 torch.Tensor 或者 autograd.Variable類型的數據
a:該層后面一層的激活函數中負的斜率(默認為ReLU,此時a=0)
mode:‘fan_in’ (default) 或者 ‘fan_out’. 使用fan_in保持weights的方差在前向傳播中不變;使用fan_out保持weights的方差在反向傳播中不變。
例如:
w = torch.Tensor(3, 5) nn.init.kaiming_uniform(w, mode='fan_in')
torch.nn.init.kaiming_normal(tensor, a=0, mode='fan_in')
對於輸入的tensor或者變量,通過論文“Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification” - He, K. et al. (2015)的方法初始化數據。初始化服從高斯分布$N(0,std)$,其中$std=\sqrt{2/((1+{{a}^{2}})\times fan\_in)}$,該初始化方法也稱He initialisation。
參數:
tensor:n維的 torch.Tensor 或者 autograd.Variable類型的數據
a:該層后面一層的激活函數中負的斜率(默認為ReLU,此時a=0)
mode:‘fan_in’ (default) 或者 ‘fan_out’. 使用fan_in保持weights的方差在前向傳播中不變;使用fan_out保持weights的方差在反向傳播中不變。
例如:
w = torch.Tensor(3, 5) nn.init.kaiming_normal(w, mode='fan_out')
使用的例子(具體參見原始網址):
https://github.com/prlz77/ResNeXt.pytorch/blob/master/models/model.py
from torch.nn import init self.classifier = nn.Linear(self.stages[3], nlabels) init.kaiming_normal(self.classifier.weight) for key in self.state_dict(): if key.split('.')[-1] == 'weight': if 'conv' in key: init.kaiming_normal(self.state_dict()[key], mode='fan_out') if 'bn' in key: self.state_dict()[key][...] = 1 elif key.split('.')[-1] == 'bias': self.state_dict()[key][...] = 0
3. 實際使用中看到的初始化
3.1 ResNeXt,densenet中初始化
https://github.com/facebookresearch/ResNeXt/blob/master/models/resnext.lua
https://github.com/bamos/densenet.pytorch/blob/master/densenet.py
conv
n = kW* kH*nOutputPlane weight:normal(0,math.sqrt(2/n)) bias:zero()
batchnorm
weight:fill(1) bias:zero()
linear
bias:zero()
3.2 wide-residual-networks中初始化(MSRinit)
https://github.com/szagoruyko/wide-residual-networks/blob/master/models/utils.lua
conv
n = kW* kH*nInputPlane weight:normal(0,math.sqrt(2/n)) bias:zero()
linear
bias:zero()