huggingface/pytorch-image-models · error · ValueError

Invalid learning rate: {}

Error message

Invalid learning rate: {}

What it means

Mars constructor validates that lr is >= 0 is not enough phrasing-wise — it requires lr to satisfy 0.0 <= lr; any lr that fails that (i.e. negative) is rejected before the optimizer is built.

Source

Thrown at timm/optim/mars.py:113

        https://arxiv.org/abs/2411.10438

    """
    def __init__(
            self,
            params: ParamsT,
            lr: float = 3e-3,
            betas: Tuple[float, float] = (0.9, 0.99),
            eps: float = 1e-8,
            weight_decay: float = 0.,
            gamma: float = 0.025,
            mars_type: str = "adamw",
            optimize_1d: bool = False,
            lr_1d_factor: float = 1.0,
            betas_1d: Optional[Tuple[float, float]] = None,
            caution: bool = False
    ):
        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]))
        assert mars_type in ["adamw", "lion"], "MARS type not supported"

        defaults = dict(
            lr=lr,
            betas=betas,
            eps=eps,
            weight_decay=weight_decay,
            mars_type=mars_type,
            gamma=gamma,
            optimize_1d=optimize_1d,
            lr_1d_factor=lr_1d_factor,
            betas_1d=betas_1d or betas,

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Pass a non-negative lr (e.g. 1e-3); note 0 is accepted here, unlike some other timm optimizers
  2. Fix the config/sweep that produced the negative value

Example fix

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

Strategy: validation

Validate before calling

assert cfg.lr >= 0, 'lr must be non-negative for Mars'

Type guard

def is_valid_mars_lr(lr: float) -> bool:
    return isinstance(lr, (int, float)) and lr >= 0

Prevention

When it happens

Trigger: Passing a negative lr (lr=-0.1) to mars.Mars(). Zero lr passes this check.

Common situations: Sign typos in configs; sweeps with negative lr values; misconfigured LR schedulers feeding the constructor.

Related errors


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