溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Pytorch十九種損失函數(shù)的實現(xiàn)方法

發(fā)布時間:2020-07-29 13:42:08 來源:億速云 閱讀:569 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了Pytorch十九種損失函數(shù)的實現(xiàn)方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

損失函數(shù)通過torch.nn包實現(xiàn),

1 基本用法

criterion = LossCriterion() #構(gòu)造函數(shù)有自己的參數(shù)
loss = criterion(x, y) #調(diào)用標(biāo)準(zhǔn)時也有參數(shù)

2 損失函數(shù)

2-1 L1范數(shù)損失 L1Loss

計算 output 和 target 之差的絕對值。

torch.nn.L1Loss(reduction='mean')

參數(shù):

reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值; sum:返回loss的和。默認(rèn):mean。

2-2 均方誤差損失 MSELoss

計算 output 和 target 之差的均方差。

torch.nn.MSELoss(reduction='mean')

參數(shù):

reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值; sum:返回loss的和。默認(rèn):mean。

2-3 交叉熵?fù)p失 CrossEntropyLoss

當(dāng)訓(xùn)練有 C 個類別的分類問題時很有效. 可選參數(shù) weight 必須是一個1維 Tensor, 權(quán)重將被分配給各個類別. 對于不平衡的訓(xùn)練集非常有效。

在多分類任務(wù)中,經(jīng)常采用 softmax 激活函數(shù)+交叉熵?fù)p失函數(shù),因為交叉熵描述了兩個概率分布的差異,然而神經(jīng)網(wǎng)絡(luò)輸出的是向量,并不是概率分布的形式。所以需要 softmax激活函數(shù)將一個向量進(jìn)行“歸一化”成概率分布的形式,再采用交叉熵?fù)p失函數(shù)計算 loss。

Pytorch十九種損失函數(shù)的實現(xiàn)方法

torch.nn.CrossEntropyLoss(weight=None, ignore_index=-100, reduction='mean')

參數(shù):

weight (Tensor, optional) – 自定義的每個類別的權(quán)重. 必須是一個長度為 C 的 Tensor
ignore_index (int, optional) – 設(shè)置一個目標(biāo)值, 該目標(biāo)值會被忽略, 從而不會影響到 輸入的梯度。
reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值; sum:返回loss的和。默認(rèn):mean。

2-4 KL 散度損失 KLDivLoss

計算 input 和 target 之間的 KL 散度。KL 散度可用于衡量不同的連續(xù)分布之間的距離, 在連續(xù)的輸出分布的空間上(離散采樣)上進(jìn)行直接回歸時 很有效.

torch.nn.KLDivLoss(reduction='mean')

參數(shù):

reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值; sum:返回loss的和。默認(rèn):mean。

2-5 二進(jìn)制交叉熵?fù)p失 BCELoss

二分類任務(wù)時的交叉熵計算函數(shù)。用于測量重構(gòu)的誤差, 例如自動編碼機. 注意目標(biāo)的值 t[i] 的范圍為0到1之間.

torch.nn.BCELoss(weight=None, reduction='mean')

參數(shù):

weight (Tensor, optional) – 自定義的每個 batch 元素的 loss 的權(quán)重. 必須是一個長度為 “nbatch” 的 的 Tensor
pos_weight(Tensor, optional) – 自定義的每個正樣本的 loss 的權(quán)重. 必須是一個長度 為 “classes” 的 Tensor

2-6 BCEWithLogitsLoss

BCEWithLogitsLoss損失函數(shù)把 Sigmoid 層集成到了 BCELoss 類中. 該版比用一個簡單的 Sigmoid 層和 BCELoss 在數(shù)值上更穩(wěn)定, 因為把這兩個操作合并為一個層之后, 可以利用 log-sum-exp 的 技巧來實現(xiàn)數(shù)值穩(wěn)定.

torch.nn.BCEWithLogitsLoss(weight=None, reduction='mean', pos_weight=None)

參數(shù):

weight (Tensor, optional) – 自定義的每個 batch 元素的 loss 的權(quán)重. 必須是一個長度 為 “nbatch” 的 Tensor
pos_weight(Tensor, optional) – 自定義的每個正樣本的 loss 的權(quán)重. 必須是一個長度 為 “classes” 的 Tensor

2-7 MarginRankingLoss

torch.nn.MarginRankingLoss(margin=0.0, reduction='mean')

對于 mini-batch(小批量) 中每個實例的損失函數(shù)如下:

Pytorch十九種損失函數(shù)的實現(xiàn)方法

參數(shù):

margin:默認(rèn)值0

