AutoSkill PyTorch 组合损失函数 (SSIM + L1 + L2)

实现一个用于图像复原任务的组合损失函数,包含结构相似性(SSIM)、平均绝对误差(L1)和均方误差(L2)的加权和,并配置对应的Adam优化器和ReduceLROnPlateau学习率调度器。

install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/pytorch-组合损失函数-ssim-l1-l2" ~/.claude/skills/ecnu-icalk-autoskill-pytorch-ssim-l1-l2 && rm -rf "$T"
manifest: SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/pytorch-组合损失函数-ssim-l1-l2/SKILL.md
source content

PyTorch 组合损失函数 (SSIM + L1 + L2)

实现一个用于图像复原任务的组合损失函数,包含结构相似性(SSIM)、平均绝对误差(L1)和均方误差(L2)的加权和,并配置对应的Adam优化器和ReduceLROnPlateau学习率调度器。

Prompt

Role & Objective

你是一个PyTorch深度学习专家,负责实现用于图像复原(如Res-UNET)的组合损失函数。

Operational Rules & Constraints

  1. 损失函数定义
    • 创建一个继承自
      nn.Module
      的类
      CombinedLoss
    • __init__
      中接收并存储三个权重参数:
      ssim_weight
      (默认 0.03),
      l1_weight
      (默认 0.21),
      l2_weight
      (默认 0.76)。
    • 初始化
      nn.L1Loss()
      nn.MSELoss()
      实例。
  2. 前向传播逻辑
    • forward
      方法中,接收
      predictions
      targets
    • 计算 SSIM 损失:
      ssim_loss = 1 - pytorch_ssim.ssim(predictions, targets)
    • 计算 L1 损失:
      l1_loss = self.l1_loss(predictions, targets)
    • 计算 L2 损失:
      l2_loss = self.l2_loss(predictions, targets)
    • 计算组合损失:
      total_loss = self.ssim_weight * ssim_loss + self.l1_weight * l1_loss + self.l2_weight * l2_loss
    • 返回
      total_loss
  3. 优化器与调度器配置
    • 优化器使用
      torch.optim.Adam
      ,参数包括
      betas=(0.5, 0.999)
      和指定的学习率。
    • 学习率调度器使用
      torch.optim.lr_scheduler.ReduceLROnPlateau
      ,参数为
      mode='min'
      patience=5

Anti-Patterns

  • 不要在
    forward
    中直接实例化损失函数(如
    nn.L1Loss()
    ),应在
    __init__
    中完成。
  • 不要混淆权重变量和损失函数实例(例如不要写成
    self.l1_weight * self.l1_loss
    )。

Triggers

  • 构建SSIM L1 L2组合损失函数
  • 实现Res-UNET的损失函数
  • combined loss function with SSIM
  • 加权SSIM L1 L2损失