{"record":{"id":"55e54053693d6c54","repo":"keras-team/keras","slug":"x-images-tensor-and-sample-weight-should-hav","errorCode":null,"errorMessage":"`x` (images tensor) and `sample_weight` should have the same length. Found: x.shape = {np.asarray(x).shape}, sample_weight.shape = {np.asarray(sample_weight).shape}","messagePattern":"`x` \\(images tensor\\) and `sample_weight` should have the same length\\. Found: x\\.shape = (.+?), sample_weight\\.shape = (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":573,"sourceCode":"                if len(x) != len(xx):\n                    raise ValueError(\n                        \"All of the arrays in `x` \"\n                        \"should have the same length. \"\n                        \"Found a pair with: \"\n                        f\"len(x[0]) = {len(x)}, len(x[?]) = {len(xx)}\"\n                    )\n        else:\n            x_misc = []\n\n        if y is not None and len(x) != len(y):\n            raise ValueError(\n                \"`x` (images tensor) and `y` (labels) \"\n                \"should have the same length. \"\n                f\"Found: x.shape = {np.asarray(x).shape}, \"\n                f\"y.shape = {np.asarray(y).shape}\"\n            )\n        if sample_weight is not None and len(x) != len(sample_weight):\n            raise ValueError(\n                \"`x` (images tensor) and `sample_weight` \"\n                \"should have the same length. \"\n                f\"Found: x.shape = {np.asarray(x).shape}, \"\n                f\"sample_weight.shape = {np.asarray(sample_weight).shape}\"\n            )\n        if subset is not None:\n            if subset not in {\"training\", \"validation\"}:\n                raise ValueError(\n                    f\"Invalid subset name: {subset}\"\n                    '; expected \"training\" or \"validation\".'\n                )\n            split_idx = int(len(x) * image_data_generator._validation_split)\n\n            if (\n                y is not None\n                and not ignore_class_split\n                and not np.array_equal(\n                    np.unique(y[:split_idx]), np.unique(y[split_idx:])","sourceCodeStart":555,"sourceCodeEnd":591,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L555-L591","documentation":"NumpyArrayIterator (ImageDataGenerator.flow) validates that x and sample_weight have matching first dimensions. sample_weight must be None or have exactly len(x) entries, one weight per image. The error shows both shapes so you can see the mismatch.","triggerScenarios":"Calling image_data_generator.flow(x, y, sample_weight=w) where w is not None and len(w) != len(x), e.g. weights computed per-class or per-batch instead of per-sample.","commonSituations":"Passing class weights (from compute_class_weight) as sample_weight; slicing x after building weights; forgetting weights apply per image, not per label class.","solutions":["Set sample_weight to an array of length x.shape[0]","If you meant class balancing, use model.fit(..., class_weight=...) instead of sample_weight in flow()","Recompute/align weights after any train/validation split of x"],"exampleFix":"// before\nsw = class_weights  # dict-like / len != n_samples\nit = gen.flow(x, y, sample_weight=sw)\n// after\nmodel.fit(it, class_weight=class_weights)\n# or: sw = np.ones(len(x))\n","handlingStrategy":"validation","validationCode":"assert sample_weight is None or len(sample_weight) == len(x), (len(x), len(sample_weight))","typeGuard":null,"tryCatchPattern":"try: gen.flow(x, y, sample_weight=sw)\nexcept ValueError as e: assert 'sample_weight' in str(e); ...","preventionTips":["Keep one weight per sample; never pass class_weight dicts to flow()","Re-check lengths after slicing x"],"tags":["keras","data-augmentation","shape-mismatch"],"backgroundTag":"input-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}