Here we demonstrate a simple grid search to optimize a tuning parameter of a keras neural network.

The Ames housing data is used to demonstrate. There are a number of predictors for these data but, for simplicity, we’ll see how far we can get by just using the geocodes for the properties as predictors of price. The outcome will be modeled on the log10 scale.

ames <- make_ames() %>%
  select(Sale_Price, Longitude, Latitude)

To be consistent with other analyses of these data, a training/test split is made. However, this article focuses on the training set.

Normally, feature preprocessing should be estimated within the resampling process to get generalizable estimates of performance. Here, the two predictors are simply centered and scaled beforehand to avoid complexity in this analysis. However, this is generally a bad idea and the article on recipes describes a proper methodology for preprocessing the data.

data_split <- initial_split(ames, strata = "Sale_Price")

ames_train <- 
  training(data_split) %>%
  mutate(Sale_Price = log10(Sale_Price),
         Longitude  = scale(Longitude, center = TRUE),
         Latitude   = scale(Latitude, center = TRUE))

The resample the model, simple 10-fold cross-validation is done such that the splits use the outcome as a stratification variable. On average, there should be 219 properties in the assessment set and this should be enough to obtain good estimates of the model RMSE.

cv_splits <- vfold_cv(ames_train, v = 10, strata = "Sale_Price")

A single layer feed-forward neural network with 10 hidden units will be used to model these data. There are a great many tuning parameters for these models including those for structural aspects (e.g. number of hidden units, activation type, number of layers), the optimization (momentum, dropout rate, etc.), and so on. For simplicity, this article will only optimize the number of training epochs (i.e. iterations); basically this is testing for early stopping.

A function is needed to compute the model on the analysis set, predict the assessment set, and compute the holdout root mean squared error (in log10 units). The function below constructs the model sequentially and takes the number of epochs as a parameter. The argument split will be used to pass a single element of cv_splits$splits. This object will contain the two splits of the data for a single resample. The ellipses (...) will be used to pass arbitrary arguments to keras::fit.

In this function, the seed is set. A few of the model components, such as initializer_glorot_uniform and layer_dropout, use random numbers and their specific seeds are set from the session’s seed. This helps with reproducibility.

Let’s execute the function on the first fold of the data using a batch size of 128 and disable the print/plotting of the optimization:

## <1977/222/2199>
## [1] 0.32008

This works for a single resample and a single epoch setting. To tune over epochs, another function uses map to work over the tuning parameter for a specific resample. This is more advantageous than fixing the tuning parameter and then iterating over the data. If there was a complex feature engineering process being used, it only needs to be called once if functions are configured so that the inner loop is over the tuning parameters.

The result value is a tibble with the tuning parameter values, the performance estimates, and an indicator for the fold.

Note that the grid could easily have a been multidimensional so that many parameters could be optimized using a regular grid or via random search. If that were the case, the candidate tuning parameter values could be in rows and the parameters in columns and mlp_rmse would then map the columns in the tibble to their respective arguments.

map_df is used to operate over the folds. This will row-bind the resulting data frames together so that a single data set is assembled with the individual resampling results.

##   epoch      rmse     id
## 1    50 0.9935545 Fold01
## 2   100 0.4388598 Fold01
## 3   150 0.3189029 Fold01
## 4   200 0.2840765 Fold01
## 5   250 0.1876097 Fold01
## 6   300 0.1636652 Fold01

The mean RMSE per epoch is computed and plotted along with the individual curves for each resample.

## # A tibble: 12 x 2
##    epoch  rmse
##    <dbl> <dbl>
##  1    50 0.798
##  2   100 0.366
##  3   150 0.318
##  4   200 0.261
##  5   250 0.200
##  6   300 0.171
##  7   350 0.153
##  8   400 0.149
##  9   450 0.149
## 10   500 0.148
## 11   550 0.149
## 12   600 0.148

ggplot(mean_values, aes(x = epoch, y = rmse)) + 
  geom_point() + 
  geom_line() + 
  geom_line(data = tune_results, aes(group = id), alpha = 0.1) + 
  scale_y_continuous(trans = "log2") +

For this analysis, there doesn’t seem to be any overfitting issues with a large number of iterations (since the RMSE does not increase).

caret includes some pre-defined keras models for single layer networks that can be used to optimize the model across a number of parameters.