Communitygithub.com

PyTorch 模式指南

构建健壮、高效且可复现的 PyTorch 训练流程、模型架构和数据加载的深度学习方法与最佳实践。

PyTorch 模式指南 是什么?

该技能(Skill)是一份详尽的 PyTorch 开发模式与最佳实践指南,专门为需要使用 PyTorch 构建深度学习应用的开发者设计。它涵盖了从核心原则(如与设备无关的代码、可复现性和显式形状管理)到模型架构、训练循环、数据加载、检查点保存以及性能优化(混合精度训练、梯度检查点、torch.compile)等各个方面的典型 Python 代码示例。该技能适用于多种 AI 编码代理(如 Claude Code, Cursor, Codex),当用户编写或审查 PyTorch 代码,尤其是在处理训练循环、模型定义或调试时,建议激活此技能。其文档以清晰的 Markdown 格式呈现,包含最佳实践和常见反模式,有助于提升代码质量、运行效率和可复现性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/ECC/tree/main/skills/pytorch-patterns

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

PyTorch 开发模式

用于构建健壮、高效且可复现的深度学习应用程序的惯用 PyTorch 模式和最佳实践。

何时激活

  • 编写新的 PyTorch 模型或训练脚本
  • 审查深度学习代码
  • 调试训练循环或数据管道
  • 优化 GPU 内存使用或训练速度
  • 设置可复现的实验

核心原则

1. 与设备无关的代码

始终编写能在 CPU 和 GPU 上运行且不硬编码设备的代码。

# 推荐:与设备无关
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = MyModel().to(device)
data = data.to(device)

# 不推荐:硬编码设备
model = MyModel().cuda()  # 如果没有 GPU 会崩溃
data = data.cuda()

2. 可复现性优先

设置所有随机种子以获得可复现的结果。

# 推荐:完整的可复现性设置
def set_seed(seed: int = 42) -> None:
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

# 不推荐:无种子控制
model = MyModel()  # 每次运行权重都不同

3. 显式形状管理

始终记录和验证张量形状。

# 推荐:带形状注释的前向传播
def forward(self, x: torch.Tensor) -> torch.Tensor:
    # x: (batch_size, channels, height, width)
    x = self.conv1(x)    # -> (batch_size, 32, H, W)
    x = self.pool(x)     # -> (batch_size, 32, H//2, W//2)
    x = x.view(x.size(0), -1)  # -> (batch_size, 32*H//2*W//2)
    return self.fc(x)    # -> (batch_size, num_classes)

# 不推荐:无形状跟踪
def forward(self, x):
    x = self.conv1(x)
    x = self.pool(x)
    x = x.view(x.size(0), -1)  # 这到底是什么尺寸?
    return self.fc(x)           # 这能正常工作吗?

模型架构模式

清晰的 nn.Module 结构

