liveBook · Manning

Validation loss

As explained in Deep Learning with R, Third Edition

Validation loss is the loss value computed from a model's predictions on validation data.

Validation loss is the loss computed from a model’s predictions on data withheld from training. Keeping training and validation data separate reveals whether learning transfers to unseen examples rather than memorizing training pairs. section 3.6.7

model <- keras_model_sequential() |> layer_dense(units = 1)

model |> compile(
  optimizer = optimizer_rmsprop(learning_rate = 0.1),
  loss = loss_mean_squared_error(),
  metrics = list(metric_binary_accuracy())
)

indices_permutation <- sample.int(nrow(inputs))  #1
shuffled_inputs <- inputs[indices_permutation, , drop = FALSE] 
shuffled_targets <- targets[indices_permutation, , drop = FALSE] 

num_validation_samples <- as.integer(0.3 * nrow(inputs))  #2
val_inputs <- shuffled_inputs[1:num_validation_samples, ] 
val_targets <- shuffled_targets[1:num_validation_samples, ] 
training_inputs <- shuffled_inputs[-(1:num_validation_samples), ] 
training_targets <- shuffled_targets[-(1:num_validation_samples), ] 

model |> fit(
  training_inputs, training_targets,  #3




  epochs = 5, batch_size = 16,
  validation_data = list(val_inputs, val_targets)  #4
)

Split shuffled inputs and targets, reserve 30% for validation, then pass the untouched subset through fit() with validation_data. section 3.6.7

Use validation loss to:

  • Monitor generalization alongside training loss and validation metrics; worsening validation performance signals that the model has begun to overfit. section 6.2.4
  • Evaluate after training with evaluate(), which returns validation loss first, followed by configured metrics. section 3.6.7
Listing 8.15 Fitting the model using a Dataset
callbacks <- list(
  callback_model_checkpoint(
    filepath = "convnet_from_scratch.keras",
    save_best_only = TRUE,
    monitor = "val_loss"
  )
)

history <- model |> fit(
  train_dataset,
  epochs = 50,
  validation_data = validation_dataset,
  callbacks = callbacks
)

Fit a model on training and validation datasets while configuring ModelCheckpoint to save only the epoch with the lowest val_loss. section 8.2.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Defines the metric, explains strict data separation, demonstrates fit() and evaluate(), and stops short of detailed generalization theory.
Examples
Demonstrates a 30% R/Keras validation split, evaluate(), image datasets, loss curves, and val_loss-based checkpointing.
What it assumes
Assumes a trainable model, loss function, and separate validation inputs and targets; introduces validation splitting and monitoring in practice.
What you can do after
Operate/configure Keras training to monitor validation loss, evaluate held-out data, and retain the best-performing epoch.
How it connects
Connects validation loss with training loss, validation metrics, overfitting, model checkpoints, and test-set evaluation.

Also explained in

Compare how 5 books explain validation loss →

Related concepts

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