1.为什么需要损失函数
神经网络中的“学习”是指从训练数据中自动获取最优权重参数的过程。学习的目的就是以该损失函数为基准,找出能使它的值达到最小的权重参数。
2.常见的损失函数
1. 0-1损失函数(zero-one loss):0-1损失是指预测值和目标值不相等为1, 否则为0
(1)0-1损失函数直接对应分类判断错误的个数,但是它是一个非凸函数,不太适用。
2. 对数损失函数
(1) log对数损失函数能非常好的表征概率分布,在很多场景尤其是多分类,如果需要知道结果属于每个类别的置信度,那它非常适合。
(2)健壮性不强,相比于hinge loss对噪声更敏感。
(3)逻辑回归的损失函数就是log对数损失函数
3. 平方损失函数MSE(均值平方差):对每个预测值与真实值作差求平方的平均值
MSE越小代表模型越好,类似的算法还包括RMSE和MAD。
线性回归就是使用MSE作为损失函数。
4. Hinge 损失函数
1)hinge损失函数表示如果被分类正确,损失为0,否则损失就为 1 − y f ( x ) 1-yf(x)1−yf(x)。SVM就是使用这个损失函数。
(2)一般的 f ( x ) f(x)f(x) 是预测值,在-1到1之间,y yy 是目标值(-1或1)。其含义是, f ( x ) f(x)f(x) 的值在-1和+1之间就可以了,并不鼓励 ∣ f ( x ) ∣ > 1 |f(x)| > 1∣f(x)∣>1 ,即并不鼓励分类器过度自信,让某个正确分类的样本距离分割线超过1并不会有任何奖励,从而使分类器可以更专注于整体的误差。
(3) 健壮性相对较高,对异常点、噪声不敏感,但它没太好的概率解释。
5. 交叉熵损失函数 (Cross-entropy loss function):两个概率分布之间的距离
本质上也是一种对数似然函数,可用于二分类和多分类任务中
6、L1Loss 平均绝对误差
平均绝对误差(MAE)是一种用于回归模型的损失函数。MAE是目标变量和预测变量之间绝对差值之和。因此它衡量的是一组预测值中的平均误差大小,而不考虑它们的方向(如果我们考虑方向的话,那就是均值误差(MBE)了,即误差之和)。范围为0到∞
loss = nn.L1Loss() input = torch.randn(3, 5, requires_grad=True) target = torch.randn(3, 5) output = loss(input, target) output.backward()
7. NLLLoss() 负对数似然损失
用于进行分类任务,把input转换为概率分布。
m = nn.LogSoftmax(dim=1) loss = nn.NLLLoss() input = torch.randn(3, 5, requires_grad=True) target = torch.tensor([1, 0, 4]) output = loss(m(input), target) output.backward()
8、NLLLoss2d()
9、PoissonNLLLoss 目标泊松分布的负对数似然损失
用于target服从泊松分布的分类任务
loss = nn.PoissonNLLLoss() log_input = torch.randn(5, 2, requires_grad=True) target = torch.randn(5, 2) output = loss(log_input, target) output.backward()
10、GaussianNLLLoss() 高斯负对数似然损失
目标作为神经网络预测的具有期望和方差的高斯分布样本。
loss = nn.GaussianNLLLoss() input = torch.randn(5, 2, requires_grad=True) target = torch.randn(5, 2) var = torch.ones(5, 2, requires_grad=True) #heteroscedastic output = loss(input, target, var) output.backward()
11、KLDivLoss() Kullback-Leibler散度损失测度
Kullback-Leibler散度是一个有用的连续分布的距离度量,通常在(离散采样)连续输出分布空间上执行直接回归时很有用。计算input和target之间的KL散度。用于描述两个概率分布之间的差异
kl_loss = nn.KLDivLoss(reduction="batchmean") input = F.log_softmax(torch.randn(3, 5, requires_grad=True)) target = F.softmax(torch.rand(3, 5)) output = kl_loss(input, target)
12、MSELoss() 均方损失函数
创建一个标准来测量输入a和目标y中每个元素之间的平均平方误差(L2平方范数)。
MSELoss()多用于回归问题,也可以用于one_hotted编码形式
loss = nn.MSELoss() input = torch.randn(3, 5, requires_grad=True) target = torch.randn(3, 5) output = loss(input, target) output.backward()
13、BCELoss()
创建一个衡量目标和输入概率之间的二元交叉熵
m = nn.Sigmoid() loss = nn.BCELoss() input = torch.randn(3, requires_grad=True) target = torch.empty(3).random_(2) output = loss(m(input), target) output.backward()
14.BCEWithLogitsLoss
结合了一个'Sigmoid'层和'BCELoss'在一个单一的类。这个版本比使用普通的Sigmoid后跟BCELoss在数值上更稳定,因为通过将操作合并到一个层中,我们利用了log-sum-exp技巧来实现数值稳定性。