Deep Learning with PyTorch
Build neural networks with PyTorch - tensors, custom datasets, training loops, transfer learning with pretrained models, and GPU acceleration.
skill: Deep Learning with PyTorch category: AI & ML tags: [PyTorch, Neural Networks, Deep Learning, GPU Training, Transfer Learning] readingTime: 7
PyTorch is the de facto standard for deep learning research and production. Its define-by-run (eager execution) model makes debugging natural - you can print tensors, set breakpoints, and use standard Python tools. For most practical image/text tasks, fine-tuning a pretrained model outperforms training from scratch by a wide margin.
Workflow
- Understand tensors and autogradPyTorch tensors are multi-dimensional arrays with GPU support. Autograd automatically computes gradients for any tensor computation - the foundation of neural network training.
- Build a neural network with nn.Modulenn.Module is the base class for all PyTorch models. It tracks parameters, enables GPU movement, and integrates with optimizers and checkpoint saving.
- Create custom Datasets and DataLoadersDataLoader handles batching, shuffling, and parallel data loading. Custom Dataset wraps any data source and applies per-sample transforms.
- Write the training loopPyTorch training loops are explicit - zero gradients, forward pass, compute loss, backward pass, update weights. Explicit means debuggable and customizable.
- Fine-tune pretrained modelsImageNet pretrained ResNet/EfficientNet features transfer to almost any image task. Fine-tuning requires orders of magnitude less data than training from scratch.
Output Format
PyTorch: device = cuda/cpu, nn.Module with Sequential, custom Dataset + DataLoader with transforms, explicit 5-step training loop, and ResNet50 fine-tuning with layer-specific LRs.
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Model(nn.Module): def forward(self, x): return self.net(x)
DataLoader(dataset, batch_size=32, shuffle=True, num_workers=4)
zero_grad → forward → loss → backward → clip_grad → step
model.fc = nn.Linear(in_features, num_classes)Examples
Edge Cases & Gotchas
- GPU memory OOM - Reduce batch size, use gradient accumulation (accumulate N batches before step), or use torch.cuda.amp for mixed precision (FP16 on GPU). torch.cuda.empty_cache() clears cached memory but doesn't solve fundamental OOM.
- Reproducibility - Set seeds for reproducibility: torch.manual_seed(42); np.random.seed(42); torch.backends.cudnn.deterministic = True. Note: deterministic mode is slower.
- DataLoader num_workers on Windows - num_workers > 0 on Windows requires if __name__ == "__main__": guard. On macOS with MPS, start with num_workers=0 and increase if stable.
- Gradient accumulation - To simulate larger batch size: loss = loss / accumulation_steps; loss.backward(). Only call optimizer.step() and zero_grad() every accumulation_steps iterations.
When to Break These Rules
- Use Keras/TensorFlow for production ML pipelines requiring TFX, TF Serving, or TF Lite - the TF ecosystem for production deployment is more mature than PyTorch serving.
- Use Hugging Face Transformers (which wraps PyTorch) instead of raw PyTorch for NLP tasks - the pretrained model ecosystem is far richer than building from scratch.
Reference Files
https://pytorch.org/tutorials/https://pytorch.org/docs/stable/