%% Classify Images Using Trained ConvNet
% Load the sample data.

[XTrain,YTrain] = digitTrain4DArrayData;

% digitTrain4DArrayData loads the digit training set as 4-D array data. XTrain is a 28-by-28-by-1-by-5000 array, where 28 is the height and 28 is the width of the images. 1 is the number of channels and 5000 is the number of synthetic images of handwritten digits. YTrain is a categorical vector containing the labels for each observation.
%
% Construct the convolutional neural network architecture.

layers = [ ...
    imageInputLayer([28 28 1])
    convolution2dLayer(5,20)
    reluLayer
    maxPooling2dLayer(2,'Stride',2)
    fullyConnectedLayer(10)
    softmaxLayer
    classificationLayer];

%
% Set the options to default settings for the stochastic gradient descent with momentum.

options = trainingOptions('sgdm');

%
% Train the network.

rng('default')
net = trainNetwork(XTrain,YTrain,layers,options);

%
% Run the trained network on a test set.

[XTest,YTest]= digitTest4DArrayData;
YPred = classify(net,XTest);

%
% Display the first 10 images in the test data and compare to the classification from classify.

[YTest(1:10,:) YPred(1:10,:)]

% The results from classify match the true digits for the first ten images.
%
% Calculate the accuracy over all test data.

accuracy = sum(YPred == YTest)/numel(YTest)

% Copyright 2012 The MathWorks, Inc.