Fitting the model

Fitting or training of the model is carried out with the help of data. An example of a code used for fitting a classification model is provided as follows:

model %>%   
fit(training,
trainLabels,
epochs = 200,
batch_size = 32,
validation_split = 0.2)

In the preceding code, fitting a model includes training, which is the data on independent variables, and trainLabels, which contain labels for the response variable. The number of epochs is specified to indicate the number of iterations of all samples in the training data that will be used during the training process. Batch size refers to the number of samples from the training data to be used, after which the model parameters will be updated. In addition, we also specify any validation split, where a 0.2 or 20% split means that the last 20% of samples from the training data will be kept separate from the training process to assess the model performance.

When fitting a model, different layers in the network have a random initialization of weights. Due to this random initialization of network weights, if we fit a model again with the same data, same architecture, and same settings, we will get slightly different results. This will occur not only in a different session of R, but also in the same session when a model is trained again.

There are many situations where getting repeatable results is important. As an example, while publishing a deep learning-related article in a peer-reviewed international journal, you may need to generate more plots from the same model based on reviewer feedback. Another situation could be where a team working on the same project may like to share a model and also results with other members of the team. The easiest way to obtain the same results from the model is to save and then reload the model using the following code:

# Save/reload model
save_model_hdf5(model,
filepath,
overwrite = TRUE,
include_optimizer = TRUE)
model_x <- load_model_hdf5(filepath,
custom_objects = NULL,
compile = TRUE)

We can save the model by specifying filepath and then reload when required. Saving a model allows us to obtain repeatable results when we use the model again. It also allows us to have a way to share the same model with others who can obtain exactly the same results, as well as helping in situations where each run takes a lot of time. Saving and reloading the model allows you to resume the training process when you train the model again.

Once a model is fit, its performance can be assessed using both training and testing data.

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

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