對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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Python使用numpy實現(xiàn)BP神經(jīng)網(wǎng)絡(luò)
- pytorch下使用LSTM神經(jīng)網(wǎng)絡(luò)寫詩實例
- 基于MATLAB神經(jīng)網(wǎng)絡(luò)圖像識別的高識別率代碼
- 純用NumPy實現(xiàn)神經(jīng)網(wǎng)絡(luò)的示例代碼
- Python中LSTM回歸神經(jīng)網(wǎng)絡(luò)時間序列預(yù)測詳情
- Python基于numpy靈活定義神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)的方法
- Python利用全連接神經(jīng)網(wǎng)絡(luò)求解MNIST問題詳解
- tensorflow學(xué)習(xí)筆記之mnist的卷積神經(jīng)網(wǎng)絡(luò)實例
- numpy實現(xiàn)神經(jīng)網(wǎng)絡(luò)反向傳播算法的步驟
- Pytorch搭建簡單的卷積神經(jīng)網(wǎng)絡(luò)(CNN)實現(xiàn)MNIST數(shù)據(jù)集分類任務(wù)
- 如何在M芯片的Macbook上訓(xùn)練神經(jīng)網(wǎng)絡(luò)
相關(guān)文章
Python中BeautifulSoup通過查找Id獲取元素信息
這篇文章主要介紹了Python中BeautifulSoup通過查找Id獲取元素信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
peewee創(chuàng)建連接前的前置操作wireshark抓包實現(xiàn)
這篇文章主要為大家介紹了peewee創(chuàng)建連接前的前置操作wireshark?抓包實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
python實現(xiàn)將html表格轉(zhuǎn)換成CSV文件的方法
這篇文章主要介紹了python實現(xiàn)將html表格轉(zhuǎn)換成CSV文件的方法,涉及Python操作csv文件的相關(guān)技巧,需要的朋友可以參考下2015-06-06
如何使用 Python 讀取 Excel 數(shù)據(jù)
這篇文章主要介紹了使用 Python 讀取 Excel 數(shù)據(jù)的詳細(xì)教程,通過 pandas 和 openpyxl,你可以輕松讀取 Excel 文件,并進(jìn)行各種數(shù)據(jù)處理操作,pandas 更適合快速、簡單的數(shù)據(jù)分析,而 openpyxl 則適合需要對 Excel 文件進(jìn)行更深入控制的場景,需要的朋友可以參考下2025-04-04
一文詳解Python中常用的初等函數(shù)(內(nèi)置函數(shù))
初等函數(shù)是由基本初等函數(shù)經(jīng)過有限次的四則運(yùn)算和復(fù)合運(yùn)算所得到的函數(shù),這篇文章主要介紹了Python中常用初等函數(shù)(內(nèi)置函數(shù))的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-06-06
使用Anaconda3建立虛擬獨(dú)立的python2.7環(huán)境方法
今天小編就為大家分享一篇使用Anaconda3建立虛擬獨(dú)立的python2.7環(huán)境方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

