torch.nn.CrossEntropyLoss()用法

CLASS torch.nn.CrossEntropyLoss(weight: Optional[torch.Tensor] = None, size_average=None, ignore_index: int = -100, reduce=None, reduction: str = 'mean')

这个评价损失将 nn.LogSoftmax() 和 nn.NLLLoss() 结合在一个类中。

在训练带有C类的分类问题时很有用。 如果提供,则可选参数weight应为一维张量,为每个类分配权重。 当您的训练集不平衡时,这特别有用。

input预期将包含每个类的原始未标准化分数。

input 必须为张量,其大小为torch.nn.CrossEntropyLoss()用法torch.nn.CrossEntropyLoss()用法,K≥1对于K维情况(后述)。

该评价损失期望类别索引范围为torch.nn.CrossEntropyLoss()用法,作为大小为minibatch的一维张量target的每个值。 如果指定了ignore_index,则此评价损失也接受该类索引(此索引可能不一定在类范围内)。

损失可以描述为:

torch.nn.CrossEntropyLoss()用法

或在指定weight参数的情况下:

torch.nn.CrossEntropyLoss()用法

损失是对每个minibatch的观察结果的平均值。 如果指定了weight参数,则这是一个加权平均值:

torch.nn.CrossEntropyLoss()用法

还可用于更高维度的输入,例如2D图像,提供一个大小为torch.nn.CrossEntropyLoss()用法,K≥1的input,其中K是维度数,target具有适当形状(请参见下文)。

Parameters

  • weight (Tensor可选) – 给定每个类手动调整缩放权重。 如果给定,则必须是大小为C的张量

  • size_average (bool, 可选) – Deprecated (see reduction). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average is set to False, the losses are instead summed for each minibatch. Ignored when reduce is False. Default: 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.

  • reduce (bool, 可选) – Deprecated (see reduction). By default, the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch element instead and ignores size_average. Default: True

  • reduction (string, 可选) – Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum''none': no reduction will be applied, 'mean': the weighted mean of the output is taken, 'sum': the output will be summed. Note: size_average and reduce are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction. Default: 'mean'

Shape:

  • Input: (N, C)(N,C) where C = number of classes, or (N, C, d_1, d_2, ..., d_K)(N,C,d1​,d2​,...,dK​) with K \geq 1K≥1 in the case of K-dimensional loss.

  • Target: (N)(N) where each value is 0 \leq \text{targets}[i] \leq C-10≤targets[i]≤C−1 , or (N, d_1, d_2, ..., d_K)(N,d1​,d2​,...,dK​) with K \geq 1K≥1 in the case of K-dimensional loss.

  • Output: scalar. If reduction is 'none', then the same size as the target: (N)(N) , or (N, d_1, d_2, ..., d_K)(N,d1​,d2​,...,dK​) with K \geq 1K≥1 in the case of K-dimensional loss.

 

 

 

 

上一篇:基本数据类型和string的转换


下一篇:golang中string int float bool类型相互转换