Multiple optimizers but only one loss #5813
-
Hey! I have a question regarding this library. Really, like how it forces me to structure my code better. I encountered one problem I did not know how to solve based on the documentation. Let's say I have two optimizers for two parts of the network, e.g. my
now in the training loop I forward pass the encoder, then the decoder and compute my loss based on the output:
Since I have two optimizers I have to respect that this function is called two times with different What have you tried?I tried something like this
However, this leads to an error since no loss key is present in |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
in that case, just pass in both sets of params to a single optimizer |
Beta Was this translation helpful? Give feedback.
-
But I explicitly want two different learning rates for different parts of the network. That is not really possible with a single optimizer AFAIK. One possibility could be to scale gradients on the weights for which I want lower learning rate before running the optimizer but that is really not a a clean solution. |
Beta Was this translation helpful? Give feedback.
-
It is possible with parameter groups using a single optimizer. Your use-case is actually the example in the docs: https://pytorch.org/docs/stable/optim.html#per-parameter-options |
Beta Was this translation helpful? Give feedback.
-
That’s nice. Thank you for hint! |
Beta Was this translation helpful? Give feedback.
-
Is it possible #14728? |
Beta Was this translation helpful? Give feedback.
in that case, just pass in both sets of params to a single optimizer