pytorch-patterns

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

Updated Mar 18, 2026
One-click install
npx skills add https://github.com/freedom909/real-estate-saas --skill pytorch-patterns-freedom909
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: pytorch-patterns
Source: https://github.com/freedom909/real-estate-saas/tree/main/.trae/skills/pytorch-patterns
Command: npx skills add https://github.com/freedom909/real-estate-saas --skill pytorch-patterns-freedom909

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing PyTorch code that is device-agnostic, reproducible, and memory-efficient requires knowing many idioms and avoiding subtle bugs like forgetting model.eval(), breaking autograd with in-place operations, or losing training state in incomplete checkpoints. ## Core Features & Use Cases - Training Loop Patterns: Standard training and 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. - Checkpointing & Optimization: Complete checkpoint save/load with optimizer state, gradient checkpointing for large models, and torch.compile for faster execution. - Use Case: When writing a new image classifier training script, apply these patterns to set random seeds, configure an efficient DataLoader, run mixed-precision training, and save resumable checkpoints. ## Quick Start Ask the AI to write a reproducible PyTorch training loop with mixed precision and checkpointing for your model.

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 PyTorch training loop with mixed precision?▼

Use torch.amp.GradScaler with torch.amp.autocast to run forward passes in mixed precision, then scale the loss before backward and step the optimizer through the scaler. This typically doubles training speed on compatible GPUs while reducing memory usage.

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, though it may slightly reduce training speed.

Why does my PyTorch validation accuracy fluctuate between runs?▼

This usually happens when model.eval() is not called before validation, leaving dropout active and BatchNorm using batch statistics. Always call model.eval() and wrap inference in torch.no_grad() before evaluating.

Does PyTorch DataLoader support parallel data loading?▼

Yes, set num_workers greater than zero to load batches in parallel processes. Combine with pin_memory=True for faster CPU-to-GPU transfer and persistent_workers=True to keep workers alive between epochs.

How do I resume PyTorch training from a checkpoint?▼

Save a dictionary containing epoch, model_state_dict, optimizer_state_dict, and loss with torch.save. To resume, load with weights_only=True, restore both state dicts, and continue from the saved epoch.

When should I use gradient checkpointing in PyTorch?▼

Use torch.utils.checkpoint when training large models that exceed GPU memory, as it recomputes activations during backward instead of storing them. It trades roughly 30% more compute for significant memory savings.