Load Training Data
Load the digits training data.
Define Network
Define the network architecture and specify the average image value using the 'Mean'
option in the image input layer.
Create a dlnetwork
object from the layer graph.
Define Model Gradients Function
Create the helper function modelGradients
, listed at the end of the example. The function takes a dlnetwork
object dlnet
and a mini-batch of input data dlX
with corresponding labels Y
, and returns the loss and the gradients of the loss with respect to the learnable parameters in dlnet
.
Specify Training Options
Specify the options to use during training.
Train on a GPU, if one is available. Using a GPU requires Parallel Computing Toolbox™ and a CUDA® enabled NVIDIA® GPU with compute capability 3.0 or higher.
Visualize the training progress in a plot.
Train Network
Train the model using a custom training loop. For each epoch, shuffle the data and loop over mini-batches of data. Update the network parameters using the sgdmupdate
function. At the end of each epoch, display the training progress.
Initialize the training progress plot.
Initialize the velocity parameter.
Train the network.
Test the Network
Test the classification accuracy of the model by comparing the predictions on a test set with the true labels.
Convert the data to a dlarray
with the dimension format 'SSCB'
. For GPU prediction, also convert the data to a gpuArray
.
To classify images using a dlnetwork
object, use the predict
function and find the classes with the highest scores.
Evaluate the classification accuracy.
Model Gradients Function
The modelGradients
helper function takes a dlnetwork
object dlnet
and a mini-batch of input data dlX
with corresponding labels Y
, and returns the loss and the gradients of the loss with respect to the learnable parameters in dlnet
. To compute the gradients automatically, use the dlgradient
function.