Using multiple optimizers without multiple calls to training_step, in order not to slow down the training #10714
Unanswered
amansouri3476
asked this question in
Lightning Trainer API: Trainer, LightningModule, LightningDataModule
Replies: 1 comment
-
I went deeper in the docs and found that when you have multiple optimizers, here’s what happens under the hood:
So even another pseudocode for what I want under the hood, is the following:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, thanks for your great project!
I have a question about using multiple optimizers. I'll share a minimal example below.
Let's say I have 3 modules: F, Z, M
I want to update the parameters for M upon every batch that comes into the training loop. I want to update Z every 5 batches, and F every 10 batches. I have assigned a 'name' key to each optimizer, and I store these intervals in correspondence with these names. In optimizer step, if the batch_idx is a multiple of 5 for instance, then optimizer_step is invoked to update the parameters of Z. Here's the code:
I have followed the style of examples here.
When there are multiple optimizers, why is there a need for training step to be run with different optimizer_idx? Or put better, what should I do if I want the
training_step
to run 'just once', get that loss, and do optimizer_step based on batch_idx. A pseudocode would be like this:It seems using optimizer_closure, would invoke
training_step
again, for the next optimizer. So if you have 4 optimizers, it's liketraining_step
is called 4 times, which slows down the program, and is not one would want. Ideally you'd want one call totraining_step
and here inoptimizer_step
, each optimizer should update the parameters on its turn.Can I get that behaviour if I remove
closure=optimizer_closure
from optimizer.step argument?I'm not sure if I was clear or not, so I'll put another psuedocode of what I have in mind for things to happen under the hood:
Many thanks for your support.
Beta Was this translation helpful? Give feedback.
All reactions