To create and fit a deep neural network model for a regression problem, we will make use of Keras. The code used for the model architecture is as follows:
# Model architecture
model <- keras_model_sequential()
model %>%
layer_dense(units = 10, activation = 'relu', input_shape = c(13)) %>%
layer_dense(units = 5, activation = 'relu') %>%
layer_dense(units = 1)
summary(model)
OUTPUT
___________________________________________________________________________ Layer (type) Output Shape Param # =========================================================================== dense_1 (Dense) (None, 10) 140 ___________________________________________________________________________ dense_2 (Dense) (None, 5) 55 ___________________________________________________________________________ dense_3 (Dense) (None, 1) 6 =========================================================================== Total params: 201 Trainable params: 201 Non-trainable params: 0 ___________________________________________________________________________
As you can see from the preceding code, we use the keras_model_sequential function to create a sequential model. The structure of the neural network is defined using the layer_dense function. Since there are 13 independent variables, input_shape is used to specify 13 units. The first hidden layer has 10 units and the rectified linear unit, or relu, is used as the activation function in this first hidden layer. The second hidden layer has 5 units, with relu as the activation function. The last, layer_dense, has 1 unit, which represents one dependent variable, medv. Using the summary function, you can print a model summary, which shows 201 total parameters.