Skip to content
Back to skills

Pytorch 组合损失函数 Ssim L1 L2

ASecurity

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

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 27, 2026
ai-agents

Security analysis

A100/100

Scanned September 27, 2026

npx -y skills add David-Li0406/meta-skill-evloving --skill 'pytorch-组合损失函数-ssim-l1-l2' --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Pytorch 组合损失函数 Ssim L1 L2?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Pytorch 组合损失函数 Ssim L1 L2
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/david-li0406-pytorch-ssim-l1-l2/badge)](https://www.skillsdirectory.com/skills/david-li0406-pytorch-ssim-l1-l2)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…