keras-team/keras · error · ValueError
`validation_split` must be strictly between 0 and 1. …
Error message
`validation_split` must be strictly between 0 and 1. Received: {validation_split} What it means
Raised by legacy image data iterators when validation_split is not strictly between 0 and 1 (e.g. 0, 1, negative, or >= 1). The fraction of samples reserved for validation must satisfy 0 < validation_split < 1.
Solutions
- Pass a fraction like 0.2 (0 < v < 1)
- Omit validation_split entirely if no split is needed
- If percentage in config, divide by 100 before constructing
Example fix
// before ImageDataGenerator(validation_split=20) // after ImageDataGenerator(validation_split=0.2)
Defensive patterns
Strategy: validation
Validate before calling
assert validation_split is None or 0 < validation_split < 1, validation_split
Type guard
def valid_split(v): return v is None or (isinstance(v,(int,float)) and 0 < v < 1)
Try / catch
try: ImageDataGenerator(validation_split=vs) except ValueError as e: if 'validation_split' in str(e): vs = 0.2
Prevention
- Store splits as fractions, never percentages
When it happens
Trigger: ImageDataGenerator(validation_split=0), validation_split=1.0, validation_split=-0.2, or 100 (percent instead of fraction).
Common situations: Thinking 0/1 booleans; passing a percentage (20) instead of a fraction (0.2).
Related errors
- Argument `precision` must be in the range [0, 1]. Received…
- Argument `recall` must be in the range [0, 1]. Received…
- Argument `sensitivity` must be in the range [0, 1]…
- Argument `specificity` must be in the range [0, 1]…
- Invalid `beta` argument value. It should be > 0. Received…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/3a9eeabf948529b6.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/legacy/preprocessing/image.py:1021
if data_format not in {"channels_last", "channels_first"}:
raise ValueError(
'`data_format` should be `"channels_last"` '
"(channel after row and column) or "
'`"channels_first"` (channel before row and column). '
f"Received: {data_format}"
)
self.data_format = data_format
if data_format == "channels_first":
self.channel_axis = 1
self.row_axis = 2
self.col_axis = 3
if data_format == "channels_last":
self.channel_axis = 3
self.row_axis = 1
self.col_axis = 2
if validation_split and not 0 < validation_split < 1:
raise ValueError(
"`validation_split` must be strictly between 0 and 1. "
f" Received: {validation_split}"
)
self._validation_split = validation_split
self.mean = None
self.std = None
self.zca_whitening_matrix = None
if isinstance(zoom_range, (float, int)):
self.zoom_range = [1 - zoom_range, 1 + zoom_range]
elif len(zoom_range) == 2 and all(
isinstance(val, (float, int)) for val in zoom_range
):
self.zoom_range = [zoom_range[0], zoom_range[1]]
else:
raise ValueError(
"`zoom_range` should be a float or "View on GitHub (pinned to 7a34a03db6)