17.md 42.3 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4
# torch.nn.functional

## 卷积函数

W
wizardforcel 已提交
5
```py
W
wizardforcel 已提交
6 7 8 9 10 11 12 13 14
torch.nn.functional.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/modules/conv.html#Conv1d)

对由几个平面组成的输入进行卷积操作 对于细节和输出形状,详细可见[Conv1d](http://pytorch.org/docs/master/nn.html#torch.nn.Conv1d)

### 参数:

W
wizardforcel 已提交
15
```py
W
wizardforcel 已提交
16 17 18 19 20 21 22 23 24 25 26
input输入的张量形状(minibatch x in_channels x iW)
weight  过滤器的形状 (out_channels, in_channels, kW) 
bias  可选偏置的形状(out_channels)默认值None
stride  卷积内核的步长默认为 1 
padding  输入上的隐含零填充可以是单个数字或元组默认值:0 
dilation  内核元素之间的间距默认值:1 
groups  将输入分成组in_channels 应该被组数整除默认值1 
```

举例:

W
wizardforcel 已提交
27
```py
W
wizardforcel 已提交
28 29 30 31 32 33 34
>>> filters = autograd.Variable(torch.randn(33, 16, 3)
>>> inputs = autograd.Variable(torch.randn(20, 16, 50))
>>> F.conv1d(inputs, filters) 
```

* * *

W
wizardforcel 已提交
35
```py
W
wizardforcel 已提交
36 37 38 39 40 41 42 43 44
torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/modules/conv.html#Conv2d)

在由几个输入平面组成的输入图像上应用 2D 卷积。 对于细节和输出形状详细可见[Conv2d](http://pytorch.org/docs/master/nn.html#torch.nn.Conv2d)

### 参数:

W
wizardforcel 已提交
45
```py
W
wizardforcel 已提交
46 47 48 49 50 51 52 53 54 55 56
input  输入的张量 (minibatch x in_channels x iH x iW) 
weight  过滤器 (out_channels, in_channels/groups, kH, kW) 
bias  可选偏置张量(out_channels)默认值None 
stride  卷积核的步长可以是单个数字或元组sh x sw)。默认值1 
padding  输入中默认 0 填充可以是单个数字或元组默认值:0 
dilation  核元素之间的间距默认值1 
groups  将输入分成组in_channels 应该被组数整除默认值1 
```

举例:

W
wizardforcel 已提交
57
```py
W
wizardforcel 已提交
58 59 60 61 62 63 64 65
>>> # With square kernels and equal stride
>>> filters = torch.randn(8,4,3,3)
>>> inputs = torch.randn(1,4,5,5)
>>> F.conv2d(inputs, filters, padding=1) 
```

* * *

W
wizardforcel 已提交
66
```py
W
wizardforcel 已提交
67 68 69 70 71 72 73 74 75
torch.nn.functional.conv3d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/modules/conv.html#Conv3d)

在由几个输入平面组成的输入图像上应用 3D 卷积。 对于细节和输出形状,查看[Conv3d](http://pytorch.org/docs/master/nn.html#torch.nn.Conv3d)

### 参数:

W
wizardforcel 已提交
76
```py
W
wizardforcel 已提交
77 78 79 80 81 82 83 84 85 86 87
input  输入张量的形状 (minibatch x in_channels x iT x iH x iW)
weight  过滤器的形状 (out_channels x in_channels/groups x kT x kH x kW)
bias  可选的偏差项的大小(out_channels).默认值是None
stride  卷积核的步长可以是单个数字或元组st x sh x sw).默认值1
padding  在输入中默认的 0 填充可以是单个数字或元组(padT, padH, padW).默认值:0
dilation  核元素之间的间距(dT, dH, dW)默认值1
groups  将输入分成组in_channels 应该被组数整除默认值1 
```

### 举例:

W
wizardforcel 已提交
88
```py
W
wizardforcel 已提交
89 90 91 92 93 94 95
>>> filters = torch.randn(33, 16, 3, 3, 3)
>>> inputs = torch.randn(20, 16, 50, 10, 20)
>>> F.conv3d(inputs, filters) 
```

* * *

W
wizardforcel 已提交
96
```py
W
wizardforcel 已提交
97 98 99 100 101 102 103 104 105
torch.nn.functional.conv_transpose1d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1, dilation=1)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/modules/conv.html#ConvTranspose1d)

在由几个输入平面组成的输入图像上应用 1D 转置卷积,有时也被称为去卷积。 有关详细信息和输出形状,参考[ConvTranspose1d](http://pytorch.org/docs/master/nn.html#torch.nn.ConvTranspose1d)

### 参数:

W
wizardforcel 已提交
106
```py
W
wizardforcel 已提交
107 108 109 110 111 112 113 114 115 116 117 118
input输入的张量形状(minibatch x in_channels x iW)
weight  过滤器的形状 (in_channels x out_channels/groups x kW) 
bias  可选偏置的形状(out_channels)默认值None
stride  卷积内核的步长也可以是一个数字或者元组sW),默认为 1 
padding  输入上的隐含零填充(0padding<stride)可以是单个数字或者元组padW)。默认值:0 
output_padding-在两端的进行 0 填充0padding<stride),可以是个单一数字或者元组(out_padW)默认是 0
dilation  内核元素之间的间距可以是单个数字或者元组dW)。默认值:1 
groups  将输入分成组in_channels 应该被组数整除默认值1 
```

### 举例:

W
wizardforcel 已提交
119
```py
W
wizardforcel 已提交
120 121 122 123 124 125 126 127
>>> inputs = torch.randn(20, 16, 50)
>>> weights = torch.randn(16, 33, 5)
>>> F.conv_transpose1d(inputs, weights)
> 
```

* * *

W
wizardforcel 已提交
128
```py
W
wizardforcel 已提交
129 130 131 132 133 134 135 136 137
torch.nn.functional.conv_transpose2d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1, dilation=1)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/modules/conv.html#ConvTranspose2d)

在由几个输入平面组成的输入图像上应用 2D 转置卷积,有时也被称为去卷积。 有关详细信息和输出形状,参考[ConvTranspose2d](http://pytorch.org/docs/master/nn.html#torch.nn.ConvTranspose2d)

### 参数:

W
wizardforcel 已提交
138
```py
W
wizardforcel 已提交
139 140 141 142 143 144 145 146 147 148 149 150
input输入的张量形状(minibatch x in_channels x iH x iW)
weight  过滤器的形状 (in_channels x out_channels/groups x kH x kW) 
bias  可选偏置的形状(out_channels)默认值None
stride  卷积内核的步长也可以是一个数字或者元组sHsW),默认为 1 
padding  输入上的隐含零填充(0padding<stride)可以是单个数字或者元组padHpadW)。默认值:0 
output_padding-在两端的进行 0 填充0padding<stride),可以是个单一数字或者元组( out_padH, out_padW)默认是 0
dilation  内核元素之间的间距可以是单个数字或者元组dH,dW)。默认值:1 
groups  将输入分成组in_channels 应该被组数整除默认值1 
```

### 举例:

W
wizardforcel 已提交
151
```py
W
wizardforcel 已提交
152 153 154 155 156 157 158 159
>>> # With square kernels and equal stride
>>> inputs = torch.randn(1, 4, 5, 5)
>>> weights = torch.randn(4, 8, 3, 3)
>>> F.conv_transpose2d(inputs, weights, padding=1) 
```

* * *

W
wizardforcel 已提交
160
```py
W
wizardforcel 已提交
161 162 163 164 165 166 167
torch.nn.functional.conv_transpose3d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1, dilation=1)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/modules/conv.html#ConvTranspose3d) 在由几个输入平面组成的输入图像上应用 3D 转置卷积,有时也被称为去卷积。 有关详细信息和输出形状,参考[ConvTranspose3d](http://pytorch.org/docs/master/nn.html#torch.nn.ConvTranspose3d)

