AI and Deep Learning Community Question

Can I still use custom training loops while using the PyTorch Lightning Trainer?

Posted on 2025-09-18 • In AI and Deep Learning • By Donna King

Sometimes I need to do something very non-standard, like GAN training or meta-learning, where the standard fit() method might be too restrictive. Does PyTorch Lightning allow for manual optimization where I control the backward() and step() calls myself? I love the logging and hardware management of Lightning, but I don't want to be "boxed in" by the Trainer's automation.

3 Answers 15,704 Views

All answers to this question 3 Answers

Betty Nelson Answered on 2025-09-19
Answered

Yes, you can definitely do this! You just need to set 'self.automatic_optimization = False' in your LightningModule's init. Then, you manually call 'self.manual_backward(loss)' and 'opt.step()' inside your training_step. This gives you total control over the optimization process while still letting the Trainer handle things like multi-GPU logic and progress bars. I used this recently for a research paper on reinforcement learning, and it worked perfectly without any issues. It’s the best of both worlds.

Paul Rodriguez Answered on 2025-09-20
Answered

When using manual optimization, do we still get the benefits of the built-in loggers, or do we have to manually log everything to TensorBoard?

Donna King Commented on 2025-09-21
Commented

You still get to use self.log()! The only thing that changes is that the Trainer doesn't handle the optimizer steps. All the logging, checkpointing, and profiling features remain fully functional, which is why it's so powerful.

Brian Green Answered on 2025-09-22
Answered

This feature is why I chose Lightning over other high-level wrappers. It doesn't treat you like a beginner; it trusts you.

Betty Nelson Commented on 2025-09-23
Commented

Spot on, Brian. The flexibility to opt-out of the automation is what makes it a professional tool rather than just a toy.

Write a Comment

Your email address will not be published. Required fields are marked (*)

Join the LearnersEra Community

Ask questions, share knowledge and learn from professionals across technology, project management and certification topics.

Change Country and Nearest City

Select your country and nearest city to see courses available in your region.