Back to skills
SKILL.md
Pytorch 组合损失函数 Ssim L1 L2
ASecurity实现一个用于图像复原任务的组合损失函数,包含结构相似性(SSIM)、平均绝对误差(L1)和均方误差(L2)的加权和,并配置对应的Adam优化器和ReduceLROnPlateau学习率调度器。
- 2 stars
- 0 votes
- 0 copies
- 0 views
- Added September 27, 2026
Security analysis
100/100npx -y skills add David-Li0406/meta-skill-evloving --skill 'pytorch-组合损失函数-ssim-l1-l2' --agent claude-codeAre you the author of Pytorch 组合损失函数 Ssim L1 L2?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/david-li0406-pytorch-ssim-l1-l2)---
id: "766354a3-a661-4a35-bcf7-5e9756d9a39b"
name: "PyTorch 组合损失函数 (SSIM + L1 + L2)"
description: "实现一个用于图像复原任务的组合损失函数,包含结构相似性(SSIM)、平均绝对误差(L1)和均方误差(L2)的加权和,并配置对应的Adam优化器和ReduceLROnPlateau学习率调度器。"
version: "0.1.0"
tags:
- "pytorch"
- "loss-function"
- "ssim"
- "image-restoration"
- "res-unet"
triggers:
- "构建SSIM L1 L2组合损失函数"
- "实现Res-UNET的损失函数"
- "combined loss function with SSIM"
- "加权SSIM L1 L2损失"
---
# 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损失
Attribution
Comments
Loading comments…