# torch.nn.functional

译者:hijkzzz

卷积函数

conv1d

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

对由多个输入平面组成的输入信号进行一维卷积.

有关详细信息和输出形状, 请参见Conv1d.

注意

在某些情况下, 当使用CUDA后端与CuDNN时, 该操作符可能会选择不确定性算法来提高性能. 如果这不是您希望的, 您可以通过设置torch.backends.cudn .deterministic = True来尝试使操作具有确定性(可能会以性能为代价). 请参阅关于 Reproducibility 了解背景.

参数:

  • input – 输入tensor, 形状为
  • weight – 卷积核, 形状为
  • bias – 可选的偏置, 形状为 . 默认值: None
  • stride – 卷积核的步幅, 可以是单个数字或一个元素元组(sW,). 默认值: 1
  • padding – 在输入的两边隐式加零. 可以是单个数字或一个元素元组(padW, ). 默认值: 0
  • dilation – 核元素之间的空洞. 可以是单个数字或单元素元组(dW,). 默认值: 1
  • groups – 将输入分成组, 应该可以被组的数目整除. 默认值: 1

例子:

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

对由多个输入平面组成的输入图像应用二维卷积.

有关详细信息和输出形状, 请参见Conv2d.

注意

在某些情况下, 当使用CUDA后端与CuDNN时, 该操作符可能会选择不确定性算法来提高性能. 如果这不是您希望的, 您可以通过设置torch.backends.cudn .deterministic = True来尝试使操作具有确定性(可能会以性能为代价). 请参阅关于 Reproducibility 了解背景.

参数:

  • input – 输入tensor, 形状为
  • weight – 卷积核, 形状为
  • bias – 可选的偏置, 形状为 . 默认值: None
  • stride – 卷积核的步幅, 可以是单个数字或一个元素元组 (sH, sW). 默认值: 1
  • padding – 在输入的两边隐式加零. 可以是单个数字或一个元素元组 (padH, padW). 默认值: 0
  • dilation – 核元素之间的空洞. 可以是单个数字或单元素元组 (dH, dW). 默认值: 1
  • groups – 将输入分成组, 应该可以被组的数目整除. 默认值: 1

例子:

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

conv3d

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

对由多个输入平面组成的输入图像应用三维卷积.

有关详细信息和输出形状, 请参见 Conv3d.

注意

在某些情况下, 当使用CUDA后端与CuDNN时, 该操作符可能会选择不确定性算法来提高性能. 如果这不是您希望的, 您可以通过设置torch.backends.cudn .deterministic = True来尝试使操作具有确定性(可能会以性能为代价). 请参阅关于 Reproducibility 了解背景.

参数:

  • input – 输入tensor, 形状为
  • weight – 卷积核, 形状为
  • bias – 可选的偏置, 形状为 . 默认值: None
  • stride – 卷积核的步幅, 可以是单个数字或一个元素元组 (sT, sH, sW). 默认值: 1
  • padding – 在输入的两边隐式加零. 可以是单个数字或一个元素元组 (padT, padH, padW). 默认值: 0
  • dilation – 核元素之间的空洞. 可以是单个数字或单元素元组 (dT, dH, dW). 默认值: 1
  • groups – 将输入分成组, 应该可以被组的数目整除. 默认值: 1

例子:

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

对由多个输入平面组成的输入信号应用一维转置卷积算子, 有时也称为反卷积.

有关详细信息和输出形状, 请参见 ConvTranspose1d

注意

在某些情况下, 当使用CUDA后端与CuDNN时, 该操作符可能会选择不确定性算法来提高性能. 如果这不是您希望的, 您可以通过设置torch.backends.cudn .deterministic = True来尝试使操作具有确定性(可能会以性能为代价). 请参阅关于 Reproducibility 了解背景.

