keras-team/keras · error · ValueError
Invalid subset name: {subset}; expected "training" or "valid
Error message
Invalid subset name: {subset}; expected "training" or "validation". What it means
ImageDataGenerator splits data via subset='training'/'validation' only. NumpyArrayIterator rejects any other subset string.
Source
Thrown at keras/src/legacy/preprocessing/image.py:581
x_misc = []
if y is not None and len(x) != len(y):
raise ValueError(
"`x` (images tensor) and `y` (labels) "
"should have the same length. "
f"Found: x.shape = {np.asarray(x).shape}, "
f"y.shape = {np.asarray(y).shape}"
)
if sample_weight is not None and len(x) != len(sample_weight):
raise ValueError(
"`x` (images tensor) and `sample_weight` "
"should have the same length. "
f"Found: x.shape = {np.asarray(x).shape}, "
f"sample_weight.shape = {np.asarray(sample_weight).shape}"
)
if subset is not None:
if subset not in {"training", "validation"}:
raise ValueError(
f"Invalid subset name: {subset}"
'; expected "training" or "validation".'
)
split_idx = int(len(x) * image_data_generator._validation_split)
if (
y is not None
and not ignore_class_split
and not np.array_equal(
np.unique(y[:split_idx]), np.unique(y[split_idx:])
)
):
raise ValueError(
"Training and validation subsets "
"have different number of classes after "
"the split. If your numpy arrays are "
"sorted by the label, you might want "
"to shuffle them."View on GitHub (pinned to 7a34a03db6)
Solutions
- Use subset='training' or subset='validation' exactly
- Pass validation_split=0.2 (0<split<1) to ImageDataGenerator when using subsets
Example fix
// before gen.flow(x, y, subset='train') // after gen = ImageDataGenerator(validation_split=0.2) gen.flow(x, y, subset='training')
Defensive patterns
Strategy: validation
Validate before calling
assert subset in (None, 'training', 'validation')
Type guard
def is_valid_subset(s): return s is None or s in {'training','validation'} Try / catch
try: gen.flow(x, y, subset=s) except ValueError as e: if 'Invalid subset' in str(e): s = 'training'
Prevention
- Centralize subset strings as constants
- Require validation_split when subsets are used
When it happens
Trigger: Calling gen.flow(x, y, subset='train'), subset='val', or subset='test' (also requires validation_split set on the generator).
Common situations: Copying tutorial code and renaming subsets; using subset without validation_split; Keras 2 vs 3 naming confusion.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
- ConvNeXt does not support the `channels_first` image data fo
- If using `weights="imagenet"` with `include_top=True`, `clas
- The `weights` argument should be either `None` (random initi
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/4263661a99535d0f.
Report an issue: GitHub.