You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source-pytorch/model/manual_optimization.rst
+33-8
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,10 @@ Manual Optimization
3
3
*******************
4
4
5
5
For advanced research topics like reinforcement learning, sparse coding, or GAN research, it may be desirable to
6
-
manually manage the optimization process.
6
+
manually manage the optimization process, especially when dealing with multiple optimizers at the same time.
7
7
8
-
This is only recommended for experts who need ultimate flexibility.
9
-
Lightning will handle only accelerator, precision and strategy logic.
10
-
The users are left with ``optimizer.zero_grad()``, gradient accumulation, model toggling, etc..
8
+
In this mode, Lightning will handle only accelerator, precision and strategy logic.
9
+
The users are left with ``optimizer.zero_grad()``, gradient accumulation, optimizer toggling, etc..
11
10
12
11
To manually optimize, do the following:
13
12
@@ -18,6 +17,7 @@ To manually optimize, do the following:
18
17
* ``optimizer.zero_grad()`` to clear the gradients from the previous training step
19
18
* ``self.manual_backward(loss)`` instead of ``loss.backward()``
20
19
* ``optimizer.step()`` to update your model parameters
20
+
* ``self.toggle_optimizer()`` and ``self.untoggle_optimizer()`` if needed
21
21
22
22
Here is a minimal example of manual optimization.
23
23
@@ -39,10 +39,6 @@ Here is a minimal example of manual optimization.
39
39
self.manual_backward(loss)
40
40
opt.step()
41
41
42
-
.. warning::
43
-
Before 1.2, ``optimizer.step()`` was calling ``optimizer.zero_grad()`` internally.
44
-
From 1.2, it is left to the user's expertise.
45
-
46
42
.. tip::
47
43
Be careful where you call ``optimizer.zero_grad()``, or your model won't converge.
48
44
It is good practice to call ``optimizer.zero_grad()`` before ``self.manual_backward(loss)``.
@@ -132,6 +128,7 @@ To perform gradient clipping with one optimizer with manual optimization, you ca
132
128
.. warning::
133
129
* Note that ``configure_gradient_clipping()`` won't be called in Manual Optimization. Instead consider using ``self. clip_gradients()`` manually like in the example above.
134
130
131
+
135
132
Use Multiple Optimizers (like GANs)
136
133
===================================
137
134
@@ -285,6 +282,34 @@ If you want to call schedulers that require a metric value after each epoch, con
285
282
if isinstance(sch, torch.optim.lr_scheduler.ReduceLROnPlateau):
286
283
sch.step(self.trainer.callback_metrics["loss"])
287
284
285
+
286
+
Optimizer Steps at Different Frequencies
287
+
========================================
288
+
289
+
In manual optimization, you are free to ``step()`` one optimizer more often than another one.
290
+
For example, here we step the optimizer for the *discriminator* weights twice as often as the optimizer for the *generator*.
291
+
292
+
.. testcode:: python
293
+
294
+
# Alternating schedule for optimizer steps (e.g. GANs)
0 commit comments