参数:

  • input – 输入tensor, 形状为
  • weight – 卷积核, 形状为
  • bias – 可选的偏置, 形状为 . 默认值: None
  • stride – 卷积核的步幅, 可以是单个数字或一个元素元组 (sW,). 默认值: 1
  • padding – 输入中的每个维度的两边都将添加零填充kernel_size - 1 - padding. 可以是单个数字或元组 (padW,). 默认值: 0
  • output_padding – 添加到输出形状中每个维度的一侧的额外大小. 可以是单个数字或元组 (out_padW). 默认值: 0
  • groups – 将输入分成组, 应该可以被组的数目整除. 默认值: 1
  • dilation – 核元素之间的空洞. 可以是单个数字或单元素元组 (dW,). 默认值: 1

例子:

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

对由多个输入平面组成的输入图像应用二维转置卷积算子, 有时也称为反卷积.

有关详细信息和输出形状, 请参见 ConvTranspose2d.

注意

在某些情况下, 当使用CUDA后端与CuDNN时, 该操作符可能会选择不确定性算法来提高性能. 如果这不是您希望的, 您可以通过设置torch.backends.cudn .deterministic = True来尝试使操作具有确定性(可能会以性能为代价). 请参阅关于 Reproducibility 了解背景.

参数:

  • input – 输入tensor, 形状为
  • weight – 卷积核, 形状为
  • bias –可选的偏置, 形状为 . 默认值: None
  • stride – 卷积核的步幅, 可以是单个数字或一个元素元组 (sH, sW). 默认值: 1
  • padding – 输入中的每个维度的两边都将添加零填充kernel_size - 1 - padding. 可以是单个数字或元组 (padH, padW). 默认值: 0
  • output_padding – 添加到输出形状中每个维度的一侧的额外大小. 可以是单个数字或元组 (out_padH, out_padW). 默认值: 0
  • groups – 将输入分成组, 应该可以被组的数目整除. 默认值: 1
  • dilation – 核元素之间的空洞. 可以是单个数字或单元素元组 (dH, dW). 默认值: 1

例子:

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

conv_transpose3d

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

对由多个输入平面组成的输入图像应用一个三维转置卷积算子, 有时也称为反卷积

有关详细信息和输出形状, 请参见 ConvTranspose3d.

注意

在某些情况下, 当使用CUDA后端与CuDNN时, 该操作符可能会选择不确定性算法来提高性能. 如果这不是您希望的, 您可以通过设置torch.backends.cudn .deterministic = True来尝试使操作具有确定性(可能会以性能为代价). 请参阅关于 Reproducibility 了解背景.

参数:

  • input – 输入tensor, 形状为
  • weight – 卷积核, 形状为
  • bias –可选的偏置, 形状为 . 默认值: None
  • stride – 卷积核的步幅, 可以是单个数字或一个元素元组 (sT, sH, sW). 默认值: 1
  • padding – 输入中的每个维度的两边都将添加零填充kernel_size - 1 - padding. 可以是单个数字或元组 (padT, padH, padW). 默认值: 0
  • output_padding – 添加到输出形状中每个维度的一侧的额外大小. 可以是单个数字或元组 (out_padT, out_padH, out_padW). 默认值: 0
  • groups – 将输入分成组, 应该可以被组的数目整除. 默认值: 1
  • dilation – 核元素之间的空洞. 可以是单个数字或单元素元组 (dT, dH, dW). 默认值: 1

例子:

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

从批量的inputtensor中提取滑动局部块.

警告

目前, 仅支持 4-D input tensors (如批量的图像 tensors).

细节请参阅 torch.nn.Unfold

fold

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

将一组滑动局部块数组合成一个大的tensor.

警告

目前, 仅支持 4-D input tensors (如批量的图像 tensors).

细节请参阅 torch.nn.Fold

池化函数

avg_pool1d

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

对由多个输入平面组成的输入信号应用一维平均池化.

有关详细信息和输出形状, 请参见 AvgPool1d.

参数:

  • input – 输入tensor, 形状为
  • kernel_size – 窗口的大小. 可以是单个数字或元组
  • 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

