Part 2: Autograd and Automatic Differentiation
The Night I Understood Gradients
What is Automatic Differentiation?
Basic Autograd
Simple Example
Multiple Operations
Computational Graphs
Leaf vs Non-Leaf Tensors
Gradient Accumulation
Controlling Gradient Computation
Detach from Graph
No Grad Context
Inference Mode
Gradient for Non-Scalar Outputs
Real Example: Custom Loss Function
Higher-Order Gradients
Custom Autograd Functions
Gradient Checking
Common Patterns
Training Loop with Autograd
Gradient Clipping
Autograd Profiler
Best Practices
Common Issues
Issue 1: "RuntimeError: Trying to backward through the graph a second time"
Issue 2: "RuntimeError: element 0 of tensors does not require grad"
Issue 3: Gradients are None
What's Next?
PreviousPart 1: Introduction to PyTorch and TensorsNextPart 3: Building Neural Networks with torch.nn
Last updated