## PyTorch 1.0 中文文档 & 教程

﻿# torch.nn.functional

## 卷积函数

### conv1d

torch.nn.functional.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) → Tensor


>>> filters = torch.randn(33, 16, 3)
>>> inputs = torch.randn(20, 16, 50)
>>> F.conv1d(inputs, filters)



### conv2d

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) → Tensor


>>> # With square kernels and equal stride
>>> filters = torch.randn(8,4,3,3)
>>> inputs = torch.randn(1,4,5,5)



### conv3d

torch.nn.functional.conv3d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) → Tensor


>>> filters = torch.randn(33, 16, 3, 3, 3)
>>> inputs = torch.randn(20, 16, 50, 10, 20)
>>> F.conv3d(inputs, filters)



### conv_transpose1d

torch.nn.functional.conv_transpose1d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1, dilation=1) → Tensor


>>> inputs = torch.randn(20, 16, 50)
>>> weights = torch.randn(16, 33, 5)
>>> F.conv_transpose1d(inputs, weights)



### conv_transpose2d

torch.nn.functional.conv_transpose2d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1, dilation=1) → Tensor


>>> # With square kernels and equal stride
>>> inputs = torch.randn(1, 4, 5, 5)
>>> weights = torch.randn(4, 8, 3, 3)



### conv_transpose3d

torch.nn.functional.conv_transpose3d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1, dilation=1) → Tensor


>>> inputs = torch.randn(20, 16, 50, 10, 20)
>>> weights = torch.randn(16, 33, 3, 3, 3)
>>> F.conv_transpose3d(inputs, weights)



### unfold

torch.nn.functional.unfold(input, kernel_size, dilation=1, padding=0, stride=1)


### fold

torch.nn.functional.fold(input, output_size, kernel_size, dilation=1, padding=0, stride=1)


## 池化函数

### avg_pool1d

torch.nn.functional.avg_pool1d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True) → Tensor


• input – 输入tensor, 形状为 $(\text{minibatch} \times \text{in\_channels} \times iW)$
• kernel_size – 窗口的大小. 可以是单个数字或元组 $(kW,)$
• stride – 窗户的步幅. 可以是单个数字或元组 (sW,). 默认值: kernel_size
• padding – 在输入的两边隐式加零. 可以是单个数字或一个元素元组 (padW,). 默认值: 0
• ceil_mode – 如果 True, 将用 ceil 代替 floor计算输出形状. 默认值: False
• count_include_pad – 如果 True, 将在平均计算中包括零填充. 默认值: True
例子::

>>> # 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.]]])



### avg_pool2d

torch.nn.functional.avg_pool2d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True) → Tensor


### avg_pool3d

torch.nn.functional.avg_pool3d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True) → Tensor


### max_pool1d

torch.nn.functional.max_pool1d(*args, **kwargs)


### max_pool2d

torch.nn.functional.max_pool2d(*args, **kwargs)


### max_pool3d

torch.nn.functional.max_pool3d(*args, **kwargs)


### max_unpool1d

torch.nn.functional.max_unpool1d(input, indices, kernel_size, stride=None, padding=0, output_size=None)


### max_unpool2d

torch.nn.functional.max_unpool2d(input, indices, kernel_size, stride=None, padding=0, output_size=None)


### max_unpool3d

torch.nn.functional.max_unpool3d(input, indices, kernel_size, stride=None, padding=0, output_size=None)


### lp_pool1d

torch.nn.functional.lp_pool1d(input, norm_type, kernel_size, stride=None, ceil_mode=False)


### lp_pool2d

torch.nn.functional.lp_pool2d(input, norm_type, kernel_size, stride=None, ceil_mode=False)


torch.nn.functional.adaptive_max_pool1d(*args, **kwargs)


• output_size – 输出目标大小(单个整数)
• return_indices – 是否返回池化索引. 默认值: False

torch.nn.functional.adaptive_max_pool2d(*args, **kwargs)


• output_size – 输出目标大小(单个整数 or 双整数元组)
• return_indices – 是否返回池化索引. 默认值: False

torch.nn.functional.adaptive_max_pool3d(*args, **kwargs)