### 参数:

W
wizardforcel 已提交
168
```py
W
wizardforcel 已提交
169 170 171 172 173 174 175 176 177 178 179 180
input输入的张量形状(minibatch x in_channels x iT x iH x iW)
weight  过滤器的形状 (in_channels x out_channels/groups x kT x kH x kW) 
bias  可选偏置的形状(out_channels)默认值None
stride  卷积内核的步长也可以是一个数字或者元组sT,sHsW),默认为 1 
padding  在输入的两端上的隐含零填充可以是单个数字或者元组padT,padHpadW)。默认值:0 
output_padding-在两端的进行 0 填充0padding<stride),可以是个单一数字或者元组(out_padT, out_padH, out_padW)默认是 0
dilation  内核元素之间的间距可以是单个数字或者元组dT,dH,dW)。默认值:1 
groups  将输入分成组in_channels 应该被组数整除默认值1 
```

### 举例:

W
wizardforcel 已提交
181
```py
W
wizardforcel 已提交
182 183 184 185 186 187 188
>>> inputs = torch.randn(20, 16, 50, 10, 20)
>>> weights = torch.randn(16, 33, 3, 3, 3)
>>> F.conv_transpose3d(inputs, weights) 
```

## 池化函数

W
wizardforcel 已提交
189
```py
W
wizardforcel 已提交
190 191 192 193 194 195 196 197 198
torch.nn.functional.avg_pool1d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#avg_pool1d)

对由几个输入平面组成的输入进行 1D 平均池化。 有关详细信息和输出形状,参考[AvgPool1d](http://pytorch.org/docs/master/nn.html#torch.nn.AvgPool1d)

### 参数:

W
wizardforcel 已提交
199
```py
W
wizardforcel 已提交
200 201 202 203 204 205 206 207 208 209
input  输入的张量 (minibatch x in_channels x iW)
kernel_size  池化区域的大小可以是单个数字或者元组 (kw)
stride  池化操作的步长可以是单个数字或者元组 (ssw)默认值等于内核大小
padding  在输入上隐式的零填充可以是单个数字或者一个元组 ( padw)默认: 0
ceil_mode  当为 True 公式中将使用 ceil 而不是 floor 来计算输出形状默认值False
count_include_pad  当为 True 将包括平均计算中的零填充默认值True 
```

### 举例:

W
wizardforcel 已提交
210
```py
W
wizardforcel 已提交
211 212 213 214 215 216 217 218
>>> # pool of square window of size=3, stride=2
>>> input = torch.tensor([[[1,2,3,4,5,6,7]]])
>>> F.avg_pool1d(input, kernel_size=3, stride=2)
tensor([[[ 2.,  4.,  6.]]]) 
```

* * *

W
wizardforcel 已提交
219
```py
W
wizardforcel 已提交
220 221 222 223 224 225 226 227 228 229 230
torch.nn.functional.avg_pool2d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)  Tensor 
```

[source](http://pytorch.org/docs/master/nn.html#torch.nn.functional.avg_pool2d)

通过步长 dh x dw 步骤在 kh x kw 区域中应用二维平均池操作。输出特征的数量等于输入平面的数量。

有关详细信息和输出形状,参考[AvgPool2d](http://pytorch.org/docs/master/nn.html#torch.nn.AvgPool2d)

### 参数:

W
wizardforcel 已提交
231
```py
W
wizardforcel 已提交
232 233 234 235 236 237 238 239 240 241
input  输入的张量 (minibatch x in_channels x iH x iW)
kernel_size  池化区域的大小可以是单个数字或者元组 (kh x kw)
stride  池化操作的步长可以是单个数字或者元组 (sh x sw)默认值等于内核大小
padding  在输入上隐式的零填充可以是单个数字或者一个元组 (padh x padw)默认: 0
ceil_mode  当为 True 公式中将使用 ceil 而不是 floor 来计算输出形状默认值False
count_include_pad  当为 True 将包括平均计算中的零填充默认值True 
```

* * *

W
wizardforcel 已提交
242
```py
W
wizardforcel 已提交
243 244 245 246 247 248 249 250 251
torch.nn.functional.avg_pool3d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)  Tensor 
```

[source](http://pytorch.org/docs/master/nn.html#torch.nn.functional.avg_pool3d)

通过步长 dt x dh x dw 步骤在 kt x kh x kw 区域中应用 3D 平均池操作。输出功能的数量等于输入平面数/ dt。 有关详细信息和输出形状,参考[AvgPool3d](http://pytorch.org/docs/master/nn.html#torch.nn.AvgPool3d)

### 参数:

W
wizardforcel 已提交
252
```py
W
wizardforcel 已提交
253 254 255 256 257 258 259 260 261 262
input  输入的张量 (minibatch x in_channels x iT x iH x iW)
kernel_size  池化区域的大小可以是单个数字或者元组 (kT x kh x kw)
stride  池化操作的步长可以是单个数字或者元组 (sT x sh x sw)默认值等于内核大小
padding  在输入上隐式的零填充可以是单个数字或者一个元组 (padT x padh x padw)默认: 0
ceil_mode  当为 True 公式中将使用 ceil 而不是 floor 来计算输出形状默认值False
count_include_pad  当为 True 将包括平均计算中的零填充默认值True 
```

* * *

W
wizardforcel 已提交
263
```py
W
wizardforcel 已提交
264 265 266 267 268 269 270 271 272
torch.nn.functional.max_pool1d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#max_pool1d)

