nn_init.md 7.5 KB
Newer Older
飞龙 已提交
1 2 3
# torch.nn.init

> 译者:[GeneZC](https://github.com/GeneZC)
W
wizardforcel 已提交
4 5 6 7 8

```py
torch.nn.init.calculate_gain(nonlinearity, param=None)
```

W
WhereIsMyHead 已提交
9
返回给定非线性函数的推荐的增益值。对应关系如下表:
W
wizardforcel 已提交
10

W
WhereIsMyHead 已提交
11
| 非线性函数 | 增益 |
W
wizardforcel 已提交
12 13 14 15 16 17 18 19 20
| --- | --- |
| Linear / Identity | ![](http://latex.codecogs.com/gif.latex?1) |
| Conv{1,2,3}D | ![](http://latex.codecogs.com/gif.latex?1) |
| Sigmoid | ![](http://latex.codecogs.com/gif.latex?1) |
| Tanh | ![](http://latex.codecogs.com/gif.latex?%5Cfrac%7B5%7D%7B3%7D) |
| ReLU | ![](http://latex.codecogs.com/gif.latex?%5Csqrt%7B2%7D) |
| Leaky Relu | ![](http://latex.codecogs.com/gif.latex?%5Csqrt%7B%5Cfrac%7B2%7D%7B1%20%2B%20%5Ctext%7Bnegative%5C_slope%7D%5E2%7D%7D) |

 
W
WhereIsMyHead 已提交
21
参数:
W
wizardforcel 已提交
22

W
WhereIsMyHead 已提交
23 24
*   **nonlinearity** – 非线性函数 (`nn.functional` 中的名字)
*   **param** – 对应非线性函数的可选参数
W
wizardforcel 已提交
25 26


W
WhereIsMyHead 已提交
27
例子
W
wizardforcel 已提交
28 29 30 31 32 33 34 35 36 37

```py
>>> gain = nn.init.calculate_gain('leaky_relu')

```

```py
torch.nn.init.uniform_(tensor, a=0, b=1)
```

W
WhereIsMyHead 已提交
38
用均匀分布 ![](http://latex.codecogs.com/gif.latex?%5Cmathcal%7BU%7D(a%2C%20b)) 初始化输入 `Tensor`
W
wizardforcel 已提交
39 40

 
W
WhereIsMyHead 已提交
41
参数: 
W
wizardforcel 已提交
42

W
WhereIsMyHead 已提交
43 44 45
*   **tensor** – n 维 `torch.Tensor`
*   **a** – 均匀分布的下界
*   **b** – 均匀分布的上界
W
wizardforcel 已提交
46 47


W
WhereIsMyHead 已提交
48
例子
W
wizardforcel 已提交
49 50 51 52 53 54 55 56 57 58 59

```py
>>> w = torch.empty(3, 5)
>>> nn.init.uniform_(w)

```

```py
torch.nn.init.normal_(tensor, mean=0, std=1)
```

W
WhereIsMyHead 已提交
60
用正态分布 ![](http://latex.codecogs.com/gif.latex?%5Cmathcal%7BN%7D(%5Ctext%7Bmean%7D%2C%20%5Ctext%7Bstd%7D)) 初始化输入 `Tensor`
W
wizardforcel 已提交
61 62

 
W
WhereIsMyHead 已提交
63
参数: 
W
wizardforcel 已提交
64

W
WhereIsMyHead 已提交
65 66 67
*   **tensor** – n 维 `torch.Tensor`
*   **mean** – 正态分布的均值
*   **std** – 正态分布的标准差
W
wizardforcel 已提交
68 69


W
WhereIsMyHead 已提交
70
例子
W
wizardforcel 已提交
71 72 73 74 75 76 77 78 79 80 81

```py
>>> w = torch.empty(3, 5)
>>> nn.init.normal_(w)

```

```py
torch.nn.init.constant_(tensor, val)
```

W
WhereIsMyHead 已提交
82
用常数 ![](http://latex.codecogs.com/gif.latex?%5Ctext%7Bval%7D) 初始化输入 `Tensor`
W
wizardforcel 已提交
83 84

 
W
WhereIsMyHead 已提交
85
参数: 
W
wizardforcel 已提交
86

W
WhereIsMyHead 已提交
87 88
*   **tensor** – n 维 `torch.Tensor`
*   **val** – 用以填入张量的常数
W
wizardforcel 已提交
89 90


W
WhereIsMyHead 已提交
91
例子
W
wizardforcel 已提交
92 93 94 95 96 97 98 99 100 101 102

```py
>>> w = torch.empty(3, 5)
>>> nn.init.constant_(w, 0.3)

```

```py
torch.nn.init.eye_(tensor)
```

W
WhereIsMyHead 已提交
103
用单位矩阵初始化 2 维输入 `Tensor`。 保持输入张量输入 `Linear` 时的独一性,并且越多越好.
W
wizardforcel 已提交
104 105

 
W
WhereIsMyHead 已提交
106 107 108 109
参数:  

*   **tensor** – 2 维 `torch.Tensor` 

W
wizardforcel 已提交
110

W
WhereIsMyHead 已提交
111
例子
W
wizardforcel 已提交
112 113 114 115 116 117 118 119 120 121 122

```py
>>> w = torch.empty(3, 5)
>>> nn.init.eye_(w)

```

```py
torch.nn.init.dirac_(tensor)
```

W
WhereIsMyHead 已提交
123
用狄拉克δ函数初始化 {3, 4, 5} 维输入 `Tensor`。 保持输入张量输入 `Convolutional` 时的独一性,并且越多通道越好。
W
wizardforcel 已提交
124 125

 
W
WhereIsMyHead 已提交
126 127 128
参数:  

*   **tensor** – {3, 4, 5} 维 `torch.Tensor` 
W
wizardforcel 已提交
129

W
WhereIsMyHead 已提交
130
例子
W
wizardforcel 已提交
131 132 133 134 135 136 137 138 139 140 141

```py
>>> w = torch.empty(3, 16, 5, 5)
>>> nn.init.dirac_(w)

```

```py
torch.nn.init.xavier_uniform_(tensor, gain=1)
```

W
WhereIsMyHead 已提交
142
用论文 “Understanding the difficulty of training deep feedforward neural networks” - Glorot, X. & Bengio, Y. (2010) 中提及的均匀分布初始化输入 `Tensor`。初始化后的张量中的值采样自 ![](http://latex.codecogs.com/gif.latex?%5Cmathcal%7BU%7D(-a%2C%20a)) 且
W
wizardforcel 已提交
143 144 145

![](http://latex.codecogs.com/gif.latex?%0D%0Aa%20%3D%20%5Ctext%7Bgain%7D%20%5Ctimes%20%5Csqrt%7B%5Cfrac%7B6%7D%7B%5Ctext%7Bfan%5C_in%7D%20%2B%20%5Ctext%7Bfan%5C_out%7D%7D%7D%0D%0A%0D%0A)

W
WhereIsMyHead 已提交
146
也被称作 Glorot 初始化。
W
wizardforcel 已提交
147 148

 
W
WhereIsMyHead 已提交
149
参数: 
W
wizardforcel 已提交
150

W
WhereIsMyHead 已提交
151 152
*   **tensor** – n 维 `torch.Tensor`
*   **gain** – 可选缩放因子
W
wizardforcel 已提交
153 154


W
WhereIsMyHead 已提交
155
例子
W
wizardforcel 已提交
156 157 158 159 160 161 162 163 164 165 166

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

```

```py
torch.nn.init.xavier_normal_(tensor, gain=1)
```

W
WhereIsMyHead 已提交
167
用论文 “Understanding the difficulty of training deep feedforward neural networks” - Glorot, X. & Bengio, Y. (2010) 中提及的正态分布初始化输入 `Tensor`。初始化后的张量中的值采样自 ![](http://latex.codecogs.com/gif.latex?%5Cmathcal%7BN%7D(0%2C%20%5Ctext%7Bstd%7D)) 且
W
wizardforcel 已提交
168 169 170

![](http://latex.codecogs.com/gif.latex?%0D%0A%5Ctext%7Bstd%7D%20%3D%20%5Ctext%7Bgain%7D%20%5Ctimes%20%5Csqrt%7B%5Cfrac%7B2%7D%7B%5Ctext%7Bfan%5C_in%7D%20%2B%20%5Ctext%7Bfan%5C_out%7D%7D%7D%0D%0A%0D%0A)

W
WhereIsMyHead 已提交
171
也被称作 Glorot initialization。
W
wizardforcel 已提交
172 173

 
W
WhereIsMyHead 已提交
174
参数: 
W
wizardforcel 已提交
175

W
WhereIsMyHead 已提交
176 177
*   **tensor** – n 维 `torch.Tensor`
*   **gain** – 可选缩放因子
W
wizardforcel 已提交
178 179


W
WhereIsMyHead 已提交
180
例子
W
wizardforcel 已提交
181 182 183 184 185 186 187 188 189 190 191

```py
>>> w = torch.empty(3, 5)
>>> nn.init.xavier_normal_(w)

```

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

W
WhereIsMyHead 已提交
192
用论文 “Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification” - He, K. et al. (2015) 中提及的均匀分布初始化输入 `Tensor`。初始化后的张量中的值采样自 ![](http://latex.codecogs.com/gif.latex?%5Cmathcal%7BU%7D(-%5Ctext%7Bbound%7D%2C%20%5Ctext%7Bbound%7D)) 且
W
wizardforcel 已提交
193 194 195

![](http://latex.codecogs.com/gif.latex?%0D%0A%5Ctext%7Bbound%7D%20%3D%20%5Csqrt%7B%5Cfrac%7B6%7D%7B(1%20%2B%20a%5E2)%20%5Ctimes%20%5Ctext%7Bfan%5C_in%7D%7D%7D%0D%0A%0D%0A)

W
WhereIsMyHead 已提交
196
也被称作 He initialization。
W
wizardforcel 已提交
197 198

 
W
WhereIsMyHead 已提交
199
参数: 
W
wizardforcel 已提交
200

W
WhereIsMyHead 已提交
201 202
*   **tensor** – n 维 `torch.Tensor`
*   **a** – 该层后面一层的整流函数中负的斜率 (默认为 0,此时为 Relu)
片刻小哥哥's avatar
片刻小哥哥 已提交
203 204
*   **mode** – 'fan_in' (default) 或者 'fan_out'。使用fan_in保持weights的方差在前向传播中不变;使用fan_out保持weights的方差在反向传播中不变。
*   **nonlinearity** – 非线性函数 (`nn.functional` 中的名字),推荐只使用 'relu' 或 'leaky_relu' (default)。
W
wizardforcel 已提交
205 206


W
WhereIsMyHead 已提交
207
例子
W
wizardforcel 已提交
208 209 210 211 212 213 214 215 216 217 218

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

```

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

W
WhereIsMyHead 已提交
219
用论文 “Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification” - He, K. et al. (2015) 中提及的正态分布初始化输入 `Tensor`。初始化后的张量中的值采样 ![](http://latex.codecogs.com/gif.latex?%5Cmathcal%7BN%7D(0%2C%20%5Ctext%7Bstd%7D)) 且
W
wizardforcel 已提交
220 221 222

![](http://latex.codecogs.com/gif.latex?%0D%0A%5Ctext%7Bstd%7D%20%3D%20%5Csqrt%7B%5Cfrac%7B2%7D%7B(1%20%2B%20a%5E2)%20%5Ctimes%20%5Ctext%7Bfan%5C_in%7D%7D%7D%0D%0A%0D%0A)

W
WhereIsMyHead 已提交
223
也被称作 He initialization。
W
wizardforcel 已提交
224 225

 
W
WhereIsMyHead 已提交
226
参数: 
W
wizardforcel 已提交
227

W
WhereIsMyHead 已提交
228 229
*   **tensor** – n 维 `torch.Tensor`
*   **a** – 该层后面一层的整流函数中负的斜率 (默认为 0,此时为 Relu)
片刻小哥哥's avatar
片刻小哥哥 已提交
230 231
*   **mode** – 'fan_in' (default) 或者 'fan_out'。使用fan_in保持weights的方差在前向传播中不变;使用fan_out保持weights的方差在反向传播中不变。
*   **nonlinearity** – 非线性函数 (`nn.functional` 中的名字),推荐只使用 'relu' 或 'leaky_relu' (default)。
W
wizardforcel 已提交
232 233


W
WhereIsMyHead 已提交
234
例子
W
wizardforcel 已提交
235 236 237 238 239 240 241 242 243 244 245

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

```

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

片刻小哥哥's avatar
片刻小哥哥 已提交
246
用论文 “Exact solutions to the nonlinear dynamics of learning in deep linear neural networks” - Saxe, A. et al. (2013) 中描述的(半)正定矩阵初始化输入 `Tensor`。输入张量必须至少有 2 维,如果输入张量的维度大于 2, 则对后续维度进行放平操作。
W
wizardforcel 已提交
247 248

 
W
WhereIsMyHead 已提交
249
参数: 
W
wizardforcel 已提交
250

W
WhereIsMyHead 已提交
251 252
*   **tensor** – n 维 `torch.Tensor`,且 ![](http://latex.codecogs.com/gif.latex?n%20%5Cgeq%202)
*   **gain** – 可选缩放因子
W
wizardforcel 已提交
253 254


W
WhereIsMyHead 已提交
255
例子
W
wizardforcel 已提交
256 257 258 259 260 261 262 263 264 265 266

```py
>>> w = torch.empty(3, 5)
>>> nn.init.orthogonal_(w)

```

```py
torch.nn.init.sparse_(tensor, sparsity, std=0.01)
```

W
WhereIsMyHead 已提交
267
用论文 “Deep learning via Hessian-free optimization” - Martens, J. (2010). 提及的稀疏矩阵初始化 2 维输入 `Tensor`,且使用正态分布 ![](http://latex.codecogs.com/gif.latex?%5Cmathcal%7BN%7D(0%2C%200.01)) 初始化非零元素。
W
wizardforcel 已提交
268 269

 
W
WhereIsMyHead 已提交
270
参数: 
W
wizardforcel 已提交
271

W
WhereIsMyHead 已提交
272 273 274
*   **tensor** – n 维 `torch.Tensor`
*   **sparsity** – 每一行置零元素的比例
*   **std** – 初始化非零元素时使用正态分布的标准差
W
wizardforcel 已提交
275 276


W
WhereIsMyHead 已提交
277
例子
W
wizardforcel 已提交
278 279 280 281 282

```py
>>> w = torch.empty(3, 5)
>>> nn.init.sparse_(w, sparsity=0.1)

飞龙 已提交
283
```