当前位置: 首页 > ai >正文

PyTorch学习笔记 - 损失函数

文章目录

  • 1. 内置损失函数
  • 2. 继承 nn.Module 自定义损失函数
  • 3. 继承 autograd.Function 自定义损失函数
  • 3. 三种不同方式实现 MSE 实验

PyTorch 除了内置损失函数,还可以自定义损失函数。我们以均方误差为例来讲解 PyTorch 中损失函数的使用方法。均方误差(Mean Squared Error, MSE)是预测值 x = ( x 1 , x 2 , . . . , x n ) x=(x_1, x_2, ..., x_n) x=(x1,x2,...,xn) 与真实值 y = ( y 1 , y 2 , . . . , y n ) y=(y_1, y_2, ..., y_n) y=(y1,y2,...,yn) 之差的平方和的平均值,数学公式如下:
MSE = 1 n ∑ i = 1 n ( x i − y i ) 2 \text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (x_i - y_i)^2 MSE=n1i=1n(xiyi)2计算 MSE \text{MSE} MSE 损失函数对输入向量 x x x 的梯度如下:
d M S E d x = 2 n ( x − y ) \dfrac{dMSE}{dx} = \dfrac{2}{n}(x-y) dxdMSE=n2(xy)具体而言,
d M S E d x i = 2 n ( x i − y i ) \dfrac{dMSE}{dx_i} = \dfrac{2}{n}(x_i - y_i) dxidMSE=n2(xiyi)

1. 内置损失函数

PyTorch 在 torch.nn 模块中提供了均方误差函数:

import torch.nn as nnmse_loss = nn.MSELoss()

2. 继承 nn.Module 自定义损失函数

只需实现 forward() 方法,无需手动编写反向传播(自动求导引擎处理)。自定义损失函数类实例化后直接调用即可计算损失值。
继承 nn.Module 自定义均方误差损失函数的实现代码如下:

import torch.nn as nnclass MSELossV1(nn.Module):def __init__(self):super().__init__()def forward(self, input, target):squared_diff = (input - target) ** 2n = squared_diff.numel()return squared_diff.sum() / n

3. 继承 autograd.Function 自定义损失函数

在 PyTorch 中,torch.autograd.Function 是一个用于定义自定义自动求导操作的类。它允许用户实现自定义的前向传播forward 和反向传播 backward 逻辑。这对于实现非标准操作、自定义激活函数、或在某些特殊场景中替代现有 PyTorch 操作非常有用。
torch.autograd.Function 实现自定义求导,需要实现 forwardbackward 方法,这意味着需要自己手算反向传播求梯度公式。
ctx 是上下文对象,用于在 forward 和 backward 之间传递数据。常用方法是:

  • ctx.save_for_backward(*tensors):保存张量供反向传播使用
  • ctx.saved_tensors:获取保存的张量

forward 方法返回计算结果,而 backward 返回对每个输入的梯度。
Function.apply(input) 是调用自定义函数的标准方式。继承 autograd.Function 自定义均方误差损失函数的实现代码如下:

import torch
from torch.autograd import Functionclass MSELossV2(Function):@staticmethoddef forward(ctx, input, target):squared_diff = (input - target) ** 2n = squared_diff.numel()ctx.save_for_backward(input, target)return squared_diff.sum() / n@staticmethoddef backward(ctx, grad_output):input, target = ctx.saved_tensorsn = input.numel()grad_input = 2 / n * (input - target) * grad_outputreturn grad_input, None

在 PyTorch 的 torch.autograd.Function 中,backward 方法的返回值数量和顺序必须与 forward 方法的输入参数一一对应。例如,forward 传入的参数为 input 和 target,则 backward 也要返回两个梯度(例如 grad_input, None)。
每个输入参数都需要对应一个梯度输出:

  • 如果输入参数是张量且需要梯度(requires_grad=True),返回其梯度
  • 如果输入参数是整数或不需要梯度的张量,返回 None

backward 中的 grad_output 是一个张量,其形状与当前操作的输出张量一致。它表示在反向传播时,每个输出元素的梯度乘以一个
权重(即 grad_output 的值),从而影响输入梯度的计算。

  • 如果 grad_output 未指定(默认为 None),PyTorch 会假设输出是一个标量,并自动使用全 1 的权重,即 torch.ones_like(output)
  • 如果输出是向量或张量,则必须显式指定 grad_output,否则会报错

grad_output 的使用总结如下:

