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

  1. Make the lists equal length: one weight per batch size (they need not sum to 1; they're normalized).
  2. Omit choice_weights for uniform weighting.
  3. 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

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


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