损失函数
损失函数(loss): L o s s = f ( y ^ , y ) Loss=f(\hat{y}, y) Loss=f(y^,y) ,衡量模型输出与真实标签的差异,针对一个样本。
代价函数(cost): C o s s = 1 N ∑ i N f ( y ^ i , y i ) Coss=\frac{1}{N}\sum_i^Nf(\hat{y}_i, y_i) Coss=N1∑iNf(y^i,yi) ,计算整个样本集loss的平均值。
目标函数(objective): O b j = C o s t + R e g u l a r i z a t i o n Obj=Cost+Regularization Obj=Cost+Regularization ,最后模型拟合的函数。
18种损失函数
-
nn.CrossEntropyLoss(weight=None, ignore_index=-100, reduction=‘mean’)
nn.LogSoftnax()与nn.NLLLoss()的结合,进行交叉熵计算。
weight:各类别的loss设置权值。
ignore_index:忽略某个类别。
reduction:计算模式,可为none/sum/mean
- none:逐个元素计算
- sum:所有元素求和,返回标量
- mean:加权平均,返回标量
熵:描述一个事件的不确定性,熵越小,不确定性越大。
交叉熵衡量两个概率分布之间的差异,值越低,分布越近。
-
nn.NLLLoss(weight=None, ignore_index=-100, reduction=‘mean’)
实现负对数似然函数中的负号功能
weight:各类别的loss设置权值。
ignore_index:忽略某个类别。
reduction:计算模式,可为none/sum/mean
- none:逐个元素计算
- sum:所有元素求和,返回标量
- mean:加权平均,返回标量
-
nn.BCELoss(weight=None, ignore_index=-100, reduction=‘mean’)
二分类交叉熵(输入取值在[0, 1])
weight:各类别的loss设置权值。
ignore_index:忽略某个类别。
reduction:计算模式,可为none/sum/mean
- none:逐个元素计算
- sum:所有元素求和,返回标量
- mean:加权平均,返回标量
-
nn.BCEWithLogitsLoss(weight=None, ignore_index=-100, reduction=‘mean’, pos_weight=None)
结合Sigmoid与二分类交叉熵(网络最后不加sigmoid函数)
pos_weight:正样本的权值
weight:各类别的loss设置权值
ignore_index:忽略某个类别
reduction:计算模式,可为none/sum/mean
- none:逐个元素计算
- sum:所有元素求和,返回标量
- mean:加权平均,返回标量
-
nn.L1Loss(reduction=‘mean’)
计算inputs与target之差的绝对值
-
nn.MSELoss(reduction=‘mean’)
计算inputs与target之差的平方
-
nn.SmoothL1Loss(reduction=‘mean’)
平滑的L1Loss
-
nn.PoissonNLLLoss(log_input=True, full=False, eps=1e-08, reduction=‘mean’)
泊松分布的负对数似然损失函数
log_input:输入是否为对数形式,决定计算公式
log_input=True
- loss(input, target) = exp(input) - target*input
log_input=False
- loss(input, target) = input - target*log(input+eps)
full:计算所有loss
eps:修正项,避免log(input)为nan
-
nn.KLDivLoss(reduction=‘mean’)
计算KLD(divergence),KL散度,相对熵
(需要提前将输入计算log-probabilities,如通过nn.logsoftmax())
reduction:计算模式,可为none/sum/mean/batchmean
- none:逐个元素计算
- sum:所有元素求和,返回标量
- mean:加权平均,返回标量
- batchmean:batchsize维度求平均
-
nn.MarginRankingLoss(margin=0.0, reduction=‘mean’)
计算两个向量之间的相似度,通常用于排序任务
(该方法计算两组数据之间的差异,返回一个n*n的loss矩阵)
margin:边界值,x1与x2之间的差异值。
reduction:计算模式,可为none/sum/mean
loss(x, y) = max(0, -y*(x1-x2)+margin)
- y=1时,希望x1比x2大,当x1>x2时,不产生loss
- y=-1时,希望x1比x2小,当x1<x2时,不产生loss
-
nn.MultiLabelMarginLoss(reduction=‘mean’)
多标签边界损失函数
多标签:一张图片中,有好多类别
reduction:计算模式,可为none/sum/mean
-
nn.SoftMarginLoss(reduction=‘mean’)
计算二分类的logistic损失
reduction:计算模式,可为none/sum/mean
-
nn.MultiLabelSoftMarginLoss(weight=None, reduction=‘mean’)
SoftMarginLoss()多标签版
weight:各类别的loss设置权值
reduction:计算模式,可为none/sum/mean
-
nn.MultiMarginLoss(p = 1, margin=1.0, weight=None, reduction=‘mean’)
计算多分类的折页损失
p:可选1或2
weight:各类别的loss设置权值
margin:边界值
reduction:计算模式,可为none/sum/mean
-
nn.TripletMarginLoss(p = 2.0, margin=1.0, eps=1e-06, swap=False, reduction=‘mean’)
计算三元组损失,人脸验证中常用
p:范数的阶,默认为2
margin:边界值
reduction:计算模式,可为none/sum/mean
-
nn.HingeEmbeddingLoss(margin=1.0, reduction=‘mean’)
计算两个输入的相似性,常用于非线性embedding和半监督学习(输入x应为两个输入之差的绝对值)
margin:边界值
reduction:计算模式,可为none/sum/mean
-
nn.CosineEmbeddingLoss(margin=0.0, reduction=‘mean’)
采用余弦相似度计算两个输入的相似性
margin:可取[-1, 1],推荐[0, 0.5]
reduction:计算模式,可为none/sum/mean
-
nn.CTCLoss(blank=0, zero_infinity = False, reduction=‘mean’)
计算CTC损失,解决时序类数据的分类
blank:blank label
zero_infinity:无穷大的值或梯度置0
reduction:计算模式,可为none/sum/mean