CrossEntropyLoss#
- class torch.nn.CrossEntropyLoss(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean', label_smoothing=0.0)[source]#
此準則計算輸入 logits 和 target 之間的交叉熵損失。
當訓練具有 C 個類別的分類問題時,此準則非常有用。如果提供了可選引數
weight,它應該是一個一維 Tensor,為每個類別分配權重。這在訓練集不平衡時特別有用。期望 input 包含每個類別的未歸一化 logits(通常 不 需要為正數或總和為 1)。對於無批次的輸入,input 必須是大小為 的 Tensor;對於批次輸入,大小為 或 的 Tensor,其中 。最後一個形式適用於更高維度的輸入,例如計算 2D 影像的每畫素交叉熵損失。
此準則期望的 target 應包含以下兩者之一:
類別索引,範圍為 ,其中 是類別數;如果指定了 ignore_index,此損失也接受該類別索引(該索引不一定在類別範圍內)。在此情況下,未歸一化(即
reduction設定為'none')的損失可描述為:其中 是輸入, 是目標, 是權重, 是類別數, 跨越了小批次維度以及 for the K-dimensional case. If
reductionis not'none'(default'mean'), then請注意,這種情況等同於對輸入應用
LogSoftmax,然後是NLLLoss。每個類別的機率;當需要每個小批次項的標籤超出單個類別時(例如,對於混合標籤、標籤平滑等),這很有用。在這種情況下,未歸一化(即
reduction設定為'none')的損失可描述為:其中 是輸入, 是目標, 是權重, 是類別數, 跨越了小批次維度以及 for the K-dimensional case. If
reductionis not'none'(default'mean'), then
注意
當 target 包含類別索引時,此準則的效能通常更好,因為它允許進行最佳化計算。僅當每個小批次項的單個類別標籤過於受限時,才考慮將 target 提供為類別機率。
- 引數
weight (Tensor, optional) – 為每個類別手動指定的重取樣權重。如果提供,則必須是大小為 C 的 Tensor。
size_average (bool, optional) – 已棄用 (參見
reduction)。預設情況下,損失值在批次中的每個損失元素上取平均值。請注意,對於某些損失,每個樣本有多個元素。如果欄位size_average設定為False,則損失值在每個小批次中而是求和。當reduce為False時忽略。預設值:Trueignore_index (int, optional) – 指定一個被忽略的目標值,該值不計入輸入梯度。當
size_average為True時,損失將對非忽略的目標取平均。請注意,ignore_index僅適用於 target 包含類別索引的情況。reduce (bool, optional) – 已棄用 (參見
reduction)。預設情況下,損失值在每個小批次中根據size_average對觀測值進行平均或求和。當reduce為False時,返回每個批次元素的損失值,並忽略size_average。預設值:Truereduction (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 中所述。預設值:。
- 形狀
輸入:形狀為 , 或 的 Tensor,其中 用於 K 維損失。
目標:如果包含類別索引,則形狀為 , 或 的 K 維損失,其中每個值應介於 之間。使用類別索引時,目標資料型別必須是 long 型。如果包含類別機率,則目標必須與輸入形狀相同,並且每個值應介於 之間。這意味著使用類別機率時,目標資料型別必須是 float 型。請注意,PyTorch 不嚴格強制類別機率中的機率約束,使用者有責任確保
target包含有效的機率分佈(有關更多詳細資訊,請參閱下面的示例部分)。輸出:如果 reduction 為 'none',則形狀為 , 或 的 K 維損失,取決於輸入的形狀。否則,為標量。
其中
示例
>>> # Example of target with class indices >>> loss = nn.CrossEntropyLoss() >>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.empty(3, dtype=torch.long).random_(5) >>> output = loss(input, target) >>> output.backward() >>> >>> # Example of target with class probabilities >>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.randn(3, 5).softmax(dim=1) >>> output = loss(input, target) >>> output.backward()
注意
當
target包含類別機率時,它應包含軟標籤——即,每個target條目應代表給定資料樣本的類別機率分佈,其中單個機率介於[0,1]之間,並且整個分佈的總和為 1。這就是為什麼在上面的類別機率示例中將softmax()函式應用於target。PyTorch 不會驗證
target中的值是否在[0,1]範圍內,也不會驗證每個資料樣本的分佈是否總和為1。不會發出警告,使用者有責任確保target包含有效的機率分佈。提供任意值可能導致誤導性的損失值和不穩定的訓練梯度。示例
>>> # Example of target with incorrectly specified class probabilities >>> loss = nn.CrossEntropyLoss() >>> torch.manual_seed(283) >>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.randn(3, 5) >>> # Provided target class probabilities are not in range [0,1] >>> target tensor([[ 0.7105, 0.4446, 2.0297, 0.2671, -0.6075], [-1.0496, -0.2753, -0.3586, 0.9270, 1.0027], [ 0.7551, 0.1003, 1.3468, -0.3581, -0.9569]]) >>> # Provided target class probabilities do not sum to 1 >>> target.sum(axis=1) tensor([2.8444, 0.2462, 0.8873]) >>> # No error message and possible misleading loss value >>> loss(input, target).item() 4.6379876136779785 >>> >>> # Example of target with correctly specified class probabilities >>> # Use .softmax() to ensure true probability distribution >>> target_new = target.softmax(dim=1) >>> # New target class probabilities all in range [0,1] >>> target_new tensor([[0.1559, 0.1195, 0.5830, 0.1000, 0.0417], [0.0496, 0.1075, 0.0990, 0.3579, 0.3860], [0.2607, 0.1355, 0.4711, 0.0856, 0.0471]]) >>> # New target class probabilities sum to 1 >>> target_new.sum(axis=1) tensor([1.0000, 1.0000, 1.0000]) >>> loss(input, target_new).item() 2.55349063873291