Path: blob/main/C1/W3/assignment/C1W3_Assignment.ipynb
2955 views
Week 3: Improve MNIST with Convolutions
In the lectures you looked at how you would improve Fashion MNIST using Convolutions. For this assignment, see if you can improve MNIST to 99.5% accuracy or more by adding only a single convolutional layer and a single MaxPooling 2D layer to the model from the assignment of the previous week.
Some notes:
Your network should succeed in less than 10 epochs.
When it reaches 99.5% or greater it should print out the string "Reached 99.5% accuracy so cancelling training!" and stop training.
TIPS FOR SUCCESSFUL GRADING OF YOUR ASSIGNMENT:
All cells are frozen except for the ones where you need to submit your solutions or when explicitly mentioned you can interact with it.
You can add new cells to experiment but these will be omitted by the grader, so don't rely on newly created cells to host your solution code, use the provided places for this.
You can add the comment # grade-up-to-here in any graded cell to signal the grader that it must only evaluate up to that point. This is helpful if you want to check if you are on the right track even if you are not done with the whole assignment. Be sure to remember to delete the comment afterwards!
Avoid using global variables unless you absolutely have to. The grader tests your code in an isolated environment without running all cells from the top. As a result, global variables may be unavailable when scoring your submission. Global variables that are meant to be used will be defined in UPPERCASE.
To submit your notebook, save it and then click on the blue submit button at the beginning of the page.
Load and inspect the data
Begin by loading the data. A couple of things to notice:
The file
mnist.npz
is already included in the current workspace under thedata
directory. By default theload_data
from Keras accepts a path relative to~/.keras/datasets
but in this case it is stored somewhere else, as a result of this, you need to specify the full path.tf.keras.datasets.mnist.load_data
returns the train and test sets in the form of the tuples(training_images, training_labels), (testing_images, testing_labels)
but in this exercise you will be needing only the train set so you can ignore the second tuple.
Pre-processing the data
One important step when dealing with image data is to preprocess the data. During the preprocess step you can apply transformations to the dataset that will be fed into your convolutional neural network. This will be your first task of this assignment.
Exercise 1: reshape_and_normalize
You will apply two transformations to the data:
Reshape the data so that it has an extra dimension at the end, counting the dimensions from left to right (such as you would count in a Python list). The reason for this is that commonly you will use 3-dimensional arrays (without counting the batch dimension) to represent image data. The third dimension represents the color using RGB (Red, Green and Blue) values. This data might be in black and white format so the third dimension doesn't really add any additional information for the classification process but it is a good practice regardless.
Normalize the pixel values so that these are values between 0 and 1. You can achieve this by dividing every value in the array by the maximum pixel value.
Remember that these tensors are of type numpy.ndarray
so you can use functions like reshape or divide to complete the reshape_and_normalize
function below. Vectorized operations also work!
Test your function with the next cell:
Expected Output:
Exercise 2: EarlyStoppingCallback
Now it is time to create your own custom callback. For this complete the EarlyStoppingCallback
class and the on_epoch_end
method in the cell below. If you need some guidance on how to proceed, check out this link.
Exercise 3: convolutional_model
Now that you have defined your callback it is time to complete the convolutional_model
function below. This function should return your convolutional neural network.
Your model should achieve an accuracy of 99.5% or more before 10 epochs to pass this assignment.
Hints:
The first layer should take into consideration the
input_shape
of the data, which in this case is the size of each image plus the extra dimension you added earlier.The last layer should take into account the number of classes you are trying to predict.
Remember you should add a Conv2d layer and a MaxPooling2D layer.
You can try any architecture for the network but try to keep in mind you don't need a complex one. For instance, only one convolutional layer is needed.
In case you need extra help you can check out an architecture that works pretty well at the end of this notebook.
To avoid timeout issues with the autograder, please limit the number of units in your convolutional and dense layers. An exception will be raised if your model is too large.
The next cell allows you to check the number of total and trainable parameters of your model and prompts a warning in case these exceeds those of a reference solution, this serves the following 3 purposes listed in order of priority:
Helps you prevent crashing the kernel during training.
Helps you avoid longer-than-necessary training times.
Provides a reasonable estimate of the size of your model. In general you will usually prefer smaller models given that they accomplish their goal successfully.
Notice that this is just informative and may be very well below the actual limit for size of the model necessary to crash the kernel. So even if you exceed this reference you are probably fine. However, if the kernel crashes during training or it is taking a very long time and your model is larger than the reference, come back here and try to get the number of parameters closer to the reference.
Expected Output:
Reached 99.5% accuracy so cancelling training!
printed out before reaching 10 epochs.
Need more help?
Run the following cell to see an architecture that works well for the problem at hand:
Congratulations on finishing this week's assignment!
You have successfully implemented a CNN to assist you in the image classification task. Nice job!
Keep it up!