對Pytorch神經(jīng)網(wǎng)絡(luò)初始化kaiming分布詳解
函數(shù)的增益值
torch.nn.init.calculate_gain(nonlinearity, param=None)
提供了對非線性函數(shù)增益值的計算。
增益值gain是一個比例值,來調(diào)控輸入數(shù)量級和輸出數(shù)量級之間的關(guān)系。
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
xavier分布
xavier分布解析:https://prateekvjoshi.com/2016/03/29/understanding-xavier-initialization-in-deep-neural-networks/
假設(shè)使用的是sigmoid函數(shù)。當(dāng)權(quán)重值(值指的是絕對值)過小,輸入值每經(jīng)過網(wǎng)絡(luò)層,方差都會減少,每一層的加權(quán)和很小,在sigmoid函數(shù)0附件的區(qū)域相當(dāng)于線性函數(shù),失去了DNN的非線性性。
當(dāng)權(quán)重的值過大,輸入值經(jīng)過每一層后方差會迅速上升,每層的輸出值將會很大,此時每層的梯度將會趨近于0.
xavier初始化可以使得輸入值x x x<math><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math>x方差經(jīng)過網(wǎng)絡(luò)層后的輸出值y y y<math><semantics><mrow><mi>y</mi></mrow><annotation encoding="application/x-tex">y</annotation></semantics></math>y方差不變。
(1)xavier的均勻分布
torch.nn.init.xavier_uniform_(tensor, gain=1)
也稱為Glorot initialization。
>>> w = torch.empty(3, 5) >>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu'))
(2) xavier正態(tài)分布
torch.nn.init.xavier_normal_(tensor, gain=1)
也稱為Glorot initialization。
kaiming分布
Xavier在tanh中表現(xiàn)的很好,但在Relu激活函數(shù)中表現(xiàn)的很差,所何凱明提出了針對于relu的初始化方法。pytorch默認(rèn)使用kaiming正態(tài)分布初始化卷積層參數(shù)。
(1) kaiming均勻分布
torch.nn.init.kaiming_uniform_ (tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')
也被稱為 He initialization。
a – the negative slope of the rectifier used after this layer (0 for ReLU by default).激活函數(shù)的負(fù)斜率,
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.默認(rèn)為fan_in模式,fan_in可以保持前向傳播的權(quán)重方差的數(shù)量級,fan_out可以保持反向傳播的權(quán)重方差的數(shù)量級。
>>> w = torch.empty(3, 5) >>> nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu')
(2) kaiming正態(tài)分布
torch.nn.init.kaiming_normal_ (tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')
也被稱為 He initialization。
>>> w = torch.empty(3, 5) >>> nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')
以上這篇對Pytorch神經(jīng)網(wǎng)絡(luò)初始化kaiming分布詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python(TensorFlow框架)實現(xiàn)手寫數(shù)字識別系統(tǒng)的方法
這篇文章主要介紹了Python(TensorFlow框架)實現(xiàn)手寫數(shù)字識別系統(tǒng)的方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05python實現(xiàn)提取jira bug列表的方法示例
公司要求內(nèi)部每日整理jira bug發(fā)郵件,手動執(zhí)行了一段時間,想著用自動化的方式實現(xiàn),所以本文主要介紹了python實現(xiàn)提取jira bug列表,感興趣的可以了解一下2021-05-05