liveBook · Manning

Regularization

As explained in Deep Learning with R, Third Edition

Regularization is a modeling technique that constrains learning to reduce overfitting and improve generalization to unseen data.

A network can fit its training examples so closely that it performs worse on new data. Regularization deliberately limits that fit: it reduces what the model can memorize or encourages smoother, more general patterns. The aim is better validation performance, not a perfect training score. section 5.1.2 section 5.4.4

Add an L2 penalty to an IMDb classifier

In R/Keras, attach an L2 regularizer to each hidden dense layer, then fit the model with a validation split: [train_data, train_labels] <- dataset_imdb(num_words = 10000)[[1]] vectorize <- function(sequences) { results <- matrix(0, nrow = length(sequences), ncol = 10000) for (i in seq_along(sequences)) { idx <- sequences[[i]] + 1L results[i, idx[idx <= 10000]] <- 1 } results } train_data <- vectorize(train_data) model <- keras_model_sequential() |> layer_dense(16, activation = "relu", kernel_regularizer = regularizer_l2(0.002)) |> layer_dense(16, activation = "relu", kernel_regularizer = regularizer_l2(0.002)) |> layer_dense(1, activation = "sigmoid") model |> compile(optimizer = "rmsprop", loss = "binary_crossentropy", metrics = "accuracy") history <- model |> fit(train_data, train_labels, epochs = 20, batch_size = 512, validation_split = 0.4) Compare history$metrics$val_loss with an otherwise identical model without kernel_regularizer. L2 adds a weight penalty to the training loss; the comparison asks whether that constraint helps on held-out reviews. section 5.4.4 section 7.4.5

Other practical ways to constrain the same classifier are:

  • Reduce capacity: try fewer layers or units. A smaller model has less room to memorize, but one that is too small may underfit. Compare candidate sizes by validation loss; there is no fixed formula for choosing them. section 5.4.4
  • Use dropout: insert layer_dropout(0.5) after hidden layers. It randomly zeros activations during training; compare its validation loss with the unmodified model. section 5.4.4
  • Try L1 or combined L1/L2 weight regularization. Early stopping is another option once overfitting begins. section 5.4.4 section 5.5
Listing 5.15 Adding dropout to the IMDb model
model <- keras_model_sequential() |>
  layer_dense(16, activation = "relu") |>
  layer_dropout(0.5) |>
  layer_dense(16, activation = "relu") |>
  layer_dropout(0.5) |>
  layer_dense(1, activation = "sigmoid")

model |> compile(
  optimizer = "rmsprop",
  loss = "binary_crossentropy",
  metrics = "accuracy"
)

history_dropout <- model |> fit(
  train_data, train_labels,
  epochs = 20, batch_size = 512,
  validation_split = 0.4
)

The dropout version of the IMDb classifier shows exactly where to place dropout layers between dense layers. section 5.4.4

First establish a model that can learn and overfit. Then change its architecture or regularization, retrain, and judge each change on validation data—not the test set. Repeated tuning can itself overfit the validation process, so reserve a separate test set for final evaluation. More or better data can also improve generalization. section 6.2.5 section 5.5

Regularization can constrain more than classifier weights. In a variational autoencoder, a regularization loss nudges the encoder’s latent distribution toward a normal distribution, helping make the latent space structured while reducing overfitting. section 17.1.2

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through capacity reduction, weight penalties, and dropout with validation comparisons; offers no formula for optimal network size.
Examples
Compares IMDb review classifiers with smaller or larger hidden layers, L2 weight penalties, and dropout.
What it assumes
Builds on fitting a model that can overfit, while introducing regularization through concrete classifier changes.
What you can do after
Apply it via R/Keras by changing layer size, kernel_regularizer, or layer_dropout and comparing validation loss.
How it connects
Connects overfitting and generalization to model capacity, validation loss, dropout, weight regularization, and early stopping.

Also explained in

Compare how 13 books explain regularization →

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.