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
