Optimization — PyTorch Lightning 2.4.0 documentation
For the majority of research cases, automatic optimization will do the right thing for you and it is what most users should use. For more advanced use cases like multiple optimizers, esoteric optimization schedules or techniques, use manual optimization. For advanced research topics like reinforcement learning, sparse coding, or GAN research, it may be desirable to manually manage the optimization process, especially when dealing with multiple optimizers at the same time. In this mode, Lightning will handle only accelerator, precision and strategy logic. The users are left with optimizer.zero_grad(), gradient accumulation, optimizer toggling, etc.. To manually optimize, do the following: Set self.automatic_optimization=False in your LightningModule’s __init__. Use the following functions and call them manually: self.optimizers() to access your optimizers (one or multiple) optimizer.zero_grad() to clear the gradients from the previous training step self.manual_backward(loss) instead of
Optimization ¶ Lightning offers two modes for managing the optimization process: Manual Optimization Automatic Optimization For the majority of research cases, automatic optimization will do the right thing for you and it is what most users should use. For more advanced use cases like multiple optimizers, esoteric optimization schedules or techniques, use manual optimization . Manual Optimization ¶ For advanced research topics like reinforcement learning, sparse coding, or GAN research, it may be desirable to manually manage the optimization process, especially when dealing with multiple optim
related reading
- LightningModule - PyTorch Lightning 2.6.1 documentationlightning.ai
- Trainer - PyTorch Lightning 2.6.1 documentationlightning.ai
- The Little Book of Deep Learningfleuret.org
- Learning PyTorch with Examples — PyTorch Tutorials 2.12.0+cu130 documentationpytorch.org
- PyTorch internals : ezyang's blogblog.ezyang.com
- Adampytorch.org
- Transformer Math 101 | EleutherAI Blogblog.eleuther.ai
- ♻️ Auto Microbatching - Composerdocs.mosaicml.com
- the bug that taught me more about PyTorch than years of using it | Elana Simonelanapearl.github.io
- Ji-Ha's Blogjiha-kim.github.io
- Keller Jordan (@kellerjordan0) on Xx.com
- GitHub - google-research/tuning_playbook: A playbook for systematically maximizing the performance of deep learning models.github.com