Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/source-pytorch/common/optimization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ since Lightning automates that for you.
Under the hood, Lightning does the following:

.. code-block:: python
:emphasize-lines: 4-8,10,12

for epoch in epochs:
for batch in data:
Expand Down Expand Up @@ -130,7 +131,8 @@ configured in your :meth:`~lightning.pytorch.core.LightningModule.configure_opti
You can access your own optimizer with ``optimizer.optimizer``. However, if you use your own optimizer
to perform a step, Lightning won't be able to support accelerators, precision and profiling for you.

.. testcode:: python
.. code-block:: python
:emphasize-lines: 9,20-21

# function hook in LightningModule
def optimizer_step(
Expand Down Expand Up @@ -168,6 +170,7 @@ relying on a different API from Native PyTorch ones, you should override the :me
If you are using native PyTorch schedulers, there is no need to override this hook since Lightning will handle it automatically by default.

.. code-block:: python
:emphasize-lines: 5-6,10-11

from timm.scheduler import TanhLRScheduler

Expand Down Expand Up @@ -202,7 +205,8 @@ the arguments along with your optimizer.

For example, here we will apply a stronger gradient clipping after a certain number of epochs:

.. testcode:: python
.. code-block:: python
:emphasize-lines: 2,6

def configure_gradient_clipping(self, optimizer, gradient_clip_val, gradient_clip_algorithm):
if self.current_epoch > 5:
Expand All @@ -221,6 +225,7 @@ distributed setting into consideration so you don't have to derive it manually.
:class:`~torch.optim.lr_scheduler.OneCycleLR` scheduler, which requires pre-computed ``total_steps`` during initialization.

.. code-block:: python
:emphasize-lines: 4

def configure_optimizers(self):
optimizer = ...
Expand Down
Loading