2020-05-05 02:16:54 +00:00
|
|
|
.. testsetup:: *
|
|
|
|
|
|
|
|
from pytorch_lightning.trainer.trainer import Trainer
|
|
|
|
from pytorch_lightning.core.lightning import LightningModule
|
2021-01-07 05:24:47 +00:00
|
|
|
|
2020-08-13 22:56:51 +00:00
|
|
|
.. _lr_finder:
|
2020-05-05 02:16:54 +00:00
|
|
|
|
2020-04-10 18:34:23 +00:00
|
|
|
Learning Rate Finder
|
|
|
|
--------------------
|
|
|
|
|
2020-10-08 09:49:56 +00:00
|
|
|
.. raw:: html
|
|
|
|
|
2020-10-08 19:54:52 +00:00
|
|
|
<video width="50%" max-width="400px" controls
|
2020-10-08 09:49:56 +00:00
|
|
|
poster="https://pl-bolts-doc-images.s3.us-east-2.amazonaws.com/pl_docs/trainer_flags/thumb/auto_lr_find.jpg"
|
|
|
|
src="https://pl-bolts-doc-images.s3.us-east-2.amazonaws.com/pl_docs/trainer_flags/auto_lr_find.mp4"></video>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-10 18:34:23 +00:00
|
|
|
For training deep neural networks, selecting a good learning rate is essential
|
|
|
|
for both better performance and faster convergence. Even optimizers such as
|
|
|
|
`Adam` that are self-adjusting the learning rate can benefit from more optimal
|
|
|
|
choices.
|
|
|
|
|
|
|
|
To reduce the amount of guesswork concerning choosing a good initial learning
|
2021-01-07 05:24:47 +00:00
|
|
|
rate, a `learning rate finder` can be used. As described in this `paper <https://arxiv.org/abs/1506.01186>`_
|
|
|
|
a learning rate finder does a small run where the learning rate is increased
|
|
|
|
after each processed batch and the corresponding loss is logged. The result of
|
2020-05-07 13:25:54 +00:00
|
|
|
this is a `lr` vs. `loss` plot that can be used as guidance for choosing a optimal
|
2021-01-07 05:24:47 +00:00
|
|
|
initial lr.
|
2020-04-10 18:34:23 +00:00
|
|
|
|
2021-01-07 05:24:47 +00:00
|
|
|
.. warning::
|
|
|
|
For the moment, this feature only works with models having a single optimizer.
|
2020-09-29 12:28:14 +00:00
|
|
|
LR Finder support for DDP is not implemented yet, it is coming soon.
|
2020-04-10 18:34:23 +00:00
|
|
|
|
2020-06-19 06:38:10 +00:00
|
|
|
----------
|
|
|
|
|
2020-05-17 14:59:46 +00:00
|
|
|
Using Lightning's built-in LR finder
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2020-04-10 18:34:23 +00:00
|
|
|
|
2020-10-13 10:42:33 +00:00
|
|
|
To enable the learning rate finder, your :ref:`lightning_module` needs to have a ``learning_rate`` or ``lr`` property.
|
2020-10-06 02:28:38 +00:00
|
|
|
Then, set ``Trainer(auto_lr_find=True)`` during trainer construction,
|
|
|
|
and then call ``trainer.tune(model)`` to run the LR finder. The suggested ``learning_rate``
|
2020-10-13 10:42:33 +00:00
|
|
|
will be written to the console and will be automatically set to your :ref:`lightning_module`,
|
2020-10-06 02:28:38 +00:00
|
|
|
which can be accessed via ``self.learning_rate`` or ``self.lr``.
|
2020-05-24 22:59:08 +00:00
|
|
|
|
2020-10-06 02:28:38 +00:00
|
|
|
.. code-block:: python
|
2020-04-10 18:34:23 +00:00
|
|
|
|
|
|
|
class LitModel(LightningModule):
|
2020-05-05 02:16:54 +00:00
|
|
|
|
2020-05-24 22:59:08 +00:00
|
|
|
def __init__(self, learning_rate):
|
|
|
|
self.learning_rate = learning_rate
|
2020-04-10 18:34:23 +00:00
|
|
|
|
|
|
|
def configure_optimizers(self):
|
2020-05-24 22:59:08 +00:00
|
|
|
return Adam(self.parameters(), lr=(self.lr or self.learning_rate))
|
2021-01-07 05:24:47 +00:00
|
|
|
|
2020-10-06 02:28:38 +00:00
|
|
|
model = LitModel()
|
2020-04-10 18:34:23 +00:00
|
|
|
|
|
|
|
# finds learning rate automatically
|
|
|
|
# sets hparams.lr or hparams.learning_rate to that learning rate
|
2020-05-05 02:16:54 +00:00
|
|
|
trainer = Trainer(auto_lr_find=True)
|
2020-04-10 18:34:23 +00:00
|
|
|
|
2020-10-06 02:28:38 +00:00
|
|
|
trainer.tune(model)
|
|
|
|
|
2020-10-07 18:25:52 +00:00
|
|
|
If your model is using an arbitrary value instead of ``self.lr`` or ``self.learning_rate``, set that value as ``auto_lr_find``:
|
2020-04-10 18:34:23 +00:00
|
|
|
|
2020-10-06 02:28:38 +00:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
model = LitModel()
|
2020-04-10 18:34:23 +00:00
|
|
|
|
|
|
|
# to set to your own hparams.my_value
|
2020-05-05 02:16:54 +00:00
|
|
|
trainer = Trainer(auto_lr_find='my_value')
|
2020-04-10 18:34:23 +00:00
|
|
|
|
2020-10-06 02:28:38 +00:00
|
|
|
trainer.tune(model)
|
2020-04-10 18:34:23 +00:00
|
|
|
|
|
|
|
|
2020-10-06 02:28:38 +00:00
|
|
|
If you want to inspect the results of the learning rate finder or just play around
|
|
|
|
with the parameters of the algorithm, this can be done by invoking the ``lr_find``
|
|
|
|
method of the trainer. A typical example of this would look like
|
2020-04-10 18:34:23 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
model = MyModelClass(hparams)
|
2020-05-05 02:16:54 +00:00
|
|
|
trainer = Trainer()
|
2021-01-07 05:24:47 +00:00
|
|
|
|
2020-04-10 18:34:23 +00:00
|
|
|
# Run learning rate finder
|
2020-09-10 02:12:27 +00:00
|
|
|
lr_finder = trainer.tuner.lr_find(model)
|
2021-01-07 05:24:47 +00:00
|
|
|
|
2020-04-10 18:34:23 +00:00
|
|
|
# Results can be found in
|
|
|
|
lr_finder.results
|
2021-01-07 05:24:47 +00:00
|
|
|
|
2020-04-10 18:34:23 +00:00
|
|
|
# Plot with
|
|
|
|
fig = lr_finder.plot(suggest=True)
|
|
|
|
fig.show()
|
2021-01-07 05:24:47 +00:00
|
|
|
|
2020-04-10 18:34:23 +00:00
|
|
|
# Pick point based on plot, or get suggestion
|
|
|
|
new_lr = lr_finder.suggestion()
|
2021-01-07 05:24:47 +00:00
|
|
|
|
2020-04-10 18:34:23 +00:00
|
|
|
# update hparams of the model
|
|
|
|
model.hparams.lr = new_lr
|
2020-05-24 22:59:08 +00:00
|
|
|
|
2020-04-10 18:34:23 +00:00
|
|
|
# Fit model
|
|
|
|
trainer.fit(model)
|
2021-01-07 05:24:47 +00:00
|
|
|
|
2020-04-10 18:34:23 +00:00
|
|
|
The figure produced by ``lr_finder.plot()`` should look something like the figure
|
2020-12-08 21:27:43 +00:00
|
|
|
below. It is recommended to not pick the learning rate that achieves the lowest
|
2020-04-10 18:34:23 +00:00
|
|
|
loss, but instead something in the middle of the sharpest downward slope (red point).
|
|
|
|
This is the point returned py ``lr_finder.suggestion()``.
|
|
|
|
|
|
|
|
.. figure:: /_images/trainer/lr_finder.png
|
|
|
|
|
|
|
|
The parameters of the algorithm can be seen below.
|
|
|
|
|
2020-09-10 02:12:27 +00:00
|
|
|
.. autofunction:: pytorch_lightning.tuner.lr_finder.lr_find
|
2020-04-10 18:34:23 +00:00
|
|
|
:noindex:
|