应用2D平均池化操作于 区域, 步幅为 . 输出特征的数量等于输入平面的数量.

有关详细信息和输出形状, 请参见 AvgPool2d.

参数:

  • input – input tensor
  • kernel_size – 池化区域的大小, 可以是一个数字或者元组
  • stride – 池化步幅, 可以是一个数字或者元组 (sH, sW). 默认值: kernel_size
  • padding – 在输入的两边隐式加零. 可以是单个数字或一个元素元组 (padH, padW). 默认值: 0
  • ceil_mode – 如果 True, 将用 ceil 代替 floor计算输出形状. 默认值: False
  • count_include_pad – 如果 True, 将在平均计算中包括零填充. 默认值: True

avg_pool3d

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

应用3D平均池化操作于 区域, 步幅为 . 输出特征的数量等于 .

有关详细信息和输出形状, 请参见 AvgPool3d.

参数:

  • input – input tensor
  • kernel_size – 池化区域的大小, 可以是一个数字或者元组
  • stride – 池化步幅, 可以是一个数字或者元组 (sT, sH, sW). 默认值: kernel_size
  • padding – 在输入的两边隐式加零. 可以是单个数字或一个元素元组 (padT, padH, padW), 默认值: 0
  • ceil_mode – 如果 True, 将用 ceil 代替 floor计算输出形状. 默认值: False
  • count_include_pad – 如果 True, 将在平均计算中包括零填充. 默认值: True

max_pool1d

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

在由多个输入平面组成的输入信号上应用1D 最大池化.

详情见 MaxPool1d.

max_pool2d

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

在由多个输入平面组成的输入信号上应用2D 最大池化.

详情见 MaxPool2d.

max_pool3d

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

在由多个输入平面组成的输入信号上应用3D 最大池化.

详情见 MaxPool3d.

max_unpool1d

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

计算逆 MaxPool1d.

请参见 MaxUnpool1d.

max_unpool2d

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

计算逆 MaxPool2d.

详情见 MaxUnpool2d.

max_unpool3d

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

计算逆 MaxPool3d.

详情见 MaxUnpool3d.

lp_pool1d

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

在由多个输入平面组成的输入信号上应用1D幂平均池化. 如果所有输入的p次方的和为零, 梯度也为零.

详情见 LPPool1d.

lp_pool2d

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

在由多个输入平面组成的输入信号上应用2D幂平均池化. 如果所有输入的p次方的和为零, 梯度也为零.

详情见 LPPool2d.

adaptive_max_pool1d

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

在由多个输入平面组成的输入信号上应用1D自适应最大池.

请参见 AdaptiveMaxPool1d和输出形状.

参数:

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

adaptive_max_pool2d

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

在由多个输入平面组成的输入信号上应用2D自适应最大池.

请参见 AdaptiveMaxPool2d 关于详情和输出形状.

参数:

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

adaptive_max_pool3d

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

在由多个输入平面组成的输入信号上应用3D自适应最大池.

请参见 AdaptiveMaxPool3d和输出形状.

参数:

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

adaptive_avg_pool1d

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

在由多个输入平面组成的输入信号上应用1D自适应平均池化.

请参见 AdaptiveAvgPool1d 关于详情和输出形状.

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

adaptive_avg_pool2d

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

在由多个输入平面组成的输入信号上应用2D自适应平均池化.

请参见 AdaptiveAvgPool2d 关于详情和输出形状.

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

adaptive_avg_pool3d

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

在由多个输入平面组成的输入信号上应用3D自适应平均池化.

请参见 AdaptiveAvgPool3d 关于详情和输出形状.

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

非线性激活函数

threshold

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

为 input Tensor 的每个元素设置阈值.

请参见 Threshold.

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

原地版本的 threshold().

relu

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

逐元素应用整流线性单元函数. 请参见 ReLU.

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

原地版本的 relu().

hardtanh

torch.nn.functional.hardtanh(input, min_val=-1., max_val=1., inplace=False) → Tensor

