(Code) Step by step Convolutions
Let's look at the code again, and see, step by step how the Convolutions were built:
Step 1: Gather the Data¶
The training data needs to be reshaped, this is because the convolution layer is expecting a single Tensor but instead we have a 60,000 28x28x1 in a list, so what we need is to create a single 4D, the tensor mentioned before, a list that will look like 60000x28x28x1, and the same for the rest of the images. If you don't do this, you'll get an error when training as the Convolutions do not recognize the shape.
Reshaping the list¶
60000
is the amount of pictures, 28,28
is the size of the pictures 28x28, and, finally 1
is the number of channels in this case is a gray scale pictures thus 1 channel.
Normalize¶
this is something we already mentioned before, this models work better with smaller number, there fore normalization of information is quite common, and recommended practice.
Step 2: Define the model (Convolutional and Maxpooling Layers)¶
Next is to define the models, the first type of model we saw, we started with the Flatten
layer, but in this case we are going to start with the convolutional layer.
Convolutional Layer¶
So the Layer will have a series of parameters:
- The number of convolution, or filters (check here for more info), at this point, it is purely arbitrary, but the suggesting start is with something in the order of 32.
- The size of the Convolution, in this case a 3x3 grid.
- The activation to use in this case will the the Rectified Linear Unit or ReLU ( which it will return X when x>0, else return 0)
- In the first layer of the convolution, we need to add the input data.
MaxPooling Layer¶
The next layer will be a MaxPooling layer which will compress the image, it will maintain the content of the features that were previously highlighted by the convolution, by specifying (2,2) for the Maxpooling (check here for more information about maxpooling), the effect is to quarter the size of the image. this layer create a 2x2 array of pixels, and picks the biggest one thus turning 4 pixels into 1, effectively reducing the image by 25%.
You can call
model.summary()
to see the size and shape of the network, and you'll notice that after every MaxPooling layer, the image size is reduced in this way.
Add another convolution and Maxpooling
At this point we have part of the model, the first part that deal with the data preparation.Step 3: Define the model (Deep neural network)¶
Now flatten the output. After this you'll just have the same DNN structure as the non convolutional version
Step 4: Compile and Train the model¶
Now compile the model, call the fit method to do the training, and evaluate the loss and accuracy from the test set.
The whole model¶
This model we use just one layer of convolution and Max pooling
Additional Information¶
- Lode's Computer Graphics Tutorial
- Convolutions Sidebar
- (https://www.youtube.com/playlist?list=PLkDaE6sCZn6Gl29AoE31iwdVwSG-KnDzF)