pytorch-patterns

Standardize PyTorch training patterns for device placement, reproducibility, and checkpointing.

1|Updated Mar 3, 2026
One-click install
npx skills add https://github.com/samymity/bridge-ventures-backend --skill pytorch-patterns-samymity
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytorch-patterns
Source: https://github.com/samymity/bridge-ventures-backend/tree/main/.claude/skills/pytorch-patterns
Command: npx skills add https://github.com/samymity/bridge-ventures-backend --skill pytorch-patterns-samymity

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It helps developers avoid common PyTorch training pitfalls by providing reliable patterns for device handling, reproducibility, tensor shape clarity, data loading, checkpointing, and performance optimization.

Core Features & Use Cases

  • Device-agnostic training: Moves models and tensors correctly to CPU/GPU without hardcoded .cuda() calls.
  • Reproducible experiments: Centralizes seeding across Python, NumPy, and PyTorch and addresses CuDNN determinism.
  • Robust training/validation loops: Uses model.train(), model.eval(), torch.no_grad(), gradient clipping, and AMP safely.
  • Clean architecture and initialization: Encourages well-structured nn.Module components and explicit weight initialization.
  • Efficient input pipelines: Provides dataset patterns, optimized DataLoader configuration, and custom collate_fn for variable-length data.
  • Checkpointing and performance: Saves full training state, resumes reliably, and applies torch.compile / gradient checkpointing patterns.

Use case example: You are implementing an image classification model and need a training pipeline that is reproducible across runs, avoids GPU memory regressions, validates correctly with BatchNorm/Dropout behavior, and supports resuming from checkpoints after interruptions.

Quick Start

Use the pytorch-patterns skill to generate a device-agnostic, reproducible training loop and matching validation routine for your PyTorch model and DataLoader.

Frequently Asked Questions about pytorch-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I make PyTorch training reproducible across runs?

To make PyTorch training reproducible, you must centralize seeding across Python, NumPy, and PyTorch while addressing CuDNN determinism to ensure consistent experimental results across runs.

What is the best way to handle device placement in PyTorch without hardcoding .cuda()?

The best way to handle device placement is using device-agnostic training patterns that dynamically move models and tensors to CPU or GPU, avoiding brittle hardcoded `.cuda()` calls in your code.

How do I safely use mixed-precision training and gradient clipping in PyTorch?

To safely use mixed-precision training and gradient clipping, apply robust training loop patterns that integrate AMP and clip gradients properly while maintaining correct train and eval mode handling.

Does PyTorch checkpointing save the full training state for resuming?

Yes, proper PyTorch checkpointing saves the full training state, including model architecture, optimizer details, and epoch progress, allowing you to resume training reliably after interruptions.

Why does my validation loss behave incorrectly with BatchNorm and Dropout?

Validation loss behaves incorrectly when you fail to toggle `model.eval()` and use `torch.no_grad()`, which properly freezes BatchNorm statistics and disables Dropout during model evaluation.

How do I configure PyTorch DataLoader for variable-length data?

To configure a PyTorch DataLoader for variable-length data, implement a custom `collate_fn` to pad and batch sequences correctly, ensuring efficient input pipelines without shape errors.