Step 5:  

Add a convolution layer

clear all;
close all;
clc;


[XTrain,YTrain] = digitTrain4DArrayData;
size(XTrain)      %images
size(YTrain)      %correct answer labels


XTrain=1-XTrain;  % Reverse the black and white colors.  Save and run the program to see the difference.

perm = randperm(size(XTrain,4),20);  % Randomize the order of images in XTrain
for i = 1:20
subplot(4,5,i);
imshow(XTrain(:,:,:,perm(i)));
end
 

layers = [
imageInputLayer([28 28 1])   
convolution2dLayer(3,8,'Padding','same')

The first value is the size of the filter (3x3), the second value is the number of filters (8 convolution filters). Each 28x28 images will be convolved wtih each of the 8 filters.  The next pair of inputs you see "Padding" and "same" come together as a pair.  Padding means whether you want to have zeros padded to the edges of each image so that the convolution filters will be go just slightly past the edges of the images.  You can specify how many pixels of zeros you'd like padded to the edges but entering a number.  However, in the current examples we don't want any zero padding, so we set it's value to "same", which means that we want to keep the original size of the image after convolution.   

In the next step add  an activation layer (ReLU) and a pooling layer.  Try this first without looking at the next step.  In the command window, type help ReluLayer or maxPooling2dLayer  to get more information. For ReLU, you can just go with the default values, but for the pooling layer, you have to decide on how much you want to reduce the size of the image by (pool the average values across how many pixels?).