LearningRateLogger in multi-scheduler setting (#1944)

* fixed undesired behaviour due to dict.fromkeys

* a test for log length consistency

* runtime-warn if no schedulers are configured

* chlog

* move

Co-authored-by: Jirka <jirka@pytorchlightning.ai>
This commit is contained in:
Ivan Nazarov
2020-05-27 22:44:46 -04:00
committed by GitHub
co-authored by Jirka
parent 3af4994d5a
commit 7c19c373ac
4 changed files with 118 additions and 84 deletions
+10 -7
View File
@@ -10,6 +10,8 @@ Log learning rate for lr schedulers during training
from pytorch_lightning.callbacks.base import Callback
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities import rank_zero_warn
class LearningRateLogger(Callback):
r"""
@@ -45,21 +47,22 @@ class LearningRateLogger(Callback):
schedulers in the case of multiple of the same type or in
the case of multiple parameter groups
"""
if trainer.lr_schedulers == []:
raise MisconfigurationException(
'Cannot use LearningRateLogger callback with models that have no'
' learning rate schedulers. Please see documentation for'
' `configure_optimizers` method.')
if not trainer.logger:
raise MisconfigurationException(
'Cannot use LearningRateLogger callback with Trainer that has no logger.')
if not trainer.lr_schedulers:
rank_zero_warn(
'You are using LearningRateLogger callback with models that'
' have no learning rate schedulers. Please see documentation'
' for `configure_optimizers` method.', RuntimeWarning
)
# Find names for schedulers
names = self._find_names(trainer.lr_schedulers)
# Initialize for storing values
self.lrs = dict.fromkeys(names, [])
self.lrs = {name: [] for name in names}
def on_batch_start(self, trainer, pl_module):
latest_stat = self._extract_lr(trainer, 'step')