Fitting the model

To fit the model, we make use of the following code:

# Fit model
model_one <- model %>%
fit(training,
trainLabels,
epochs = 200,
batch_size = 32,
validation_split = 0.2)

OUTPUT (last 3 epochs)
Epoch 198/200
1218/1218 [==============================] - 0s 43us/step - loss: 0.3662 - acc: 0.8555 - val_loss: 0.5777 - val_acc: 0.8000
Epoch 199/200
1218/1218 [==============================] - 0s 41us/step - loss: 0.3654 - acc: 0.8530 - val_loss: 0.5763 - val_acc: 0.8000
Epoch 200/200
1218/1218 [==============================] - 0s 40us/step - loss: 0.3654 - acc: 0.8571 - val_loss: 0.5744 - val_acc: 0.8000

As seen from the preceding code, we see the following observations:

  • To fit the model, we provide training data that has data for 21 independent variables and trainLabels, which contains data for the target variable.
  • The number of iterations or epochs is specified as 200. An epoch is a single pass of the training data followed by model assessment using validation data.
  • To avoid overfitting, we specified that the validation split is 0.2, which means that 20% of the training data will be used to assess the model performance as the training proceeds.
  • Note that this 20% of data is the bottom 20% of the data points in the training data. We stored data on the loss and accuracy values for the training and validation data generated during the training of the model in model_one for later use.
  • For batch_size, we used the default value of 32, which represents the number of samples that will be used per gradient.
  • As the training of the model proceeds, we get a visual display of plots for loss and accuracy based on training and validation data after each epoch.
  • For accuracy, we would like the model to have higher values, as accuracy is a the-higher-the-better type of metric, whereas for loss, which is a the-lower-the-better type of metric, we would like the model to have lower values.
  • In addition, we also obtained the numeric summary of the loss output based on the last 3 epochs, as shown in the preceding code output. For each epoch, we saw that 1,218 samples out of 1,523 samples of the training data (about 80%) were used for fitting the model. The remaining 20% of the data was used for calculating accuracy and loss values for the validation data.
A word of caution. When using validation_split, note that the validation data is not selected randomly from the training data—for example, when validation_split = 0.2, the last 20% of the training data is used for validation and the first 80% is used for training. Therefore, if the values of the target variable are not random, then validation_split may introduce bias in the classification model.

After the training process completes 200 epochs, we can plot the training progress in terms of loss and accuracy for training and validation data using the plot function, as shown in the following code:

 plot(model_one)

The following graph provides a plot that has accuracy in the top window and loss in the lower window:

Accuracy and loss for training and validation data

From the preceding plot for loss and accuracy, we can make the following observations:

  • From the plot for accuracy in the top graph, you can see that accuracy values increase significantly after about 25 epochs and then continue to increase gradually for the training data.
  • For validation data, the progress is more uneven, with a drop in accuracy between the 25th and 50th epochs.
  • A somewhat similar pattern is observed in the opposite direction for loss values.
  • Note that if the training data accuracy increases with the number of epochs, but the validation data accuracy decreases, that would suggest an overfitting of the model. We do not see any major pattern suggesting model overfitting from this plot.
..................Content has been hidden....................

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