pytorch-patterns

Implements idiomatic PyTorch patterns for training loops, data pipelines, and model architectures.

1|Updated Oct 11, 2025
One-click install
npx skills add https://github.com/ibytechaos/claude --skill pytorch-patterns-ibytechaos
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytorch-patterns
Source: https://github.com/ibytechaos/claude/tree/main/plugins/everything-claude-code/skills/pytorch-patterns
Command: npx skills add https://github.com/ibytechaos/claude --skill pytorch-patterns-ibytechaos

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Deep learning code often suffers from hidden bugs like device mismatches, non-reproducible results, broken autograd graphs, and slow data loading. This Skill provides battle-tested PyTorch patterns that prevent these common mistakes when writing models, training loops, and data pipelines. ## Core Features & Use Cases - Training Loop Patterns: Complete train/validation loops with mixed precision (torch.amp), gradient clipping, and proper train/eval mode handling. - Data Pipeline Patterns: Custom Dataset classes, optimized DataLoader configuration (num_workers, pin_memory, persistent_workers), and collate functions for variable-length sequences. - Model & Checkpoint Patterns: Clean nn.Module structure, weight initialization, gradient checkpointing, torch.compile, and full checkpoint save/load for resuming training. - Use Case: When writing a new image classifier training script, apply these patterns to get device-agnostic code, reproducible seeds, AMP-accelerated training, and resumable checkpoints from the start. ## Quick Start Ask the AI to write a PyTorch training loop for an image classifier following pytorch-patterns best practices with mixed precision and checkpointing.

Frequently Asked Questions about pytorch-patterns

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

FAQPage Schema
How do I write a proper PyTorch training loop?

Set model.train(), move data to the device, call optimizer.zero_grad(set_to_none=True), compute loss, run backward(), optionally clip gradients, then step the optimizer. Wrap forward passes in torch.amp.autocast for mixed precision speedups.

How to make PyTorch experiments reproducible?

Set seeds for torch, torch.cuda, numpy, and random, then enable torch.backends.cudnn.deterministic and disable cudnn.benchmark. This ensures identical results across runs on the same hardware.

Does PyTorch DataLoader support parallel data loading?

Yes, set num_workers greater than zero to load batches in background processes. Combine with pin_memory=True for faster CPU-to-GPU transfer and persistent_workers=True to avoid worker respawn between epochs.

Why does my PyTorch validation give inconsistent results?

The most common cause is forgetting model.eval(), which leaves dropout active and BatchNorm using batch statistics. Always call model.eval() and wrap validation in torch.no_grad() before running inference.

How do I reduce GPU memory usage in PyTorch training?

Use gradient checkpointing via torch.utils.checkpoint to recompute activations during backward, enable mixed precision with torch.amp, and reduce batch size. Check usage with torch.cuda.memory_summary().

Should I save the whole PyTorch model or state_dict?

Save the state_dict, not the full model object. Saving the entire model is fragile and not portable across code changes, while state_dict checkpoints can include optimizer state and epoch for resuming training.