Creating the model function

OK, we are going to create a model function, but not the model. The key function is keras_model_sequential(). There is a ton of stuff you can specify. What I'm going to show are two hidden layers with 64 neurons each. In both layers, the activation function is relu, which I covered earlier, and they work well for a regression problem. After the first layer, I demonstrate how to incorporate a dropout layer of 30%. Then, after the second hidden layer, I incorporate L1 regularization or LASSO, which we discussed in Chapter 4, Advanced Feature Selection in Linear Models. I thought it was important to show how to use both regularization methods, so you can use and adjust them as you deem fit.

The next function within the function is compile(), where I specify the loss as mean-squared-error (MSE) and the validation data metric as mean-absolute-error:

> model <- keras_model_sequential() %>%
layer_dense(units = 64, activation = "relu",
input_shape = dim(trainT)[2]
) %>%
layer_dropout(0.3) %>%
layer_dense(units = 64, activation = "relu",
kernel_regularizer = regularizer_l1(l = 0.001)) %>%
#layer_dropout(0.5) %>%
layer_dense(units = 1)

model %>% compile(
loss = "mse",
optimizer = optimizer_rmsprop(),
metrics = list("mean_absolute_error")
)

model
}

Now, you can build the model and examine it. One thing of note, and something I don't see in very many vignettes out there, is to specify the seed, otherwise your results will vary wildly:

> use_session_with_seed(1800)

> model <- build_model()

> model %>% summary()

The output of the preceding code is as follows:

_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================================
dense_1 (Dense) (None, 64) 7360
_________________________________________________________________
dropout_1 (Dropout) (None, 64) 0
_________________________________________________________________
dense_2 (Dense) (None, 64) 4160
_________________________________________________________________
dense_3 (Dense) (None, 1) 65
=================================================================================
Total params: 11,585
Trainable params: 11,585
Non-trainable params: 0

The output should be self-explanatory, which means we can finally train the model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.60.158