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_average為True時,損失將根據非忽略目標計算平均值。請注意,ignore_index僅在 target 包含類索引時適用。預設值:-100reduce (bool, optional) – 已棄用(請參閱
reduction)。reduction (str, optional) – 指定應用於輸出的縮減方式:
'none'|'mean'|'sum'。'none':不應用縮減,'mean':輸出的總和將除以輸出中的元素數量,'sum':輸出將求和。注意:size_average和reduce正在被棄用,在此期間,指定其中任何一個引數都將覆蓋reduction。預設值:'mean'label_smoothing (float, optional) – 一個在 [0.0, 1.0] 範圍內的浮點數。指定計算損失時的平滑量,0.0 表示無平滑。目標變為原始真實標籤和均勻分佈的混合,如 Rethinking the Inception Architecture for Computer Vision 中所述。預設值:。
- 返回型別
- 形狀
輸入:形狀為 、 或 K 維損失情況下的 。
Target: 如果包含類索引,形狀為 、 或 K 維損失情況下的 ,其中每個值應介於 。如果包含類機率,則形狀與輸入相同,並且每個值應介於 。
其中
示例
>>> # 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()