婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

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

熱門標(biāo)簽:公司電話機(jī)器人 唐山智能外呼系統(tǒng)一般多少錢 海南400電話如何申請(qǐng) 騰訊外呼線路 哈爾濱ai外呼系統(tǒng)定制 陜西金融外呼系統(tǒng) 廣告地圖標(biāo)注app 激戰(zhàn)2地圖標(biāo)注 白銀外呼系統(tǒng)

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失 多分類
torch.nn.NLLLoss() 負(fù)對(duì)數(shù)似然函數(shù)損失 多分類
torch.nn.NLLLoss2d() 圖片負(fù)對(duì)數(shù)似然函數(shù)損失 圖像分割
torch.nn.KLDivLoss() KL散度損失 回歸
torch.nn.BCELoss() 二分類交叉熵?fù)p失 二分類
torch.nn.MarginRankingLoss() 評(píng)價(jià)相似度的損失
torch.nn.MultiLabelMarginLoss() 多標(biāo)簽分類的損失 多標(biāo)簽分類
torch.nn.SmoothL1Loss() 平滑的L1損失 回歸
torch.nn.SoftMarginLoss() 多標(biāo)簽二分類問題的損失

多標(biāo)簽二分類

2. 比較CrossEntropyLoss() 和NLLLoss()

(1). CrossEntropyLoss():

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

參數(shù):

weight: 1D張量,含n個(gè)元素,分別代表n類的權(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ù)無非有四五種,為了方便Pytorch使用者,所以簡(jiǎn)要做以下總結(jié)

1)L1損失函數(shù)

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

2)L2損失函數(shù)

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

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

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

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

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

5)多分類交叉熵?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ì)算過程詳解
  • Python機(jī)器學(xué)習(xí)pytorch交叉熵?fù)p失函數(shù)的深刻理解

標(biāo)簽:四川 上海 惠州 常德 鷹潭 黔西 黑龍江 益陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytorch中常用的損失函數(shù)用法說明》,本文關(guān)鍵詞  pytorch,中常,用的,損失,函數(shù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《pytorch中常用的損失函數(shù)用法說明》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于pytorch中常用的損失函數(shù)用法說明的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 连平县| 阿克苏市| 荆州市| 巫溪县| 成都市| 辛集市| 郑州市| 龙川县| 通州市| 二手房| 资阳市| 姚安县| 台安县| 中方县| 原平市| 灌云县| 大安市| 崇州市| 揭西县| 大方县| 梨树县| 阿拉善右旗| 宽甸| 茂名市| 资溪县| 且末县| 越西县| 荆州市| 包头市| 日土县| 铁岭县| 磐安县| 剑川县| 靖宇县| 翁牛特旗| 德兴市| 信阳市| 明光市| 固原市| 宜君县| 乌拉特后旗|