TheAlgorithms/Python · error · ValueError

Validation size should be between 0 and {len(train_images)}.

Error message

Validation size should be between 0 and {len(train_images)}. Received: {validation_size}.

What it means

Raised by read_data_sets when the validation_size argument is negative or larger than the number of training images. The function splits the front of train_images into a validation set, so validation_size must satisfy 0 <= validation_size <= len(train_images). Any value outside that range makes the slice split invalid and is rejected.

Source

Thrown at neural_network/input_data.py:329

    local_file = _maybe_download(
        test_images_file, train_dir, source_url + test_images_file
    )
    with gfile.Open(local_file, "rb") as f:
        test_images = _extract_images(f)

    local_file = _maybe_download(
        test_labels_file, train_dir, source_url + test_labels_file
    )
    with gfile.Open(local_file, "rb") as f:
        test_labels = _extract_labels(f, one_hot=one_hot)

    if not 0 <= validation_size <= len(train_images):
        msg = (
            "Validation size should be between 0 and "
            f"{len(train_images)}. Received: {validation_size}."
        )
        raise ValueError(msg)

    validation_images = train_images[:validation_size]
    validation_labels = train_labels[:validation_size]
    train_images = train_images[validation_size:]
    train_labels = train_labels[validation_size:]

    options = {"dtype": dtype, "reshape": reshape, "seed": seed}

    train = _DataSet(train_images, train_labels, **options)
    validation = _DataSet(validation_images, validation_labels, **options)
    test = _DataSet(test_images, test_labels, **options)

    return _Datasets(train=train, validation=validation, test=test)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Set validation_size within [0, number of training images] — the classic MNIST split uses validation_size=5000 out of 55000
  2. Compute it relative to the data: validation_size = len(train_images) // 10
  3. Pass validation_size=0 if you do not want a validation split

Example fix

# before
validation_size = 60000  # exceeds 55000 training images -> ValueError

# after
validation_size = min(60000, len(train_images))  # or a fixed valid value like 5000
Defensive patterns

Strategy: validation

Validate before calling

def check_validation_size(validation_size, num_train_images) -> bool:
    return 0 <= validation_size <= num_train_images

Try / catch

try:
    datasets = read_data_sets(dir, validation_size=vs)
except ValueError as e:
    if 'Validation size' in str(e):
        vs = len(datasets_placeholder_train) // 10  # recompute to a valid fraction
    else:
        raise

Prevention

When it happens

Trigger: Calling read_data_sets(..., validation_size=60000) on a 55000-image training set, passing a negative value such as validation_size=-500, or computing validation_size as a fraction (e.g. 0.1) which slices to 0 and is accepted but yields an empty validation set.

Common situations: Hard-coding a validation size copied from a differently-sized dataset, using a percentage where an absolute count is expected, or passing a validation size after switching from the full MNIST set to a subsampled one.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/80e56c33a1b78922. Report an issue: GitHub.