逐元素应用hardtanh函数. 请参见 Hardtanh.

torch.nn.functional.hardtanh_(input, min_val=-1., max_val=1.) → Tensor

原地版本的 hardtanh().

relu6

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

逐元素应用函数 .

请参见 ReLU6.

elu

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

逐元素应用, .

请参见 ELU.

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

原地版本的 elu().

selu

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

逐元素应用, , with and .

请参见 SELU.

celu

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

逐元素应用, .

请参见 CELU.

leaky_relu

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

逐元素应用,

请参见 LeakyReLU.

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

原地版本的 leaky_relu().

prelu

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

逐元素应用函数 where weight is a learnable parameter.

请参见 PReLU.

rrelu

torch.nn.functional.rrelu(input, lower=1./8, upper=1./3, training=False, inplace=False) → Tensor

随机的 leaky ReLU.

请参见 RReLU.

torch.nn.functional.rrelu_(input, lower=1./8, upper=1./3, training=False) → Tensor

原地版本的 rrelu().

glu

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

门控线性单元. 计算:

其中inpuy沿dim分成两半, 形成AB.

Language Modeling with Gated Convolutional Networks.

参数:

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

logsigmoid

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

逐元素应用

请参见 LogSigmoid.

hardshrink

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

逐元素应用hardshrink函数

请参见 Hardshrink.

tanhshrink

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

逐元素应用,

请参见 Tanhshrink.

softsign

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

逐元素应用, the function

请参见 Softsign.

softplus

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

softmin

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

应用 softmin 函数.

注意 . See softmax definition for mathematical formula.

请参见 Softmin.

参数:

  • 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 函数.

Softmax定义为:

它应用于dim上的所有切片, 并将对它们进行重新缩放, 使元素位于(0,1)范围内, 和为1.

请参见 Softmax.

参数:

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

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

注意

此函数不能直接用于NLLLoss, NLLLoss期望在Softmax和它自己之间计算对数. 改用log_softmax (它速度更快, 数值性能更好).

softshrink

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

逐元素应用 soft shrinkage 函数

请参见 Softshrink.

gumbel_softmax

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

采样自Gumbel-Softmax分布, 并可选地离散化.

参数:

  • 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

基于 https://github.com/ericjang/gumbel-softmax/blob/3c8584924603869e90ca74ac20a6a03d99a91ef9/Categorical VAE.ipynb , (MIT license)

log_softmax

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

应用 softmax 跟随对数运算.

虽然在数学上等价于log(softmax(x)), 但分别执行这两个操作比较慢, 而且在数值上不稳定. 这个函数使用另一种公式来正确计算输出和梯度.

请参见 LogSoftmax.

参数:

  • 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

逐元素应用,

请参见 Tanh.

sigmoid

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

逐元素应用函数

请参见 Sigmoid.

规范化函数

batch_norm

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

对一批数据中的每个通道应用批量标准化.

请参见 BatchNorm1d, BatchNorm2d, BatchNorm3d.

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)

对批中每个数据样本中的每个通道应用实例规范化.

请参见 InstanceNorm1d, InstanceNorm2d, InstanceNorm3d.

layer_norm

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

对最后特定数量的维度应用layer规范化.

请参见 LayerNorm.

local_response_norm

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

对由多个输入平面组成的输入信号进行局部响应归一化, 其中通道占据第二维. 跨通道应用标准化.

请参见 LocalResponseNorm.

normalize

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

执行 规范化对于指定维度.

对于一个 tensor input 尺寸为 , 每一 -元素向量 沿着维度 dim 被转换为

对于默认参数, 它使用沿维度的欧几里得范数进行标准化.

参数:

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

对传入数据应用线性转换: .

形状:

  • Input: * 表示任意数量的附加维度
  • Weight:
  • Bias:
  • Output:

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将输入张量的一些元素归零.

请参见 Dropout.

参数:

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

应用 alpha dropout.

