Lightning-AI/pytorch-lightning · error · AttributeError

When using the learning rate finder, either `model` or `mode

Error message

When using the learning rate finder, either `model` or `model.hparams` should have one of these fields: {attr_options}. If your model has a different name for the learning rate, set it with `.lr_find(attr_name=...)`.

What it means

The LR finder could not auto-detect a learning-rate field: it looks for an attribute named lr or learning_rate on the model or in model.hparams, and found neither. Without knowing where the LR lives it cannot run the search, so it raises AttributeError with instructions to specify attr_name.

Source

Thrown at src/lightning/pytorch/tuner/lr_finder.py:62

_MATPLOTLIB_AVAILABLE = RequirementCache("matplotlib")
log = logging.getLogger(__name__)


def _determine_lr_attr_name(model: "pl.LightningModule", attr_name: str = "") -> str:
    if attr_name:
        if not lightning_hasattr(model, attr_name):
            raise AttributeError(
                f"The attribute name for the learning rate was set to {attr_name}, but"
                " could not find this as a field in `model` or `model.hparams`."
            )
        return attr_name

    attr_options = ("lr", "learning_rate")
    for attr in attr_options:
        if lightning_hasattr(model, attr):
            return attr

    raise AttributeError(
        "When using the learning rate finder, either `model` or `model.hparams` should"
        f" have one of these fields: {attr_options}. If your model has a different name for the learning rate, set"
        f" it with `.lr_find(attr_name=...)`."
    )


class _LRFinder:
    """LR finder object. This object stores the results of lr_find().

    Args:
        mode: either `linear` or `exponential`, how to increase lr after each step
        lr_min: lr to start search from
        lr_max: lr to stop search
        num_training: number of steps to take between lr_min and lr_max

    """

    def __init__(self, mode: str, lr_min: float, lr_max: float, num_training: int) -> None:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Add an lr or learning_rate attribute to the model (self.lr = 0.001 or save_hyperparameters including it)
  2. Pass the actual name: tuner.lr_find(model, attr_name="base_lr")
  3. If LR is hardcoded in configure_optimizers, hoist it into self.lr and reference it there

Example fix

# before
class LitModel(pl.LightningModule):
    def __init__(self, base_lr=1e-3): ...
    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=1e-3)
# after
class LitModel(pl.LightningModule):
    def __init__(self, base_lr=1e-3):
        super().__init__()
        self.base_lr = base_lr
    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=self.base_lr)
# then: tuner.lr_find(model, attr_name="base_lr")
Defensive patterns

Strategy: type-guard

Validate before calling

from lightning.pytorch.utilities.model_helpers import lightning_hasattr
if not any(lightning_hasattr(model, a) for a in ("lr", "learning_rate")):
    raise ValueError("model needs an `lr`/`learning_rate` field or attr_name must be passed")
tuner.lr_find(model)

Type guard

def lr_field_known(model) -> bool:
    from lightning.pytorch.utilities.model_helpers import lightning_hasattr
    return any(lightning_hasattr(model, a) for a in ("lr", "learning_rate"))

Prevention

When it happens

Trigger: tuner.lr_find(model) where the model stores the learning rate under a different name (e.g. self.base_lr) or only inside the optimizer created in configure_optimizers.

Common situations: Custom models that name the LR field differently (lr init, init_lr, base_lr) or that hardcode the value inside configure_optimizers without any model-level field.

Related errors


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