Lightning-AI/pytorch-lightning · error · AttributeError

The attribute name for the learning rate was set to {attr_na

Error message

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`.

What it means

The LR finder was given attr_name=<name> via tuner.lr_find(model, attr_name=...), but that attribute exists neither on the LightningModule itself nor in model.hparams. The finder needs to know which field holds the learning rate so it can update it during the search.

Source

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

# check if ipywidgets is installed before importing tqdm.auto
# to ensure it won't fail and a progress bar is displayed
if importlib.util.find_spec("ipywidgets") is not None:
    from tqdm.auto import tqdm
else:
    from tqdm import tqdm

if TYPE_CHECKING:
    import matplotlib.pyplot as plt
    from matplotlib.axes import Axes

_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:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Fix the attr_name to match the actual attribute or hparams key (e.g. "lr" or "learning_rate")
  2. Expose the LR as model.hparams.lr or as a plain self.lr attribute
  3. Omit attr_name so the finder auto-detects lr/learning_rate

Example fix

# before
tuner.lr_find(model, attr_name="lr_rate")  # model uses `lr`
# after
tuner.lr_find(model)  # auto-detects `lr`
Defensive patterns

Strategy: type-guard

Validate before calling

from lightning.pytorch.utilities.model_helpers import lightning_hasattr
if not lightning_hasattr(model, attr_name):
    raise ValueError(f"{attr_name!r} not a model field or hparams key")
tuner.lr_find(model, attr_name=attr_name)

Type guard

def attr_exists(model, name: str) -> bool:
    from lightning.pytorch.utilities.model_helpers import lightning_hasattr
    return lightning_hasattr(model, name)

Prevention

When it happens

Trigger: tuner.lr_find(model, attr_name="lr_rate") where the model defines no such attribute or hparams key (typo or renamed field).

Common situations: Renaming the learning-rate field in the model (e.g. lr -> base_lr) without updating attr_name, or misspelling it.

Related errors


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