torch.nn.functional

    对由几个平面组成的输入进行卷积操作对于细节和输出形状,详细可见Conv1d

    1. weight 过滤器的形状 (out_channels, in_channels, kW)
    2. bias 可选偏置的形状(out_channels)默认值:None
    3. stride 卷积内核的步长,默认为1
    4. padding 输入上的隐含零填充。可以是单个数字或元组。默认值:0
    5. dilation 内核元素之间的间距。默认值:1
    6. groups 将输入分成组,in_channels应该被组数整除。默认值:1

    举例:

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

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

    在由几个输入平面组成的输入图像上应用2D卷积。对于细节和输出形状详细可见Conv2d

    参数:

    1. input 输入的张量 (minibatch x in_channels x iH x iW)
    2. weight 过滤器 (out_channels, in_channels/groups, kH, kW)
    3. bias 可选偏置张量(out_channels)。默认值:None
    4. stride 卷积核的步长,可以是单个数字或元组(sh x sw)。默认值:1
    5. padding 输入中默认0填充。可以是单个数字或元组。默认值:0
    6. dilation 核元素之间的间距。默认值:1
    7. groups 将输入分成组,in_channels应该被组数整除。默认值:1

    举例:

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

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

    source

    在由几个输入平面组成的输入图像上应用3D卷积。对于细节和输出形状,查看

    参数:

    1. input 输入张量的形状 (minibatch x in_channels x iT x iH x iW)
    2. weight 过滤器的形状 (out_channels x in_channels/groups x kT x kH x kW)
    3. bias 可选的偏差项的大小(out_channels).默认值是:None
    4. stride 卷积核的步长,可以是单个数字或元组(st x sh x sw).默认值:1
    5. padding 在输入中默认的0填充。可以是单个数字或元组(padT, padH, padW).默认值:0
    6. dilation 核元素之间的间距(dT, dH, dW)。默认值:1
    7. groups 将输入分成组,in_channels应该被组数整除。默认值:1

    举例:

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

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

    source

    在由几个输入平面组成的输入图像上应用1D转置卷积,有时也被称为去卷积。有关详细信息和输出形状,参考。

    参数:

    1. input:输入的张量形状(minibatch x in_channels x iW)
    2. weight 过滤器的形状 (in_channels x out_channels/groups x kW)
    3. bias 可选偏置的形状(out_channels)默认值:None
    4. stride 卷积内核的步长,也可以是一个数字或者元组(sW),默认为1
    5. padding 输入上的隐含零填充(0padding<stride)。可以是单个数字或者元组(padW)。默认值:0
    6. output_padding-在两端的进行0填充(0padding<stride),可以是个单一数字或者元组(out_padW)。默认是0
    7. dilation 内核元素之间的间距。可以是单个数字或者元组(dW)。默认值:1
    8. groups 将输入分成组,in_channels应该被组数整除。默认值:1

    举例:

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

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

    source

    在由几个输入平面组成的输入图像上应用2D转置卷积,有时也被称为去卷积。有关详细信息和输出形状,参考。

    参数:

    1. input:输入的张量形状(minibatch x in_channels x iH x iW)
    2. weight 过滤器的形状 (in_channels x out_channels/groups x kH x kW)
    3. bias 可选偏置的形状(out_channels)默认值:None
    4. stride 卷积内核的步长,也可以是一个数字或者元组(sHsW),默认为1
    5. padding 输入上的隐含零填充(0padding<stride)。可以是单个数字或者元组(padHpadW)。默认值:0
    6. output_padding-在两端的进行0填充(0padding<stride),可以是个单一数字或者元组( out_padH, out_padW)。默认是0
    7. dilation 内核元素之间的间距。可以是单个数字或者元组(dH,dW)。默认值:1
    8. groups 将输入分成组,in_channels应该被组数整除。默认值:1

    举例:

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

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

    source在由几个输入平面组成的输入图像上应用3D转置卷积,有时也被称为去卷积。有关详细信息和输出形状,参考。

    参数:

    1. input:输入的张量形状(minibatch x in_channels x iT x iH x iW)
    2. weight 过滤器的形状 (in_channels x out_channels/groups x kT x kH x kW)
    3. bias 可选偏置的形状(out_channels)默认值:None
    4. stride 卷积内核的步长,也可以是一个数字或者元组(sT,sHsW),默认为1
    5. padding 在输入的两端上的隐含零填充。可以是单个数字或者元组(padT,padHpadW)。默认值:0
    6. output_padding-在两端的进行0填充(0padding<stride),可以是个单一数字或者元组(out_padT, out_padH, out_padW)。默认是0
    7. dilation 内核元素之间的间距。可以是单个数字或者元组(dT,dH,dW)。默认值:1
    8. groups 将输入分成组,in_channels应该被组数整除。默认值:1

    举例:

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

    池化函数

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

    对由几个输入平面组成的输入进行1D平均池化。有关详细信息和输出形状,参考AvgPool1d

    参数:

    1. input 输入的张量 (minibatch x in_channels x iW)
    2. kernel_size 池化区域的大小,可以是单个数字或者元组 (kw)
    3. stride 池化操作的步长,可以是单个数字或者元组 (ssw)。默认值等于内核大小
    4. padding 在输入上隐式的零填充,可以是单个数字或者一个元组 ( padw),默认: 0
    5. ceil_mode 当为True时,公式中将使用ceil而不是floor来计算输出形状。默认值:False
    6. count_include_pad 当为True时,将包括平均计算中的零填充。默认值:True

    举例:

    1. >>> # pool of square window of size=3, stride=2
    2. >>> input = torch.tensor([[[1,2,3,4,5,6,7]]])
    3. >>> F.avg_pool1d(input, kernel_size=3, stride=2)
    4. tensor([[[ 2., 4., 6.]]])

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

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

    有关详细信息和输出形状,参考AvgPool2d

    参数:

    1. input 输入的张量 (minibatch x in_channels x iH x iW)
    2. kernel_size 池化区域的大小,可以是单个数字或者元组 (kh x kw)
    3. stride 池化操作的步长,可以是单个数字或者元组 (sh x sw)。默认值等于内核大小
    4. padding 在输入上隐式的零填充,可以是单个数字或者一个元组 (padh x padw),默认: 0
    5. ceil_mode 当为True时,公式中将使用ceil而不是floor来计算输出形状。默认值:False
    6. count_include_pad 当为True时,将包括平均计算中的零填充。默认值:True

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

    source

    通过步长dt x dh x dw步骤在kt x kh x kw区域中应用3D平均池操作。输出功能的数量等于输入平面数/ dt。有关详细信息和输出形状,参考

    参数:

    1. input 输入的张量 (minibatch x in_channels x iT x iH x iW)
    2. kernel_size 池化区域的大小,可以是单个数字或者元组 (kT x kh x kw)
    3. stride 池化操作的步长,可以是单个数字或者元组 (sT x sh x sw)。默认值等于内核大小
    4. padding 在输入上隐式的零填充,可以是单个数字或者一个元组 (padT x padh x padw),默认: 0
    5. ceil_mode 当为True时,公式中将使用ceil而不是floor来计算输出形状。默认值:False
    6. count_include_pad 当为True时,将包括平均计算中的零填充。默认值:True

    1. torch.nn.functional.max_pool1d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False)

    对由几个输入平面组成的输入进行1D最大池化。有关详细信息和输出形状,参考MaxPool1d


    1. torch.nn.functional.max_pool2d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False)

    对由几个输入平面组成的输入进行2D最大池化。有关详细信息和输出形状,参考MaxPool2d


    1. torch.nn.functional.max_pool3d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False)

    对由几个输入平面组成的输入进行3D最大池化。有关详细信息和输出形状,参考MaxPool3d


    计算MaxPool1d的部分逆。

    有关详细信息和输出形状,参考


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

    计算MaxPool2d的部分逆。

    有关详细信息和输出形状,参考MaxUnPool2d


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

    计算MaxPool3d的部分逆。

    有关详细信息和输出形状,参考


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

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

    有关详细信息LPPool1d


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

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

    有关详细信息


    1. torch.nn.functional.adaptive_max_pool1d(input, output_size, return_indices=False)

    source

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

    有关详细信息可见


    1. torch.nn.functional.adaptive_max_pool2d(input, output_size, return_indices=False)

    source

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

    有关详细信息可见


    1. torch.nn.functional.adaptive_max_pool3d(input, output_size, return_indices=False)

    source

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

    有关详细信息可见


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

    source

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

    有关详细信息可见


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

    source

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

    有关详细信息可见


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

    source

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

    有关详细信息可见

    非线性激活函数

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

    对于输入的张量进行筛选

    详细请看


    1. torch.nn.functional.threshold_(input, threshold, value)

    source

    和threshold函数一样

    详细请看


    1. torch.nn.functional.relu(input, inplace=False) Tensor
    2. torch.nn.functional.relu_(input) Tensor

    source

    对输入元素应用relu函数

    详细请看


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

    source

    对输入元素应用HardTanh函数

    详细请看


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

    source

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

    详细请看


    source

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

    详细请看


    1. torch.nn.functional.selu(input, inplace=False) Tensor[source]

    详细可见selu


    1. torch.nn.functional.leaky_relu(input, negative_slope=0.01, inplace=False) Tensor
    2. torch.nn.functional.leaky_relu_(input, negative_slope=0.01) Tensor

    详细可见


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

    详细可见PRelu


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

    详细可见


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

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

    可以参看论文Language Modeling with Gated Convolutional Networks

    参数:

    1. input:输入的张量
    2. dim:需要被分割的输入张量的维度

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

    具体细节可以看LogSigmoid


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

    具体细节可以看


    1. torch.nn.functional.tanhshrink(input, lambd=0.5) Tensor

    具体细节可以看Tanhshrink


    1. torch.nn.functional.softsign(input, lambd=0.5) Tensor

    具体细节可以看


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

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

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

    具体细节可以看Softmin

    参数:

    1. input:输入张量
    2. dimsoftmin将被计算的维度(因此每个沿着dim的切分将总计为1)。

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

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

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

    具体细节可以看Softmax

    1. input:输入张量
    2. dimsoftmax被计算的维度。

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

    具体细节可以看


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

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

    具体细节可以看LogSoftmax


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

    具体细节可以看


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

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

    具体细节可以看Sigmoid


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

    具体细节可以看BatchNorm1d BatchNorm3d


    1. 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 InstanceNorm3d


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

    具体细节可以看


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

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

    具体细节可以看LocalResponseNorm


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

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

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

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

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

    参数:

    1. input - 输入张量的形状
    2. p(float - 规范公式中的指数值。默认值:2
    3. dim(int - 要缩小的维度。默认值:1
    4. eps(float - 小值以避免除以零。默认值:1e-12

    线性函数

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

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

    形状:

    1. Input: (N,∗,in_features)(N,∗,in_features)这里的*表示为任意数量的附加维度
    2. Weight: (out_features,in_features)(out_features,in_features)
    3. Bias: (out_features)(out_features)
    4. Output: (N,∗,out_features)

    Dropout 函数

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

    source


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

    参数:

    1. p(float,optional)-丢弃的可能性,默认是0.5
    2. training(bool,optinal)-在训练模型和验证模型之间的切换,默认是false

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


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

    source


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

    详细可见


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

    source

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

    参数:

    1. x1 (Variable) 首先输入参数.
    2. x2 (Variable) 第二个输入参数 (of size matching x1).
    3. dim (int, optional) 向量维数. 默认为: 1
    4. eps (float, optional) 小值避免被零分割。默认为: 1e-8 模型:

    形状:

    1. input: (∗1,D,∗2)(∗1,D,∗2) D是位置维度
    2. output: (∗1,∗2)(∗1,∗2) 1是位置维度

    举例:

    1. >>> input1 = autograd.Variable(torch.randn(100, 128))
    2. >>> input2 = autograd.Variable(torch.randn(100, 128))
    3. >>> output = F.cosine_similarity(input1, input2)
    4. >>> print(output)

    损失函数

    1. torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_average=True, reduce=True)

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

    详细可见BCELoss

    参数:

    1. input:任意维度
    2. target:与输入维度相同
    3. weight(张量,可选):如果提供的权重矩阵能匹配输入张量形状,则手动调整重量
    4. reduce(布尔值,可选):默认情况下,根据size_average的不同,对每个小批次的损失进行平均或累计。 reduceFalse时,将返回每个输入/目标元素的损失,而忽略size_average 默认值:True

    举例:

    1. >>> input = torch.randn((3, 2), requires_grad=True)
    2. >>> target = torch.rand((3, 2), requires_grad=False)
    3. >>> loss = F.binary_cross_entropy(F.sigmoid(input), target)
    4. >>> loss.backward()

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

    泊松负对数似然损失 详细可见PoissonNLLLoss

    参数:

    1. input:按照泊松分布的期望
    2. target:随机样例target~Poisson(input
    3. log_input:如果为真,则损失计算为exp(inout)-target * input,如果为False,则input-target *log⁡(input + eps)。 默认值:True
    4. full:是否计算完全损失。即添加斯特林近似项。 默认值:False target * log(target)-target + 0.5 * log(2 *π* target)
    5. size_average:默认情况下,对每个小批次的损失进行平均观察。 但是,如果field size_average设置为False,则每个小批次的损失将相加。 默认值:True
    6. eps(float,可选) - log_input`=“False”时避免评估log(0log⁡(0)的较小值。 默认:1e-8
    7. reduce(布尔值,可选):默认情况下,根据每个小批次的观测结果对损失进行平均,或根据size_average进行汇总。 如果reduceFalse,则返回每批损失,并忽略size_average 默认值:True

    1. torch.nn.functional.cosine_embedding_loss(input1, input2, target, margin=0, size_average=True, reduce=True) Tensor

    source

    详细可见


    source

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

    参数:

    1. input(张量)- (N,C) 其中,C 是类别的个数
    2. target(张量)- (N) 其大小是 0 <= targets[i] <= C-1 1\. weight (Variable, optional) (N) 其大小是 0 <= targets[i] <= C-1
    3. weight (张量, optional) 为每个类别提供的手动权重。如果给出,必须是大小为C的张量
    4. size_average (bool, optional) 默认情况下,是mini-batchloss的平均值;如果size_average=False,则是mini-batchloss的总和。
    5. ignore_index(int,可选) - 指定被忽略且不对输入渐变有贡献的目标值。当size_averageTrue时,对非忽略目标的损失是平均的。默认值:-100
    6. reduce(布尔值,可选)-默认情况下,根据每个小批次的观测结果对损失进行平均,或根据size_average进行汇总。 如果reduceFalse,则返回每批损失,并忽略size_average 默认值:True

    举例:

    1. >>> target = torch.randint(5, (3,), dtype=torch.int64)
    2. >>> loss = F.cross_entropy(input, target)
    3. >>> loss.backward()

    1. torch.nn.functional.hinge_embedding_loss(input, target, margin=1.0, size_average=True, reduce=True) Tensor

    source

    详细可见


    1. torch.nn.functional.kl_div(input, target, size_average=True) Tensor

    source

    The Loss

    详细可见KLDivLoss

    参数:

    1. input 变量的任意形状
    2. target - 与输入相同形状的变量
    3. size_average 如果是真的,输出就除以输入张量中的元素个数
    4. reduce(布尔值,可选)-默认情况下,根据每个小批次的观测结果对损失进行平均,或根据size_average进行汇总。 如果reduceFalse,则返回每批损失,并忽略size_average 默认值:True

    1. torch.nn.functional.l1_loss(input, target, size_average=True, reduce=True) Tensor

    详细可见L1Loss


    1. torch.nn.functional.mse_loss(input, target, size_average=True, reduce=True) Tensor

    详细可见


    1. torch.nn.functional.margin_ranking_loss(input, target, size_average=True, reduce=True) Tensor

    详细可见MarginRankingLoss


    1. torch.nn.functional.multilabel_soft_margin_loss(input, target, size_average=True, reduce=True) Tensor

    详细可见


    1. torch.nn.functional.multi_margin_loss(input, target, p=1, margin=1, weight=None, size_average=True, reduce=True) Tensor

    详细可见MultiMarginLoss


    1. torch.nn.functional.nll_loss(input, target, weight=None, size_average=True, ignore_index=-100, reduce=True)

    详细可见

    参数:

    1. input - \((NC\)其中C =类的数量或 (2DLoss )的情况下的(NCHW target - \((N\),其中每个值为0 <= targets [i] <= C-1
    2. weight(可变,可选) - 给每个类别的手动重新调整重量。如果给定,必须变量大小是C
    3. size_average(bool,可选) - 默认情况下,损失是对每个小型服务器的观察值进行平均。如果size_averageFalse,则对于每个minibatch都会将损失相加。默认值:True
    4. ignore_index(int,可选) - 指定被忽略且不对输入渐变有贡献的目标值。当size_averageTrue时,对非忽略目标的损失是平均的。默认值:-100

    举例:

    1. >>> # input is of size N x C = 3 x 5
    2. >>> input = torch.randn(3, 5, requires_grad=True)
    3. >>> # each element in target has to have 0 <= value < C
    4. >>> target = torch.tensor([1, 0, 4])
    5. >>> output = F.nll_loss(F.log_softmax(input), target)
    6. >>> output.backward()

    1. torch.nn.functional.binary_cross_entropy_with_logits(input, target, weight=None, size_average=True, reduce=True)

    source

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

    详细可见

    参数:

    1. input - 任意形状的变量
    2. target - 与输入形状相同的变量
    3. weight(可变,可选) - 手动重量重量,如果提供重量以匹配输入张量形状
    4. size_average(bool,可选) - 默认情况下,损失是对每个小型服务器的观察值进行平均。然而,如果字段sizeAverage设置为False,则相应的损失代替每个minibatch的求和。默认值:True
    5. reduce(布尔值,可选)-默认情况下,根据每个小批次的观测结果对损失进行平均,或根据size_average进行汇总。 如果reduceFalse,则返回每批损失,并忽略size_average 默认值:True
    1. >>> input = torch.randn(3, requires_grad=True)
    2. >>> target = torch.empty(3).random_(2)
    3. >>> loss = F.binary_cross_entropy_with_logits(input, target)
    4. >>> loss.backward()

    1. torch.nn.functional.smooth_l1_loss(input, target, size_average=True, reduce=True) Tensor

    详细可见


    1. torch.nn.functional.soft_margin_loss(input, target, size_average=True, reduce=True) Tensor

    详细可见Soft_margin_loss


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

    详细可见

    视觉函数

    1. torch.nn.functional.pixel_shuffle(input, upscale_factor)

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

    详细请看PixelShuffle

    参数:

    1. input (Variable) 输入
    2. upscale_factor (int) 增加空间分辨率的因子.

    举例:

    1. >>> ps = nn.PixelShuffle(3)
    2. >>> input = autograd.Variable(torch.Tensor(1, 9, 4, 4))
    3. >>> output = ps(input)
    4. >>> print(output.size())
    5. torch.Size([1, 1, 12, 12])

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

    填充张量.

    可以参考torch.nn.ConstantPad2d, , 和 torch.nn.ReplicationPad2d对于每个填充模式如何工作的具体例子。

    参数:

    1. input (Variable) 4D 5D tensor
    2. pad (tuple) 4元素 6-元素 tuple
    3. mode constant’, reflect or replicate
    4. value 用于constant padding 的值.

    举例:

    1. >>> t4d = torch.empty(3, 3, 4, 2)
    2. >>> p1d = (1, 1) # pad last dim by 1 on each side
    3. >>> out = F.pad(t4d, p1d, "constant", 0) # effectively zero padding
    4. >>> print(out.data.size())
    5. torch.Size([3, 3, 4, 4])
    6. >>> p2d = (1, 1, 2, 2) # pad last dim by (1, 1) and 2nd to last by (2, 2)
    7. >>> out = F.pad(t4d, p2d, "constant", 0)
    8. >>> print(out.data.size())
    9. torch.Size([3, 3, 8, 4])
    10. >>> t4d = torch.empty(3, 3, 4, 2)
    11. >>> p3d = (0, 1, 2, 1, 3, 3) # pad by (0, 1), (2, 1), and (3, 3)
    12. >>> out = F.pad(t4d, p3d, "constant", 0)
    13. >>> print(out.data.size())
    14. torch.Size([3, 9, 7, 3])

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

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

    参数:

    1. input (Variable) 输入内容
    2. size (int or Tuple[int, int] or Tuple[int, int, int]) 输出空间的大小。
    3. scale_factor (int) 乘数的空间大小。必须是一个整数。
    4. mode (string) 用于向上采样的算法: nearest | bilinear | trilinear
    5. align_corners(bool,可选) - 如果为True,则输入和输出张量的角点像素对齐,从而保留这些像素的值。 这只在模式为线性,双线性或三线性时才有效。 默认值:False

    警告:

    1. 使用align_corners = True时,线性插值模式(线性,双线性和三线性)不会按比例对齐输出和输入像素,因此输出值可能取决于输入大小。
    2. 这是这些模式到版本0.3.1的默认行为。 此后,默认行为是align_corners = False 有关这将如何影响输出的具体示例,请参见Upsample

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

    source 使用最接近的邻居的像素值来对输入进行采样。

    警告:

    1. 此功能已弃用,以支持torch.nn.functional.upsample()。 这相当于nn.functional.upsample(...,mode ='nearest')。

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

    参数:

    1. input (Variable) 输入内容
    2. size (int or Tuple[int, int]) 输出空间的大小。
    3. scale_factor (int or Tuple[int, int]) 乘数的空间大小

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

    使用双线性向上采样来扩展输入

    警告:

    1. 这个函数是被弃用的。使用nn.functionalupsample相反 预期的输入是空间的(4维)。
    2. 使用upsampletri线性来进行体积(5维)输入。

    参数:

    1. input (Variable) 输入内容
    2. size (int or Tuple[int, int]) 输出空间的大小。
    3. scale_factor (int or Tuple[int, int]) 乘数的空间大小

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

    source

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

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

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

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

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

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

    参数:

    1. input(Tensor - 输入批次(N x C x IH x IW)或(N x C x ID x IH x IW
    2. grid(Tensor - 尺寸(N x OH x OW x 2)或(N x OD x OH x OW x 3)的流场,
    3. padding_mode(str - 用于外部网格值“零”|的填充模式 '边境' 默认值:'零'

    返回:

    1. output(tensor

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

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

    参数:

    1. theta(Tensor - 输入一批仿射矩阵(N×2×3N×2×3
    2. size(torch.Size - 目标输出图像尺寸(N×C×H×WN×C×H×W)例如:torch.Size((32,3,24,24))

    返回:

    1. output(tensor):输出张量大小 (N×H×W×2N×H×W×2)
    1. torch.nn.parallel.data_parallel(module, inputs, device_ids=None, output_device=None, dim=0, module_kwargs=None)

    source

    参数:

    1. module - 并行评估的模块
    2. input - 输入到模块
    3. device_ids - 要在其上复制模块的GPU ID

    译者署名