liveBook · Manning

Validation_data argument

As explained in Deep Learning with Python, Third Edition

A fit() argument that supplies a separate dataset for computing validation loss and metrics during training.

A model can improve on its training samples without improving on new data. Pass a separate dataset through `validation_data` in `model.fit()` to monitor validation loss and metrics during training. Keep every validation sample out of the training data; otherwise, the measurements no longer provide a trustworthy check on generalization. section 3.6.7

Hold out samples, then fit

Set aside inputs and targets together: `x_val = x_train[:10000]` and `y_val = y_train[:10000]`. Train on the remaining samples with `model.fit(partial_x_train, partial_y_train, epochs=20, batch_size=512, validation_data=(x_val, y_val))`. The model computes validation loss and accuracy at the end of each epoch without training on those held-out samples. section 4.1.4

Listing 4.7 Training your model
history = model.fit(
    partial_x_train,
    partial_y_train,
    epochs=20,
    batch_size=512,
    validation_data=(x_val, y_val),
)

The fit call shows the validation input-and-target pair alongside the distinct training pair. section 4.1.4

In practice:

  • Pass NumPy arrays or a separate TensorFlow Dataset object. For array inputs, `validation_split=0.2` is an alternative that holds out a fraction instead of requiring a manual split. section 3.6.7 section 4.1.4
  • Inspect `history.history` for training and validation values such as `loss`, `accuracy`, `val_loss`, and `val_accuracy`. Comparing their curves can reveal overfitting. section 4.1.4 section 8.2.4
  • Use validation feedback for decisions such as checkpointing on `val_loss`, stopping early, or tuning hyperparameters. Reserve the test set for final evaluation: repeated tuning can itself overfit the validation process. section 8.2.4 section 6.2.5

For a separate measurement after training, call `model.evaluate()` on the held-out data. It returns validation loss and, when configured, validation metrics. section 3.6.7

How Deep Learning with Python, Third Edition covers it

How deep it goes
Shows held-out array splits, fit calls, per-epoch history, and a Dataset example; does not detail validation computation internals.
Examples
Uses a 10,000-sample holdout for loss and accuracy, plus a separate image Dataset with checkpointing.
What it assumes
Assumes a compiled Keras model and training inputs and targets; demonstrates how to set aside validation samples.
What you can do after
Apply it via Keras `model.fit()` to monitor held-out performance and use validation loss to guide training decisions.
How it connects
Connects validation loss to overfitting, early stopping, model checkpointing, hyperparameter optimization, and test-set evaluation.

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.