PyTorch torch.nn.init

2020-09-15 10:41 更新

原文:PyTorch torch.nn.init

  1. torch.nn.init.calculate_gain(nonlinearity, param=None)?

返回給定非線性函數(shù)的推薦增益值。 取值如下:

|

非線性

|

獲得

| | --- | --- | | 線性/身份 | img | | 轉(zhuǎn)換{1,2,3} D | img | | 乙狀結(jié)腸 | img | | h | img | | ReLU | img | | 泄漏的露露 | img |

參數(shù)

  • 非線性 –非線性函數(shù) (<cite>nn.functional</cite> 名稱)
  • 參數(shù) –非線性功能的可選參數(shù)

例子

  1. >>> gain = nn.init.calculate_gain('leaky_relu', 0.2) # leaky_relu with negative_slope=0.2

  1. torch.nn.init.uniform_(tensor, a=0.0, b=1.0)?

用從均勻分布img中得出的值填充輸入張量。

Parameters

  • 張量 – n 維<cite>torch.張量</cite>
  • a –均勻分布的下限
  • b –均勻分布的上限

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.uniform_(w)

  1. torch.nn.init.normal_(tensor, mean=0.0, std=1.0)?

使用從正態(tài)分布img中得出的值填充輸入張量。

Parameters

  • tensor – an n-dimensional <cite>torch.Tensor</cite>
  • 平均值 –正態(tài)分布的平均值
  • std –正態(tài)分布的標準偏差

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.normal_(w)

  1. torch.nn.init.constant_(tensor, val)?

用值img填充輸入張量。

Parameters

  • tensor – an n-dimensional <cite>torch.Tensor</cite>
  • val –用張量填充張量的值

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.constant_(w, 0.3)

  1. torch.nn.init.ones_(tensor)?

用標量值 <cite>1</cite> 填充輸入張量。

Parameters

tensor – an n-dimensional <cite>torch.Tensor</cite>

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.ones_(w)

  1. torch.nn.init.zeros_(tensor)?

用標量值 <cite>0</cite> 填充輸入張量。

Parameters

tensor – an n-dimensional <cite>torch.Tensor</cite>

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.zeros_(w)

  1. torch.nn.init.eye_(tensor)?

用單位矩陣填充二維輸入<cite>張量</cite>。 在<cite>線性</cite>層中保留輸入的身份,在該層中將保留盡可能多的輸入。

Parameters

張量 –二維<cite>torch.張量</cite>

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.eye_(w)

  1. torch.nn.init.dirac_(tensor)?

使用 Dirac delta 函數(shù)填充{3,4,5}維輸入<cite>張量</cite>。 保留<cite>卷積</cite>層中輸入的身份,其中保留盡可能多的輸入通道。

Parameters

張量 – {3,4,5}維的<cite>torch.張量</cite>

Examples

  1. >>> w = torch.empty(3, 16, 5, 5)
  2. >>> nn.init.dirac_(w)

  1. torch.nn.init.xavier_uniform_(tensor, gain=1.0)?

根據(jù)<cite>中所述的方法,用值填充輸入<cite>張量</cite>的值。了解訓(xùn)練深度前饋神經(jīng)網(wǎng)絡(luò)</cite>的難度-Glorot,X。& Bengio,Y.(2010),使用 均勻分布。 結(jié)果張量將具有從img采樣的值,其中

img

也稱為 Glorot 初始化。

Parameters

  • tensor – an n-dimensional <cite>torch.Tensor</cite>
  • 增益 –可選的比例因子

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu'))

  1. torch.nn.init.xavier_normal_(tensor, gain=1.0)?

根據(jù)<cite>中所述的方法,用值填充輸入<cite>張量</cite>。了解訓(xùn)練深度前饋神經(jīng)網(wǎng)絡(luò)</cite>的難度-Glorot,X。& Bengio,Y.(2010),使用 正態(tài)分布。 結(jié)果張量將具有從img采樣的值,其中

img

Also known as Glorot initialization.

Parameters

  • tensor – an n-dimensional <cite>torch.Tensor</cite>
  • gain – an optional scaling factor

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.xavier_normal_(w)

  1. torch.nn.init.kaiming_uniform_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')?

根據(jù)<cite>中描述的方法,用值填充輸入<cite>張量</cite>的值。深入研究整流器:在 ImageNet 分類</cite>上超越人類水平的性能-He,K.等。 (2015),使用均勻分布。 結(jié)果張量將具有從img采樣的值,其中

img

也稱為 He 初始化。

Parameters

  • tensor – an n-dimensional <cite>torch.Tensor</cite>
  • a –在該層之后使用的整流器的負斜率(僅
  • with'leaky_relu')(使用了)–
  • 模式'fan_in'(默認)或'fan_out'。 選擇'fan_in'會保留前向傳遞中權(quán)重差異的大小。 選擇'fan_out'可以保留反向傳遞的幅度。
  • 非線性 –非線性函數(shù)(<cite>為功能性</cite>名稱),建議僅與'relu''leaky_relu'(默認)一起使用。

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu')

  1. torch.nn.init.kaiming_normal_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')?

根據(jù)<cite>中描述的方法,用值填充輸入<cite>張量</cite>的值。深入研究整流器:在 ImageNet 分類</cite>上超越人類水平的性能-He,K.等。 (2015),使用正態(tài)分布。 結(jié)果張量將具有從img采樣的值,其中

img

Also known as He initialization.

Parameters

  • tensor – an n-dimensional <cite>torch.Tensor</cite>
  • a – the negative slope of the rectifier used after this layer (only
  • with 'leaky_relu') (used) –
  • 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.
  • nonlinearity – the non-linear function (<cite>nn.functional</cite> name), recommended to use only with 'relu' or 'leaky_relu' (default).

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')

  1. torch.nn.init.orthogonal_(tensor, gain=1)?

用(半)正交矩陣填充輸入的<cite>張量</cite>,如<cite>中所述,用于深度線性神經(jīng)網(wǎng)絡(luò)</cite>中學(xué)習(xí)的非線性動力學(xué)的精確解-Saxe,A.等。 (2013)。 輸入張量必須至少具有 2 個維度,對于 2 個以上的張量,尾隨維度將被展平。

Parameters

  • 張量 – n 維<cite>torch張量</cite>,其中img
  • 增益 –可選比例因子

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.orthogonal_(w)

  1. torch.nn.init.sparse_(tensor, sparsity, std=0.01)?

將 2D 輸入<cite>張量</cite>填充為稀疏矩陣,其中將從正態(tài)分布img提取非零元素,如<cite>通過無麻木優(yōu)化的深度學(xué)習(xí)</cite>中所述- Martens,J.(2010 年)。

Parameters

  • tensor – an n-dimensional <cite>torch.Tensor</cite>
  • 稀疏性 –每列中要設(shè)置為零的元素比例
  • std –用于生成非零值的正態(tài)分布的標準偏差

Examples

  1. >>> w = torch.empty(3, 5)
  2. >>> nn.init.sparse_(w, sparsity=0.1)
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號