liveBook · Manning

Dataset split

As explained in Deep Learning with Python, Third Edition

One of the K disjoint subsets created when data is split for cross-validation.

With few data points, a single validation set may be so small that the score changes substantially depending on which examples it contains. K-fold cross-validation addresses this by dividing the available data into K disjoint partitions, or folds—typically four or five—and giving each fold a turn as the validation set. section 4.3.4

For each fold:

  • Set that partition aside for validation and use the other K − 1 partitions for training. section 4.3.4
  • Create a fresh, identical model, train it, and record its validation score. section 4.3.4
  • Average the K scores rather than trusting any one split. section 4.3.4

Four folds in practice

With K = 4, one run produced validation scores of 0.298, 0.349, 0.232, and 0.305. Their mean was 0.296. That spread shows why the choice of validation fold matters; the mean gives a more reliable metric than a single score. Averaging each fold’s validation MAE at every epoch can also help choose a training duration before fitting a final model on all the training data. section 4.3.4

Listing 4.28 K-fold validation
k = 4
num_val_samples = len(x_train) // k
num_epochs = 50
all_scores = []
for i in range(k):
    print(f"Processing fold #{i + 1}")
    fold_x_val = x_train[i * num_val_samples : (i + 1) * num_val_samples]  #1
    fold_y_val = y_train[i * num_val_samples : (i + 1) * num_val_samples]
    fold_x_train = np.concatenate(                                       #2
        [x_train[: i * num_val_samples], x_train[(i + 1) * num_val_samples :]],  #2
        axis=0,                                                           #2
    )     #2                                                                
    fold_y_train = np.concatenate(
        [y_train[: i * num_val_samples], y_train[(i + 1) * num_val_samples :]],
        axis=0,
    )
    model = get_model()                                                  #3
    model.fit(                                                           #4
        fold_x_train,                                                     #4
        fold_y_train,                                                     #4
        epochs=num_epochs,                                                #4
        batch_size=16,                                                    #4
        verbose=0,                                                        #4
    )                                                                     #4
    scores = model.evaluate(fold_x_val, fold_y_val, verbose=0)          #5
    val_loss, val_mae = scores
    all_scores.append(val_mae)

The K-fold validation listing shows how array slices select one fold and concatenation assembles the remaining training data. section 4.3.4

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through fold slicing, training on remaining partitions, score averaging, and per-epoch validation histories.
Examples
Shows four validation scores ranging from 0.232 to 0.349, their 0.296 mean, and NumPy-based fold construction.
What it assumes
Assumes training arrays and a model-building function; demonstrates fold construction through slicing and concatenation.
What you can do after
Build it from scratch by slicing training arrays into folds, evaluating fresh models, and averaging their validation scores.
How it connects
Connects K-fold cross-validation with training sets, validation sets, mean absolute error, and model selection by training duration.

Also explained in

Compare how 18 books explain dataset split →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.