KerasTuner automates hyperparameter optimization for Keras models by replacing fixed values such as units = 32 with searchable ranges. Those ranges form a search space, while a tuner evaluates candidate configurations against an objective. 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
}
Build a compiled model from an hp argument, sampling integer units and optimizer choices before returning the model. section 18.1.1
A tuning workflow defines the builder, creates a tuner such as BayesianOptimization, selects an objective such as validation accuracy, and limits trials and repeated executions. Search then trains candidates with validation data and optional early stopping. section 18.1.1
MNIST multilayer perceptron search
Flatten MNIST images, scale pixels, reserve validation data, and search a dense classifier over units from 16 to 64 and either RMSprop or Adam. Query the best hyperparameter configurations or trained models, then retrain selected configurations and evaluate them on the test set. section 18.1.1
For a more modular design, subclass kt$HyperModel and implement build; R can express this Python-style subclass with %py_class%. section 18.1.1


