监督学习中,如果预测的变量是离散的,我们称其为分类(如决策树,支持向量机等),如果预测的变量是连续的,我们称其为回归。
L1损失函数
调用函数:nn.L1Loss
计算 output 和 target 之差的绝对值
torch.nn.L1Loss(reduction='mean')
L2损失函数MSELoss--- 回归问题
调用函数:nn.MSELoss
torch.nn.MSELoss(reduction='mean')
均方误差-----由于梯度计算过程与sigmoid函数相关,因此反向传播过程可能会发生梯度消失的问题,即梯度趋向于0
Huber Loss损失函数
调用函数:nn.SmoothL1Loss
L1和L2损失函数的综合版本,结合了两者的优点---与MSELoss相比,它对异常值的敏感度较低; 在某些情况下,它可以防止梯度的爆炸式增长
‘二分类’交叉熵损失函数BCELoss
调用函数:
nn.BCELoss # 使用时要结合sigmoid函数
nn.BCEWithLogitsLoss() # 将sigmoid函数和BCELoss方法结合到一个类中
度量两个概率分布间的差异性信息,在某一程度上也可以防止梯度学习过慢
torch.nn.BCELoss(weight=None, reduction='mean')
‘多分类’交叉熵损失函数
调用函数:
nn.NLLLoss # 使用时要结合log softmax
nn.CrossEntropyLoss # 该criterion将nn.LogSoftmax()和nn.NLLLoss()方法结合到一个类中
度量两个概率分布间的差异性信息---
CrossEntropyLoss() = softmax + log + NLLLoss() = log_softmax + NLLLoss(), 具体等价应用如下:
####################---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)