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
- Making Deep Learning go Brrrr From First Principleshorace.io
- Customize checkpointing behavior (intermediate) - PyTorch Lightning 2.6.1 documentationlightning.ai
- ⚡ Migrating from PTL - Composerdocs.mosaicml.com
- Get Started with Distributed Training using PyTorch Lightning — Ray 2.56.0docs.ray.io
- Saving and loading checkpoints (basic) - PyTorch Lightning 2.6.1 documentationlightning.ai
- ModelCheckpoint - PyTorch Lightning 2.6.1 documentationlightning.ai
- An overview of gradient descent optimization algorithmsruder.io
- [D] Modern best coding practices for Pytorch (for research)? : r/MachineLearningreddit.com
- GitHub - google-research/tuning_playbook: A playbook for systematically maximizing the performance of deep learning models.github.com
- PyTorch in One Hour: From Tensors to Training Neural Networks on Multiple GPUssebastianraschka.com