{"record":{"id":"c42986634e765ab3","repo":"Lightning-AI/pytorch-lightning","slug":"the-swa-lrs-should-a-positive-float-or-a-list-o","errorCode":null,"errorMessage":"The `swa_lrs` should a positive float, or a list of positive floats","messagePattern":"The `swa_lrs` should a positive float, or a list of positive floats","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/callbacks/stochastic_weight_avg.py","lineNumber":110,"sourceCode":"                equally weighted average is used (default: ``None``)\n\n            device: if provided, the averaged model will be stored on the ``device``.\n                When None is provided, it will infer the `device` from ``pl_module``.\n                (default: ``\"cpu\"``)\n\n        \"\"\"\n\n        err_msg = \"swa_epoch_start should be a >0 integer or a float between 0 and 1.\"\n        if isinstance(swa_epoch_start, int) and swa_epoch_start < 1:\n            raise MisconfigurationException(err_msg)\n        if isinstance(swa_epoch_start, float) and not (0 <= swa_epoch_start <= 1):\n            raise MisconfigurationException(err_msg)\n\n        wrong_type = not isinstance(swa_lrs, (float, list))\n        wrong_float = isinstance(swa_lrs, float) and swa_lrs <= 0\n        wrong_list = isinstance(swa_lrs, list) and not all(lr > 0 and isinstance(lr, float) for lr in swa_lrs)\n        if wrong_type or wrong_float or wrong_list:\n            raise MisconfigurationException(\"The `swa_lrs` should a positive float, or a list of positive floats\")\n\n        if avg_fn is not None and not callable(avg_fn):\n            raise MisconfigurationException(\"The `avg_fn` should be callable.\")\n\n        if device is not None and not isinstance(device, (torch.device, str)):\n            raise MisconfigurationException(f\"device is expected to be a torch.device or a str. Found {device}\")\n\n        self.n_averaged: Optional[Tensor] = None\n        self._swa_epoch_start = swa_epoch_start\n        self._swa_lrs = swa_lrs\n        self._annealing_epochs = annealing_epochs\n        self._annealing_strategy = annealing_strategy\n        self._avg_fn = avg_fn or self.avg_fn\n        self._device = device\n        self._model_contains_batch_norm: Optional[bool] = None\n        self._average_model: Optional[pl.LightningModule] = None\n        self._initialized = False\n        self._swa_scheduler: Optional[LRScheduler] = None","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/callbacks/stochastic_weight_avg.py#L92-L128","documentation":"StochasticWeightAveraging requires swa_lrs (the constant learning rate used during SWA) to be a positive float or a list of positive floats (one per optimizer param group / one per optimizer). This MisconfigurationException fires when the value has the wrong type, is non-positive, or the list contains non-positive/non-float entries.","triggerScenarios":"SWA(swa_lrs=-0.1), SWA(swa_lrs=0), SWA(swa_lrs='1e-3') (string), or SWA(swa_lrs=[0.01, 0]) with multiple param groups.","commonSituations":"Reusing the peak LR scheduler value as swa_lrs when it decays to 0; passing ints like swa_lrs=1 (isinstance(1, float) is False -> wrong_type); building the list programmatically and including an int element.","solutions":["Pass a positive float, e.g. swa_lrs=1e-3","For multiple param groups pass a list of positive floats matching them: swa_lrs=[1e-3, 1e-4]","Ensure list elements are Python floats, not ints or strings"],"exampleFix":"# before\nswa = SWA(swa_lrs=0)  # or 1 (int)\n# after\nswa = SWA(swa_lrs=1e-3)  # or [1e-3, 1e-4] for two param groups","handlingStrategy":"validation","validationCode":"def valid_swa_lrs(v):\n    if isinstance(v, float):\n        return v > 0\n    if isinstance(v, list):\n        return all(type(x) is float and x > 0 for x in v)\n    return False\nassert valid_swa_lrs(cfg.swa_lrs)","typeGuard":"def is_valid_swa_lrs(v) -> bool:\n    return (type(v) is float and v > 0) or (isinstance(v, list) and v and all(type(x) is float and x > 0 for x in v))","tryCatchPattern":null,"preventionTips":["Always write LRs as float literals: 1e-3, 0.01","Match list length to the number of param groups"],"tags":["swa","swa-lrs","validation","learning-rate"],"backgroundTag":"invalid-hyperparameter-value","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}