一、損失函數概念
損失函數:衡量模型輸出與真實標簽的差異
- 損失函數Loss Function :Loss = f(y^ , y)
- 代價函數Cost Function:Cost = 1/N ∑ f (y^ , y)
- 目標函數Objective Function:Obj = Cost + Regularization
二、交叉熵損失函數
三、NLL \ BCE \ BCEWithLogits Loss.
1、nn.CrossEntropyLoss
- 功能:nn.LogSoftmax()與nn.NLLLoss()結合,進行交叉熵計算 數據值進行歸一化
- 主要參數:
- weight:各類別的loss設置權值
- ignore_index:忽略某個類別
- reduction:計算模式
- 交叉熵 = 信息熵 + 相對熵
- 交叉熵:
- 自信息
- 相對熵:兩個分部之間的差異,不具備對稱性
5、nn.L1Loss
功能:計算inputs與target之差的絕對值
6、nn.MSELoss
功能:計算inputs與target之差的平方
主要參數:reduction:計算模式,可為none/sum/mean
none:逐個元素計算
sum:所有元素求和,返回標量
mean:加權平均,返回標量
7、nn.SmoothL1Loss
8、nn.PoissonNULLLoss
功能:泊松分布的負對數似然損失函數
主要參數:log_input:輸入是否為對數形式,決定計算公式
full:計算所有loss,默認為False
eps:修正項,避免log(input)為nan
9、nn.KLDivLoss
功能:計算KLD(divergence),KL散度,相對熵
注意事項:需提前將輸入計算log-probabilities,如通過nn.logsoftmax()
主要參數:
reduction:none/sum/mean/batchmean
batchmean-batchsize維度求平均值
10、nn.MarginRankingLoss
功能:計算兩個向量之間的相似度,用於排序任務
特別說明:該方法計算兩組數據之間的差異,返回一個n*n的loss矩陣
主要參數:
margin:邊界值,x1和x2之間的差異值
reduction:計算模式,可為none/sum/mean
11、nn.MultiLabelSoftMarginLoss
12、nn.SoftMarginLoss
功能:計算二分類的logistic損失
主要參數:reduction
13、nn.MultiLabelSoftMarginLoss
功能:SoftMarginLoss多標簽版本
主要參數:weight:各類別的loss設置權限
reduction:
14、nn.MultiMarginLoss
功能:計算多分類的折頁損失
主要參數:p:可選1或2
weight:
margin:邊界值
reduction
15、nn.TripletMarginLoss
功能:計算三元組損失,人臉驗證中常用
主要參數:p margin reduction
16、nn.HingeEmbeddingLoss
功能:計算兩個輸入的相似性,常用於非線性embedding和半監督學習
特別注意:輸入x應為兩個輸入之差的絕對值
主要參數:margin reduction
17、nn.CosineEmbeddingLoss
功能:采用余弦相似度計算兩個輸入的相似性
主要參數:margin reduction
18、nn.CTCLoss
功能:計算CTC損失,解決時序類數據的分類Connctionist Temporal Classification
主要參數:blank:blank label
zero_infinity:無窮大的值或梯度置0
reduction