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 必须为张量,其大小为或,K≥1对于K维情况(后述)。
该评价损失期望类别索引范围为,作为大小为minibatch的一维张量target的每个值。 如果指定了ignore_index,则此评价损失也接受该类索引(此索引可能不一定在类范围内)。
损失可以描述为:
或在指定weight
参数的情况下:
损失是对每个minibatch的观察结果的平均值。 如果指定了weight
参数,则这是一个加权平均值:
还可用于更高维度的输入,例如2D图像,提供一个大小为,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 fieldsize_average
is set toFalse
, the losses are instead summed for each minibatch. Ignored when reduce isFalse
. Default:True
-
ignore_index (int, 可选) – Specifies a target value that is ignored and does not contribute to the input gradient. When
size_average
isTrue
, 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 onsize_average
. Whenreduce
isFalse
, returns a loss per batch element instead and ignoressize_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
andreduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will overridereduction
. 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.
标签:loss,nn,...,average,torch,reduction,CrossEntropyLoss,mean,size 来源: https://blog.csdn.net/qq_36201400/article/details/111335423