# torch.nn.init
# torch.nn.init
```
torch.nn.init.calculate_gain(nonlinearity,param=None)
```
對于給定的非線性函數,返回推薦的增益值。這些值如下所示:
nonlinearitygainlinear1conv{1,2,3}d1sigmoid1tanh5/3relusqrt(2)leaky\_relusqrt(2/(1+negative\_slope^2))**參數:**
- **nonlinearity** - 非線性函數(`nn.functional`名稱)
- **param** - 非線性函數的可選參數
**例子:**
```
>>> gain = nn.init.gain('leaky_relu')
```
```
torch.nn.init.uniform(tensor, a=0, b=1)
```
從均勻分布U(a, b)中生成值,填充輸入的張量或變量
**參數:**
- **tensor** - n維的torch.Tensor
- **a** - 均勻分布的下界
- **b** - 均勻分布的上界
**例子**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.uniform(w)
```
```
torch.nn.init.normal(tensor, mean=0, std=1)
```
從給定均值和標準差的正態分布N(mean, std)中生成值,填充輸入的張量或變量
**參數:**
- **tensor** – n維的torch.Tensor
- **mean** – 正態分布的均值
- **std** – 正態分布的標準差
**例子**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.normal(w)
```
```
torch.nn.init.constant(tensor, val)
```
用*val*的值填充輸入的張量或變量
**參數:**
- **tensor** – n維的torch.Tensor或autograd.Variable
- **val** – 用來填充張量的值
**例子:**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.constant(w)
```
```
torch.nn.init.eye(tensor)
```
用單位矩陣來填充2維輸入張量或變量。在線性層盡可能多的保存輸入特性。
**參數:**
- **tensor** – 2維的torch.Tensor或autograd.Variable
**例子:**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.eye(w)
```
```
torch.nn.init.dirac(tensor)
```
用Dirac $\\delta$ 函數來填充{3, 4, 5}維輸入張量或變量。在卷積層盡可能多的保存輸入通道特性。
**參數:**
- **tensor** – {3, 4, 5}維的torch.Tensor或autograd.Variable
**例子:**
```
>>> w = torch.Tensor(3, 16, 5, 5)
>>> nn.init.dirac(w)
```
```
torch.nn.init.xavier_uniform(tensor, gain=1)
```
根據Glorot, X.和Bengio, Y.在“Understanding the difficulty of training deep feedforward neural networks”中描述的方法,用一個均勻分布生成值,填充輸入的張量或變量。結果張量中的值采樣自U(-a, a),其中a= gain *sqrt( 2/(fan\_in + fan\_out))* sqrt(3). 該方法也被稱為Glorot initialisation
**參數:**
- **tensor** – n維的torch.Tensor
- **gain** - 可選的縮放因子
**例子:**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.xavier_uniform(w, gain=math.sqrt(2.0))
```
```
torch.nn.init.xavier_normal(tensor, gain=1)
```
根據Glorot, X.和Bengio, Y. 于2010年在“Understanding the difficulty of training deep feedforward neural networks”中描述的方法,用一個正態分布生成值,填充輸入的張量或變量。結果張量中的值采樣自均值為0,標準差為gain \* sqrt(2/(fan\_in + fan\_out))的正態分布。也被稱為Glorot initialisation.
**參數:**
- **tensor** – n維的torch.Tensor
- **gain** - 可選的縮放因子
**例子:**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.xavier_normal(w)
```
```
torch.nn.init.kaiming_uniform(tensor, a=0, mode='fan_in')
```
根據He, K等人于2015年在“Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification”中描述的方法,用一個均勻分布生成值,填充輸入的張量或變量。結果張量中的值采樣自U(-bound, bound),其中bound = sqrt(2/((1 + a^2) *fan\_in))* sqrt(3)。也被稱為He initialisation.
**參數:**
- **tensor** – n維的torch.Tensor或autograd.Variable
- **a** -這層之后使用的rectifier的斜率系數(ReLU的默認值為0)
- **mode** -可以為“fan\_in”(默認)或“fan\_out”。“fan\_in”保留前向傳播時權值方差的量級,“fan\_out”保留反向傳播時的量級。
**例子:**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.kaiming_uniform(w, mode='fan_in')
```
```
torch.nn.init.kaiming_normal(tensor, a=0, mode='fan_in')
```
根據He, K等人在“Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification”中描述的方法,用一個正態分布生成值,填充輸入的張量或變量。結果張量中的值采樣自均值為0,標準差為sqrt(2/((1 + a^2) \* fan\_in))的正態分布。
**參數:**
- **tensor** – n維的torch.Tensor或 autograd.Variable
- **a** -這層之后使用的rectifier的斜率系數(ReLU的默認值為0)
- **mode** -可以為“fan\_in”(默認)或“fan\_out”。“fan\_in”保留前向傳播時權值方差的量級,“fan\_out”保留反向傳播時的量級。
**例子:**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.kaiming_normal(w, mode='fan_out')
```
```
torch.nn.init.orthogonal(tensor, gain=1)
```
用(半)正交矩陣填充輸入的張量或變量。輸入張量必須至少是2維的,對于更高維度的張量,超出的維度會被展平,視作行等于第一個維度,列等于稀疏矩陣乘積的2維表示。其中非零元素生成自均值為0,標準差為std的正態分布。
參考:Saxe, A等人(2013)的“Exact solutions to the nonlinear dynamics of learning in deep linear neural networks”
**參數:**
- **tensor** – n維的torch.Tensor或 autograd.Variable,其中n>=2
- **gain** -可選
**例子:**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.orthogonal(w)
```
```
torch.nn.init.sparse(tensor, sparsity, std=0.01)
```
將2維的輸入張量或變量當做稀疏矩陣填充,其中非零元素根據一個均值為0,標準差為std的正態分布生成。 參考Martens, J.(2010)的 “Deep learning via Hessian-free optimization”.
**參數:**
- **tensor** – n維的torch.Tensor或autograd.Variable
- **sparsity** - 每列中需要被設置成零的元素比例
- **std** - 用于生成非零值的正態分布的標準差
**例子:**
```
>>> w = torch.Tensor(3, 5)
>>> nn.init.sparse(w, sparsity=0.1)
```
- PyTorch 中文文檔
- 主頁
- 自動求導機制
- CUDA語義
- 擴展PyTorch
- 多進程最佳實踐
- 序列化語義
- torch
- torch.Tensor
- torch.Storage
- torch.nn
- torch.nn.functional
- torch.autograd
- torch.optim
- torch.nn.init
- torch.multiprocessing
- torch.legacy
- torch.cuda
- torch.utils.ffi
- torch.utils.data
- torch.utils.model_zoo
- torchvision
- torchvision.datasets
- torchvision.models
- torchvision.transforms
- torchvision.utils
- 致謝