主頁(yè) > 知識(shí)庫(kù) > pytorch中常用的損失函數(shù)用法說(shuō)明

pytorch中常用的損失函數(shù)用法說(shuō)明

熱門(mén)標(biāo)簽:語(yǔ)音系統(tǒng) 電話運(yùn)營(yíng)中心 客戶服務(wù) 呼叫中心市場(chǎng)需求 企業(yè)做大做強(qiáng) 百度AI接口 Win7旗艦版 硅谷的囚徒呼叫中心

1. pytorch中常用的損失函數(shù)列舉

pytorch中的nn模塊提供了很多可以直接使用的loss函數(shù), 比如MSELoss(), CrossEntropyLoss(), NLLLoss() 等

官方鏈接: https://pytorch.org/docs/stable/_modules/torch/nn/modules/loss.html

pytorch中常用的損失函數(shù)
損失函數(shù) 名稱 適用場(chǎng)景
torch.nn.MSELoss() 均方誤差損失 回歸
torch.nn.L1Loss() 平均絕對(duì)值誤差損失 回歸
torch.nn.CrossEntropyLoss() 交叉熵?fù)p失 多分類(lèi)
torch.nn.NLLLoss() 負(fù)對(duì)數(shù)似然函數(shù)損失 多分類(lèi)
torch.nn.NLLLoss2d() 圖片負(fù)對(duì)數(shù)似然函數(shù)損失 圖像分割
torch.nn.KLDivLoss() KL散度損失 回歸
torch.nn.BCELoss() 二分類(lèi)交叉熵?fù)p失 二分類(lèi)
torch.nn.MarginRankingLoss() 評(píng)價(jià)相似度的損失
torch.nn.MultiLabelMarginLoss() 多標(biāo)簽分類(lèi)的損失 多標(biāo)簽分類(lèi)
torch.nn.SmoothL1Loss() 平滑的L1損失 回歸
torch.nn.SoftMarginLoss() 多標(biāo)簽二分類(lèi)問(wèn)題的損失

多標(biāo)簽二分類(lèi)

2. 比較CrossEntropyLoss() 和NLLLoss()

(1). CrossEntropyLoss():

torch.nn.CrossEntropyLoss(weight=None,   # 1D張量,含n個(gè)元素,分別代表n類(lèi)的權(quán)重,樣本不均衡時(shí)常用
                          size_average=None, 
                          ignore_index=-100, 
                          reduce=None, 
                          reduction='mean' )

參數(shù):

weight: 1D張量,含n個(gè)元素,分別代表n類(lèi)的權(quán)重,樣本不均衡時(shí)常用, 默認(rèn)為None.

計(jì)算公式:

weight = None時(shí):

weight ≠ None時(shí):

輸入:

output: 網(wǎng)絡(luò)未加softmax的輸出

target: label值(0,1,2 不是one-hot)

代碼:

loss_func = CrossEntropyLoss(weight=torch.from_numpy(np.array([0.03,0.05,0.19,0.26,0.47])).float().to(device) ,size_average=True)
loss = loss_func(output, target)

(2). NLLLoss():

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

輸入:

output: 網(wǎng)絡(luò)在logsoftmax后的輸出

target: label值(0,1,2 不是one-hot)

代碼:

loss_func = NLLLoss(weight=torch.from_numpy(np.array([0.03,0.05,0.19,0.26,0.47])).float().to(device) ,size_average=True)
loss = loss_func(output, target)


(3). 二者總結(jié)比較:

總之, CrossEntropyLoss() = softmax + log + NLLLoss() = log_softmax + NLLLoss(), 具體等價(jià)應(yīng)用如下:

####################---CrossEntropyLoss()---#######################
 
loss_func = CrossEntropyLoss()
loss = loss_func(output, target)
 
####################---Softmax+log+NLLLoss()---####################
 
self.softmax = nn.Softmax(dim = -1)
 
x = self.softmax(x)
output = torch.log(x)
 
loss_func = NLLLoss()
loss = loss_func(output, target)
 
####################---LogSoftmax+NLLLoss()---######################
 
self.log_softmax = nn.LogSoftmax(dim = -1)
 
output = self.log_softmax(x)
 
loss_func = NLLLoss()
loss = loss_func(output, target)

補(bǔ)充:常用損失函數(shù)用法小結(jié)之Pytorch框架

在用深度學(xué)習(xí)做圖像處理的時(shí)候,常用到的損失函數(shù)無(wú)非有四五種,為了方便Pytorch使用者,所以簡(jiǎn)要做以下總結(jié)

1)L1損失函數(shù)

預(yù)測(cè)值與標(biāo)簽值進(jìn)行相差,然后取絕對(duì)值,根據(jù)實(shí)際應(yīng)用場(chǎng)所,可以設(shè)置是否求和,求平均,公式可見(jiàn)下,Pytorch調(diào)用函數(shù):nn.L1Loss

2)L2損失函數(shù)

預(yù)測(cè)值與標(biāo)簽值進(jìn)行相差,然后取平方,根據(jù)實(shí)際應(yīng)用場(chǎng)所,可以設(shè)置是否求和,求平均,公式可見(jiàn)下,Pytorch調(diào)用函數(shù):nn.MSELoss

3)Huber Loss損失函數(shù)

簡(jiǎn)單來(lái)說(shuō)就是L1和L2損失函數(shù)的綜合版本,結(jié)合了兩者的優(yōu)點(diǎn),公式可見(jiàn)下,Pytorch調(diào)用函數(shù):nn.SmoothL1Loss

4)二分類(lèi)交叉熵?fù)p失函數(shù)

簡(jiǎn)單來(lái)說(shuō),就是度量?jī)蓚€(gè)概率分布間的差異性信息,在某一程度上也可以防止梯度學(xué)習(xí)過(guò)慢,公式可見(jiàn)下,Pytorch調(diào)用函數(shù)有兩個(gè),一個(gè)是nn.BCELoss函數(shù),用的時(shí)候要結(jié)合Sigmoid函數(shù),另外一個(gè)是nn.BCEWithLogitsLoss()

5)多分類(lèi)交叉熵?fù)p失函數(shù)

也是度量?jī)蓚€(gè)概率分布間的差異性信息,Pytorch調(diào)用函數(shù)也有兩個(gè),一個(gè)是nn.NLLLoss,用的時(shí)候要結(jié)合log softmax處理,另外一個(gè)是nn.CrossEntropyLoss

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PyTorch的SoftMax交叉熵?fù)p失和梯度用法
  • Pytorch十九種損失函數(shù)的使用詳解
  • pytorch交叉熵?fù)p失函數(shù)的weight參數(shù)的使用
  • pytorch中交叉熵?fù)p失(nn.CrossEntropyLoss())的計(jì)算過(guò)程詳解
  • Python機(jī)器學(xué)習(xí)pytorch交叉熵?fù)p失函數(shù)的深刻理解

標(biāo)簽:安康 山西 海南 喀什 濟(jì)南 崇左 山西 長(zhǎng)沙

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytorch中常用的損失函數(shù)用法說(shuō)明》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266