• output_size – 输出目标大小(单个整数 或者 三整数元组)
• return_indices – 是否返回池化索引. 默认值: False

torch.nn.functional.adaptive_avg_pool1d(input, output_size) → Tensor


| 参数:| output_size – 输出目标大小(单个整数)

torch.nn.functional.adaptive_avg_pool2d(input, output_size)


| 参数:| output_size – 输出目标大小(单个整数 或者双整数元组)

torch.nn.functional.adaptive_avg_pool3d(input, output_size)


| 参数:| output_size – 输出目标大小(单个整数 或者三整数元组)

## 非线性激活函数

### threshold

torch.nn.functional.threshold(input, threshold, value, inplace=False)


torch.nn.functional.threshold_(input, threshold, value) → Tensor


### relu

torch.nn.functional.relu(input, inplace=False) → Tensor


torch.nn.functional.relu_(input) → Tensor


### hardtanh

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


### relu6

torch.nn.functional.relu6(input, inplace=False) → Tensor


### elu

torch.nn.functional.elu(input, alpha=1.0, inplace=False)


torch.nn.functional.elu_(input, alpha=1.) → Tensor


### selu

torch.nn.functional.selu(input, inplace=False) → Tensor


### celu

torch.nn.functional.celu(input, alpha=1., inplace=False) → Tensor


### leaky_relu

torch.nn.functional.leaky_relu(input, negative_slope=0.01, inplace=False) → Tensor

torch.nn.functional.leaky_relu_(input, negative_slope=0.01) → Tensor


### prelu

torch.nn.functional.prelu(input, weight) → Tensor


### rrelu

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


### glu

torch.nn.functional.glu(input, dim=-1) → Tensor


$H = A \times \sigma(B)$

• input (Tensor) – input tensor
• dim (int) – 维度用于分割 input

### logsigmoid

torch.nn.functional.logsigmoid(input) → Tensor


### hardshrink

torch.nn.functional.hardshrink(input, lambd=0.5) → Tensor


### tanhshrink

torch.nn.functional.tanhshrink(input) → Tensor


### softsign

torch.nn.functional.softsign(input) → Tensor


### softplus

torch.nn.functional.softplus(input, beta=1, threshold=20) → Tensor


### softmin

torch.nn.functional.softmin(input, dim=None, _stacklevel=3, dtype=None)


• input (Tensor) – input
• dim (int) – 计算softmin的维度(因此dim上的每个切片的和为1).
• dtype (torch.dtype, 可选的) – 返回tenosr的期望数据类型.

:param如果指定, 输入张量在执行::param操作之前被转换为dtype. 这对于防止数据类型溢出非常有用. 默认值: None.

### softmax

torch.nn.functional.softmax(input, dim=None, _stacklevel=3, dtype=None)


Softmax定义为:

$\text{Softmax}(x_{i}) = \frac{exp(x_i)}{\sum_j exp(x_j)}$

• input (Tensor) – input
• dim (int) – 将计算softmax的维度.
• dtype (torch.dtype, 可选的) – 返回tenosr的期望数据类型.

:param如果指定, 输入张量在执行::param操作之前被转换为dtype. 这对于防止数据类型溢出非常有用. 默认值: None.

### softshrink

torch.nn.functional.softshrink(input, lambd=0.5) → Tensor


### gumbel_softmax

torch.nn.functional.gumbel_softmax(logits, tau=1.0, hard=False, eps=1e-10)


• logits[batch_size, num_features] 非规范化对数概率
• tau – 非负的温度标量
• hard – 如果 True, 返回的样本将会离散为 one-hot 向量, 但将是可微分的似乎它是autograd中的soft sample

| 返回值: | 从 Gumbel-Softmax 分布采样的 tensor, 形状为 batch_size x num_features . 如果 hard=True, 返回值是 one-hot 编码, 否则, 它们就是特征和为1的概率分布

• 目前仅支持 2D 输入 logits tensor , 形状为 batch_size x num_features

### log_softmax

torch.nn.functional.log_softmax(input, dim=None, _stacklevel=3, dtype=None)


