Hyperparameter tuning for DNNs tends to be a bit more involved than other ML models due to the number of hyperparameters that can/should be assessed and the dependencies between these parameters. To automate the hyperparameter tuning for keras and tensorflow, we use the tfruns package.

This notebook shows an example of performing a grid search on a densley connected feedforward neural network for the IMDB movie review classifier introduced in this case study.

library(tfruns)
library(dplyr)

tfruns provides added flexibility for tracking, visualizing, and managing training runs. The most common way to use tfruns is to create an R script that contains the code to be executed. For this example, I created the imdb-grid-search.R script.

Within this script, we create “flags”, which identify the hyperparameters of interest. For this example, I assess:

This equates to 324 models. Since these models run relatively quickly, and since I ran this at the end of the day, I performed a full cartesian grid search. This means I ran and assessed every single one of the 324 models. If you are pressed for time you can run a stochastic hyperparameter grid search by using the sample parameter in tuning_run below.

Next, to execute this grid search, you first specify the grid search hyperparameter values for the flags you created in your .R script like below. You then run tuning_run() in place of source() to execute your .R script for the supplied hyperparameter grid.

Note: this takes over 3 hours to run on a non-GPU

grid_search <- list(
  batch_size = c(128, 512),
  layers = c(1, 2, 3),
  units = c(16, 32, 64),
  learning_rate = c(0.001, 0.0001),
  dropout = c(0, 0.3, 0.5),
  weight_decay = c(0, 0.01, 0.001)
)

tuning_run("imdb-grid-search.R", flags = grid_search, confirm = FALSE, echo = FALSE)

This grid search execution will create a “runs” subdirectory within your working directory. This fold contains information for every single training run executed during the grid search.

To list the results you can run ls_runs():

data.frame(ls_runs(runs_dir = "imdb_runs"))

You can even filter and order the results. The following illustrates that a few models tied with the lowest loss score of 0.262.

ls_runs(runs_dir = "imdb_runs", order = eval_best_loss, decreasing = FALSE)
Data frame: 324 x 31 
# ... with 314 more rows
# ... with 23 more columns:
#   flag_batch_size, flag_layers, flag_units, flag_learning_rate, flag_dropout,
#   flag_weight_decay, samples, batch_size, epochs, epochs_completed, metrics, model,
#   loss_function, optimizer, learning_rate, script, start, end, completed, output,
#   source_code, context, type

To see details about any one of these models you can run view_run(). In this example, I take the first optimal model from above. When you execute this, a pop up window will appear with that models summary information as illustrated below.

best_run <- ls_runs(
  runs_dir = "imdb_runs",
  order = eval_best_loss,
  decreasing = FALSE
  ) %>%
  slice(1) %>%
  pull(run_dir)

view_run(best_run)

There are many other handy features to the tfruns package. I suggest you check it out at https://tensorflow.rstudio.com/tools/tfruns/overview/ and take it for a test drive.

LS0tCnRpdGxlOiAiSU1EQiBNb3ZpZSBDbGFzc2lmaWNhdGlvbiBHcmlkIFNlYXJjaCIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKYGBge3Igc2V0dXAsIGluY2x1ZGU9RkFMU0V9CmtuaXRyOjpvcHRzX2NodW5rJHNldChlY2hvID0gVFJVRSkKZ2dwbG90Mjo6dGhlbWVfc2V0KGdncGxvdDI6OnRoZW1lX21pbmltYWwoKSkKCiMgY2xlYW4gdXAgaW4gY2FzZSB5b3UgcnVuIHRoaXMgbXVsdGlwbGUgdGltZXMKdGZydW5zOjpjbGVhbl9ydW5zKGNvbmZpcm0gPSBGQUxTRSkKYGBgCgpIeXBlcnBhcmFtZXRlciB0dW5pbmcgZm9yIEROTnMgdGVuZHMgdG8gYmUgYSBiaXQgbW9yZSBpbnZvbHZlZCB0aGFuIG90aGVyIE1MCm1vZGVscyBkdWUgdG8gdGhlIG51bWJlciBvZiBoeXBlcnBhcmFtZXRlcnMgdGhhdCBjYW4vc2hvdWxkIGJlIGFzc2Vzc2VkIGFuZCB0aGUKZGVwZW5kZW5jaWVzIGJldHdlZW4gdGhlc2UgcGFyYW1ldGVycy4gVG8gYXV0b21hdGUgdGhlIGh5cGVycGFyYW1ldGVyIHR1bmluZwpmb3Iga2VyYXMgYW5kIHRlbnNvcmZsb3csIHdlIHVzZSB0aGUgW19fdGZydW5zX19dKGh0dHBzOi8vZ2l0aHViLmNvbS9yc3R1ZGlvL3RmcnVucykKcGFja2FnZS4KClRoaXMgbm90ZWJvb2sgc2hvd3MgYW4gZXhhbXBsZSBvZiBwZXJmb3JtaW5nIGEgZ3JpZCBzZWFyY2ggb24gYSBkZW5zbGV5IGNvbm5lY3RlZApmZWVkZm9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