{"record":{"id":"80e56c33a1b78922","repo":"TheAlgorithms/Python","slug":"validation-size-should-be-between-0-and-len-train","errorCode":null,"errorMessage":"Validation size should be between 0 and {len(train_images)}. Received: {validation_size}.","messagePattern":"Validation size should be between 0 and (.+?)\\. Received: (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"neural_network/input_data.py","lineNumber":329,"sourceCode":"\n    local_file = _maybe_download(\n        test_images_file, train_dir, source_url + test_images_file\n    )\n    with gfile.Open(local_file, \"rb\") as f:\n        test_images = _extract_images(f)\n\n    local_file = _maybe_download(\n        test_labels_file, train_dir, source_url + test_labels_file\n    )\n    with gfile.Open(local_file, \"rb\") as f:\n        test_labels = _extract_labels(f, one_hot=one_hot)\n\n    if not 0 <= validation_size <= len(train_images):\n        msg = (\n            \"Validation size should be between 0 and \"\n            f\"{len(train_images)}. Received: {validation_size}.\"\n        )\n        raise ValueError(msg)\n\n    validation_images = train_images[:validation_size]\n    validation_labels = train_labels[:validation_size]\n    train_images = train_images[validation_size:]\n    train_labels = train_labels[validation_size:]\n\n    options = {\"dtype\": dtype, \"reshape\": reshape, \"seed\": seed}\n\n    train = _DataSet(train_images, train_labels, **options)\n    validation = _DataSet(validation_images, validation_labels, **options)\n    test = _DataSet(test_images, test_labels, **options)\n\n    return _Datasets(train=train, validation=validation, test=test)\n","sourceCodeStart":311,"sourceCodeEnd":343,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/neural_network/input_data.py#L311-L343","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set validation_size within [0, number of training images] — the classic MNIST split uses validation_size=5000 out of 55000","Compute it relative to the data: validation_size = len(train_images) // 10","Pass validation_size=0 if you do not want a validation split"],"exampleFix":"# before\nvalidation_size = 60000  # exceeds 55000 training images -> ValueError\n\n# after\nvalidation_size = min(60000, len(train_images))  # or a fixed valid value like 5000","handlingStrategy":"validation","validationCode":"def check_validation_size(validation_size, num_train_images) -> bool:\n    return 0 <= validation_size <= num_train_images","typeGuard":null,"tryCatchPattern":"try:\n    datasets = read_data_sets(dir, validation_size=vs)\nexcept ValueError as e:\n    if 'Validation size' in str(e):\n        vs = len(datasets_placeholder_train) // 10  # recompute to a valid fraction\n    else:\n        raise","preventionTips":["Derive validation_size from the dataset length instead of hard-coding it","Validate numeric config values against their documented bounds before passing them to library calls"],"tags":["mnist","validation-split","argument-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}