请参见 AlphaDropout.

dropout2d

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

随机归零input tensor的整个通道 (一个通道是一个 2D 特征图, 例如, -th channel of the -th sample in the batched input is a 2D tensor )). 每次前向传递时, 每个信道都将被独立清零. 用概率 p 从 Bernoulli 分布采样.

请参见 Dropout2d.

参数:

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

dropout3d

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

随机归零input tensor的整个通道 (一个通道是一个 3D 特征图, 例如, the -th channel of the -th sample in the batched input is a 3D tensor ). 每次前向传递时, 每个信道都将被独立清零. 用概率 p 从 Bernoulli 分布采样.

请参见 Dropout3d.

参数:

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

一个简单的查找表, 查找固定字典中的embedding(嵌入)内容和大小.

这个模块通常用于使用索引检索单词嵌入. 模块的输入是索引列表和嵌入矩阵, 输出是相应的单词嵌入.

请参见 torch.nn.Embedding.

参数:

  • 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]]])

>>> # example with padding_idx
>>> weights = torch.rand(10, 3)
>>> weights[0, :].zero_()
>>> embedding_matrix = weights
>>> input = torch.tensor([[0,2,0,5]])
>>> F.embedding(input, embedding_matrix, padding_idx=0)
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)

计算嵌入bags的和、平均值或最大值, 而不实例化中间嵌入. .

请参见 torch.nn.EmbeddingBag

参数:

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

请参见 torch.nn.PairwiseDistance

cosine_similarity

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

返回x1和x2之间的余弦相似度, 沿dim计算

参数:

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

例子:

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

计算输入中每​​对行向量之间的p范数距离. 这与torch.norm(input[:, None] - input, dim=2, p=p)的上三角形部分(不包括对角线)相同. 如果行是连续的, 则此函数将更快

如果输入具有形状 则输出将具有形状 .

这个函数相当于 scipy.spatial.distance.pdist(input, ‘minkowski’, p=p) 如果 . 当 它等价于 scipy.spatial.distance.pdist(input, ‘hamming’) * M. 当 , 最相近的scipy函数是 scipy.spatial.distance.pdist(xn, lambda x, y: np.abs(x - y).max()).

参数:

  • input – 输入tensor, 形状为 .
  • p – 计算每个向量对之间的p范数距离的p值 .

损失函数

binary_cross_entropy

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

计算目标和输出之间二进制交叉熵的函数.

请参见 BCELoss.

参数:

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

计算目标和输出logits之间的二进制交叉熵的函数.

请参见 BCEWithLogitsLoss.

参数:

  • 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')

泊松负对数似然损失.

请参见 PoissonNLLLoss.

