损失函数笔记

损失函数(loss)

简介

  • 损失函数(loss function)或代价函数(cost function)是将随机事件或其有关随机变量的取值映射为非负实数以表示该随机事件的“风险”或“损失”的函数。
  • 在应用中,损失函数通常作为学习准则与优化问题相联系,即通过最小化损失函数求解和评估模型。例如在统计学和机器学习中被用于模型的参数估计(parametric estimation),在宏观经济学中被用于风险管理(risk mangement)和决策 ,在控制理论中被应用于最优控制理论(optimal control theory)
    在这里插入图片描述

常用loss

BCELoss

用于二分类任务,二值交叉熵(Binary Cross Entropy)。

公式如下,其中y是真实值, y ^ \hat{y} y^是预测值:

loss = -ylog\ y ^ \hat{y} y^ - (1-y)log(1- y ^ \hat{y} y^ )


class torch.nn.BCELoss
 
 
Examples::
 
        >>> m = nn.Sigmoid()
        >>> loss = nn.BCELoss()
        >>> input = torch.randn(3, requires_grad=True)  # 从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的3个随机数
        >>> target = torch.empty(3).random_(2)  # 生成3个值,值为0 或者 1
        >>> output = loss(m(input), target)  # m(input)激活,生成概率p,区间为(0, 1)
        >>> output.backward()

CELoss

用于多分类任务,交叉熵(Cross Entropy)。

公式如下,其中y是真实值, y ^ \hat{y} y^是预测值:

loss = -ylog y ^ \hat{y} y^

class torch.nn.CrossEntropyLoss
 
 
It is useful when training a classification problem with `C` classes.
The `input` is expected to contain scores for each class.
 
Examples::
 
        >>> loss = nn.CrossEntropyLoss()
        >>> input = torch.randn(3, 5, requires_grad=True)  # 35,每一行代码当前数据特征
        >>> target = torch.empty(3, dtype=torch.long).random_(5)  # 3个数,值范围是0,1...4
        >>> output = loss(input, target)
        >>> output.backward()

支持多目标分割、分类

 def forward(self, predictive, target):
        """
        :param predictive:NC)or (N,C,H,W)
        :param target:N,)or (N,C,H,W,C)
        :return:
        """
        c = predictive.size()[1]
        if len(predictive.shape) == 4:  # 语义分割任务  # [b,c,h,w]-> [b,h,w,c] -> [b*h*w, c]
            predictive = predictive.transpose(1, 2).transpose(2, 3).contiguous().view(-1, c)
            target = torch.argmax(target, dim=1)  # 输入是one hot,但是loss不支持  [b,c,h,w]
        else:  # 分类任务
            predictive = predictive.view(-1, c)  # [b*h*w, c]
        target = target.view(-1).long()  # [b*h*w, c]
        return F.cross_entropy(predictive, target, weight=self.weight, reduction=self.reduction)

MSELoss

计算均方误差 Mean Squared Error (squared L2 Norm)。

公式如下,其中y是真实值, y ^ \hat{y} y^是预测值:

在这里插入图片描述

class torch.nn.MSELoss
Creates a criterion that measures the mean squared error (squared L2 norm) between
    each element in the input `x` and target `y`.
Examples::
        >>> loss = nn.MSELoss()
        >>> input = torch.randn(3, 5, requires_grad=True)
        >>> target = torch.randn(3, 5)
        >>> output = loss(input, target)
        >>> output.backward()

Binary Cross Entropy (BCE) 损失函数是一种广泛应用于二分类问题中的损失函数,其核心思想在于衡量预测概率与真实标签之间的差异。以下是关于 BCE 损失函数的一些学术背景及其相关研究: ### Binary Cross Entropy Loss Function Overview Binary Cross Entropy (BCE) 是一种基于信息论的损失函数,通常用于评估模型在二分类任务上的表现。它的定义如下[^3]: \[ \text{BCE} = -\frac{1}{N}\sum_{i=1}^{N}[y_i \log(p_i) + (1-y_i)\log(1-p_i)] \] 其中 \( y_i \) 表示样本的真实标签(取值为 0 或 1),\( p_i \) 则表示模型对于第 i 个样本属于正类的概率。 #### 关键特性 - **凸性**:BCE 函数是一个凸函数,在优化过程中有助于收敛到全局最优解。 - **敏感度**:它对错误分类的结果非常敏感,尤其是当预测概率接近零或一的时候。 ### 相关学术论文推荐 1. **Goodfellow et al., "Deep Learning"** 这本书提供了有关交叉熵损失的基础理论以及如何将其应用到神经网络训练中的详细介绍。书中讨论了为什么交叉熵适合处理分类问题,并解释了其背后的数学原理[^4]。 2. **Hinton G.E., Srivastava N., Swersky K., “Neural Networks for Machine Learning” Lecture Notes** Geoffrey Hinton 的课程笔记深入探讨了各种类型的损失函数,包括 binary cross entropy 和其他变体形式。这些资料特别强调了不同损失函数的选择依据及其实际应用场景[^5]。 3. **Zhang C., Bengio S., Hardt M.et al., Understanding Deep Learning Requires Rethinking Generalization** 虽然这篇论文主要关注深度学习泛化能力的研究,但它也提到了使用诸如 BCE 等标准作为目标函数的重要性,尤其是在复杂数据集上的实验分析部分[^6]。 4. **Chollet F., Xception: Deep Convolutional Neural Networks Based on Inception** François Chollet 提出了一个新的卷积架构——Xception 并在其工作中多次提到利用 binary cross entropy 来指导模型参数调整过程,从而实现更优性能的表现评价指标体系构建方法之一就是通过比较各类误差项大小关系来进行决策制定流程设计思路分享给大家参考借鉴一下吧! ```python import torch.nn as nn criterion = nn.BCELoss() output = torch.tensor([0.9, 0.1]) # Example output probabilities target = torch.tensor([1.0, 0.0]) # Corresponding true labels loss_value = criterion(output, target) print(f'Calculated BCE Loss Value: {loss_value.item()}') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值