Lightning-AI/pytorch-lightning · error · MisconfigurationException

Cannot use `LearningRateMonitor` callback with `Trainer` tha

Error message

Cannot use `LearningRateMonitor` callback with `Trainer` that has no logger.

What it means

LearningRateMonitor writes learning rates (and optionally momentum/weight decay) to the Trainer's logger(s). If `trainer.loggers` is empty, `on_train_start` raises MisconfigurationException — there is nowhere to log to.

Source

Thrown at src/lightning/pytorch/callbacks/lr_monitor.py:128

        self.log_weight_decay = log_weight_decay
        self.log_key_prefix = log_key_prefix or ""

        self.lrs: dict[str, list[float]] = {}
        self.last_momentum_values: dict[str, Optional[list[float]]] = {}
        self.last_weight_decay_values: dict[str, Optional[list[float]]] = {}

    @override
    def on_train_start(self, trainer: "pl.Trainer", *args: Any, **kwargs: Any) -> None:
        """Called before training, determines unique names for all lr schedulers in the case of multiple of the same
        type or in the case of multiple parameter groups.

        Raises:
            MisconfigurationException:
                If ``Trainer`` has no ``logger``.

        """
        if not trainer.loggers:
            raise MisconfigurationException(
                "Cannot use `LearningRateMonitor` callback with `Trainer` that has no logger."
            )

        if self.log_momentum:

            def _check_no_key(key: str) -> bool:
                if trainer.lr_scheduler_configs:
                    return any(
                        key not in config.scheduler.optimizer.defaults for config in trainer.lr_scheduler_configs
                    )

                return any(key not in optimizer.defaults for optimizer in trainer.optimizers)

            if _check_no_key("momentum") and _check_no_key("betas"):
                rank_zero_warn(
                    "You have set log_momentum=True, but some optimizers do not"
                    " have momentum. This will log a value 0 for the momentum.",
                    category=RuntimeWarning,

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Attach a logger: `Trainer(logger=CSVLogger('logs'))` (or TensorBoardLogger/WandbLogger)
  2. Or remove LearningRateMonitor from callbacks when running with logger=False
  3. If you conditionally disable loggers, filter the callback list the same way

Example fix

# before
Trainer(logger=False, callbacks=[LearningRateMonitor()])
# after
Trainer(logger=CSVLogger('logs'), callbacks=[LearningRateMonitor()])
Defensive patterns

Strategy: validation

Validate before calling

if not trainer.loggers:
    callbacks = [c for c in callbacks if not isinstance(c, LearningRateMonitor)]
# or ensure a logger:
# trainer = Trainer(logger=CSVLogger('logs'), ...)

Type guard

def lr_monitor_ok(trainer) -> bool:
    return bool(trainer.loggers)

Prevention

When it happens

Trigger: `Trainer(logger=False, callbacks=[LearningRateMonitor()])` or a logger list that resolves empty. Fails at the start of training.

Common situations: Disabling logging for a debug run but leaving the monitor callback in the list; CSV/TensorBoard logger path misconfigured so loggers ends up empty; conditional logger setup in a script.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/2448f6577ae59cfd. Report an issue: GitHub.