• input (Tensor) – input
• dim (int) – A dimension along which log_softmax will be computed.
• dtype (torch.dtype, 可选的) – 返回tenosr的期望数据类型.

:param如果指定, 输入张量在执行::param操作之前被转换为dtype. 这对于防止数据类型溢出非常有用. 默认值: None.

### tanh

torch.nn.functional.tanh(input) → Tensor


### sigmoid

torch.nn.functional.sigmoid(input) → Tensor


## 规范化函数

### batch_norm

torch.nn.functional.batch_norm(input, running_mean, running_var, weight=None, bias=None, training=False, momentum=0.1, eps=1e-05)


### instance_norm

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)


### layer_norm

torch.nn.functional.layer_norm(input, normalized_shape, weight=None, bias=None, eps=1e-05)


### local_response_norm

torch.nn.functional.local_response_norm(input, size, alpha=0.0001, beta=0.75, k=1.0)


### normalize

torch.nn.functional.normalize(input, p=2, dim=1, eps=1e-12, out=None)


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

• input – 任意形状的输入 tensor
• p (float) – 范数公式中的指数值. 默认值: 2
• dim (int) – 进行规约的维度. 默认值: 1
• eps (float) – 避免除以零的小值. 默认值: 1e-12
• out (Tensor, 可选的) – 输出 tensor. 如果 out 被设置, 此操作不可微分.

## 线性函数

### linear

torch.nn.functional.linear(input, weight, bias=None)


### bilinear

torch.nn.functional.bilinear(input1, input2, weight, bias=None)


## Dropout 函数

### dropout

torch.nn.functional.dropout(input, p=0.5, training=True, inplace=False)


• p – 清零概率. 默认值: 0.5
• training – 如果 True 使用 dropout. 默认值: True
• inplace – 如果设置为 True, 将会原地操作. 默认值: False

### alpha_dropout

torch.nn.functional.alpha_dropout(input, p=0.5, training=False, inplace=False)


### dropout2d

torch.nn.functional.dropout2d(input, p=0.5, training=True, inplace=False)


• p – 通道清零的概率. 默认值: 0.5
• training – 使用 dropout 如果设为 True. 默认值: True
• inplace – 如果设置为 True, 将会做原地操作. 默认值: False

### dropout3d

torch.nn.functional.dropout3d(input, p=0.5, training=True, inplace=False)


• p – 通道清零的概率. 默认值: 0.5
• training – 使用 dropout 如果设为 True. 默认值: True
• inplace – 如果设置为 True, 将会做原地操作. 默认值: False

## 稀疏函数

### embedding

torch.nn.functional.embedding(input, weight, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False)


• input (LongTensor) – 包含嵌入矩阵中的索引的tensor
• weight (Tensor) – 嵌入矩阵的行数等于可能的最大索引数+ 1, 列数等于嵌入大小
• padding_idx (int, 可选的) – 如果给定, 每当遇到索引时, 在padding_idx (初始化为零)用嵌入向量填充输出.
• max_norm (float, 可选的) – 如果给定, 则将范数大于max_norm的每个嵌入向量重新规范化, 得到范数max_norm. 注意:这将修改适当的weight.
• norm_type (float, 可选的) – 用于计算max_norm选项的p范数的p. 默认 2.
• scale_grad_by_freq (boolean_,_ 可选的) – 如果给定, 这将通过小批处理中单词频率的倒数来缩放梯度. 默认 False.
• sparse (bool, 可选的) – If True, 梯度 w.r.t. weight 将会是一个稀疏 tensor. 请看 torch.nn.Embedding有关稀疏梯度的更多详细信息.

• Input: 任意形状LongTensor, 包含要提取的索引的
• Weight: 浮点型嵌入矩阵, 形状为 (V, embedding_dim), V = maximum index + 1 并且 embedding_dim = the embedding size
• Output: (*, embedding_dim), * 是输入形状