场景grad_output 的作用示例
标量输出默认为 1,无需显式指定loss.backward()
向量输出必须指定,形状与输出一致y.backward(torch.ones_like(y))
多输出每个输出对应一个 grad_outputgrad_output=[v1, v2]
自定义反向传播传递上层梯度,计算输入梯度backward(ctx, grad_output)

代码示例:

import torchx = torch.tensor([2.0], requires_grad=True)
у = x**2
у.backward()   # 等价于 y.backward(torch.tensor(1.0))
print(x.grad)  # 输出 4.0 (dy/dx = 2x = 4)x2 = torch.tensor([1.0, 2.0], requires_grad=True)
y = x2 * 2
grad_output = torch.tensor([1.0, 0.5])  # 权重分别为 1 和 0.5
y.backward(grad_output)  # x2_grad = tensor([2., 1.]) (grad_output · dy/dx = [1.0, 0.5] · [2., 2.] = [2., 1.])

3. 三种不同方式实现 MSE 实验

实验代码如下:

import torch
import torch.nn as nn
from torch.autograd import Functionclass MSELossV1(nn.Module):def __init__(self):super().__init__()def forward(self, input, target):squared_diff = (input - target) ** 2n = squared_diff.numel()return squared_diff.sum() / nclass MSELossV2(Function):@staticmethoddef forward(ctx, input, target):squared_diff = (input - target) ** 2n = squared_diff.numel()ctx.save_for_backward(input, target)return squared_diff.sum() / n@staticmethoddef backward(ctx, grad_output):input, target = ctx.saved_tensorsn = input.numel()grad_input = 2 / n * (input - target) * grad_outputreturn grad_input, Noneif __name__ == "__main__":mse_loss = nn.MSELoss()mse_loss_v2 = MSELossV1()x = torch.tensor([[1.0, 2.0, 3.0],[4.0, 5.0, 6.0],[7.0, 8.0, 9.0]], requires_grad=True)x2 = x.detach().clone().requires_grad_(True)x3 = x.detach().clone().requires_grad_(True)y = torch.tensor([[0.5, 2.5, 2.0],[3.5, 5.5, 5.0],[6.5, 8.5, 8.0]])loss = mse_loss(x, y)loss2 = mse_loss_v2(x2, y)loss3 = MSELossV2.apply(x3, y)print(f"loss: {loss}, loss2: {loss2}, loss3: {loss3}")loss.backward()loss2.backward()loss3.backward()print(f"x.grad: \n{x.grad}\n x2.grad: \n{x2.grad}\n x3.grad: \n{x3.grad}")

运行结果如下:
在这里插入图片描述
从图中可以看出,三种不同方式实现的均方误差损失函数的计算结果一致。

http://www.xdnf.cn/news/11800.html

相关文章:

  • unix/linux,sudo,其历史争议、兼容性、生态、未来展望
  • 如何有效删除 iPhone 上的所有内容?
  • 激光干涉仪:解锁协作机器人DD马达的精度密码
  • 前端工具库lodash与lodash-es区别详解
  • ES海量数据更新及导入导出备份
  • 设计模式之单例模式(二): 心得体会
  • UE 5 和simulink联合仿真,如果先在UE5这一端结束Play,过一段时间以后**Unreal Engine 5** 中会出现显存不足错误
  • 功能测试、性能测试、安全测试详解
  • 近端策略优化(PPO,Proximal Policy Optimization)
  • vue实现点击按钮input保持聚焦状态
  • Oracle实用参考(13)——Oracle for Linux静默安装(1)
  • springboot 微服务 根据tomcat maxthread 和 等待用户数量,达到阈值后,通知用户前面还有多少用户等待,请稍后重试
  • 低代码采购系统搭建:鲸采云+能源行业订单管理自动化案例
  • Electron打包前端和后端为exe
  • el-table 树形数据,子行数据可以异步加载
  • 破解HTTP无状态:基于Java的Session与Cookie协同工作指南
  • 618浴室柜推荐,小户型浴室柜怎么选才省心?
  • 江科大睡眠,停止,待机模式hal库实现
  • MySQL范式和反范式
  • Windows安装docker desktop
  • 【使用JAVA调用deepseek】实现自能回复
  • Devops自动化运维---py基础篇一
  • Appium如何支持ios真机测试
  • CppCon 2014 学习:Mixins for C++
  • 基于行为分析的下一代安全防御指南
  • webPack基本使用步骤
  • Cocos creator游戏开发面试题
  • Windows+Linux安装redis教程
  • Qt 中,设置事件过滤器(Event Filter)的方式
  • Java面试专项一-准备篇