{"record":{"id":"4eda4ec325eea9fa","repo":"huggingface/pytorch-image-models","slug":"choice-weights-and-batch-sizes-must-have-the-same","errorCode":null,"errorMessage":"choice_weights and batch_sizes must have the same length.","messagePattern":"choice_weights and batch_sizes must have the same length\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"timm/data/scheduled_sampler.py","lineNumber":117,"sourceCode":"        if choice_schedule == 'progressive' and num_batches is None:\n            num_batches = self._infer_num_batches()\n        self.num_batches = int(num_batches) if num_batches is not None else None\n        self._sample_budget_schedule: Tuple[Tuple[int, int], ...] = ()\n        if self.num_batches is None:\n            self._sample_budget_schedule = self._create_sample_budget_schedule()\n            if not self._sample_budget_schedule:\n                raise ValueError(\n                    'No full scheduled batch fits the sampler; reduce the batch sizes.'\n                )\n\n    def _normalize_choice_weights(\n            self,\n            choice_weights: Optional[Sequence[float]],\n    ) -> torch.Tensor:\n        if choice_weights is None:\n            return torch.full((len(self.batch_sizes),), 1.0 / len(self.batch_sizes), dtype=torch.float64)\n        if len(choice_weights) != len(self.batch_sizes):\n            raise ValueError('choice_weights and batch_sizes must have the same length.')\n\n        weights = torch.tensor(choice_weights, dtype=torch.float64)\n        if not torch.isfinite(weights).all() or (weights < 0).any():\n            raise ValueError('choice_weights must contain finite, non-negative values.')\n        weight_sum = weights.sum()\n        if weight_sum <= 0:\n            raise ValueError('choice_weights must have a positive sum.')\n        return weights / weight_sum\n\n    def choice_weights_for_epoch(self, epoch: int) -> torch.Tensor:\n        \"\"\"Return normalized choice weights for an epoch.\n\n        Args:\n            epoch: Zero-based training epoch.\n\n        Returns:\n            Normalized floating-point choice weights.\n        \"\"\"","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/data/scheduled_sampler.py#L99-L135","documentation":"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.","triggerScenarios":"ScheduledBatchSampler(s, batch_sizes=[128, 256], choice_weights=[0.7]) — 2 sizes but 1 weight, or any len(choice_weights) != len(batch_sizes).","commonSituations":"Adding a batch size to the config without updating the weights list (or vice versa); weights generated programmatically over a different list.","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]."],"exampleFix":"# before\nScheduledBatchSampler(s, batch_sizes=[128, 256, 512], choice_weights=[0.5, 0.5])\n\n# after\nScheduledBatchSampler(s, batch_sizes=[128, 256, 512], choice_weights=[0.6, 0.3, 0.1])","handlingStrategy":"validation","validationCode":"if choice_weights is not None:\n    assert len(choice_weights) == len(batch_sizes)","typeGuard":"def weights_match_sizes(weights, sizes) -> bool:\n    return weights is None or (hasattr(weights,'__len__') and len(weights) == len(sizes))","tryCatchPattern":null,"preventionTips":["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."],"tags":["timm","sampler","choice-weights","validation"],"backgroundTag":"config-list-length-mismatch","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}