{"record":{"id":"453b7f232dc594f7","repo":"Lightning-AI/pytorch-lightning","slug":"swa-epoch-start-should-be-a-0-integer-or-a-float","errorCode":null,"errorMessage":"swa_epoch_start should be a >0 integer or a float between 0 and 1.","messagePattern":"swa_epoch_start should be a >0 integer or a float between 0 and 1\\.","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/callbacks/stochastic_weight_avg.py","lineNumber":102,"sourceCode":"\n                - ``\"cos\"``. For cosine annealing.\n                - ``\"linear\"`` For linear annealing\n\n            avg_fn: the averaging function used to update the parameters;\n                the function must take in the current value of the\n                :class:`AveragedModel` parameter, the current value of :attr:`model`\n                parameter and the number of models already averaged; if None,\n                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","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/callbacks/stochastic_weight_avg.py#L84-L120","documentation":"StochasticWeightAveraging validates swa_epoch_start in __init__: if passed as an int it must be >=1 (an epoch number), and if passed as a float it must be in [0,1] (fraction of training). This branch fires for an int < 1, e.g. 0 or negative.","triggerScenarios":"SWA(swa_epoch_start=0) or any integer less than 1 (e.g. -2).","commonSituations":"Assuming epoch numbering starts at 0; passing 0 intending 'start immediately'; copy-pasting a float default as int.","solutions":["Use swa_epoch_start=1 to start averaging from the first epoch","Or use a float fraction like 0.75 to start SWA in the last quarter of training"],"exampleFix":"# before\nswa = SWA(swa_epoch_start=0)\n# after\nswa = SWA(swa_epoch_start=1)  # or SWA(swa_epoch_start=0.75)","handlingStrategy":"validation","validationCode":"def valid_swa_epoch_start(v):\n    return (isinstance(v, int) and not isinstance(v, bool) and v >= 1) or (isinstance(v, float) and 0 <= v <= 1)\nassert valid_swa_epoch_start(cfg.swa_epoch_start)","typeGuard":"def is_valid_swa_start(v) -> bool:\n    return (type(v) is int and v >= 1) or (type(v) is float and 0.0 <= v <= 1.0)","tryCatchPattern":null,"preventionTips":["Document whether the value is an epoch int or a fraction float","Validate config before constructing callbacks"],"tags":["swa","stochastic-weight-averaging","validation","callback"],"backgroundTag":"invalid-hyperparameter-value","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}