liveBook · Manning

Hyperparameter

As explained in Deep Learning with R, Third Edition

A hyperparameter is a configuration value selected outside parameter learning that controls a model, training procedure, or optimization process.

A hyperparameter is a choice you make about a model or its training before its weights are learned. The number of layers and units per layer are hyperparameters; the weights learned during training are model parameters. The optimizer and its learning rate are choices you can tune, too. section 5.2.1 section 6.2.5 section 18.1.1

Choose units and an optimizer

For a model trained on MNIST, a KerasTuner model-building function can let `hp$Int()` choose 16, 32, 48, or 64 units for a dense layer. `hp$Choice()` can choose between `rmsprop` and `adam`. Each choice produces a model to train and compare—not a weight learned by that model. section 18.1.1

Listing 18.1 KerasTuner model-building function
build_model <- function(hp, num_classes = 10) {
  units  <- hp$Int(  #1
    name = "units",  #1
    min_value = 16L,  #1
    max_value = 64L,  #1
    step = 16L)  #1
  model  <- keras_model_sequential() |> #1
    layer_dense(units, activation = "relu") |>
    layer_dense(num_classes, activation = "softmax")

  optimizer <- hp$Choice(name = "optimizer", #2
                         values = c("rmsprop", "adam")) 
  model |> compile(optimizer = optimizer,
                   loss = "sparse_categorical_crossentropy",
                   metrics = "accuracy")
  model  #3
}

The model-building function shows where two searchable hyperparameters enter an R/Keras model: dense-layer units and optimizer choice. section 18.1.1

A practical tuning loop separates model selection from final evaluation:

  • Train each candidate on the training set and compare its performance on the validation set. A tuner can choose the next configuration automatically. section 18.1.1
  • Once the configuration is satisfactory, train a final model on the available training and validation data, then evaluate it on the untouched test set. section 6.2.5
  • Watch for validation-set overfitting: repeated changes based on validation results leak information into your choices, even though the model is never trained directly on validation data. section 5.2.1

Tune after a simple baseline works and the model has enough capacity to overfit. Try architecture changes, regularization, and learning-rate changes to improve generalization. If test performance is much worse than validation performance, reconsider the validation procedure or whether tuning has overspecialized the configuration. section 6.2.5 section 6.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Defines hyperparameters against learned weights and develops a train–validate–repeat search procedure, without prescribing formal rules for choosing values.
Examples
Demonstrates an MNIST KerasTuner search over dense-layer units and optimizer choice, with validation accuracy as the objective.
What you can do after
Apply it via KerasTuner to compare model configurations on validation data and reserve test data for final evaluation.
How it connects
Connects hyperparameters to model architecture, optimizers, regularization, validation sets, test sets, and overfitting.

Also explained in

Compare how 19 books explain hyperparameter →

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.