{"record":{"id":"2d62b7df2744bb33","repo":"Lightning-AI/pytorch-lightning","slug":"when-using-the-learning-rate-finder-either-model","errorCode":null,"errorMessage":"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=...)`.","messagePattern":"When using the learning rate finder, either `model` or `model\\.hparams` should have one of these fields: (.+?)\\. If your model has a different name for the learning rate, set it with `\\.lr_find\\(attr_name=\\.\\.\\.\\)`\\.","errorType":"validation","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/tuner/lr_finder.py","lineNumber":62,"sourceCode":"_MATPLOTLIB_AVAILABLE = RequirementCache(\"matplotlib\")\nlog = logging.getLogger(__name__)\n\n\ndef _determine_lr_attr_name(model: \"pl.LightningModule\", attr_name: str = \"\") -> str:\n    if attr_name:\n        if not lightning_hasattr(model, attr_name):\n            raise AttributeError(\n                f\"The attribute name for the learning rate was set to {attr_name}, but\"\n                \" could not find this as a field in `model` or `model.hparams`.\"\n            )\n        return attr_name\n\n    attr_options = (\"lr\", \"learning_rate\")\n    for attr in attr_options:\n        if lightning_hasattr(model, attr):\n            return attr\n\n    raise AttributeError(\n        \"When using the learning rate finder, either `model` or `model.hparams` should\"\n        f\" have one of these fields: {attr_options}. If your model has a different name for the learning rate, set\"\n        f\" it with `.lr_find(attr_name=...)`.\"\n    )\n\n\nclass _LRFinder:\n    \"\"\"LR finder object. This object stores the results of lr_find().\n\n    Args:\n        mode: either `linear` or `exponential`, how to increase lr after each step\n        lr_min: lr to start search from\n        lr_max: lr to stop search\n        num_training: number of steps to take between lr_min and lr_max\n\n    \"\"\"\n\n    def __init__(self, mode: str, lr_min: float, lr_max: float, num_training: int) -> None:","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/tuner/lr_finder.py#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add an lr or learning_rate attribute to the model (self.lr = 0.001 or save_hyperparameters including it)","Pass the actual name: tuner.lr_find(model, attr_name=\"base_lr\")","If LR is hardcoded in configure_optimizers, hoist it into self.lr and reference it there"],"exampleFix":"# before\nclass LitModel(pl.LightningModule):\n    def __init__(self, base_lr=1e-3): ...\n    def configure_optimizers(self):\n        return torch.optim.Adam(self.parameters(), lr=1e-3)\n# after\nclass LitModel(pl.LightningModule):\n    def __init__(self, base_lr=1e-3):\n        super().__init__()\n        self.base_lr = base_lr\n    def configure_optimizers(self):\n        return torch.optim.Adam(self.parameters(), lr=self.base_lr)\n# then: tuner.lr_find(model, attr_name=\"base_lr\")","handlingStrategy":"type-guard","validationCode":"from lightning.pytorch.utilities.model_helpers import lightning_hasattr\nif not any(lightning_hasattr(model, a) for a in (\"lr\", \"learning_rate\")):\n    raise ValueError(\"model needs an `lr`/`learning_rate` field or attr_name must be passed\")\ntuner.lr_find(model)","typeGuard":"def lr_field_known(model) -> bool:\n    from lightning.pytorch.utilities.model_helpers import lightning_hasattr\n    return any(lightning_hasattr(model, a) for a in (\"lr\", \"learning_rate\"))","tryCatchPattern":null,"preventionTips":["Expose the learning rate as a model attribute or hparams key","Never hardcode the LR only inside configure_optimizers"],"tags":["lr-finder","tuner","learning-rate","hyperparameters","pytorch-lightning"],"backgroundTag":"attribute-not-found","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}