agentsclimarketplace

Pytorch 组合损失函数 ssim l1 l2

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/pytorch-组合损失函数-ssim-l1-l2

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

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill pytorch-组合损失函数-ssim-l1-l2

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

2.4 KB, 654 tokens by cl100k_base, as published. Nobody here has run it

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 方法中,接收 predictionstargets
    • 计算 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损失

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.