評價此頁

torch.nn.functional.cross_entropy#

torch.nn.functional.cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean', label_smoothing=0.0)[source]#

計算輸入 logits 和 target 之間的交叉熵損失。

有關詳細資訊,請參閱 CrossEntropyLoss

引數
  • input (Tensor) – 預測的未歸一化 logits;請參閱下面的 Shape 部分了解支援的形狀。

  • target (Tensor) – 真實類索引或類機率;請參閱下面的 Shape 部分了解支援的形狀。

  • weight (Tensor, optional) – 手動為每個類別指定的重縮放權重。如果提供,則必須是大小為 C 的 Tensor。

  • size_average (bool, optional) – 已棄用(請參閱 reduction)。

  • ignore_index (int, optional) – 指定一個被忽略且不計入輸入梯度的目標值。當 size_averageTrue 時,損失將根據非忽略目標計算平均值。請注意,ignore_index 僅在 target 包含類索引時適用。預設值:-100

  • reduce (bool, optional) – 已棄用(請參閱 reduction)。

  • reduction (str, optional) – 指定應用於輸出的縮減方式:'none' | 'mean' | 'sum''none':不應用縮減, 'mean':輸出的總和將除以輸出中的元素數量, 'sum':輸出將求和。注意:size_averagereduce 正在被棄用,在此期間,指定其中任何一個引數都將覆蓋 reduction。預設值:'mean'

  • label_smoothing (float, optional) – 一個在 [0.0, 1.0] 範圍內的浮點數。指定計算損失時的平滑量,0.0 表示無平滑。目標變為原始真實標籤和均勻分佈的混合,如 Rethinking the Inception Architecture for Computer Vision 中所述。預設值:0.00.0

返回型別

張量

形狀
  • 輸入:形狀為 (C)(C)(N,C)(N, C)(N,C,d1,d2,...,dK)(N, C, d_1, d_2, ..., d_K) K 維損失情況下的 K1K \geq 1

  • Target: 如果包含類索引,形狀為 ()()(N)(N)(N,d1,d2,...,dK)(N, d_1, d_2, ..., d_K) K 維損失情況下的 K1K \geq 1,其中每個值應介於 [0,C)[0, C)。如果包含類機率,則形狀與輸入相同,並且每個值應介於 [0,1][0, 1]

其中

C=number of classesN=batch size\begin{aligned} C ={} & \text{number of classes} \\ N ={} & \text{batch size} \\ \end{aligned}

示例

>>> # Example of target with class indices
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randint(5, (3,), dtype=torch.int64)
>>> loss = F.cross_entropy(input, target)
>>> loss.backward()
>>>
>>> # Example of target with class probabilities
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5).softmax(dim=1)
>>> loss = F.cross_entropy(input, target)
>>> loss.backward()