对由几个输入平面组成的输入进行 1D 最大池化。 有关详细信息和输出形状,参考[MaxPool1d](http://pytorch.org/docs/master/nn.html#torch.nn.MaxPool1d)

* * *

W
wizardforcel 已提交
273
```py
W
wizardforcel 已提交
274 275 276 277 278 279 280 281 282
torch.nn.functional.max_pool2d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#max_pool2d)

对由几个输入平面组成的输入进行 2D 最大池化。 有关详细信息和输出形状,参考[MaxPool2d](http://pytorch.org/docs/master/nn.html#torch.nn.MaxPool2d)

* * *

W
wizardforcel 已提交
283
```py
W
wizardforcel 已提交
284 285 286 287 288 289 290 291 292
torch.nn.functional.max_pool3d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#max_pool3d)

对由几个输入平面组成的输入进行 3D 最大池化。 有关详细信息和输出形状,参考[MaxPool3d](http://pytorch.org/docs/master/nn.html#torch.nn.MaxPool3d)

* * *

W
wizardforcel 已提交
293
```py
W
wizardforcel 已提交
294 295 296 297 298 299 300 301 302
torch.nn.functional.max_unpool1d(input, indices, kernel_size, stride=None, padding=0, output_size=None) 
```

计算 MaxPool1d 的部分逆。

有关详细信息和输出形状,参考[MaxUnPool1d](http://pytorch.org/docs/master/nn.html#torch.nn.MaxUnpool1d)

* * *

W
wizardforcel 已提交
303
```py
W
wizardforcel 已提交
304 305 306 307 308 309 310 311 312
torch.nn.functional.max_unpool2d(input, indices, kernel_size, stride=None, padding=0, output_size=None) 
```

计算 MaxPool2d 的部分逆。

有关详细信息和输出形状,参考[MaxUnPool2d](http://pytorch.org/docs/master/nn.html#torch.nn.MaxUnpool2d)

* * *

W
wizardforcel 已提交
313
```py
W
wizardforcel 已提交
314 315 316 317 318 319 320 321 322
torch.nn.functional.max_unpool3d(input, indices, kernel_size, stride=None, padding=0, output_size=None) 
```

计算 MaxPool3d 的部分逆。

有关详细信息和输出形状,参考[MaxUnPool3d](http://pytorch.org/docs/master/nn.html#torch.nn.MaxUnpool3d)

* * *

W
wizardforcel 已提交
323
```py
W
wizardforcel 已提交
324 325 326 327 328 329 330 331 332
torch.nn.functional.lp_pool1d(input, norm_type, kernel_size, stride=None, ceil_mode=False) 
```

适用在几个输入平面组成的输入信号的 1D power-平均池。

有关详细信息[LPPool1d](http://pytorch.org/docs/master/nn.html#torch.nn.LPPool1d)

* * *

W
wizardforcel 已提交
333
```py
W
wizardforcel 已提交
334 335 336 337 338 339 340 341 342
torch.nn.functional.lp_pool2d(input, norm_type, kernel_size, stride=None, ceil_mode=False) 
```

适用在几个输入平面组成的输入信号的 2D power-平均池。

有关详细信息[LPPool2d](http://pytorch.org/docs/master/nn.html#torch.nn.LPPool2d)

* * *

W
wizardforcel 已提交
343
```py
W
wizardforcel 已提交
344 345 346 347 348 349 350 351 352 353 354
torch.nn.functional.adaptive_max_pool1d(input, output_size, return_indices=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#adaptive_max_pool1d)

对由多个输入平面组成的输入进行 1D 自适应最大池化。

有关详细信息可见[AdaptiveMaxPool1d](http://pytorch.org/docs/master/nn.html#torch.nn.AdaptiveMaxPool1d)

* * *

W
wizardforcel 已提交
355
```py
W
wizardforcel 已提交
356 357 358 359 360 361 362 363 364 365 366
torch.nn.functional.adaptive_max_pool2d(input, output_size, return_indices=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#adaptive_max_pool2d)

对由多个输入平面组成的输入进行 2D 自适应最大池化。

有关详细信息可见[AdaptiveMaxPool2d](http://pytorch.org/docs/master/nn.html#torch.nn.AdaptiveMaxPool2d)

* * *

W
wizardforcel 已提交
367
```py
W
wizardforcel 已提交
368 369 370 371 372 373 374 375 376 377 378
torch.nn.functional.adaptive_max_pool3d(input, output_size, return_indices=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#adaptive_max_pool3d)

对由多个输入平面组成的输入进行 3D 自适应最大池化。

有关详细信息可见[AdaptiveMaxPool3d](http://pytorch.org/docs/master/nn.html#torch.nn.AdaptiveMaxPool3d)

* * *

W
wizardforcel 已提交
379
```py
W
wizardforcel 已提交
380 381 382 383 384 385 386 387 388 389 390
torch.nn.functional.adaptive_avg_pool1d(input, output_size)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#adaptive_avg_pool1d)

对由多个输入平面组成的输入进行 1D 自适应平均池化。

有关详细信息可见[AdaptiveAvgPool1d](http://pytorch.org/docs/master/nn.html#torch.nn.AdaptiveAvgPool1d)

* * *

W
wizardforcel 已提交
391
```py
W
wizardforcel 已提交
392 393 394 395 396 397 398 399 400 401 402
torch.nn.functional.adaptive_avg_pool2d(input, output_size)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#adaptive_avg_pool2d)

对由多个输入平面组成的输入进行 2D 自适应平均池化。

有关详细信息可见[AdaptiveAvgPool2d](http://pytorch.org/docs/master/nn.html#torch.nn.AdaptiveAvgPool2d)

* * *

W
wizardforcel 已提交
403
```py
W
wizardforcel 已提交
404 405 406 407 408 409 410 411 412 413 414
torch.nn.functional.adaptive_avg_pool3d(input, output_size)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#adaptive_avg_pool3d)

对由多个输入平面组成的输入进行 3D 自适应平均池化。

有关详细信息可见[AdaptiveAvgPool3d](http://pytorch.org/docs/master/nn.html#torch.nn.AdaptiveAvgPool3d)

## 非线性激活函数

W
wizardforcel 已提交
415
```py
W
wizardforcel 已提交
416 417 418 419 420 421 422 423 424 425 426
torch.nn.functional.threshold(input, threshold, value, inplace=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#threshold)

对于输入的张量进行筛选

详细请看[Threshold](http://pytorch.org/docs/master/nn.html#torch.nn.Threshold)

* * *

W
wizardforcel 已提交
427
```py
W
wizardforcel 已提交
428 429 430 431 432 433 434 435 436 437 438
torch.nn.functional.threshold_(input, threshold, value) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#threshold)

和 threshold 函数一样

详细请看[Threshold](http://pytorch.org/docs/master/nn.html#torch.nn.functional.threshold)

* * *

W
wizardforcel 已提交
439
```py
W
wizardforcel 已提交
440 441 442 443 444 445 446 447 448 449 450 451
torch.nn.functional.relu(input, inplace=False)  Tensor
torch.nn.functional.relu_(input)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#relu)

对输入元素应用 relu 函数

详细请看[Relu](http://pytorch.org/docs/master/nn.html#torch.nn.ReLU)

* * *

W
wizardforcel 已提交
452
```py
W
wizardforcel 已提交
453 454 455 456 457 458 459 460 461 462 463 464
torch.nn.functional.hardtanh(input, min_val=-1., max_val=1., inplace=False)  Tensor
torch.nn.functional.hardtanh_(input, min_val=-1., max_val=1.)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#hardtanh)

对输入元素应用 HardTanh 函数

详细请看[Hardtanh](http://pytorch.org/docs/master/nn.html#torch.nn.Hardtanh)

* * *

W
wizardforcel 已提交
465
```py
W
wizardforcel 已提交
466 467 468 469 470 471 472 473 474 475 476
torch.nn.functional.relu6(input, inplace=False)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#relu6)

对输入元素应用 ReLU6 函数 ReLU6(x)=min(max(0,x),6)

详细请看[ReLU6](http://pytorch.org/docs/master/nn.html#torch.nn.ReLU6)

* * *

W
wizardforcel 已提交
477
```py
W
wizardforcel 已提交
478 479 480 481 482 483 484 485 486 487 488 489
torch.nn.functional.elu(input, alpha=1.0, inplace=False)
torch.nn.functional.elu_(input, alpha=1.)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#elu)

对输入元素应用 ELU 函数 ELU(x)=max(0,x)+min(0,α∗(exp(x)−1))

详细请看[ELU](http://pytorch.org/docs/master/nn.html#torch.nn.ELU)

* * *

W
wizardforcel 已提交
490
```py
W
wizardforcel 已提交
491 492 493 494 495 496 497
torch.nn.functional.selu(input, inplace=False)  Tensor[source] 
```

详细可见[selu](http://pytorch.org/docs/master/nn.html#torch.nn.SELU)

* * *

W
wizardforcel 已提交
498
```py
W
wizardforcel 已提交
499 500 501 502 503 504 505 506
torch.nn.functional.leaky_relu(input, negative_slope=0.01, inplace=False)  Tensor
torch.nn.functional.leaky_relu_(input, negative_slope=0.01)  Tensor 
```

详细可见[LeakyReLu](http://pytorch.org/docs/master/nn.html#torch.nn.LeakyReLU)

* * *

W
wizardforcel 已提交
507
```py
W
wizardforcel 已提交
508 509 510 511 512 513 514
torch.nn.functional.prelu(input, weight)  Tensor 
```

详细可见[PRelu](http://pytorch.org/docs/master/nn.html#torch.nn.PRelu)

* * *

W
wizardforcel 已提交
515
```py
W
wizardforcel 已提交
516 517 518 519 520 521 522 523
torch.nn.functional.rrelu(input, lower=1./8, upper=1./3, training=False, inplace=False)  Tensor
torch.nn.functional.rrelu_(input, lower=1./8, upper=1./3, training=False)  Tensor 
```

详细可见[RRelu](http://pytorch.org/docs/master/nn.html#torch.nn.RRelu)

* * *

W
wizardforcel 已提交
524
```py
W
wizardforcel 已提交
525 526 527 528 529 530 531 532 533
torch.nn.functional.glu(input, dim=-1)  Tensor 
```

门控制线性单元 H=A×σ(B),输入张量将被按照特定维度分成一半是 A,一半是 B

可以参看论文[Language Modeling with Gated Convolutional Networks](https://arxiv.org/abs/1612.08083)

### 参数:

W
wizardforcel 已提交
534
```py
W
wizardforcel 已提交
535 536 537 538 539 540
input输入的张量
dim需要被分割的输入张量的维度 
```

* * *

W
wizardforcel 已提交
541
```py
W
wizardforcel 已提交
542 543 544 545 546 547 548
torch.nn.functional.logsigmoid(input)  Tensor 
```

具体细节可以看[LogSigmoid](http://pytorch.org/docs/master/nn.html#torch.nn.LogSigmoid)

* * *

W
wizardforcel 已提交
549
```py
W
wizardforcel 已提交
550 551 552 553 554 555 556
torch.nn.functional.hardshrink(input, lambd=0.5)  Tensor 
```

具体细节可以看[Hardshrink](http://pytorch.org/docs/master/nn.html#torch.nn.Hardshrink)

* * *

W
wizardforcel 已提交
557
```py
W
wizardforcel 已提交
558 559 560 561 562 563 564
torch.nn.functional.tanhshrink(input, lambd=0.5)  Tensor 
```

具体细节可以看[Tanhshrink](http://pytorch.org/docs/master/nn.html#torch.nn.Tanhshrink)

* * *

W
wizardforcel 已提交
565
```py
W
wizardforcel 已提交
566 567 568 569 570 571 572
torch.nn.functional.softsign(input, lambd=0.5)  Tensor 
```

具体细节可以看[Softsign](http://pytorch.org/docs/master/nn.html#torch.nn.Softsign)

* * *

W
wizardforcel 已提交
573
```py
W
wizardforcel 已提交
574 575 576 577 578
torch.nn.functional.softplus(input, beta=1, threshold=20)  Tensor 
```

* * *

W
wizardforcel 已提交
579
```py
W
wizardforcel 已提交
580 581 582 583 584 585 586 587 588
torch.nn.functional.softmin(input, dim=None, _stacklevel=3) 
```

应用 softmin 函数 请注意 Softmin(x)= Softmax(-x)。 请参阅数学公式的 softmax 定义。

具体细节可以看[Softmin](http://pytorch.org/docs/master/nn.html#torch.nn.Softmin)

### 参数:

W
wizardforcel 已提交
589
```py
W
wizardforcel 已提交
590 591 592 593 594 595
input:输入张量
dimsoftmin 将被计算的维度因此每个沿着 dim 的切分将总计为 1)。 
```

* * *

W
wizardforcel 已提交
596
```py
W
wizardforcel 已提交
597 598 599 600 601 602 603 604 605 606 607
torch.nn.functional.softmax(input, dim=None, _stacklevel=3 
```

Softmax(x_i)=exp(x_i)/∑_jexp(x_j)

它被应用于沿着对应维度的所有切分,并且将对它们进行重新缩放,以使得这些元素位于范围(0,1)中并且总计为 1。

具体细节可以看[Softmax](http://pytorch.org/docs/master/nn.html#torch.nn.Softmax)

### 参数:

W
wizardforcel 已提交
608
```py
W
wizardforcel 已提交
609 610 611 612 613 614
input:输入张量
dimsoftmax 被计算的维度 
```

* * *

W
wizardforcel 已提交
615
```py
W
wizardforcel 已提交
616 617 618 619 620 621 622
torch.nn.functional.softshrink(input, lambd=0.5)  Tensor 
```

具体细节可以看[Softshrink](http://pytorch.org/docs/master/nn.html#torch.nn.Softshrink)

* * *

W
wizardforcel 已提交
623
```py
W
wizardforcel 已提交
624 625 626 627 628 629 630 631 632
torch.nn.functional.log_softmax(input, dim=None, _stacklevel=3) 
```

尽管在数学上等同于 log(softmax(x)),但单独执行这两个操作会更慢,并且数值不稳定。 该函数使用替代公式来正确计算输出和梯度。

具体细节可以看[LogSoftmax](http://pytorch.org/docs/master/nn.html#torch.nn.LogSoftmax)

* * *

W
wizardforcel 已提交
633
```py
W
wizardforcel 已提交
634 635 636 637 638 639 640
torch.nn.functional.tanh(input)  Tensor 
```

具体细节可以看[Tanh](http://pytorch.org/docs/master/nn.html#torch.nn.Tanh)

* * *

W
wizardforcel 已提交
641
```py
W
wizardforcel 已提交
642 643 644 645 646 647 648 649 650 651 652
torch.nn.functional.sigmoid(input)  Tensor 
```

sigmoid(x) = 1/1+exp(-x)

具体细节可以看[Sigmoid](http://pytorch.org/docs/master/nn.html#torch.nn.Sigmoid)

## 归一化函数

* * *

W
wizardforcel 已提交
653
```py
W
wizardforcel 已提交
654 655 656 657 658 659 660 661 662
torch.nn.functional.batch_norm(input, running_mean, running_var, weight=None, bias=None, training=False, momentum=0.1, eps=1e-05) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#batch_norm)

具体细节可以看[BatchNorm1d](http://pytorch.org/docs/master/nn.html#torch.nn.BatchNorm1d) [BatchNorm2d](http://pytorch.org/docs/master/nn.html#torch.nn.BatchNorm2d) [BatchNorm3d](http://pytorch.org/docs/master/nn.html#torch.nn.BatchNorm3d)

* * *

W
wizardforcel 已提交
663
```py
W
wizardforcel 已提交
664 665 666 667 668 669 670 671 672
torch.nn.functional.instance_norm(input, running_mean=None, running_var=None, weight=None, bias=None, use_input_stats=True, momentum=0.1, eps=1e-05) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#instance_norm)

具体细节可以看[InstanceNorm1d](http://pytorch.org/docs/master/nn.html#torch.nn.InstanceNorm1d) [InstanceNorm2d](http://pytorch.org/docs/master/nn.html#torch.nn.InstanceNorm2d) [InstanceNorm3d](http://pytorch.org/docs/master/nn.html#torch.nn.InstanceNorm3d)

* * *

W
wizardforcel 已提交
673
```py
W
wizardforcel 已提交
674 675 676 677 678 679 680
torch.nn.functional.layer_norm(input, normalized_shape, weight=None, bias=None, eps=1e-05) 
```

具体细节可以看[LayerNorm](http://pytorch.org/docs/master/nn.html#torch.nn.LayerNorm)

* * *

W
wizardforcel 已提交
681
```py
W
wizardforcel 已提交
682 683 684 685 686 687 688 689 690
torch.nn.functional.local_response_norm(input, size, alpha=0.0001, beta=0.75, k=1) 
```

对由多个输入平面组成的输入应用本地响应规范化,其中通道占据第二维。 通道应用归一化。

具体细节可以看[LocalResponseNorm](http://pytorch.org/docs/master/nn.html#torch.nn.LocalResponseNorm)

* * *

W
wizardforcel 已提交
691
```py
W
wizardforcel 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
torch.nn.functional.normalize(input, p=2, dim=1, eps=1e-12) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#linear)

对指定维度的输入执行$$L_p$$标准化。

$$v = \frac{v}{\max(\lVert v \rVert_p, \epsilon)}$$

对于输入的维度 dim 的每个子扩展 v。 每个子扩张被平化成一个向量,即‖v‖p 不是一个矩阵范数。

使用默认参数在第二维上用欧几里得范数进行归一化。

### 参数:

W
wizardforcel 已提交
707
```py
W
wizardforcel 已提交
708 709 710 711 712 713 714 715 716 717
input - 输入张量的形状
pfloat - 规范公式中的指数值默认值2
dimint - 要缩小的维度默认值1
epsfloat - 小值以避免除以零默认值1e-12 
```

* * *

## 线性函数

W
wizardforcel 已提交
718
```py
W
wizardforcel 已提交
719 720 721 722 723 724 725
torch.nn.functional.linear(input, weight, bias=None) 
```

对于输入数据进行线性变化:$$y = xA^T+b$$.

### 形状:

W
wizardforcel 已提交
726
```py
W
wizardforcel 已提交
727 728 729 730 731 732 733 734 735 736
Input: (N,,in_features)(N,,in_features)这里的*表示为任意数量的附加维度
Weight: (out_features,in_features)(out_features,in_features)
Bias: (out_features)(out_features)
Output: (N,,out_features) 
```

* * *

## Dropout 函数

W
wizardforcel 已提交
737
```py
W
wizardforcel 已提交
738 739 740 741 742 743 744
torch.nn.functional.dropout(input, p=0.5, training=False, inplace=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#dropout)

* * *

W
wizardforcel 已提交
745
```py
W
wizardforcel 已提交
746 747 748 749 750 751 752 753 754
torch.nn.functional.alpha_dropout(input, p=0.5, training=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#alpha_dropout)

详细可见[alpha_dropout](http://pytorch.org/docs/master/nn.html#torch.nn.AlphaDropout)

### 参数:

W
wizardforcel 已提交
755
```py
W
wizardforcel 已提交
756 757 758 759 760 761
p(float,optional)-丢弃的可能性默认是 0.5
training(bool,optinal)-在训练模型和验证模型之间的切换默认是 false 
```

* * *

W
wizardforcel 已提交
762
```py
W
wizardforcel 已提交
763 764 765 766 767 768 769
torch.nn.functional.dropout2d(input, p=0.5, training=False, inplace=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#dropout2d)

* * *

W
wizardforcel 已提交
770
```py
W
wizardforcel 已提交
771 772 773 774 775 776 777 778 779
torch.nn.functional.dropout3d(input, p=0.5, training=False, inplace=False) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#dropout3d)

* * *

## 距离函数

W
wizardforcel 已提交
780
```py
W
wizardforcel 已提交
781 782 783 784 785 786 787
torch.nn.functional.pairwise_distance(x1, x2, p=2, eps=1e-06, keepdim=False) 
```

详细可见 [PairwiseDistance](http://pytorch.org/docs/master/nn.html#torch.nn.PairwiseDistance)

* * *

W
wizardforcel 已提交
788
```py
W
wizardforcel 已提交
789 790 791 792 793 794 795 796 797
torch.nn.functional.cosine_similarity(x1, x2, dim=1, eps=1e-08) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#cosine_similarity)

计算向量 v1、v2 之间的距离 $$similarity = \frac{x_1 x x_2}{\max(\lVert v_1 \rVert_2 x \max(\lVert v_2 \rVert_2,\epsilon)}$$

### 参数:

W
wizardforcel 已提交
798
```py
W
wizardforcel 已提交
799 800 801 802 803 804 805 806
x1 (Variable)  首先输入参数.
x2 (Variable)  第二个输入参数 (of size matching x1).
dim (int, optional)  向量维数. 默认为: 1
eps (float, optional)  小值避免被零分割默认为: 1e-8 模型 
```

### 形状:

W
wizardforcel 已提交
807
```py
W
wizardforcel 已提交
808 809 810 811 812 813
input: (1,D,2)(1,D,2) D 是位置维度
output: (1,2)(1,2) 1 是位置维度 
```

### 举例:

W
wizardforcel 已提交
814
```py
W
wizardforcel 已提交
815 816 817 818 819 820 821 822
>>> input1 = autograd.Variable(torch.randn(100, 128))
>>> input2 = autograd.Variable(torch.randn(100, 128))
>>> output = F.cosine_similarity(input1, input2)
>>> print(output) 
```

## 损失函数

W
wizardforcel 已提交
823
```py
W
wizardforcel 已提交
824 825 826 827 828 829 830 831 832 833 834
torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_average=True, reduce=True) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#binary_cross_entropy)

测量目标和输出之间的二进制交叉熵的函数。

详细可见[BCELoss](http://pytorch.org/docs/master/nn.html#torch.nn.BCELoss)

### 参数:

W
wizardforcel 已提交
835
```py
W
wizardforcel 已提交
836 837 838 839 840 841 842 843 844
input:任意维度
target与输入维度相同
weight张量可选):如果提供的权重矩阵能匹配输入张量形状则手动调整重量
size_average(布尔值可选)默认情况下对每个小批次的损失进行平均观察 但是如果 field size_average 设置为 False则每个小批次的损失将相加 默认值True
reduce布尔值可选:默认情况下根据 size_average 的不同对每个小批次的损失进行平均或累计  reduce  False 将返回每个输入/目标元素的损失而忽略 size_average 默认值True 
```

### 举例:

W
wizardforcel 已提交
845
```py
W
wizardforcel 已提交
846 847 848 849 850 851 852 853
>>> input = torch.randn((3, 2), requires_grad=True)
>>> target = torch.rand((3, 2), requires_grad=False)
>>> loss = F.binary_cross_entropy(F.sigmoid(input), target)
>>> loss.backward() 
```

* * *

W
wizardforcel 已提交
854
```py
W
wizardforcel 已提交
855 856 857 858 859 860 861 862 863
torch.nn.functional.poisson_nll_loss(input, target, log_input=True, full=False, size_average=True, eps=1e-08, reduce=True) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#poisson_nll_loss)

泊松负对数似然损失 详细可见[PoissonNLLLoss](http://pytorch.org/docs/master/nn.html#torch.nn.PoissonNLLLoss)

### 参数:

W
wizardforcel 已提交
864
```py
W
wizardforcel 已提交
865 866 867 868 869 870 871 872 873 874 875
input按照泊松分布的期望
target随机样例 target~Poissoninput
log_input:如果为真则损失计算为 expinout-target * input如果为 False input-target *log⁡(input + eps)。 默认值True
full:是否计算完全损失即添加斯特林近似项 默认值False target * logtarget-target + 0.5 * log2 *π* target)
size_average:默认情况下对每个小批次的损失进行平均观察 但是如果 field size_average 设置为 False则每个小批次的损失将相加 默认值True
epsfloat可选 -  log_input`=False时避免评估 log0log⁡(0的较小值 默认1e-8
reduce布尔值可选:默认情况下根据每个小批次的观测结果对损失进行平均或根据 size_average 进行汇总 如果 reduce  False则返回每批损失并忽略 size_average 默认值True 
```

* * *

W
wizardforcel 已提交
876
```py
W
wizardforcel 已提交
877 878 879 880 881 882 883 884 885
torch.nn.functional.cosine_embedding_loss(input1, input2, target, margin=0, size_average=True, reduce=True)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#cosine_embedding_loss)

详细可见[CosineEmbeddingLoss](http://pytorch.org/docs/master/nn.html#torch.nn.CosineEmbeddingLoss)

* * *

W
wizardforcel 已提交
886
```py
W
wizardforcel 已提交
887 888 889 890 891 892 893 894 895 896 897
torch.nn.functional.cross_entropy(input, target, weight=None, size_average=True, ignore_index=-100, reduce=True) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#cross_entropy)

该标准将 log_softmax 和 nll_loss 结合在一个函数中。

[CrossEntropyLoss](http://pytorch.org/docs/master/nn.html#torch.nn.CrossEntropyLoss)

### 参数:

W
wizardforcel 已提交
898
```py
W
wizardforcel 已提交
899 900 901 902 903 904 905 906 907 908
input张量- (N,C) 其中C 是类别的个数
target张量- (N) 其大小是 0 <= targets[i] <= C-1 1\. weight (Variable, optional)  (N) 其大小是 0 <= targets[i] <= C-1
weight (张量, optional)  为每个类别提供的手动权重如果给出必须是大小为 C 的张量
size_average (bool, optional)  默认情况下 mini-batchloss 的平均值如果 size_average=False则是 mini-batchloss 的总和
ignore_indexint可选 - 指定被忽略且不对输入渐变有贡献的目标值 size_average  True 对非忽略目标的损失是平均的默认值-100
reduce布尔值可选-默认情况下根据每个小批次的观测结果对损失进行平均或根据 size_average 进行汇总 如果 reduce  False则返回每批损失并忽略 size_average 默认值True 
```

### 举例:

W
wizardforcel 已提交
909
```py
W
wizardforcel 已提交
910 911 912 913 914 915 916 917
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randint(5, (3,), dtype=torch.int64)
>>> loss = F.cross_entropy(input, target)
>>> loss.backward() 
```

* * *

W
wizardforcel 已提交
918
```py
W
wizardforcel 已提交
919 920 921 922 923 924 925 926 927
torch.nn.functional.hinge_embedding_loss(input, target, margin=1.0, size_average=True, reduce=True)  Tensor 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#hinge_embedding_loss)

详细可见[HingeEmbeddingLoss](http://pytorch.org/docs/master/nn.html#torch.nn.HingeEmbeddingLoss)

* * *

W
wizardforcel 已提交
928
```py
W
wizardforcel 已提交
929 930 931 932 933 934 935 936 937 938 939
torch.nn.functional.kl_div(input, target, size_average=True)  Tensor 
```

[source](http://pytorch.org/docs/master/nn.html#torch.nn.functional.kl_div)

The [Kullback-Leibler divergence](https://en.wikipedia.org/wiki/Kullback-Leibler_divergence) Loss

详细可见[KLDivLoss](http://pytorch.org/docs/master/nn.html#torch.nn.KLDivLoss)

### 参数:

W
wizardforcel 已提交
940
```py
W
wizardforcel 已提交
941 942 943 944 945 946 947 948
input  变量的任意形状
target - 与输入相同形状的变量
size_average  如果是真的输出就除以输入张量中的元素个数
reduce布尔值可选-默认情况下根据每个小批次的观测结果对损失进行平均或根据 size_average 进行汇总 如果 reduce  False则返回每批损失并忽略 size_average 默认值True 
```

* * *

W
wizardforcel 已提交
949
```py
W
wizardforcel 已提交
950 951 952 953 954 955 956
torch.nn.functional.l1_loss(input, target, size_average=True, reduce=True)  Tensor 
```

详细可见[L1Loss](http://pytorch.org/docs/master/nn.html#torch.nn.L1Loss)

* * *

W
wizardforcel 已提交
957
```py
W
wizardforcel 已提交
958 959 960 961 962 963 964
torch.nn.functional.mse_loss(input, target, size_average=True, reduce=True)  Tensor 
```

详细可见[MSELoss](http://pytorch.org/docs/master/nn.html#torch.nn.MSELoss)

* * *

W
wizardforcel 已提交
965
```py
W
wizardforcel 已提交
966 967 968 969 970 971 972
torch.nn.functional.margin_ranking_loss(input, target, size_average=True, reduce=True)  Tensor 
```

详细可见[MarginRankingLoss](http://pytorch.org/docs/master/nn.html#torch.nn.MarginRankingLoss)

* * *

W
wizardforcel 已提交
973
```py
W
wizardforcel 已提交
974 975 976 977 978 979 980
torch.nn.functional.multilabel_soft_margin_loss(input, target, size_average=True, reduce=True)  Tensor 
```

详细可见[MultiLabelSoftMarginLoss](http://pytorch.org/docs/master/nn.html#torch.nn.MultiLabelSoftMarginLoss)

* * *

W
wizardforcel 已提交
981
```py
W
wizardforcel 已提交
982 983 984 985 986 987 988
torch.nn.functional.multi_margin_loss(input, target, p=1, margin=1, weight=None, size_average=True, reduce=True)  Tensor 
```

详细可见[MultiMarginLoss](http://pytorch.org/docs/master/nn.html#torch.nn.MultiMarginLoss)

* * *

W
wizardforcel 已提交
989
```py
W
wizardforcel 已提交
990 991 992 993 994 995 996
torch.nn.functional.nll_loss(input, target, weight=None, size_average=True, ignore_index=-100, reduce=True) 
```

详细可见[NLLLoss](http://pytorch.org/docs/master/nn.html#torch.nn.NLLLoss)

### 参数:

W
wizardforcel 已提交
997
```py
W
wizardforcel 已提交
998 999 1000 1001 1002 1003 1004 1005
input - \((NC\其中 C =类的数量或 2DLoss 的情况下的NCHW target - \((N\),其中每个值为 0 <= targets [i] <= C-1
weight可变可选 - 给每个类别的手动重新调整重量如果给定必须变量大小是 C
size_averagebool可选 - 默认情况下损失是对每个小型服务器的观察值进行平均如果 size_average  False则对于每个 minibatch 都会将损失相加默认值True
ignore_indexint可选 - 指定被忽略且不对输入渐变有贡献的目标值 size_average  True 对非忽略目标的损失是平均的默认值-100 
```

### 举例:

W
wizardforcel 已提交
1006
```py
W
wizardforcel 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
>>> # input is of size N x C = 3 x 5
>>> input = torch.randn(3, 5, requires_grad=True)
>>> # each element in target has to have 0 <= value < C
>>> target = torch.tensor([1, 0, 4])
>>> output = F.nll_loss(F.log_softmax(input), target)
>>> output.backward() 
```

* * *

W
wizardforcel 已提交
1017
```py
W
wizardforcel 已提交
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
torch.nn.functional.binary_cross_entropy_with_logits(input, target, weight=None, size_average=True, reduce=True) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#binary_cross_entropy_with_logits)

测量目标和输出逻辑之间二进制十进制熵的函数

详细可见[BCEWithLogitsLoss](http://pytorch.org/docs/master/nn.html#torch.nn.BCEWithLogitsLoss)

### 参数:

W
wizardforcel 已提交
1029
```py
W
wizardforcel 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038
input - 任意形状的变量
target - 与输入形状相同的变量
weight可变可选 - 手动重量重量如果提供重量以匹配输入张量形状
size_averagebool可选 - 默认情况下损失是对每个小型服务器的观察值进行平均然而如果字段 sizeAverage 设置为 False则相应的损失代替每个 minibatch 的求和默认值True
reduce布尔值可选-默认情况下根据每个小批次的观测结果对损失进行平均或根据 size_average 进行汇总 如果 reduce  False则返回每批损失并忽略 size_average 默认值True 
```

### 举例:

W
wizardforcel 已提交
1039
```py
W
wizardforcel 已提交
1040 1041 1042 1043 1044 1045 1046 1047
>>> input = torch.randn(3, requires_grad=True)
>>> target = torch.empty(3).random_(2)
>>> loss = F.binary_cross_entropy_with_logits(input, target)
>>> loss.backward() 
```

* * *

W
wizardforcel 已提交
1048
```py
W
wizardforcel 已提交
1049 1050 1051 1052 1053 1054 1055
torch.nn.functional.smooth_l1_loss(input, target, size_average=True, reduce=True)  Tensor 
```

详细可见[SmoothL1Loss](http://pytorch.org/docs/master/nn.html#torch.nn.SmoothL1Loss)

* * *

W
wizardforcel 已提交
1056
```py
W
wizardforcel 已提交
1057 1058 1059 1060 1061 1062 1063
torch.nn.functional.soft_margin_loss(input, target, size_average=True, reduce=True)  Tensor 
```

详细可见[Soft_margin_loss](http://pytorch.org/docs/master/nn.html#torch.nn.Soft_margin_loss)

* * *

W
wizardforcel 已提交
1064
```py
W
wizardforcel 已提交
1065 1066 1067 1068 1069 1070 1071
torch.nn.functional.triplet_margin_loss(anchor, positive, negative, margin=1.0, p=2, eps=1e-06, swap=False, size_average=True, reduce=True) 
```

详细可见[TripletMarginLoss](http://pytorch.org/docs/master/nn.html#torch.nn.TripletMarginLoss)

## 视觉函数

W
wizardforcel 已提交
1072
```py
W
wizardforcel 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
torch.nn.functional.pixel_shuffle(input, upscale_factor) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#pixel_shuffle)

将形状为[_, C_r^2, H, W]的张量重新排列成形状为[C, H_r, W_r]的张量.

详细请看[PixelShuffle](http://pytorch.org/docs/master/nn.html#torch.nn.PixelShuffle)

### 参数:

W
wizardforcel 已提交
1084
```py
W
wizardforcel 已提交
1085 1086 1087 1088 1089 1090
input (Variable)  输入
upscale_factor (int)  增加空间分辨率的因子. 
```

### 举例:

W
wizardforcel 已提交
1091
```py
W
wizardforcel 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100
>>> ps = nn.PixelShuffle(3)
>>> input = autograd.Variable(torch.Tensor(1, 9, 4, 4))
>>> output = ps(input)
>>> print(output.size())
torch.Size([1, 1, 12, 12]) 
```

* * *

W
wizardforcel 已提交
1101
```py
W
wizardforcel 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
torch.nn.functional.pad(input, pad, mode='constant', value=0) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#pad)

填充张量.

可以参考[torch.nn.ConstantPad2d](http://pytorch.org/docs/master/nn.html#torch.nn.ConstantPad2d), [torch.nn.ReflectionPad2d](http://pytorch.org/docs/master/nn.html#torch.nn.ReflectionPad2d), 和 [torch.nn.ReplicationPad2d](http://pytorch.org/docs/master/nn.html#torch.nn.ReplicationPad2d)对于每个填充模式如何工作的具体例子。

### 参数:

W
wizardforcel 已提交
1113
```py
W
wizardforcel 已提交
1114 1115 1116 1117 1118 1119 1120 1121
input (Variable)  4D  5D tensor
pad (tuple)  4 元素  6-元素 tuple
mode  constant, reflect or replicate
value  用于 constant padding 的值. 
```

### 举例:

W
wizardforcel 已提交
1122
```py
W
wizardforcel 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
>>> t4d = torch.empty(3, 3, 4, 2)
>>> p1d = (1, 1) # pad last dim by 1 on each side
>>> out = F.pad(t4d, p1d, "constant", 0)  # effectively zero padding
>>> print(out.data.size())
torch.Size([3, 3, 4, 4])
>>> p2d = (1, 1, 2, 2) # pad last dim by (1, 1) and 2nd to last by (2, 2)
>>> out = F.pad(t4d, p2d, "constant", 0)
>>> print(out.data.size())
torch.Size([3, 3, 8, 4])
>>> t4d = torch.empty(3, 3, 4, 2)
>>> p3d = (0, 1, 2, 1, 3, 3) # pad by (0, 1), (2, 1), and (3, 3)
>>> out = F.pad(t4d, p3d, "constant", 0)
>>> print(out.data.size())
torch.Size([3, 9, 7, 3]) 
```

* * *

W
wizardforcel 已提交
1141
```py
W
wizardforcel 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150
torch.nn.functional.upsample(input, size=None, scale_factor=None, mode='nearest', align_corners=None) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#upsample)

Upsamples 输入内容要么就是给定的 size 或者 scale_factor 用于采样的算法是由模型决定的 目前支持的是空间和容量的采样,即期望输入的形状是 4-d 或 5-d。 输入尺寸被解释为:迷你批 x 通道 x 深度 x 高度 x 宽度 用于 upsampling 的模型是:线性的(仅 3D),双线性的(仅 4D),三线性(仅 5D)

### 参数:

W
wizardforcel 已提交
1151
```py
W
wizardforcel 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160
input (Variable)  输入内容
size (int or Tuple[int, int] or Tuple[int, int, int])  输出空间的大小
scale_factor (int)  乘数的空间大小必须是一个整数
mode (string)  用于向上采样的算法: nearest | bilinear | trilinear
align_cornersbool可选 - 如果为 True则输入和输出张量的角点像素对齐从而保留这些像素的值 这只在模式为线性双线性或三线性时才有效 默认值False 
```

### 警告:

W
wizardforcel 已提交
1161
```py
W
wizardforcel 已提交
1162 1163 1164 1165 1166 1167
使用 align_corners = True 线性插值模式线性双线性和三线性不会按比例对齐输出和输入像素因此输出值可能取决于输入大小
这是这些模式到版本 0.3.1 的默认行为 此后默认行为是 align_corners = False 有关这将如何影响输出的具体示例请参见 Upsample 
```

* * *

W
wizardforcel 已提交
1168
```py
W
wizardforcel 已提交
1169 1170 1171 1172 1173 1174 1175
torch.nn.functional.upsample_nearest(input, size=None, scale_factor=None) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#upsample_nearest) 使用最接近的邻居的像素值来对输入进行采样。

### 警告:

W
wizardforcel 已提交
1176
```py
W
wizardforcel 已提交
1177 1178 1179 1180 1181 1182 1183
此功能已弃用以支持 torch.nn.functional.upsample()。 这相当于 nn.functional.upsample...mode ='nearest')。 
```

目前支持空间和体积上采样(即预期的输入是 4 或 5 维)。

### 参数:

W
wizardforcel 已提交
1184
```py
W
wizardforcel 已提交
1185 1186 1187 1188 1189 1190 1191
input (Variable)  输入内容
size (int or Tuple[int, int])  输出空间的大小
scale_factor (int or Tuple[int, int])  乘数的空间大小 
```

* * *

W
wizardforcel 已提交
1192
```py
W
wizardforcel 已提交
1193 1194 1195 1196 1197 1198 1199
torch.nn.functional.upsample_bilinear(input, size=None, scale_factor=None) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#upsample_bilinear) 使用双线性向上采样来扩展输入

### 警告:

W
wizardforcel 已提交
1200
```py
W
wizardforcel 已提交
1201 1202 1203 1204 1205 1206
这个函数是被弃用的使用 nn.functionalupsample 相反 预期的输入是空间的(4 )
使用 upsampletri 线性来进行体积(5 )输入 
```

### 参数:

W
wizardforcel 已提交
1207
```py
W
wizardforcel 已提交
1208 1209 1210 1211 1212 1213 1214
input (Variable)  输入内容
size (int or Tuple[int, int])  输出空间的大小
scale_factor (int or Tuple[int, int])  乘数的空间大小 
```

* * *

W
wizardforcel 已提交
1215
```py
W
wizardforcel 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
torch.nn.functional.grid_sample(input, grid, mode='bilinear', padding_mode='zeros') 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#grid_sample)

给定输入和流场网格,使用网格中的输入像素位置计算输出。

使用双线性插值来对输入像素进行采样。 目前,仅支持空间(4 维)和体积(5 维)输入。

对于每个输出位置,网格具有用于计算输出的 x,y 输入像素位置。 在 5D 输入的情况下,网格具有 x,y,z 像素位置。

grid 的值在[-1,1]的范围内。 这是因为像素位置是由输入高度和宽度标准化的。

例如,值:x:-1,y:-1 是输入的左上像素,值:x:1,y:1 是输入的右下像素。

如果 grid 的值超出[-1,1]的范围,则按 padding_mode 的定义处理这些位置。 选项为零或边框,定义这些位置以使用 0 或图像边界值作为对双线性插值的贡献。

### 参数:

W
wizardforcel 已提交
1235
```py
W
wizardforcel 已提交
1236 1237 1238 1239 1240 1241 1242
inputTensor - 输入批次N x C x IH x IWN x C x ID x IH x IW
gridTensor - 尺寸N x OH x OW x 2N x OD x OH x OW x 3的流场
padding_modestr - 用于外部网格值|的填充模式 '边境' 默认值'零' 
```

### 返回:

W
wizardforcel 已提交
1243
```py
W
wizardforcel 已提交
1244 1245 1246 1247 1248
outputtensor 
```

* * *

W
wizardforcel 已提交
1249
```py
W
wizardforcel 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258
torch.nn.functional.affine_grid(theta, size) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/functional.html#affine_grid)

生成一个 2d 流场,给定一批仿射矩阵 theta 通常与 grid_sample()一起使用来实现 Spatial Transformer Networks。

### 参数:

W
wizardforcel 已提交
1259
```py
W
wizardforcel 已提交
1260 1261 1262 1263 1264 1265
thetaTensor - 输入一批仿射矩阵N×2×3N×2×3
sizetorch.Size - 目标输出图像尺寸N×C×H×WN×C×H×W例如torch.Size((32,3,24,24)) 
```

### 返回:

W
wizardforcel 已提交
1266
```py
W
wizardforcel 已提交
1267 1268 1269 1270 1271
outputtensor:输出张量大小 (N×H×W×2N×H×W×2) 
```

## 并行函数(多 GPU,分布式)

W
wizardforcel 已提交
1272
```py
W
wizardforcel 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281
torch.nn.parallel.data_parallel(module, inputs, device_ids=None, output_device=None, dim=0, module_kwargs=None) 
```

[source](http://pytorch.org/docs/master/_modules/torch/nn/parallel/data_parallel.html#data_parallel)

在 device_ids 中给出的 GPU 上并行评估模块(输入)。 这是 DataParallel 模块的功能版本。

### 参数:

W
wizardforcel 已提交
1282
```py
W
wizardforcel 已提交
1283 1284 1285 1286 1287 1288 1289 1290
module - 并行评估的模块
input - 输入到模块
device_ids - 要在其上复制模块的 GPU ID
output_device - 输出的 GPU 位置使用-1 指示 CPU 默认device_ids [0] 
```

### 返回:

W
wizardforcel 已提交
1291
```py
W
wizardforcel 已提交
1292 1293 1294 1295 1296 1297 1298 1299
包含位于 output_device 上的模块输入结果的张量 
```

### 译者署名

| 用户名 | 头像 | 职能 | 签名 |
| --- | --- | --- | --- |
| travel | ![](img/65ca6a0b.jpg) | 翻译 | 人生总要追求点什么 |