# torch.nn.functional
# torch.nn.functional
## Convolution 函數
```
torch.nn.functional.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)
```
對幾個輸入平面組成的輸入信號應用1D卷積。
有關詳細信息和輸出形狀,請參見`Conv1d`。
**參數:**
- **input** – 輸入張量的形狀 (minibatch x in\_channels x iW)
- **weight** – 過濾器的形狀 (out\_channels, in\_channels, kW)
- **bias** – 可選偏置的形狀 (out\_channels)
- **stride** – 卷積核的步長,默認為1
**例子:**
```
>>> filters = autograd.Variable(torch.randn(33, 16, 3))
>>> inputs = autograd.Variable(torch.randn(20, 16, 50))
>>> F.conv1d(inputs, filters)
```
```
torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)
```
對幾個輸入平面組成的輸入信號應用2D卷積。
有關詳細信息和輸出形狀,請參見`Conv2d`。
**參數:**
- **input** – 輸入張量 (minibatch x in\_channels x iH x iW)
- **weight** – 過濾器張量 (out\_channels, in\_channels/groups, kH, kW)
- **bias** – 可選偏置張量 (out\_channels)
- **stride** – 卷積核的步長,可以是單個數字或一個元組 (sh x sw)。默認為1
- **padding** – 輸入上隱含零填充。可以是單個數字或元組。 默認值:0
- **groups** – 將輸入分成組,in\_channels應該被組數除盡
**例子:**
```
>>> # With square kernels and equal stride
>>> filters = autograd.Variable(torch.randn(8,4,3,3))
>>> inputs = autograd.Variable(torch.randn(1,4,5,5))
>>> F.conv2d(inputs, filters, padding=1)
```
```
torch.nn.functional.conv3d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)
```
對幾個輸入平面組成的輸入信號應用3D卷積。
有關詳細信息和輸出形狀,請參見`Conv3d`。
**參數:**
- **input** – 輸入張量的形狀 (minibatch x in\_channels x iT x iH x iW)
- **weight** – 過濾器張量的形狀 (out\_channels, in\_channels, kT, kH, kW)
- **bias** – 可選偏置張量的形狀 (out\_channels)
- **stride** – 卷積核的步長,可以是單個數字或一個元組 (sh x sw)。默認為1
- **padding** – 輸入上隱含零填充。可以是單個數字或元組。 默認值:0
**例子:**
```
>>> filters = autograd.Variable(torch.randn(33, 16, 3, 3, 3))
>>> inputs = autograd.Variable(torch.randn(20, 16, 50, 10, 20))
>>> F.conv3d(inputs, filters)
```
```
torch.nn.functional.conv_transpose1d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1)
```
```
torch.nn.functional.conv_transpose2d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1)
```
在由幾個輸入平面組成的輸入圖像上應用二維轉置卷積,有時也稱為“去卷積”。
有關詳細信息和輸出形狀,請參閱`ConvTranspose2d`。
**參數:**
- **input** – 輸入張量的形狀 (minibatch x in\_channels x iH x iW)
- **weight** – 過濾器的形狀 (in\_channels x out\_channels x kH x kW)
- **bias** – 可選偏置的形狀 (out\_channels)
- **stride** – 卷積核的步長,可以是單個數字或一個元組 (sh x sw)。默認: 1
- **padding** – 輸入上隱含零填充。可以是單個數字或元組。 (padh x padw)。默認: 0
- **groups** – 將輸入分成組,`in_channels`應該被組數除盡
- **output\_padding** – 0 <= padding <stride的零填充,應該添加到輸出。可以是單個數字或元組。默認值:0
```
torch.nn.functional.conv_transpose3d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1)
```
在由幾個輸入平面組成的輸入圖像上應用三維轉置卷積,有時也稱為“去卷積”。
有關詳細信息和輸出形狀,請參閱`ConvTranspose3d`。
**參數:**
- **input** – 輸入張量的形狀 (minibatch x in\_channels x iT x iH x iW)
- **weight** – 過濾器的形狀 (in\_channels x out\_channels x kH x kW)
- **bias** – 可選偏置的形狀 (out\_channels)
- **stride** – 卷積核的步長,可以是單個數字或一個元組 (sh x sw)。默認: 1
- **padding** – 輸入上隱含零填充。可以是單個數字或元組。 (padh x padw)。默認: 0
## Pooling 函數
```
torch.nn.functional.avg_pool1d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)
```
對由幾個輸入平面組成的輸入信號進行一維平均池化。
有關詳細信息和輸出形狀,請參閱`AvgPool1d`。
**參數:**
- **kernel\_size** – 窗口的大小
- **stride** – 窗口的步長。默認值為`kernel_size`
- **padding** – 在兩邊添加隱式零填充
- **ceil\_mode** – 當為True時,將使用`ceil`代替`floor`來計算輸出形狀
- **count\_include\_pad** – 當為True時,這將在平均計算時包括補零
**例子:**
```
>>> # pool of square window of size=3, stride=2
>>> input = Variable(torch.Tensor([[[1,2,3,4,5,6,7]]]))
>>> F.avg_pool1d(input, kernel_size=3, stride=2)
Variable containing:
(0 ,.,.) =
2 4 6
[torch.FloatTensor of size 1x1x3]
```
```
torch.nn.functional.avg_pool2d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)
```
在kh x kw區域中應用步長為dh x dw的二維平均池化操作。輸出特征的數量等于輸入平面的數量。
有關詳細信息和輸出形狀,請參閱`AvgPool2d`。
**參數:**
- **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** – 定義空間輸出形狀的操作
- **count\_include\_pad** – 除以原始非填充圖像內的元素數量或kh \* kw
```
torch.nn.functional.avg_pool3d(input, kernel_size, stride=None)
```
在kt x kh x kw區域中應用步長為dt x dh x dw的二維平均池化操作。輸出特征的數量等于 input planes / dt。
```
torch.nn.functional.max_pool1d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False)
```
```
torch.nn.functional.max_pool2d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False)
```
```
torch.nn.functional.max_pool3d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False)
```
```
torch.nn.functional.max_unpool1d(input, indices, kernel_size, stride=None, padding=0, output_size=None)
```
```
torch.nn.functional.max_unpool2d(input, indices, kernel_size, stride=None, padding=0, output_size=None)
```
```
torch.nn.functional.max_unpool3d(input, indices, kernel_size, stride=None, padding=0, output_size=None)
```
```
torch.nn.functional.lp_pool2d(input, norm_type, kernel_size, stride=None, ceil_mode=False)
```
```
torch.nn.functional.adaptive_max_pool1d(input, output_size, return_indices=False)
```
在由幾個輸入平面組成的輸入信號上應用1D自適應最大池化。
有關詳細信息和輸出形狀,請參閱`AdaptiveMaxPool1d`。
**參數:**
- **output\_size** – 目標輸出大小(單個整數)
- **return\_indices** – 是否返回池化的指數
```
torch.nn.functional.adaptive_max_pool2d(input, output_size, return_indices=False)
```
在由幾個輸入平面組成的輸入信號上應用2D自適應最大池化。
有關詳細信息和輸出形狀,請參閱`AdaptiveMaxPool2d`。
**參數:**
- **output\_size** – 目標輸出大小(單整數或雙整數元組)
- **return\_indices** – 是否返回池化的指數
```
torch.nn.functional.adaptive_avg_pool1d(input, output_size)
```
在由幾個輸入平面組成的輸入信號上應用1D自適應平均池化。
有關詳細信息和輸出形狀,請參閱`AdaptiveAvgPool1d`。
**參數:**
- **output\_size** – 目標輸出大小(單整數或雙整數元組)
```
torch.nn.functional.adaptive_avg_pool2d(input, output_size)
```
在由幾個輸入平面組成的輸入信號上應用2D自適應平均池化。
有關詳細信息和輸出形狀,請參閱`AdaptiveAvgPool2d`。
**參數:**
- **output\_size** – 目標輸出大小(單整數或雙整數元組)
## 非線性激活函數
```
torch.nn.functional.threshold(input, threshold, value, inplace=False)
```
```
torch.nn.functional.relu(input, inplace=False)
```
```
torch.nn.functional.hardtanh(input, min_val=-1.0, max_val=1.0, inplace=False)
```
```
torch.nn.functional.relu6(input, inplace=False)
```
```
torch.nn.functional.elu(input, alpha=1.0, inplace=False)
```
```
torch.nn.functional.leaky_relu(input, negative_slope=0.01, inplace=False)
```
```
torch.nn.functional.prelu(input, weight)
```
```
torch.nn.functional.rrelu(input, lower=0.125, upper=0.3333333333333333, training=False, inplace=False)
```
```
torch.nn.functional.logsigmoid(input)
```
```
torch.nn.functional.hardshrink(input, lambd=0.5)
```
```
torch.nn.functional.tanhshrink(input)
```
```
torch.nn.functional.softsign(input)
```
```
torch.nn.functional.softplus(input, beta=1, threshold=20)
```
```
torch.nn.functional.softmin(input)
```
```
torch.nn.functional.softmax(input)
```
```
torch.nn.functional.softshrink(input, lambd=0.5)
```
```
torch.nn.functional.log_softmax(input)
```
```
torch.nn.functional.tanh(input)
```
```
torch.nn.functional.sigmoid(input)
```
## Normalization 函數
```
torch.nn.functional.batch_norm(input, running_mean, running_var, weight=None, bias=None, training=False, momentum=0.1, eps=1e-05)
```
## 線性函數
```
torch.nn.functional.linear(input, weight, bias=None)
```
## Dropout 函數
```
torch.nn.functional.dropout(input, p=0.5, training=False, inplace=False)
```
## 距離函數(Distance functions)
```
torch.nn.functional.pairwise_distance(x1, x2, p=2, eps=1e-06)
```
計算向量v1、v2之間的距離(成次或者成對,意思是可以計算多個,可以參看后面的參數)
$$ \\left | x \\right |*{p}:=\\left ( \\sum*{i=1}^{N}\\left | x\_{i}^{p} \\right | \\right )^{1/p}
$$ **參數:**
- x1:第一個輸入的張量
- x2:第二個輸入的張量
- p:矩陣范數的維度。默認值是2,即二范數。
**規格:**
- 輸入:(N,D)其中D等于向量的維度
- 輸出:(N,1)
**例子:**
```
>>> input1 = autograd.Variable(torch.randn(100, 128))
>>> input2 = autograd.Variable(torch.randn(100, 128))
>>> output = F.pairwise_distance(input1, input2, p=2)
>>> output.backward()
```
## 損失函數(Loss functions)
```
torch.nn.functional.nll_loss(input, target, weight=None, size_average=True)
```
負的log likelihood損失函數. 詳細請看[NLLLoss](...).
**參數:**
- **input** - (N,C) C 是類別的個數
- **target** - (N) 其大小是 0 <= targets\[i\] <= C-1
- **weight** (Variable, optional) – 一個可手動指定每個類別的權重。如果給定的話,必須是大小為nclasses的Variable
- **size\_average** (bool, optional) – 默認情況下,是`mini-batch`loss的平均值,然而,如果size\_average=False,則是`mini-batch`loss的總和。
**Variables:**
- **weight** – 對于constructor而言,每一類的權重作為輸入
```
torch.nn.functional.kl_div(input, target, size_average=True)
```
KL 散度損失函數,詳細請看[KLDivLoss](...)
**參數:**
- **input** – 任意形狀的 Variable
- **target** – 與輸入相同形狀的 Variable
- **size\_average** – 如果為TRUE,loss則是平均值,需要除以輸入 tensor 中 element 的數目
```
torch.nn.functional.cross_entropy(input, target, weight=None, size_average=True)
```
該函數使用了 log\_softmax 和 nll\_loss,詳細請看[CrossEntropyLoss](...)
**參數:**
- **input** - (N,C) 其中,C 是類別的個數
- **target** - (N) 其大小是 0 <= targets\[i\] <= C-1
- **weight** (Variable, optional) – 一個可手動指定每個類別的權重。如果給定的話,必須是大小為nclasses的Variable
- **size\_average** (bool, optional) – 默認情況下,是`mini-batch`loss的平均值,然而,如果size\_average=False,則是`mini-batch`loss的總和。
```
torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_average=True)
```
該函數計算了輸出與target之間的二進制交叉熵,詳細請看[BCELoss](...)
**參數:**
- **input** – 任意形狀的 Variable
- **target** – 與輸入相同形狀的 Variable
- **weight** (Variable, optional) – 一個可手動指定每個類別的權重。如果給定的話,必須是大小為nclasses的Variable
- **size\_average** (bool, optional) – 默認情況下,是`mini-batch`loss的平均值,然而,如果size\_average=False,則是`mini-batch`loss的總和。
```
torch.nn.functional.smooth_l1_loss(input, target, size_average=True)
```
## Vision functions
### torch.nn.functional.pixel\_shuffle(input, upscale\_factor)\[source\]
將形狀為`[*, C*r^2, H, W]`的`Tensor`重新排列成形狀為`[C, H*r, W*r]`的Tensor.
詳細請看[PixelShuffle](..).
形參說明:
- input (Variable) – 輸入
- upscale\_factor (int) – 增加空間分辨率的因子.
例子:
```
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])
```
### torch.nn.functional.pad(input, pad, mode='constant', value=0)\[source\]
填充`Tensor`.
目前為止,只支持`2D`和`3D`填充. Currently only 2D and 3D padding supported. 當輸入為`4D Tensor`的時候,`pad`應該是一個4元素的`tuple (pad_l, pad_r, pad_t, pad_b )` ,當輸入為`5D Tensor`的時候,`pad`應該是一個6元素的`tuple (pleft, pright, ptop, pbottom, pfront, pback)`.
形參說明:
- input (Variable) – 4D 或 5D `tensor`
- pad (tuple) – 4元素 或 6-元素 `tuple`
- mode – ‘constant’, ‘reflect’ or ‘replicate’
- value – 用于`constant padding` 的值.
- 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
- 致謝