# 推荐:组织良好的模块
class ImageClassifier(nn.Module):
    def __init__(self, num_classes: int, dropout: float = 0.5) -> None:
        super().__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2),
        )
        self.classifier = nn.Sequential(
            nn.Dropout(dropout),
            nn.Linear(64 * 16 * 16, num_classes),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.features(x)
        x = x.view(x.size(0), -1)
        return self.classifier(x)

# 不推荐:所有事情都在 forward 中
class ImageClassifier(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x):
        x = F.conv2d(x, weight=self.make_weight())  # 每次调用都创建权重!
        return x

正确的权重初始化

# 推荐:显式初始化
def _init_weights(self, module: nn.Module) -> None:
    if isinstance(module, nn.Linear):
        nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
        if module.bias is not None:
            nn.init.zeros_(module.bias)
    elif isinstance(module, nn.Conv2d):
        nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
    elif isinstance(module, nn.BatchNorm2d):
        nn.init.ones_(module.weight)
        nn.init.zeros_(module.bias)

model = MyModel()
model.apply(model._init_weights)

训练循环模式

标准训练循环

# 推荐:包含最佳实践的完整训练循环
def train_one_epoch(
    model: nn.Module,
    dataloader: DataLoader,
    optimizer: torch.optim.Optimizer,
    criterion: nn.Module,
    device: torch.device,
    scaler: torch.amp.GradScaler | None = None,
) -> float:
    model.train()  # 始终设置训练模式
    total_loss = 0.0

    for batch_idx, (data, target) in enumerate(dataloader):
        data, target = data.to(device), target.to(device)

        optimizer.zero_grad(set_to_none=True)  # 比 zero_grad() 更高效

        # 混合精度训练
        with torch.amp.autocast("cuda", enabled=scaler is not None):
            output = model(data)
            loss = criterion(output, target)

        if scaler is not None:
            scaler.scale(loss).backward()
            scaler.unscale_(optimizer)
            torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
            scaler.step(optimizer)
            scaler.update()
        else:
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
            optimizer.step()

        total_loss += loss.item()

    return total_loss / len(dataloader)

验证循环

# 推荐:正确评估
@torch.no_grad()  # 比包装在 torch.no_grad() 块中更高效
def evaluate(
    model: nn.Module,
    dataloader: DataLoader,
    criterion: nn.Module,
    device: torch.device,
) -> tuple[float, float]:
    model.eval()  # 始终设置评估模式——禁用 dropout,使用运行中的 BN 统计信息
    total_loss = 0.0
    correct = 0
    total = 0

    for data, target in dataloader:
        data, target = data.to(device), target.to(device)
        output = model(data)
        total_loss += criterion(output, target).item()
        correct += (output.argmax(1) == target).sum().item()
        total += target.size(0)

    return total_loss / len(dataloader), correct / total

数据管道模式

自定义数据集

# 推荐:带类型提示的清晰数据集
class ImageDataset(Dataset):
    def __init__(
        self,
        image_dir: str,
        labels: dict[str, int],
        transform: transforms.Compose | None = None,
    ) -> None:
        self.image_paths = list(Path(image_dir).glob("*.jpg"))
        self.labels = labels
        self.transform = transform

    def __len__(self) -> int:
        return len(self.image_paths)

    def __getitem__(self, idx: int) -> tuple[torch.Tensor, int]:
        img = Image.open(self.image_paths[idx]).convert("RGB")
        label = self.labels[self.image_paths[idx].stem]

        if self.transform:
            img = self.transform(img)

        return img, label

高效的数据加载器配置

# 推荐:优化后的 DataLoader
dataloader = DataLoader(
    dataset,
    batch_size=32,
    shuffle=True,            # 训练时打乱
    num_workers=4,           # 并行数据加载
    pin_memory=True,         # 更快的 CPU->GPU 传输
    persistent_workers=True, # 在 epoch 之间保持工作进程存活
    drop_last=True,          # 为 BatchNorm 保持一致的批次大小
)

# 不推荐:缓慢的默认设置
dataloader = DataLoader(dataset, batch_size=32)  # num_workers=0, no pin_memory

变长数据的自定义 Collate

# 推荐:在 collate_fn 中填充序列
def collate_fn(batch: list[tuple[torch.Tensor, int]]) -> tuple[torch.Tensor, torch.Tensor]:
    sequences, labels = zip(*batch)
    # 填充到批次中的最大长度
    padded = nn.utils.rnn.pad_sequence(sequences, batch_first=True, padding_value=0)
    return padded, torch.tensor(labels)

dataloader = DataLoader(dataset, batch_size=32, collate_fn=collate_fn)

检查点模式

保存和加载检查点

# 推荐:包含所有训练状态的完整检查点
def save_checkpoint(
    model: nn.Module,
    optimizer: torch.optim.Optimizer,
    epoch: int,
    loss: float,
    path: str,
) -> None:
    torch.save({
        "epoch": epoch,
        "model_state_dict": model.state_dict(),
        "optimizer_state_dict": optimizer.state_dict(),
        "loss": loss,
    }, path)

def load_checkpoint(
    path: str,
    model: nn.Module,
    optimizer: torch.optim.Optimizer | None = None,
) -> dict:
    checkpoint = torch.load(path, map_location="cpu", weights_only=True)
    model.load_state_dict(checkpoint["model_state_dict"])
    if optimizer:
        optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
    return checkpoint

# 不推荐:只保存模型权重(无法恢复训练)
torch.save(model.state_dict(), "model.pt")

性能优化

混合精度训练

# 推荐:带 GradScaler 的 AMP
scaler = torch.amp.GradScaler("cuda")
for data, target in dataloader:
    with torch.amp.autocast("cuda"):
        output = model(data)
        loss = criterion(output, target)
    scaler.scale(loss).backward()
    scaler.step(optimizer)
    scaler.update()
    optimizer.zero_grad(set_to_none=True)

大型模型的梯度检查点

# 推荐:用计算换内存
from torch.utils.checkpoint import checkpoint

class LargeModel(nn.Module):
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # 在反向传播期间重新计算激活以节省内存
        x = checkpoint(self.block1, x, use_reentrant=False)
        x = checkpoint(self.block2, x, use_reentrant=False)
        return self.head(x)

用于加速的 torch.compile

# 推荐:编译模型以加快执行速度(PyTorch 2.0+)
model = MyModel().to(device)
model = torch.compile(model, mode="reduce-overhead")

# 模式:"default" (安全), "reduce-overhead" (更快), "max-autotune" (最快)

快速参考:PyTorch 惯用法

惯用法描述
model.train() / model.eval()在训练/评估前始终设置模式
torch.no_grad()推理时禁用梯度
optimizer.zero_grad(set_to_none=True)更高效的梯度清零
.to(device)与设备无关的张量/模型放置
torch.amp.autocast混合精度,速度提升 2 倍
pin_memory=True更快的 CPU→GPU 数据传输
torch.compile用于加速的 JIT 编译 (2.0+)
weights_only=True安全模型加载
torch.manual_seed可复现的实验
gradient_checkpointing用计算换内存

应避免的反模式

# 不推荐:验证时忘记 model.eval()
model.train()
with torch.no_grad():
    output = model(val_data)  # Dropout 仍处于活动状态!BatchNorm 使用批次统计信息!

# 推荐:始终设置评估模式
model.eval()
with torch.no_grad():
    output = model(val_data)

# 不推荐:破坏自动求导的原地操作
x = F.relu(x, inplace=True)  # 可能破坏梯度计算
x += residual                  # 原地加法破坏自动求导图

# 推荐:非原地操作
x = F.relu(x)
x = x + residual

# 不推荐:在训练循环中重复将数据移动到 GPU
for data, target in dataloader:
    model = model.cuda()  # 每次迭代都移动模型!

# 推荐:在循环前将模型移动一次
model = model.to(device)
for data, target in dataloader:
    data, target = data.to(device), target.to(device)

# 不推荐:在 backward 之前使用 .item()
loss = criterion(output, target).item()  # 从图中分离!
loss.backward()  # 错误:无法通过 .item() 反向传播

# 推荐:仅用于日志记录时调用 .item()
loss = criterion(output, target)
loss.backward()
print(f"Loss: {loss.item():.4f}")  # 在 backward 之后使用 .item() 没问题

# 不推荐:不正确使用 torch.save
torch.save(model, "model.pt")  # 保存整个模型(脆弱,不可移植)

# 推荐:保存 state_dict
torch.save(model.state_dict(), "model.pt")

记住:PyTorch 代码应与设备无关、可复现且注意内存使用。如有疑问,请使用 torch.profiler 进行分析,并使用 torch.cuda.memory_summary() 检查 GPU 内存。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/content-engine

Create platform-native content systems for X, LinkedIn, TikTok, YouTube, newsletters, and repurposed multi-platform campaigns. Use when the user wants social posts, threads, scripts, content calendars, or one source asset adapted cleanly across platforms.

affaan-m/ECC

Multi-platform content distribution across X, LinkedIn, Threads, and Bluesky. Adapts content per platform using content-engine patterns. Never posts identical content cross-platform. Use when the user wants to distribute content across social platforms.

affaan-m/fal-ai-media

Unified media generation via fal.ai MCP — image, video, and audio. Covers text-to-image (Nano Banana), text/image-to-video (Seedance, Kling, Veo 3), text-to-speech (CSM-1B), and video-to-audio (ThinkSound). Use when the user wants to generate images, videos, or audio with AI.

affaan-m/manim-video

日本語翻訳:このファイルは manim-video 用の日本語翻訳が必要です

affaan-m/remotion-video-creation

Remotion のベストプラクティス - React で動画を作成する。3D、アニメーション、音声、字幕、チャート、トランジションなどをカバーするドメイン固有の29のルール。

affaan-m/video-editing

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

agent-self-evaluation

Use after completing any non-trivial task. The agent self-rates its output on 5 axes — accuracy, completeness, clarity, actionability, conciseness — with concrete evidence per criterion. Produces a structured 1-5 scorecard with specific improvement suggestions.

agent-sort

Build an evidence-backed ECC install plan for a specific repo by sorting skills, commands, rules, hooks, and extras into DAILY vs LIBRARY buckets using parallel repo-aware review passes. Use when ECC should be trimmed to what a project actually needs instead of loading the full bundle.

ai-first-engineering

Engineering operating model for teams where AI agents generate a large share of implementation output. Use when setting team process, review gates, or ownership rules for a codebase largely written by agents.

ai-regression-testing

Regression testing strategies for AI-assisted development. Sandbox-mode API testing without database dependencies, automated bug-check workflows, and patterns to catch AI blind spots where the same model writes and reviews code. Use when adding regression coverage to AI-assisted code, or when the same model both wrote and reviewed a change.

android-clean-architecture

Clean Architecture patterns for Android and Kotlin Multiplatform projects — module structure, dependency rules, UseCases, Repositories, and data layer patterns. Use when structuring modules, layers, or data flow in an Android or KMP project.

angular-developer

Generates Angular code and provides architectural guidance. Trigger when creating projects, components, or services, or for best practices on reactivity (signals, linkedSignal, resource), forms, dependency injection, routing, SSR, accessibility (ARIA), animations, styling (component styles, Tailwind CSS), testing, or CLI tooling.

相关技能