参数:

  • input – 潜在泊松分布的期望.
  • target – 随机抽样 .
  • log_input – 如果为True, 则损失计算为 , 如果为False, 则损失计算为 . 默认值: True
  • full – 是否计算全部损失, 即. 加入Stirling近似项. 默认值: False .
  • size_average (bool, 可选的) – 废弃的 (见 reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: True
  • eps (float, 可选的) – 一个小值避免求值 when log_input=False. 默认值: 1e-8
  • 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’

cosine_embedding_loss

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

请参见 CosineEmbeddingLoss.

cross_entropy

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

此函数结合了 log_softmaxnll_loss.

请参见 CrossEntropyLoss.

参数:

  • input (Tensor) – where C = number of classes or in case of 2D Loss, or where in the case of K-dimensional loss.
  • target (Tensor) – where each value is , or where for K-dimensional loss.
  • weight (Tensor, 可选的) – 给每个类别的手动重定权重. 如果给定, 必须是大小为C的tensor
  • size_average (bool, 可选的) – 废弃的 (见 reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: True
  • ignore_index (int, 可选的) – Specifies a target value that is ignored and does not contribute to the input gradient. When size_average is True, the loss is averaged over non-ignored targets. 默认值: -100
  • 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, 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损失.

请参见 CTCLoss.

注意

在某些情况下, 当使用CUDA后端与CuDNN时, 该操作符可能会选择不确定性算法来提高性能. 如果这不是您希望的, 您可以通过设置torch.backends.cudn .deterministic = True来尝试使操作具有确定性(可能会以性能为代价). 请参阅关于 Reproducibility 了解背景.

注意

当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.

参数:

  • log_probs where C = number of characters in alphabet including blank, T = input length, and N = batch size. The logarithmized probabilities of the outputs (e.g. obtained with torch.nn.functional.log_softmax()).
  • targets or (sum(target_lengths)). Targets (cannot be blank). In the second form, the targets are assumed to be concatenated.
  • input_lengths. Lengths of the inputs (must each be )
  • target_lengths. Lengths of the targets
  • blank (int, 可选的) – Blank label. 默认 .
  • reduction (string_,_ 可选的) - 指定要应用于输出的reduction:'none'| 'mean'| 'sum'. 'none':不会应用reduce, 'mean':输出损失将除以目标长度, 然后得到批次的平均值. 默认值:'mean'

例子:

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

请参见 HingeEmbeddingLoss.

kl_div

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

Kullback-Leibler divergence 损失.

请参见 KLDivLoss.

参数:

  • 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的数学定义.

在下一个主要版本中, “mean”将被修改为与“batchmean”相同.

l1_loss

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

函数的作用是求元素的绝对值的平均值.

请参见 L1Loss.

mse_loss

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

测量元素的均方误差.

请参见 MSELoss.

margin_ranking_loss

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

请参见 MarginRankingLoss.

multilabel_margin_loss

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

请参见 MultiLabelMarginLoss.

multilabel_soft_margin_loss

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

请参见 MultiLabelSoftMarginLoss.

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

请参见 MultiMarginLoss.

nll_loss

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

负的对数似然函数.

请参见 NLLLoss.

参数:

  • input C = 类别的数量 或者 在二维损失的情况下, 或者 在K维损失的情况下.
  • target 每个值是 , 或者 K维损失.
  • weight (Tensor, 可选的) – 给每个类别的手动重定权重. 如果给定, 必须是大小为C的tensor
  • size_average (bool, 可选的) – 废弃的 (见 reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: True
  • ignore_index (int, 可选的) – 指定一个被忽略的目标值, 该值不会影响输入梯度. 当size_averageTrue时, 损耗在未忽略的目标上平均. 默认值: -100
  • 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 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')

如果绝对元素误差低于1, 则使用平方项, 否则使用L1项的函数.

请参见 SmoothL1Loss.

soft_margin_loss

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

请参见 SoftMarginLoss.

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

请参见 TripletMarginLoss

视觉函数

pixel_shuffle

torch.nn.functional.pixel_shuffle()

重新排列tensor中的元素, 从形状 .

请参见 PixelShuffle.

参数:

  • 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])

pad

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

填充 tensor.

Pading size:

要填充的维度数为 填充的维度从最后一个维度开始向前移动. 例如, 填充输入tensor的最后一个维度, 所以 pad 形如 (padLeft, padRight); 填充最后 2 个维度, 使用 (padLeft, padRight, padTop, padBottom); 填充最后 3 个维度, 使用 (padLeft, padRight, padTop, padBottom, padFront, padBack).

Padding mode:

请参见 torch.nn.ConstantPad2d, torch.nn.ReflectionPad2d, and torch.nn.ReplicationPad2d 有关每个填充模式如何工作的具体示例. Constant padding 已经实现于任意维度. 复制填充用于填充5D输入张量的最后3个维度, 或4D输入张量的最后2个维度, 或3D输入张量的最后一个维度. 反射填充仅用于填充4D输入张量的最后两个维度, 或者3D输入张量的最后一个维度.

注意

当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.

参数:

  • input (Tensor) – Nd tensor
  • pad (tuple) – m-elem tuple, where input dimensions and is even.
  • mode – ‘constant’, ‘reflect’ or ‘replicate’. 默认值: ‘constant’
  • value – fill value for ‘constant’ padding. 默认值: 0

例子:

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

interpolate

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

向下/向上采样输入到给定的size或给定的scale_facto

mode 指定插值的算法.

目前支持时间, 空间和体积上采样, 即预期输入为3-D, 4-D或5-D形状.

输入维度形式: mini-batch x channels x [可选的 depth] x [可选的 height] x width.

可用于上采样的模式是: nearest, linear (仅3D), bilinear (仅4D), trilinear (仅5D), area

参数:

  • 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. 有关这如何影响输出的具体示例, 请参见上例.

注意

当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.

upsample

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

将输入采样到给定size或给定的scale_factor

警告

此函数已被弃用, 取而代之的是 torch.nn.functional.interpolate(). 等价于 nn.functional.interpolate(...).

注意

当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.

用于上采样的算法由 mode 确定.

目前支持时间, 空间和体积上采样, 即预期输入为3-D, 4-D或5-D形状.

输入维度形式: mini-batch x channels x [可选的 depth] x [可选的 height] x width.

可用于上采样的模式是: nearest, linear (仅3D), bilinear (仅4D), trilinear (仅5D)

参数:

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

使用最近邻居的像素值对输入进行上采样.

警告

不推荐使用此函数, 而使用 torch.nn.functional.interpolate(). 等价于h nn.functional.interpolate(..., mode='nearest').

目前支持空间和体积上采样 (即 inputs 是 4 或者 5 维的).

参数:

  • 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.

注意

当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.

upsample_bilinear

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

使用双线性上采样对输入进行上采样.

警告

不推荐使用此函数, 而使用 torch.nn.functional.interpolate(). 等价于 nn.functional.interpolate(..., mode='bilinear', align_corners=True).

期望输入是空间的 (4D). 用 upsample_trilinear 对体积 (5D) 输入.

参数:

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

注意

当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.

grid_sample

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

给定input 和流场 grid, 使用 inputgrid 中的像素位置计算output.

目前, 仅支持 spatial (4-D) 和 volumetric (5-D) input.

在 spatial (4-D) 的情况下, 对于 input 形如 grid 形如 , 输出的形状为 .

对于每个输出位置 output[n, :, h, w], 大小为2的向量 grid[n, h, w] 指定 input 的像素位置 xy, 用于插值输出值 output[n, :, h, w]. 对于 5D 的 inputs, grid[n, d, h, w] 指定 x, y, z 像素位置用于插值 output[n, :, d, h, w]. mode 参数指定 nearest or bilinear 插值方法.

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

如果 grid[-1, 1] 之外的值, 那些坐标将由 padding_mode 定义. 选项如下

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

注意

该功能常用于空间变换网络的构建.

注意

当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.

参数:

  • input (Tensor) – input of shape (4-D case) or (5-D case)
  • grid (Tensor) – flow-field of shape (4-D case) or (5-D case)
  • mode (str) – interpolation mode to calculate output values ‘bilinear’ | ‘nearest’. 默认值: ‘bilinear’
  • padding_mode (str) – padding mode for outside grid values ‘zeros’ | ‘border’ | ‘reflection’. 默认值: ‘zeros’

| 返回值: | output Tensor

| 返回类型: | output (Tensor)

affine_grid

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

在给定一批仿射矩阵theta的情况下生成二维流场. 通常与grid_sample()一起使用以实现空间变换器网络.

参数:

  • theta (Tensor) – 输入的仿射矩阵 ()
  • size (torch.Size) – 目标输出图像大小 () 例子: torch.Size((32, 3, 24, 24))

| 返回值: | 输出tensor, 形状为 ()

| 返回类型: | 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)

在设备id中给定的gpu上并行计算模块(输入).

这是DataParallel模块的函数版本.

参数:

  • 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, 包含位于输出设备上的模块(输入)的结果


书籍推荐