huggingface/pytorch-image-models · error · ValueError

Invalid learning rate: {}

Error message

Invalid learning rate: {}

What it means

Raised by timm's Adan (Adaptive Nesterov Momentum) optimizer constructor when the learning rate is negative. Adan requires lr >= 0 because the update is a scaled descent step along the aggregated gradient direction.

Source

Thrown at timm/optim/adan.py:74

        eps: Term added to the denominator to improve numerical stability.
        weight_decay: Decoupled weight decay (L2 penalty)
        no_prox: How to perform the weight decay
        caution: Enable caution from 'Cautious Optimizers'
        foreach: If True would use torch._foreach implementation. Faster but uses slightly more memory.
    """

    def __init__(self,
            params,
            lr: float = 1e-3,
            betas: Tuple[float, float, float] = (0.98, 0.92, 0.99),
            eps: float = 1e-8,
            weight_decay: float = 0.0,
            no_prox: bool = False,
            caution: bool = False,
            foreach: Optional[bool] = None,
    ):
        if not 0.0 <= lr:
            raise ValueError('Invalid learning rate: {}'.format(lr))
        if not 0.0 <= eps:
            raise ValueError('Invalid epsilon value: {}'.format(eps))
        if not 0.0 <= betas[0] < 1.0:
            raise ValueError('Invalid beta parameter at index 0: {}'.format(betas[0]))
        if not 0.0 <= betas[1] < 1.0:
            raise ValueError('Invalid beta parameter at index 1: {}'.format(betas[1]))
        if not 0.0 <= betas[2] < 1.0:
            raise ValueError('Invalid beta parameter at index 2: {}'.format(betas[2]))

        defaults = dict(
            lr=lr,
            betas=betas,
            eps=eps,
            weight_decay=weight_decay,
            no_prox=no_prox,
            caution=caution,
            foreach=foreach,
        )

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Use a non-negative learning rate, typically 1e-3 to 1e-2 for Adan
  2. Check config files and CLI parsing for a stray minus sign or bad float parse

Example fix

# before
opt = timm.optim.Adan(model.parameters(), lr=-1e-3)
# after
opt = timm.optim.Adan(model.parameters(), lr=1e-3)
Defensive patterns

Strategy: validation

Validate before calling

assert lr >= 0.0, f'lr must be >= 0, got {lr}'

Type guard

def valid_lr(lr) -> bool:
    return (isinstance(lr, (int, float)) and lr >= 0.0)

Prevention

When it happens

Trigger: Calling timm.optim.Adan(params, lr=value) with value < 0.0, e.g. lr=-1e-3 or a negative value read from a config/CLI.

Common situations: Sign error in hyperparameter sweeps, a YAML value like lr: -1e-3 from a template, or misparsed scientific notation from CLI args.

Related errors


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