深度学习——(2)几种常见的损失函数
确定性大,熵越小
确定性小,熵越大
1.L1 loss
优点:一阶导是常熟
缺点:零点处不可导
import torch
from torch import nn
criterion_l1=nn.L1Loss(reduction='mean')
input=torch.randn(3,5,requires_grad=True)
target=torch.randn(3,5)
loss_l1=criterion_l1(input,target)
loss_l11=(abs(target-input)).sum()/15
loss_l1,loss_l11
2.MSEloss
二阶函数
缺点:梯度很大,会导致梯度爆炸;求导之后会存在梯度消失的现象
criterion_mse=nn.MSELoss()
loss_mse=criterion_mse(input,target)
loss_mse1=((target-input)**2).sum()/15
loss_mse,loss_mse1
3.smoothL1 loss
优点:结合了L1和MSE的优点,避免零点不可导以及梯度爆炸的状况
criterion_SML1=nn.SmoothL1Loss()
loss_sml1=criterion_SML1(input,target)
loss_sml1
4. 交叉熵 loss
criterion_cross=nn.CrossEntropyLoss(reduction='mean',label_smoothing=0.0)
loss_cross=criterion_cross(input,target)
loss_cross
5.KL散度loss——相对熵
criterion_KL=nn.KLDivLoss()
loss_KL=criterion_KL(input,target)
loss_KL
|