>>> # a batch of 2 samples of 4 indices each
>>> input = torch.tensor([[1,2,4,5],[4,3,2,9]])
>>> # an embedding matrix containing 10 tensors of size 3
>>> embedding_matrix = torch.rand(10, 3)
>>> F.embedding(input, embedding_matrix)
tensor([[[ 0.8490,  0.9625,  0.6753],
[ 0.9666,  0.7761,  0.6108],
[ 0.6246,  0.9751,  0.3618],
[ 0.4161,  0.2419,  0.7383]],

[[ 0.6246,  0.9751,  0.3618],
[ 0.0237,  0.7794,  0.0528],
[ 0.9666,  0.7761,  0.6108],
[ 0.3385,  0.8612,  0.1867]]])

>>> weights = torch.rand(10, 3)
>>> weights[0, :].zero_()
>>> embedding_matrix = weights
>>> input = torch.tensor([[0,2,0,5]])
tensor([[[ 0.0000,  0.0000,  0.0000],
[ 0.5609,  0.5384,  0.8720],
[ 0.0000,  0.0000,  0.0000],
[ 0.6262,  0.2438,  0.7471]]])



### embedding_bag

torch.nn.functional.embedding_bag(input, weight, offsets=None, max_norm=None, norm_type=2, scale_grad_by_freq=False, mode='mean', sparse=False)


• input (LongTensor) – 包含索引bags的张量
• weight (Tensor) – 嵌入矩阵的行数等于可能的最大索引数+ 1, 列数等于嵌入大小
• offsets (LongTensor_,_ 可选的) – 仅当input为1D时使用. offsets确定输入中每个bag(序列)的起始索引位置
• max_norm (float, 可选的) – 如果给定, 范数大于max_norm的每个嵌入向量将被重新规格化为范数max_norm. 注意:这将就地修改weight
• norm_type (float, 可选的) – The p in the p-norm to compute for the max_norm option. 默认 2.
• scale_grad_by_freq (boolean_,_ 可选的) – 如果给定, 这将通过小批处理中单词频率的倒数来缩放梯度. 默认假. 注意:当mode="max"时不支持此选项.
• mode (string_,_ 可选的) – "sum", "mean" or "max". 指定reducebag的方法. 默认值: "mean"
• sparse (bool, 可选的) – 如果True, 梯度w.r.t.权值就是一个稀疏张量.请参见 torch.nn.Embedding 关于稀疏梯度. 注意: 此选项不支持 mode="max".

• input (LongTensor) 和 offsets (LongTensor, 可选的)

• 如果 input 是 2D的, 形状为 B x N,

它将被视为每个固定长度NB个bag(序列), 这将根据模式以某种方式返回B个聚合值. 在本例中, offsets被忽略, 并且要求为None

• 如果 input 是 1D的, 形状为 N,

it will be treated as a concatenation of multiple bags (sequences). offsets is required to be a 1D tensor containing the starting index positions of each bag in input. Therefore, for offsets of shape B, input will be viewed as having B bags. Empty bags (i.e., having 0-length) will have returned vectors filled by zeros. 它将被视为多个bag(序列)的串联. offsets必须是一个一维tensor, 其中包含input中每个bag的起始索引位置. 因此, 对于形状B的偏移量, 输入将被视为有B个bag. 空bags( 即, 具有0长度)将返回由0填充的向量

• weight (Tensor): 模块的可学习权重, 形状 (num_embeddings x embedding_dim)

• output: 聚合的嵌入值, 形状 B x embedding_dim

>>> # an Embedding module containing 10 tensors of size 3
>>> embedding_matrix = torch.rand(10, 3)
>>> # a batch of 2 samples of 4 indices each
>>> input = torch.tensor([1,2,4,5,4,3,2,9])
>>> offsets = torch.tensor([0,4])
>>> F.embedding_bag(embedding_matrix, input, offsets)
tensor([[ 0.3397,  0.3552,  0.5545],
[ 0.5893,  0.4386,  0.5882]])



## 距离函数

### pairwise_distance

torch.nn.functional.pairwise_distance(x1, x2, p=2.0, eps=1e-06, keepdim=False)


### cosine_similarity

torch.nn.functional.cosine_similarity(x1, x2, dim=1, eps=1e-8) → Tensor


$\text{similarity} = \dfrac{x_1 \cdot x_2}{\max(\Vert x_1 \Vert _2 \cdot \Vert x_2 \Vert _2, \epsilon)}$

