Deep neural networks for regression

Loading packages and data sets

library(readr)
library(keras)
library(plotly)
data("Boston", package = "MASS")
data.set <- Boston
dim(data.set)
## [1] 506  14

Convert dataframe to matrix without dimnames

library(DT)
 
# Cast dataframe as a matrix
data.set <- as.matrix(data.set)
# Remove column names
dimnames(data.set) = NULL
head(data.set)
##         [,1] [,2] [,3] [,4]  [,5]  [,6] [,7]   [,8] [,9] [,10] [,11]  [,12]
## [1,] 0.00632   18 2.31    0 0.538 6.575 65.2 4.0900    1   296  15.3 396.90
## [2,] 0.02731    0 7.07    0 0.469 6.421 78.9 4.9671    2   242  17.8 396.90
## [3,] 0.02729    0 7.07    0 0.469 7.185 61.1 4.9671    2   242  17.8 392.83
## [4,] 0.03237    0 2.18    0 0.458 6.998 45.8 6.0622    3   222  18.7 394.63
## [5,] 0.06905    0 2.18    0 0.458 7.147 54.2 6.0622    3   222  18.7 396.90
## [6,] 0.02985    0 2.18    0 0.458 6.430 58.7 6.0622    3   222  18.7 394.12
##      [,13] [,14]
## [1,]  4.98  24.0
## [2,]  9.14  21.6
## [3,]  4.03  34.7
## [4,]  2.94  33.4
## [5,]  5.33  36.2
## [6,]  5.21  28.7
summary(data.set[, 14])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    5.00   17.02   21.20   22.53   25.00   50.00
 hist( data.set[, 14])
<b>Fig 1</b> Histogram of the target variable

Fig 1 Histogram of the target variable

Spiting training and test data

# Split for train and test data
set.seed(123)
indx <- sample(2,
               nrow(data.set),
               replace = TRUE,
               prob = c(0.75, 0.25)) # Makes index with values 1 and 2
x_train <- data.set[indx == 1, 1:13]
x_test <- data.set[indx == 2, 1:13]
y_train <- data.set[indx == 1, 14]
y_test <- data.set[indx == 2, 14]

Normalizing xtrain and xtest data

x_train <- scale(x_train)
x_test <- scale(x_test)

Creating the model

model <- keras_model_sequential() %>% 
  layer_dense(units = 25,
              activation = "relu",
              input_shape = c(13)) %>% 
  layer_dropout(0.2) %>% 
  layer_dense(units = 25,
              activation = "relu") %>% 
  layer_dropout(0.2) %>% 
  layer_dense(units = 25,
              activation = "relu") %>% 
  layer_dropout(0.2) %>% 
  layer_dense(units = 1)
## Loaded Tensorflow version 2.8.0
model %>% summary()
## Model: "sequential"
## ________________________________________________________________________________
##  Layer (type)                       Output Shape                    Param #     
## ================================================================================
##  dense_3 (Dense)                    (None, 25)                      350         
##  dropout_2 (Dropout)                (None, 25)                      0           
##  dense_2 (Dense)                    (None, 25)                      650         
##  dropout_1 (Dropout)                (None, 25)                      0           
##  dense_1 (Dense)                    (None, 25)                      650         
##  dropout (Dropout)                  (None, 25)                      0           
##  dense (Dense)                      (None, 1)                       26          
## ================================================================================
## Total params: 1,676
## Trainable params: 1,676
## Non-trainable params: 0
## ________________________________________________________________________________
model %>% get_config()
## {'name': 'sequential', 'layers': [{'class_name': 'InputLayer', 'config': {'batch_input_shape': (None, 13), 'dtype': 'float32', 'sparse': False, 'ragged': False, 'name': 'dense_3_input'}}, {'class_name': 'Dense', 'config': {'name': 'dense_3', 'trainable': True, 'batch_input_shape': (None, 13), 'dtype': 'float32', 'units': 25, 'activation': 'relu', 'use_bias': True, 'kernel_initializer': {'class_name': 'GlorotUniform', 'config': {'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}}, {'class_name': 'Dropout', 'config': {'name': 'dropout_2', 'trainable': True, 'dtype': 'float32', 'rate': 0.2, 'noise_shape': None, 'seed': None}}, {'class_name': 'Dense', 'config': {'name': 'dense_2', 'trainable': True, 'dtype': 'float32', 'units': 25, 'activation': 'relu', 'use_bias': True, 'kernel_initializer': {'class_name': 'GlorotUniform', 'config': {'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}}, {'class_name': 'Dropout', 'config': {'name': 'dropout_1', 'trainable': True, 'dtype': 'float32', 'rate': 0.2, 'noise_shape': None, 'seed': None}}, {'class_name': 'Dense', 'config': {'name': 'dense_1', 'trainable': True, 'dtype': 'float32', 'units': 25, 'activation': 'relu', 'use_bias': True, 'kernel_initializer': {'class_name': 'GlorotUniform', 'config': {'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}}, {'class_name': 'Dropout', 'config': {'name': 'dropout', 'trainable': True, 'dtype': 'float32', 'rate': 0.2, 'noise_shape': None, 'seed': None}}, {'class_name': 'Dense', 'config': {'name': 'dense', 'trainable': True, 'dtype': 'float32', 'units': 1, 'activation': 'linear', 'use_bias': True, 'kernel_initializer': {'class_name': 'GlorotUniform', 'config': {'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}}]}

Compiling the model

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

Fitting the model

history <- model %>% 
  fit(x_train,
      y_train,
      epoch = 100,
      batch_size = 64,
      validation_split = 0.1,
      callbacks = c(callback_early_stopping(monitor = "val_mean_absolute_error",
                                            patience = 5)),
      verbose = 2)
c(loss, mae) %<-% (model %>% evaluate(x_test, y_test, verbose = 0))
paste0("Mean absolute error on test set: ", sprintf("%.2f", mae))
## [1] "Mean absolute error on test set: 3.88"

Plot the training process

plot(history)

### Calculating the predicted values on test data

pred2 <- model %>% 
  predict(x_test) %>% k_get_value()

head(cbind(pred2,y_test))
##               y_test
## [1,] 25.08657   21.6
## [2,] 34.35121   33.4
## [3,] 34.24675   36.2
## [4,] 15.04251   27.1
## [5,] 15.59313   15.0
## [6,] 15.91344   19.9
  • calculating mean absolute error and root mean square error and ploting
error <- y_test-pred2
head(error)
##            [,1]
## [1,] -3.4865746
## [2,] -0.9512077
## [3,]  1.9532463
## [4,] 12.0574862
## [5,] -0.5931273
## [6,]  3.9865622
rmse <- sqrt(mean(error)^2)
rmse
## [1] 0.6797003
plot(error)