当前位置:网站首页>深度學習19種損失函數
深度學習19種損失函數
2022-06-28 05:47:00 【TBYourHero】
本文匯總了19種損失函數,並對其進行了簡要介紹。本文作者@ mingo_敏,僅作學術分享,著作權歸作者所有,如有侵權,請聯系後臺作删文處理。
tensorflow和pytorch很多都是相似的,這裏以pytorch為例。
1. L1範數損失 L1Loss
計算 output 和 target 之差的絕對值。
torch.nn.L1Loss(reduction='mean')參數:
reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值;sum:返回loss的和。默認:mean。
2 均方誤差損失 MSELoss
計算 output 和 target 之差的均方差。
torch.nn.MSELoss(reduction='mean')參數:
reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值;sum:返回loss的和。默認:mean。
3 交叉熵損失 CrossEntropyLoss
當訓練有 C 個類別的分類問題時很有效. 可選參數 weight 必須是一個1維 Tensor, 權重將被分配給各個類別. 對於不平衡的訓練集非常有效。
在多分類任務中,經常采用 softmax 激活函數+交叉熵損失函數,因為交叉熵描述了兩個概率分布的差异,然而神經網絡輸出的是向量,並不是概率分布的形式。所以需要 softmax激活函數將一個向量進行“歸一化”成概率分布的形式,再采用交叉熵損失函數計算 loss。

torch.nn.CrossEntropyLoss(weight=None,ignore_index=-100, reduction='mean')參數:
weight (Tensor, optional) – 自定義的每個類別的權重. 必須是一個長度為 C 的 Tensor
ignore_index (int, optional) – 設置一個目標值, 該目標值會被忽略, 從而不會影響到 輸入的梯度。
reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值;sum:返回loss的和。默認:mean。
4 KL 散度損失 KLDivLoss
計算 input 和 target 之間的 KL 散度。KL 散度可用於衡量不同的連續分布之間的距離, 在連續的輸出分布的空間上(離散采樣)上進行直接回歸時 很有效.
torch.nn.KLDivLoss(reduction='mean')參數:
reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值;sum:返回loss的和。默認:mean。
5 二進制交叉熵損失 BCELoss
二分類任務時的交叉熵計算函數。用於測量重構的誤差, 例如自動編碼機. 注意目標的值 t[i] 的範圍為0到1之間.
torch.nn.BCELoss(weight=None, reduction='mean')參數:
weight (Tensor, optional) – 自定義的每個 batch 元素的 loss 的權重. 必須是一個長度為 “nbatch” 的 的 Tensor
6 BCEWithLogitsLoss
BCEWithLogitsLoss損失函數把 Sigmoid 層集成到了 BCELoss 類中. 該版比用一個簡單的 Sigmoid 層和 BCELoss 在數值上更穩定, 因為把這兩個操作合並為一個層之後, 可以利用 log-sum-exp 的 技巧來實現數值穩定.
torch.nn.BCEWithLogitsLoss(weight=None, reduction='mean', pos_weight=None)參數:
weight (Tensor, optional) – 自定義的每個 batch 元素的 loss 的權重. 必須是一個長度 為 “nbatch” 的 Tensor
7 MarginRankingLoss
torch.nn.MarginRankingLoss(margin=0.0, reduction='mean')對於 mini-batch(小批量) 中每個實例的損失函數如下:

參數:
margin:默認值0
8 HingeEmbeddingLoss
torch.nn.HingeEmbeddingLoss(margin=1.0, reduction='mean')對於 mini-batch(小批量) 中每個實例的損失函數如下:

參數:
margin:默認值1
9 多標簽分類損失 MultiLabelMarginLoss
torch.nn.MultiLabelMarginLoss(reduction='mean')對於mini-batch(小批量) 中的每個樣本按如下公式計算損失:

10 平滑版L1損失 SmoothL1Loss
也被稱為 Huber 損失函數。
torch.nn.SmoothL1Loss(reduction='mean')
其中

11 2分類的logistic損失 SoftMarginLoss
torch.nn.SoftMarginLoss(reduction='mean')
12 多標簽 one-versus-all 損失 MultiLabelSoftMarginLoss
torch.nn.MultiLabelSoftMarginLoss(weight=None, reduction='mean')
13 cosine 損失 CosineEmbeddingLoss
torch.nn.CosineEmbeddingLoss(margin=0.0, reduction='mean')
參數:
margin:默認值0
14 多類別分類的hinge損失 MultiMarginLoss
torch.nn.MultiMarginLoss(p=1, margin=1.0, weight=None, reduction='mean')
參數:
p=1或者2 默認值:1
margin:默認值1
15 三元組損失 TripletMarginLoss
和孿生網絡相似,具體例子:給一個A,然後再給B、C,看看B、C誰和A更像。

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

