huggingface/pytorch-image-models · error · ValueError

Invalid learning rate: {}

Error message

Invalid learning rate: {}

What it means

NAdam optimizer constructor validation: learning rate must satisfy 0.0 <= lr. A negative lr is rejected immediately at timm.optim.NAdam instantiation.

Source

Thrown at timm/optim/nadam.py:42

    __ http://cs229.stanford.edu/proj2015/054_report.pdf
    __ http://www.cs.toronto.edu/~fritz/absps/momentum.pdf

        Originally taken from: https://github.com/pytorch/pytorch/pull/1408
        NOTE: Has potential issues but does work well on some problems.
    """

    def __init__(
            self,
            params,
            lr=2e-3,
            betas=(0.9, 0.999),
            eps=1e-8,
            weight_decay=0,
            schedule_decay=4e-3,
    ):
        if not 0.0 <= lr:
            raise ValueError("Invalid learning rate: {}".format(lr))
        defaults = dict(
            lr=lr,
            betas=betas,
            eps=eps,
            weight_decay=weight_decay,
            schedule_decay=schedule_decay,
        )
        super(NAdamLegacy, self).__init__(params, defaults)

    @torch.no_grad()
    def step(self, closure=None):
        """Performs a single optimization step.

        Arguments:
            closure (callable, optional): A closure that reevaluates the model
                and returns the loss.
        """
        loss = None

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Fix lr to a positive value such as 1e-3
  2. Check config/CLI parsing for stray minus signs or unit mistakes
  3. If lr is computed (e.g. scaled), clamp or assert lr >= 0 before constructing

Example fix

# before
opt = NAdam(model.parameters(), lr=-1e-3)

# after
opt = NAdam(model.parameters(), lr=1e-3)
Defensive patterns

Strategy: validation

Validate before calling

if not 0.0 <= lr:
    raise ValueError(f'bad lr from config: {lr}')

Prevention

When it happens

Trigger: Calling timm.optim.NAdam(params, lr=-0.01) or any negative learning rate; also NaN lr from a miscomputed config.

Common situations: Typo'd hyperparameter in a config file (lr: -1e-3), sign errors when negating lr for LR-sweep scripts, or lr loaded as negative from a CLI arg parsed incorrectly.

Related errors


AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27). Data as JSON: /api/errors/5e5cffce44173799. Report an issue: GitHub.