• x1 (Tensor) – 第一个input.
• x2 (Tensor) – 第二个input (大小和 x1 匹配).
• dim (int, 可选的) – 维度. 默认值: 1
• eps (float, 可选的) – 非常小的值避免除以0. 默认值: 1e-8
形状:


>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> output = F.cosine_similarity(input1, input2)
>>> print(output)



### pdist

torch.nn.functional.pdist(input, p=2) → Tensor


## 损失函数

### binary_cross_entropy

torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_average=None, reduce=None, reduction='mean')


• input – 任意形状的tensor
• target – 与输入形状相同的tensor
• weight (Tensor, 可选的) – 手动重新调整weight, 如果提供, 它重复来匹配输入张量的形状
• size_average (bool, 可选的) – 废弃的 (见 reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: True
• reduce (bool, 可选的) – 废弃的 (见 reduction). 默认情况下, 根据size_average, 对每个小批量的观察结果的损失进行平均或求和. 当reduce为False时, 返回每批元素的损失并忽略size_average. 默认值: True
• reduction (string_,_ 可选的) – 指定要应用于输出的reduction：'none'| 'mean'| 'sum'. 'none'：没有reduction, 'mean'：输出的总和将除以输出中的元素数量 'sum'：输出将被求和. 注意：size_averagereduce正在被弃用, 同时, 指定这两个args中的任何一个都将覆盖reduce. 默认值：'mean', 默认值: ‘mean’

>>> 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()



### binary_cross_entropy_with_logits

torch.nn.functional.binary_cross_entropy_with_logits(input, target, weight=None, size_average=None, reduce=None, reduction='mean', pos_weight=None)


• input – 任意形状的tensor
• target – 与输入形状相同的tensor
• weight (Tensor, 可选的) – 手动重新调整weight, 如果提供, 它重复来匹配输入张量的形状
• size_average (bool, 可选的) – 废弃的 (见 reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: True
• reduce (bool, 可选的) – 废弃的 (见 reduction). 默认情况下, 根据size_average, 对每个小批量的观察结果的损失进行平均或求和. 当reduce为False时, 返回每批元素的损失并忽略size_average. 默认值: True
• reduction (string_,_ 可选的) – 指定要应用于输出的reduction：'none'| 'mean'| 'sum'. 'none'：没有reduction, 'mean'：输出的总和将除以输出中的元素数量 'sum'：输出将被求和. 注意：size_averagereduce正在被弃用, 同时, 指定这两个args中的任何一个都将覆盖reduce. 默认值：'mean', 默认值: ‘mean’
• pos_weight (Tensor, 可选的) – 正例样本的权重. 必须是长度等于类数的向量.

>>> input = torch.randn(3, requires_grad=True)
>>> target = torch.empty(3).random_(2)
>>> loss = F.binary_cross_entropy_with_logits(input, target)
>>> loss.backward()



### poisson_nll_loss

torch.nn.functional.poisson_nll_loss(input, target, log_input=True, full=False, size_average=None, eps=1e-08, reduce=None, reduction='mean')


### cosine_embedding_loss

torch.nn.functional.cosine_embedding_loss(input1, input2, target, margin=0, size_average=None, reduce=None, reduction='mean') → Tensor


### cross_entropy

torch.nn.functional.cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')


>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randint(5, (3,), dtype=torch.int64)
>>> loss = F.cross_entropy(input, target)
>>> loss.backward()



### ctc_loss

torch.nn.functional.ctc_loss(log_probs, targets, input_lengths, target_lengths, blank=0, reduction='mean')


Connectionist Temporal Classification损失.

>>> log_probs = torch.randn(50, 16, 20).log_softmax(2).detach().requires_grad_()
>>> targets = torch.randint(1, 20, (16, 30), dtype=torch.long)
>>> input_lengths = torch.full((16,), 50, dtype=torch.long)
>>> target_lengths = torch.randint(10,30,(16,), dtype=torch.long)
>>> loss = F.ctc_loss(log_probs, targets, input_lengths, target_lengths)
>>> loss.backward()



### hinge_embedding_loss

torch.nn.functional.hinge_embedding_loss(input, target, margin=1.0, size_average=None, reduce=None, reduction='mean') → Tensor


### kl_div

torch.nn.functional.kl_div(input, target, size_average=None, reduce=None, reduction='mean')


• input – Tensor of arbitrary shape
• target – Tensor of the same shape as input
• size_average (bool, 可选的) – 废弃的 (见 reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: True
• reduce (bool, 可选的) – 废弃的 (见 reduction). 默认情况下, 根据size_average, 对每个小批量的观察结果的损失进行平均或求和. 当reduce为False时, 返回每批元素的损失并忽略size_average. 默认值: True
• reduction (string_,_ 可选的) – 指定要应用于输出的缩减：'none'| 'batchmean'| 'sum'| 'mean'. 'none'：不会应用reduction 'batchmean'：输出的总和将除以batchsize 'sum'：输出将被加总 'mean'：输出将除以输出中的元素数 默认值：'mean'

:param . .注::size averagereduce正在被弃用, :同时, 指定这两个arg中的一个将覆盖reduce. :param . .注意::reduce = mean不返回真实的kl散度值, 请使用:reduce = batchmean, 它符合kl的数学定义.

### l1_loss

torch.nn.functional.l1_loss(input, target, size_average=None, reduce=None, reduction='mean') → Tensor


### mse_loss

torch.nn.functional.mse_loss(input, target, size_average=None, reduce=None, reduction='mean') → Tensor


### margin_ranking_loss

torch.nn.functional.margin_ranking_loss(input1, input2, target, margin=0, size_average=None, reduce=None, reduction='mean') → Tensor


### multilabel_margin_loss

torch.nn.functional.multilabel_margin_loss(input, target, size_average=None, reduce=None, reduction='mean') → Tensor


### multilabel_soft_margin_loss

torch.nn.functional.multilabel_soft_margin_loss(input, target, weight=None, size_average=None) → Tensor


### multi_margin_loss

torch.nn.functional.multi_margin_loss(input, target, p=1, margin=1.0, weight=None, size_average=None, reduce=None, reduction='mean')

multi_margin_loss(input, target, p=1, margin=1, weight=None, size_average=None,


reduce=None, reduction=’mean’) -> Tensor

### nll_loss

torch.nn.functional.nll_loss(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')


>>> # 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()



### smooth_l1_loss

torch.nn.functional.smooth_l1_loss(input, target, size_average=None, reduce=None, reduction='mean')


### soft_margin_loss

torch.nn.functional.soft_margin_loss(input, target, size_average=None, reduce=None, reduction='mean') → Tensor


### triplet_margin_loss

torch.nn.functional.triplet_margin_loss(anchor, positive, negative, margin=1.0, p=2, eps=1e-06, swap=False, size_average=None, reduce=None, reduction='mean')


## 视觉函数

### pixel_shuffle

torch.nn.functional.pixel_shuffle()


• input (Tensor) –输入 tensor
• upscale_factor (int) – 提高空间解析度的参数

>>> input = torch.randn(1, 9, 4, 4)
>>> output = torch.nn.functional.pixel_shuffle(input, 3)
>>> print(output.size())
torch.Size([1, 1, 12, 12])



torch.nn.functional.pad(input, pad, mode='constant', value=0)


Pading size:


Padding mode:


>>> t4d = torch.empty(3, 3, 4, 2)
>>> p1d = (1, 1) # pad last dim by 1 on each side
>>> 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])



### interpolate

torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None)


mode 指定插值的算法.

• input (Tensor) – 输入 tensor
• size (int or Tuple_[int] or_ Tuple_[int,_ int] or Tuple_[int,_ int, int]) – 输出 size.
• scale_factor (float or Tuple_[float]_) – 空间大小的乘数. 如果是元组, 则必须匹配输入大小.
• mode (string) – 上采样算法: ‘nearest’ | ‘linear’ | ‘bilinear’ | ‘trilinear’ | ‘area’. 默认值: ‘nearest’
• align_corners (bool, 可选的) – 如果为True, 则输入和输出张量的角像素对齐, 从而保留这些像素的值. 仅在 modelinear, bilinear, 或者 trilinear 时生效. 默认值: False

align_corners = True时, 线性插值模式(linear, bilinear, and trilinear)不会按比例对齐输出和输入像素, 因此输出值可能取决于输入大小. 这是0.3.1版之前这些模式的默认行为.此后, 默认行为为align_corners = False. 有关这如何影响输出的具体示例, 请参见上例.

### upsample

torch.nn.functional.upsample(input, size=None, scale_factor=None, mode='nearest', align_corners=None)


• input (Tensor) – 输入 tensor
• size (int or Tuple_[int] or_ Tuple_[int,_ int] or Tuple_[int,_ int, int]) – 输出 size.
• scale_factor (int) – 空间大小的乘数. 必须是整数.
• mode (string) – 上采样算法: ‘nearest’ | ‘linear’| ‘bilinear’ | ‘trilinear’. 默认值: ‘nearest’
• align_corners (bool, 可选的) – 如果为True, 则输入和输出张量的角像素对齐, 从而保留这些像素的值. 仅在 modelinear, bilinear, 或者 trilinear 时生效. 默认值: False

align_corners = True时, 线性插值模式(linear, bilinear, and trilinear)不会按比例对齐输出和输入像素, 因此输出值可能取决于输入大小. 这是0.3.1版之前这些模式的默认行为.此后, 默认行为为align_corners = False. 有关这如何影响输出的具体示例, 请参见 Upsample

### upsample_nearest

torch.nn.functional.upsample_nearest(input, size=None, scale_factor=None)


• input (Tensor) – input
• size (int or Tuple_[int,_ int] or Tuple_[int,_ int, int]) – output spatia size.
• scale_factor (int) – multiplier for spatial size. Has to be an integer.

### upsample_bilinear

torch.nn.functional.upsample_bilinear(input, size=None, scale_factor=None)


• input (Tensor) – input
• size (int or Tuple_[int,_ int]) – output spatial size.
• scale_factor (int or Tuple_[int,_ int]) – multiplier for spatial size

### grid_sample

torch.nn.functional.grid_sample(input, grid, mode='bilinear', padding_mode='zeros')


grid 大多数值应该处于 [-1, 1]. 这是因为像素位置由input 空间维度标准化.例如, 值 x = -1, y = -1input 的左上角, 值 x = 1, y = 1input 的右下角.

• padding_mode="zeros": 用 0 代替边界外的值,
• padding_mode="border": 用 border 值代替,
• padding_mode="reflection": 对于超出边界的值, 用反射的值. 对于距离边界较远的位置, 它会一直被反射, 直到到达边界, 例如(归一化)像素位置x = -3.5-1反射, 变成x' = 2.5, 然后被边界1反射, 变成x'' = -0.5.

| 返回值: | output Tensor

| 返回类型: | output (Tensor)

### affine_grid

torch.nn.functional.affine_grid(theta, size)


| 返回值: | 输出tensor, 形状为 ($N \times H \times W \times 2$)

| 返回类型: | output (Tensor)

## 数据并行函数 (multi-GPU, distributed)

### data_parallel

torch.nn.parallel.data_parallel(module, inputs, device_ids=None, output_device=None, dim=0, module_kwargs=None)


• module (Module) – 要并行评估的模块
• inputs (tensor) – 模块的输入
• device_ids (list of python:int or torch.device) – 用于复制模块的GPU id
• output_device (list of python:int or torch.device) – 输出的GPU位置使用 -1表示CPU. (默认值: device_ids[0])

| 返回值: | 一个tensor, 包含位于输出设备上的模块(输入)的结果

• #### Python进阶

东滨社 python 73页 2018年6月8日
2664

• #### Scapy 中文文档

wizardforcel python 10页 2018年5月3日
17

• #### 关于python的面试题

jackfrued python 271页 2019年5月26日
33

• #### Nginx 管理员手册

trimstray nginx 307页 2019年5月26日
8424

• #### Twisted与异步编程入门

likebeta python 23页 2018年6月29日
158

• #### Shell脚本编程30分钟入门

qinjx linux 5页 2019年3月3日
5224