# torch.nn.functional
译者:hijkzzz
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 了解背景.
参数:
None(sW,). 默认值: 1(padW, ). 默认值: 0(dW,). 默认值: 1例子:
>>> filters = torch.randn(33, 16, 3)
>>> inputs = 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) → Tensor
对由多个输入平面组成的输入图像应用二维卷积.
有关详细信息和输出形状, 请参见Conv2d.
注意
在某些情况下, 当使用CUDA后端与CuDNN时, 该操作符可能会选择不确定性算法来提高性能. 如果这不是您希望的, 您可以通过设置torch.backends.cudn .deterministic = True来尝试使操作具有确定性(可能会以性能为代价). 请参阅关于 Reproducibility 了解背景.
参数:
None(sH, sW). 默认值: 1(padH, padW). 默认值: 0(dH, dW). 默认值: 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)
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 了解背景.
参数:
(sT, sH, sW). 默认值: 1(padT, padH, padW). 默认值: 0(dT, dH, dW). 默认值: 1例子:
>>> filters = torch.randn(33, 16, 3, 3, 3)
>>> inputs = 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, dilation=1) → Tensor
对由多个输入平面组成的输入信号应用一维转置卷积算子, 有时也称为反卷积.
有关详细信息和输出形状, 请参见 ConvTranspose1d
注意
在某些情况下, 当使用CUDA后端与CuDNN时, 该操作符可能会选择不确定性算法来提高性能. 如果这不是您希望的, 您可以通过设置torch.backends.cudn .deterministic = True来尝试使操作具有确定性(可能会以性能为代价). 请参阅关于 Reproducibility 了解背景.
参数:
(sW,). 默认值: 1kernel_size - 1 - padding. 可以是单个数字或元组 (padW,). 默认值: 0(out_padW). 默认值: 0(dW,). 默认值: 1例子:
>>> inputs = torch.randn(20, 16, 50)
>>> weights = torch.randn(16, 33, 5)
>>> F.conv_transpose1d(inputs, weights)
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 了解背景.
参数:
(sH, sW). 默认值: 1kernel_size - 1 - padding. 可以是单个数字或元组 (padH, padW). 默认值: 0(out_padH, out_padW). 默认值: 0(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)
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 了解背景.
参数:
(sT, sH, sW). 默认值: 1kernel_size - 1 - padding. 可以是单个数字或元组 (padT, padH, padW). 默认值: 0(out_padT, out_padH, out_padW). 默认值: 0(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)
torch.nn.functional.unfold(input, kernel_size, dilation=1, padding=0, stride=1)
从批量的inputtensor中提取滑动局部块.
警告
目前, 仅支持 4-D input tensors (如批量的图像 tensors).
细节请参阅 torch.nn.Unfold
torch.nn.functional.fold(input, output_size, kernel_size, dilation=1, padding=0, stride=1)
将一组滑动局部块数组合成一个大的tensor.
警告
目前, 仅支持 4-D input tensors (如批量的图像 tensors).
细节请参阅 torch.nn.Fold
torch.nn.functional.avg_pool1d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True) → Tensor
对由多个输入平面组成的输入信号应用一维平均池化.
有关详细信息和输出形状, 请参见 AvgPool1d.
参数:
(sW,). 默认值: kernel_size(padW,). 默认值: 0True, 将用 ceil 代替 floor计算输出形状. 默认值: FalseTrue, 将在平均计算中包括零填充. 默认值: 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.]]])
torch.nn.functional.avg_pool2d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True) → Tensor
应用2D平均池化操作于 区域, 步幅为
. 输出特征的数量等于输入平面的数量.
有关详细信息和输出形状, 请参见 AvgPool2d.
参数:
(sH, sW). 默认值: kernel_size(padH, padW). 默认值: 0True, 将用 ceil 代替 floor计算输出形状. 默认值: FalseTrue, 将在平均计算中包括零填充. 默认值: Truetorch.nn.functional.avg_pool3d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True) → Tensor
应用3D平均池化操作于 区域, 步幅为
. 输出特征的数量等于
.
有关详细信息和输出形状, 请参见 AvgPool3d.
参数:
(sT, sH, sW). 默认值: kernel_size(padT, padH, padW), 默认值: 0True, 将用 ceil 代替 floor计算输出形状. 默认值: FalseTrue, 将在平均计算中包括零填充. 默认值: Truetorch.nn.functional.max_pool1d(*args, **kwargs)
在由多个输入平面组成的输入信号上应用1D 最大池化.
详情见 MaxPool1d.
torch.nn.functional.max_pool2d(*args, **kwargs)
在由多个输入平面组成的输入信号上应用2D 最大池化.
详情见 MaxPool2d.
torch.nn.functional.max_pool3d(*args, **kwargs)
在由多个输入平面组成的输入信号上应用3D 最大池化.
详情见 MaxPool3d.
torch.nn.functional.max_unpool1d(input, indices, kernel_size, stride=None, padding=0, output_size=None)
计算逆 MaxPool1d.
请参见 MaxUnpool1d.
torch.nn.functional.max_unpool2d(input, indices, kernel_size, stride=None, padding=0, output_size=None)
计算逆 MaxPool2d.
详情见 MaxUnpool2d.
torch.nn.functional.max_unpool3d(input, indices, kernel_size, stride=None, padding=0, output_size=None)
计算逆 MaxPool3d.
详情见 MaxUnpool3d.
torch.nn.functional.lp_pool1d(input, norm_type, kernel_size, stride=None, ceil_mode=False)
在由多个输入平面组成的输入信号上应用1D幂平均池化. 如果所有输入的p次方的和为零, 梯度也为零.
详情见 LPPool1d.
torch.nn.functional.lp_pool2d(input, norm_type, kernel_size, stride=None, ceil_mode=False)
在由多个输入平面组成的输入信号上应用2D幂平均池化. 如果所有输入的p次方的和为零, 梯度也为零.
详情见 LPPool2d.
torch.nn.functional.adaptive_max_pool1d(*args, **kwargs)
在由多个输入平面组成的输入信号上应用1D自适应最大池.
请参见 AdaptiveMaxPool1d和输出形状.
参数:
Falsetorch.nn.functional.adaptive_max_pool2d(*args, **kwargs)
在由多个输入平面组成的输入信号上应用2D自适应最大池.
请参见 AdaptiveMaxPool2d 关于详情和输出形状.
参数:
Falsetorch.nn.functional.adaptive_max_pool3d(*args, **kwargs)
在由多个输入平面组成的输入信号上应用3D自适应最大池.
请参见 AdaptiveMaxPool3d和输出形状.
参数:
Falsetorch.nn.functional.adaptive_avg_pool1d(input, output_size) → Tensor
在由多个输入平面组成的输入信号上应用1D自适应平均池化.
请参见 AdaptiveAvgPool1d 关于详情和输出形状.
| 参数:| output_size – 输出目标大小(单个整数)
torch.nn.functional.adaptive_avg_pool2d(input, output_size)
在由多个输入平面组成的输入信号上应用2D自适应平均池化.
请参见 AdaptiveAvgPool2d 关于详情和输出形状.
| 参数:| output_size – 输出目标大小(单个整数 或者双整数元组)
torch.nn.functional.adaptive_avg_pool3d(input, output_size)
在由多个输入平面组成的输入信号上应用3D自适应平均池化.
请参见 AdaptiveAvgPool3d 关于详情和输出形状.
| 参数:| output_size – 输出目标大小(单个整数 或者三整数元组)
torch.nn.functional.threshold(input, threshold, value, inplace=False)
为 input Tensor 的每个元素设置阈值.
请参见 Threshold.
torch.nn.functional.threshold_(input, threshold, value) → Tensor
原地版本的 threshold().
torch.nn.functional.relu(input, inplace=False) → Tensor
逐元素应用整流线性单元函数. 请参见 ReLU.
torch.nn.functional.relu_(input) → Tensor
原地版本的 relu().
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().
torch.nn.functional.relu6(input, inplace=False) → Tensor
请参见 ReLU6.
torch.nn.functional.elu(input, alpha=1.0, inplace=False)
请参见 ELU.
torch.nn.functional.elu_(input, alpha=1.) → Tensor
原地版本的 elu().
torch.nn.functional.selu(input, inplace=False) → Tensor
请参见 SELU.
torch.nn.functional.celu(input, alpha=1., inplace=False) → Tensor
请参见 CELU.
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().
torch.nn.functional.prelu(input, weight) → Tensor
逐元素应用函数 where weight is a learnable parameter.
请参见 PReLU.
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().
torch.nn.functional.glu(input, dim=-1) → Tensor
门控线性单元. 计算:
其中inpuy沿dim分成两半, 形成A和B.
见 Language Modeling with Gated Convolutional Networks.
参数:
torch.nn.functional.logsigmoid(input) → Tensor
请参见 LogSigmoid.
torch.nn.functional.hardshrink(input, lambd=0.5) → Tensor
逐元素应用hardshrink函数
请参见 Hardshrink.
torch.nn.functional.tanhshrink(input) → Tensor
请参见 Tanhshrink.
torch.nn.functional.softsign(input) → Tensor
请参见 Softsign.
torch.nn.functional.softplus(input, beta=1, threshold=20) → Tensor
torch.nn.functional.softmin(input, dim=None, _stacklevel=3, dtype=None)
应用 softmin 函数.
注意 . See softmax definition for mathematical formula.
请参见 Softmin.
参数:
torch.dtype, 可选的) – 返回tenosr的期望数据类型.:param如果指定, 输入张量在执行::param操作之前被转换为dtype. 这对于防止数据类型溢出非常有用. 默认值: None.
torch.nn.functional.softmax(input, dim=None, _stacklevel=3, dtype=None)
应用 softmax 函数.
Softmax定义为:
它应用于dim上的所有切片, 并将对它们进行重新缩放, 使元素位于(0,1)范围内, 和为1.
请参见 Softmax.
参数:
:param如果指定, 输入张量在执行::param操作之前被转换为dtype. 这对于防止数据类型溢出非常有用. 默认值: None.
注意
此函数不能直接用于NLLLoss, NLLLoss期望在Softmax和它自己之间计算对数. 改用log_softmax (它速度更快, 数值性能更好).
torch.nn.functional.softshrink(input, lambd=0.5) → Tensor
逐元素应用 soft shrinkage 函数
请参见 Softshrink.
torch.nn.functional.gumbel_softmax(logits, tau=1.0, hard=False, eps=1e-10)
采样自Gumbel-Softmax分布, 并可选地离散化.
参数:
[batch_size, num_features] 非规范化对数概率True, 返回的样本将会离散为 one-hot 向量, 但将是可微分的似乎它是autograd中的soft sample| 返回值: | 从 Gumbel-Softmax 分布采样的 tensor, 形状为 batch_size x num_features . 如果 hard=True, 返回值是 one-hot 编码, 否则, 它们就是特征和为1的概率分布
约束:
logits tensor , 形状为 batch_size x num_features基于 https://github.com/ericjang/gumbel-softmax/blob/3c8584924603869e90ca74ac20a6a03d99a91ef9/Categorical VAE.ipynb , (MIT license)
torch.nn.functional.log_softmax(input, dim=None, _stacklevel=3, dtype=None)
应用 softmax 跟随对数运算.
虽然在数学上等价于log(softmax(x)), 但分别执行这两个操作比较慢, 而且在数值上不稳定. 这个函数使用另一种公式来正确计算输出和梯度.
请参见 LogSoftmax.
参数:
torch.dtype, 可选的) – 返回tenosr的期望数据类型.:param如果指定, 输入张量在执行::param操作之前被转换为dtype. 这对于防止数据类型溢出非常有用. 默认值: None.
torch.nn.functional.tanh(input) → Tensor
请参见 Tanh.
torch.nn.functional.sigmoid(input) → Tensor
请参见 Sigmoid.
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.
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.
torch.nn.functional.layer_norm(input, normalized_shape, weight=None, bias=None, eps=1e-05)
对最后特定数量的维度应用layer规范化.
请参见 LayerNorm.
torch.nn.functional.local_response_norm(input, size, alpha=0.0001, beta=0.75, k=1.0)
对由多个输入平面组成的输入信号进行局部响应归一化, 其中通道占据第二维. 跨通道应用标准化.
请参见 LocalResponseNorm.
torch.nn.functional.normalize(input, p=2, dim=1, eps=1e-12, out=None)
对于一个 tensor input 尺寸为 , 每一
-元素向量
沿着维度
dim 被转换为
参数:
out 被设置, 此操作不可微分.torch.nn.functional.linear(input, weight, bias=None)
形状:
torch.nn.functional.bilinear(input1, input2, weight, bias=None)
torch.nn.functional.dropout(input, p=0.5, training=True, inplace=False)
在训练过程中, 使用伯努利分布的样本, 随机地用概率p将输入张量的一些元素归零.
请参见 Dropout.
参数:
True 使用 dropout. 默认值: TrueTrue, 将会原地操作. 默认值: Falsetorch.nn.functional.alpha_dropout(input, p=0.5, training=False, inplace=False)
应用 alpha dropout.
请参见 AlphaDropout.
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.
参数:
True. 默认值: TrueTrue, 将会做原地操作. 默认值: Falsetorch.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.
参数:
True. 默认值: TrueTrue, 将会做原地操作. 默认值: Falsetorch.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.
参数:
padding_idx (初始化为零)用嵌入向量填充输出.max_norm的每个嵌入向量重新规范化, 得到范数max_norm. 注意:这将修改适当的weight.max_norm选项的p范数的p. 默认 2.False.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]]])
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的和、平均值或最大值, 而不实例化中间嵌入. .
参数:
bags的张量input为1D时使用. offsets确定输入中每个bag(序列)的起始索引位置max_norm的每个嵌入向量将被重新规格化为范数max_norm. 注意:这将就地修改weightp in the p-norm to compute for the max_norm option. 默认 2.mode="max"时不支持此选项."sum", "mean" or "max". 指定reducebag的方法. 默认值: "mean"True, 梯度w.r.t.权值就是一个稀疏张量.请参见 torch.nn.Embedding 关于稀疏梯度. 注意: 此选项不支持 mode="max".形状:
input(LongTensor) 和offsets(LongTensor, 可选的)
如果
input是 2D的, 形状为B x N,它将被视为每个固定长度
N的B个bag(序列), 这将根据模式以某种方式返回B个聚合值. 在本例中,offsets被忽略, 并且要求为None如果
input是 1D的, 形状为N,it will be treated as a concatenation of multiple bags (sequences).
offsetsis required to be a 1D tensor containing the starting index positions of each bag ininput. Therefore, foroffsetsof shapeB,inputwill be viewed as havingBbags. 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]])
torch.nn.functional.pairwise_distance(x1, x2, p=2.0, eps=1e-06, keepdim=False)
torch.nn.functional.cosine_similarity(x1, x2, dim=1, eps=1e-8) → Tensor
返回x1和x2之间的余弦相似度, 沿dim计算
参数:
形状:
例子:
>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> output = F.cosine_similarity(input1, input2)
>>> print(output)
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()).
参数:
torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_average=None, reduce=None, reduction='mean')
计算目标和输出之间二进制交叉熵的函数.
请参见 BCELoss.
参数:
reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: Truereduction). 默认情况下, 根据size_average, 对每个小批量的观察结果的损失进行平均或求和. 当reduce为False时, 返回每批元素的损失并忽略size_average. 默认值: Truereduction:'none'| 'mean'| 'sum'. 'none':没有reduction, 'mean':输出的总和将除以输出中的元素数量 'sum':输出将被求和. 注意:size_average和reduce正在被弃用, 同时, 指定这两个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()
torch.nn.functional.binary_cross_entropy_with_logits(input, target, weight=None, size_average=None, reduce=None, reduction='mean', pos_weight=None)
计算目标和输出logits之间的二进制交叉熵的函数.
请参见 BCEWithLogitsLoss.
参数:
reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: Truereduction). 默认情况下, 根据size_average, 对每个小批量的观察结果的损失进行平均或求和. 当reduce为False时, 返回每批元素的损失并忽略size_average. 默认值: Truereduction:'none'| 'mean'| 'sum'. 'none':没有reduction, 'mean':输出的总和将除以输出中的元素数量 'sum':输出将被求和. 注意:size_average和reduce正在被弃用, 同时, 指定这两个args中的任何一个都将覆盖reduce. 默认值:'mean', 默认值: ‘mean’例子:
>>> input = torch.randn(3, requires_grad=True)
>>> target = torch.empty(3).random_(2)
>>> loss = F.binary_cross_entropy_with_logits(input, target)
>>> loss.backward()
torch.nn.functional.poisson_nll_loss(input, target, log_input=True, full=False, size_average=None, eps=1e-08, reduce=None, reduction='mean')
泊松负对数似然损失.
请参见 PoissonNLLLoss.
参数:
True, 则损失计算为 False, 则损失计算为 TrueFalse reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: Truelog_input=False. 默认值: 1e-8reduction). 默认情况下, 根据size_average, 对每个小批量的观察结果的损失进行平均或求和. 当reduce为False时, 返回每批元素的损失并忽略size_average. 默认值: Truereduction:'none'| 'mean'| 'sum'. 'none':没有reduction, 'mean':输出的总和将除以输出中的元素数量 'sum':输出将被求和. 注意:size_average和reduce正在被弃用, 同时, 指定这两个args中的任何一个都将覆盖reduce. 默认值:'mean', 默认值: ‘mean’torch.nn.functional.cosine_embedding_loss(input1, input2, target, margin=0, size_average=None, reduce=None, reduction='mean') → Tensor
请参见 CosineEmbeddingLoss.
torch.nn.functional.cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')
此函数结合了 log_softmax 和 nll_loss.
请参见 CrossEntropyLoss.
参数:
C = number of classes or C的tensorreduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: Truesize_average is True, the loss is averaged over non-ignored targets. 默认值: -100reduction). 默认情况下, 根据size_average, 对每个小批量的观察结果的损失进行平均或求和. 当reduce为False时, 返回每批元素的损失并忽略size_average. 默认值: Truereduction:'none'| 'mean'| 'sum'. 'none':没有reduction, 'mean':输出的总和将除以输出中的元素数量 'sum':输出将被求和. 注意:size_average和reduce正在被弃用, 同时, 指定这两个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()
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的注释.
参数:
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()).(sum(target_lengths)). Targets (cannot be blank). In the second form, the targets are assumed to be concatenated.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()
torch.nn.functional.hinge_embedding_loss(input, target, margin=1.0, size_average=None, reduce=None, reduction='mean') → Tensor
请参见 HingeEmbeddingLoss.
torch.nn.functional.kl_div(input, target, size_average=None, reduce=None, reduction='mean')
Kullback-Leibler divergence 损失.
请参见 KLDivLoss.
参数:
reduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: Truereduction). 默认情况下, 根据size_average, 对每个小批量的观察结果的损失进行平均或求和. 当reduce为False时, 返回每批元素的损失并忽略size_average. 默认值: Truereduction 'batchmean':输出的总和将除以batchsize 'sum':输出将被加总 'mean':输出将除以输出中的元素数 默认值:'mean':param . .注::size average和reduce正在被弃用, :同时, 指定这两个arg中的一个将覆盖reduce. :param . .注意::reduce = mean不返回真实的kl散度值, 请使用:reduce = batchmean, 它符合kl的数学定义.
在下一个主要版本中, “mean”将被修改为与“batchmean”相同.
torch.nn.functional.l1_loss(input, target, size_average=None, reduce=None, reduction='mean') → Tensor
函数的作用是求元素的绝对值的平均值.
请参见 L1Loss.
torch.nn.functional.mse_loss(input, target, size_average=None, reduce=None, reduction='mean') → Tensor
测量元素的均方误差.
请参见 MSELoss.
torch.nn.functional.margin_ranking_loss(input1, input2, target, margin=0, size_average=None, reduce=None, reduction='mean') → Tensor
请参见 MarginRankingLoss.
torch.nn.functional.multilabel_margin_loss(input, target, size_average=None, reduce=None, reduction='mean') → Tensor
请参见 MultiLabelMarginLoss.
torch.nn.functional.multilabel_soft_margin_loss(input, target, weight=None, size_average=None) → Tensor
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.
torch.nn.functional.nll_loss(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')
负的对数似然函数.
请参见 NLLLoss.
参数:
C = 类别的数量 或者 C的tensorreduction). 默认情况下, 批处理中的每个损失元素的平均损失. 注意, 对于某些损失, 每个样本有多个元素. 如果size_average设置为False, 则对每个小批的损失进行汇总. reduce为False时忽略. 默认值: Truesize_average为True时, 损耗在未忽略的目标上平均. 默认值: -100reduction). 默认情况下, 根据size_average, 对每个小批量的观察结果的损失进行平均或求和. 当reduce为False时, 返回每批元素的损失并忽略size_average. 默认值: Truereduction:'none'| 'mean'| 'sum'. 'none':没有reduction, 'mean':输出的总和将除以输出中的元素数量 'sum':输出将被求和. 注意:size_average和reduce正在被弃用, 同时, 指定这两个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()
torch.nn.functional.smooth_l1_loss(input, target, size_average=None, reduce=None, reduction='mean')
如果绝对元素误差低于1, 则使用平方项, 否则使用L1项的函数.
请参见 SmoothL1Loss.
torch.nn.functional.soft_margin_loss(input, target, size_average=None, reduce=None, reduction='mean') → Tensor
请参见 SoftMarginLoss.
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')
torch.nn.functional.pixel_shuffle()
请参见 PixelShuffle.
参数:
例子:
>>> 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)
填充 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的注释.
参数:
Nd tensor例子:
>>> 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])
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
参数:
mode 是 linear, bilinear, 或者 trilinear 时生效. 默认值: False警告
align_corners = True时, 线性插值模式(linear, bilinear, and trilinear)不会按比例对齐输出和输入像素, 因此输出值可能取决于输入大小. 这是0.3.1版之前这些模式的默认行为.此后, 默认行为为align_corners = False. 有关这如何影响输出的具体示例, 请参见上例.
注意
当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.
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)
参数:
mode 是 linear, bilinear, 或者 trilinear 时生效. 默认值: False警告
align_corners = True时, 线性插值模式(linear, bilinear, and trilinear)不会按比例对齐输出和输入像素, 因此输出值可能取决于输入大小. 这是0.3.1版之前这些模式的默认行为.此后, 默认行为为align_corners = False. 有关这如何影响输出的具体示例, 请参见 Upsample
torch.nn.functional.upsample_nearest(input, size=None, scale_factor=None)
使用最近邻居的像素值对输入进行上采样.
警告
不推荐使用此函数, 而使用 torch.nn.functional.interpolate(). 等价于h nn.functional.interpolate(..., mode='nearest').
目前支持空间和体积上采样 (即 inputs 是 4 或者 5 维的).
参数:
注意
当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.
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) 输入.
参数:
注意
当使用CUDA后端时, 此操作可能会导致不确定的向后行为, 并且不容易关闭. 请参阅关于Reproducibility的注释.
torch.nn.functional.grid_sample(input, grid, mode='bilinear', padding_mode='zeros')
给定input 和流场 grid, 使用 input 和 grid 中的像素位置计算output.
目前, 仅支持 spatial (4-D) 和 volumetric (5-D) input.
在 spatial (4-D) 的情况下, 对于 input 形如 和
grid 形如 , 输出的形状为
.
对于每个输出位置 output[n, :, h, w], 大小为2的向量 grid[n, h, w] 指定 input 的像素位置 x 和 y, 用于插值输出值 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 = -1 是 input 的左上角, 值 x = 1, y = 1 是 input 的右下角.
如果 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的注释.
参数:
| 返回值: | output Tensor
| 返回类型: | output (Tensor)
torch.nn.functional.affine_grid(theta, size)
在给定一批仿射矩阵theta的情况下生成二维流场. 通常与grid_sample()一起使用以实现空间变换器网络.
参数:
| 返回类型: | output (Tensor)
torch.nn.parallel.data_parallel(module, inputs, device_ids=None, output_device=None, dim=0, module_kwargs=None)
在设备id中给定的gpu上并行计算模块(输入).
这是DataParallel模块的函数版本.
参数:
| 返回值: | 一个tensor, 包含位于输出设备上的模块(输入)的结果