2-8 HingeEmbeddingLoss

torch.nn.HingeEmbeddingLoss(margin=1.0, reduction='mean')

對于 mini-batch(小批量) 中每個實例的損失函數(shù)如下:

Pytorch十九種損失函數(shù)的實現(xiàn)方法

參數(shù):

margin:默認(rèn)值1

2-9 多標(biāo)簽分類損失 MultiLabelMarginLoss

torch.nn.MultiLabelMarginLoss(reduction='mean')

對于mini-batch(小批量) 中的每個樣本按如下公式計算損失:

Pytorch十九種損失函數(shù)的實現(xiàn)方法

2-10 平滑版L1損失 SmoothL1Loss

也被稱為 Huber 損失函數(shù)。

torch.nn.SmoothL1Loss(reduction='mean')

Pytorch十九種損失函數(shù)的實現(xiàn)方法

其中

Pytorch十九種損失函數(shù)的實現(xiàn)方法

2-11 2分類的logistic損失 SoftMarginLoss

torch.nn.SoftMarginLoss(reduction='mean')

Pytorch十九種損失函數(shù)的實現(xiàn)方法

2-12 多標(biāo)簽 one-versus-all 損失 MultiLabelSoftMarginLoss

torch.nn.MultiLabelSoftMarginLoss(weight=None, reduction='mean')

Pytorch十九種損失函數(shù)的實現(xiàn)方法

2-13 cosine 損失 CosineEmbeddingLoss

torch.nn.CosineEmbeddingLoss(margin=0.0, reduction='mean')

Pytorch十九種損失函數(shù)的實現(xiàn)方法

參數(shù):

margin:默認(rèn)值0

2-14 多類別分類的hinge損失 MultiMarginLoss

torch.nn.MultiMarginLoss(p=1, margin=1.0, weight=None, reduction='mean')

Pytorch十九種損失函數(shù)的實現(xiàn)方法

參數(shù):

p=1或者2 默認(rèn)值:1
margin:默認(rèn)值1

2-15 三元組損失 TripletMarginLoss

torch.nn.TripletMarginLoss(margin=1.0, p=2.0, eps=1e-06, swap=False, reduction='mean')

Pytorch十九種損失函數(shù)的實現(xiàn)方法

其中:

Pytorch十九種損失函數(shù)的實現(xiàn)方法

2-16 連接時序分類損失 CTCLoss

CTC連接時序分類損失,可以對沒有對齊的數(shù)據(jù)進(jìn)行自動對齊,主要用在沒有事先對齊的序列化數(shù)據(jù)訓(xùn)練上。比如語音識別、ocr識別等等。

torch.nn.CTCLoss(blank=0, reduction='mean')

參數(shù):

reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值; sum:返回loss的和。默認(rèn):mean。

2-17 負(fù)對數(shù)似然損失 NLLLoss

負(fù)對數(shù)似然損失. 用于訓(xùn)練 C 個類別的分類問題.

torch.nn.NLLLoss(weight=None, ignore_index=-100, reduction='mean')

參數(shù):

weight (Tensor, optional) – 自定義的每個類別的權(quán)重. 必須是一個長度為 C 的 Tensor
ignore_index (int, optional) – 設(shè)置一個目標(biāo)值, 該目標(biāo)值會被忽略, 從而不會影響到 輸入的梯度.

2-18 NLLLoss2d

對于圖片輸入的負(fù)對數(shù)似然損失. 它計算每個像素的負(fù)對數(shù)似然損失.

torch.nn.NLLLoss2d(weight=None, ignore_index=-100, reduction='mean')

參數(shù):

weight (Tensor, optional) – 自定義的每個類別的權(quán)重. 必須是一個長度為 C 的 Tensor
reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值; sum:返回loss的和。默認(rèn):mean。

2-19 PoissonNLLLoss

目標(biāo)值為泊松分布的負(fù)對數(shù)似然損失

torch.nn.PoissonNLLLoss(log_input=True, full=False, eps=1e-08, reduction='mean')

參數(shù):

log_input (bool, optional) – 如果設(shè)置為 True , loss 將會按照公 式 exp(input) - target * input 來計算, 如果設(shè)置為 False , loss 將會按照 input - target * log(input+eps) 計算.
full (bool, optional) – 是否計算全部的 loss, i. e. 加上 Stirling 近似項 target * log(target) - target + 0.5 * log(2 * pi * target).
eps (float, optional) – 默認(rèn)值: 1e-8

看完上述內(nèi)容,是不是對Pytorch十九種損失函數(shù)的實現(xiàn)方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI