mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-09-11 12:31:23 +08:00
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:
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user