16 連接時序分類損失 CTCLoss
CTC連接時序分類損失,可以對沒有對齊的數據進行自動對齊,主要用在沒有事先對齊的序列化數據訓練上。比如語音識別、ocr識別等等。
torch.nn.CTCLoss(blank=0, reduction='mean')參數:
reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值;sum:返回loss的和。默認:mean。
17 負對數似然損失 NLLLoss
負對數似然損失. 用於訓練 C 個類別的分類問題.
torch.nn.NLLLoss(weight=None, ignore_index=-100, reduction='mean')參數:
weight (Tensor, optional) – 自定義的每個類別的權重. 必須是一個長度為 C 的 Tensor
ignore_index (int, optional) – 設置一個目標值, 該目標值會被忽略, 從而不會影響到 輸入的梯度.
18 NLLLoss2d
對於圖片輸入的負對數似然損失. 它計算每個像素的負對數似然損失.
torch.nn.NLLLoss2d(weight=None, ignore_index=-100, reduction='mean')參數:
weight (Tensor, optional) – 自定義的每個類別的權重. 必須是一個長度為 C 的 Tensor
reduction-三個值,none: 不使用約簡;mean:返回loss和的平均值;sum:返回loss的和。默認:mean。
19 PoissonNLLLoss
目標值為泊松分布的負對數似然損失
torch.nn.PoissonNLLLoss(log_input=True, full=False, eps=1e-08, reduction='mean')參數:
log_input (bool, optional) – 如果設置為 True , loss 將會按照公 式 exp(input) - target * input 來計算, 如果設置為 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) – 默認值: 1e-8
參考資料:
pytorch loss function 總結
边栏推荐
- Maskrcnn, fast RCNN, fast RCNN excellent video
- ipvs 导致syn 重传问题
- Shutter nestedscrollview sliding folding head pull-down refresh effect
- The windows environment redis uses AOF persistence and cannot generate an AOF file. After generation, the content of the AOF file cannot be loaded
- 数据中台:一篇带你深入浅出了解数据中台
- Oracle 常用基础函数
- ? How to write the position to output true
- 2022 new version NFT source code source code of China meta universe digital collection art trading platform
- CSCI GA scheduling design
- 深度学习19种损失函数
猜你喜欢

jq图片放大器

6. graduation design temperature and humidity monitoring system (esp8266 + DHT11 +oled real-time upload temperature and humidity data to the public network server and display the real-time temperature

Filecoin黑客松开发者大赛

Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)

Docker installs mysql5.7 and starts binlog

Windows环境Redis使用AOF持久化,无法生成AOF文件,生成后无法加载AOF文件内容

? How to write the position to output true
![RL practice (0) - and the platform xinchou winter season [rule based policy]](/img/dc/10d615c64123475fea180e035095ff.png)
RL practice (0) - and the platform xinchou winter season [rule based policy]

Linked list in JS (including leetcode examples) < continuous update ~>

Application of Beidou No.3 short message terminal in dam safety monitoring scheme
随机推荐
Official answers to the "network security" competition questions of the 2022 national vocational college skills competition
What does mysql---where 1=1 mean
File foundation - read / write, storage
MySQL 45 talk | 05 explain the index in simple terms (Part 2)
Main functions of 5ggnb and ng ENB
电子邮件营销的优势在哪里?为什么shopline独立站卖家如此重视?
[C language practice - printing hollow square and its deformation]
Leecode question brushing-ii
Detailed usage configuration of the shutter textbutton, overview of the shutter buttonstyle style and Practice
bash install. SH ******** error
【JVM】——JVM中内存划分
[MySQL] all query tables contain 20million data -- how to optimize SQL
V4L2 驱动层分析
1404. 将二进制表示减到1的步骤数
[CAD drawing Video] AutoCAD 2014 master's way
5G网络整体架构
Lhasa accordion
解决ValueError: Iterable over raw text documents expected, string object received.
[untitled] drv8825 stepping motor drive board schematic diagram
独立站卖家都在用的五大电子邮件营销技巧,你知道吗?