huggingface/pytorch-image-models · error · ValueError
choice_weights and batch_sizes must have the same length.
Error message
choice_weights and batch_sizes must have the same length.
What it means
When choice_weights is provided, it must align element-for-element with batch_sizes (each batch-size choice gets a sampling weight); a length mismatch fails validation inside _normalize_choice_weights during construction.
Source
Thrown at timm/data/scheduled_sampler.py:117
if choice_schedule == 'progressive' and num_batches is None:
num_batches = self._infer_num_batches()
self.num_batches = int(num_batches) if num_batches is not None else None
self._sample_budget_schedule: Tuple[Tuple[int, int], ...] = ()
if self.num_batches is None:
self._sample_budget_schedule = self._create_sample_budget_schedule()
if not self._sample_budget_schedule:
raise ValueError(
'No full scheduled batch fits the sampler; reduce the batch sizes.'
)
def _normalize_choice_weights(
self,
choice_weights: Optional[Sequence[float]],
) -> torch.Tensor:
if choice_weights is None:
return torch.full((len(self.batch_sizes),), 1.0 / len(self.batch_sizes), dtype=torch.float64)
if len(choice_weights) != len(self.batch_sizes):
raise ValueError('choice_weights and batch_sizes must have the same length.')
weights = torch.tensor(choice_weights, dtype=torch.float64)
if not torch.isfinite(weights).all() or (weights < 0).any():
raise ValueError('choice_weights must contain finite, non-negative values.')
weight_sum = weights.sum()
if weight_sum <= 0:
raise ValueError('choice_weights must have a positive sum.')
return weights / weight_sum
def choice_weights_for_epoch(self, epoch: int) -> torch.Tensor:
"""Return normalized choice weights for an epoch.
Args:
epoch: Zero-based training epoch.
Returns:
Normalized floating-point choice weights.
"""View on GitHub (pinned to 9a5261e31b)
Solutions
- Make the lists equal length: one weight per batch size (they need not sum to 1; they're normalized).
- Omit choice_weights for uniform weighting.
- If generating weights, build them as [f(b) for b in batch_sizes].
Example fix
# before ScheduledBatchSampler(s, batch_sizes=[128, 256, 512], choice_weights=[0.5, 0.5]) # after ScheduledBatchSampler(s, batch_sizes=[128, 256, 512], choice_weights=[0.6, 0.3, 0.1])
Defensive patterns
Strategy: validation
Validate before calling
if choice_weights is not None:
assert len(choice_weights) == len(batch_sizes) Type guard
def weights_match_sizes(weights, sizes) -> bool:
return weights is None or (hasattr(weights,'__len__') and len(weights) == len(sizes)) Prevention
- Keep choice_weights and batch_sizes in one config block so they change together.
- Omit weights for uniform sampling.
- Generate weights from the batch_sizes list itself.
When it happens
Trigger: ScheduledBatchSampler(s, batch_sizes=[128, 256], choice_weights=[0.7]) — 2 sizes but 1 weight, or any len(choice_weights) != len(batch_sizes).
Common situations: Adding a batch size to the config without updating the weights list (or vice versa); weights generated programmatically over a different list.
Related errors
- All scheduled batch sizes must be positive integers.
- num_batches must be a positive integer when specified.
- A progressive schedule requires at least two choices.
- schedule_epochs must be a positive integer for a progressive
- schedule_spread must be non-negative.
AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27).
Data as JSON: /api/errors/4eda4ec325eea9fa.
Report an issue: GitHub.