liveBook · Manning

Hyperparameter

As explained in Deep Learning with Python, Third Edition

A user-chosen model or training setting, such as layer count or activation choice, that is set before training and tuned externally rather than learned by backpropagation.

A hyperparameter is a choice you make about a model or its training before fitting it. The number of layers, units per layer, activation function, dropout amount, and optimizer learning rate are examples. Unlike model parameters, hyperparameters are not learned by backpropagation. A sensible first choice may still be suboptimal, so treat it as something to test rather than a fixed rule. section 18.1.1 section 6.2.5

Choose units and an optimizer

A KerasTuner model-building function can let `hp.Int` choose 16, 32, 48, or 64 units for a Dense layer, while `hp.Choice` selects either `rmsprop` or `adam`. Each combination produces a model to fit on training data and assess on validation data; the choices themselves are not adjusted by backpropagation. section 18.1.1

Listing 18.1 A KerasTuner model-building function
import keras
from keras import layers

def build_model(hp):
    units = hp.Int(name="units", min_value=16, max_value=64, step=16) #1
    model = keras.Sequential(
        [
            layers.Dense(units, activation="relu"),
            layers.Dense(10, activation="softmax"),
        ]
    )
    optimizer = hp.Choice(name="optimizer", values=["rmsprop", "adam"])#2
    model.compile(
        optimizer=optimizer,
        loss="sparse_categorical_crossentropy",
        metrics=["accuracy"],
    )
    return model                                                       #3

The model-building function shows where candidate hyperparameter values enter the layer and optimizer configuration. section 18.1.1

Tune after a first model shows statistical power and can overfit. Change a configuration, retrain, and compare validation performance; use the validation set, not the test set, to guide changes. KerasTuner can automate the search: its BayesianOptimization example targets `val_accuracy` and limits the search to 20 trials. Repeated tuning can itself overfit the validation process. Once a configuration is satisfactory, retrain on the available training and validation data and evaluate once on the held-out test set. section 6.2.5 section 18.1.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines hyperparameters against backpropagation-trained parameters and works through a KerasTuner search without deriving its search algorithm.
Examples
Uses Dense-layer unit counts, optimizer choices, and a KerasTuner search with validation accuracy as its objective.
What it assumes
Assumes a model can be built and fitted while explicitly distinguishing chosen settings from parameters learned by backpropagation.
What you can do after
Apply it via KerasTuner to compare configurations on validation data, then evaluate the selected configuration on a held-out test set.
How it connects
Connects hyperparameter tuning to regularization, model capacity, validation-set overfitting, and test-set evaluation.

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 Python, Third Edition. The section links point to the parts of the book it draws on.