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)[原始碼][原始碼]¶
計算輸入 logits 和目標之間的交叉熵損失。
有關詳細資訊,請參閱
CrossEntropyLoss
。- 參數
input (Tensor) – 預測的未正規化 logits;請參閱下面的形狀 (Shape) 章節,了解支援的形狀。
target (Tensor) – 真實的類別索引或類別機率;請參閱下面的形狀 (Shape) 章節,了解支援的形狀。
weight (Tensor, optional) – 手動調整每個類別權重的數值。如果提供,必須是大小為 C 的 Tensor。
size_average (bool, optional) – 已棄用(請參閱
reduction
)。預設情況下,損失會在批次中對每個損失元素進行平均。 請注意,對於某些損失,每個樣本有多個元素。 如果欄位size_average
設置為False
,則會對每個小批次中的損失進行加總。 當 reduce 為False
時,此參數會被忽略。 預設值:True
ignore_index (int, optional) – 指定一個要忽略的目標值,且該值不影響輸入梯度。 當
size_average
為True
時,損失會在非忽略的目標上取平均。 請注意,只有在目標包含類別索引時,ignore_index
才適用。 預設值:-100reduce (bool, optional) – 已棄用(請參閱
reduction
)。 預設情況下,損失會根據size_average
對每個小批次的觀察值進行平均或加總。 當reduce
為False
時,會改為返回每個批次元素的損失,並忽略size_average
。 預設值:True
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 中所述。 預設值:。
- Return type
- Shape
Input: 形狀 , 或 with 在 K 維損失的情況下。
目標:如果包含類別索引,形狀為 、 或 ,其中 ,在 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()