第四门课 卷积神经网络(Convolutional Neural Networks)
第一周:编程作业:Convolution model - Step by Step
本周课程笔记见:第一周 卷积神经网络(Foundations of Convolutional Neural Networks)
Convolutional Neural Networks: Step by Step
Welcome to Course 4’s first assignment! In this assignment, you will implement convolutional (CONV) and pooling (POOL) layers in numpy, including both forward propagation and (optionally) backward propagation.
Notation:
-
Superscript [ l ] [l] [l] denotes an object of the l t h l^{th} lth layer.
- Example: a [ 4 ] a^{[4]} a[4] is the 4 t h 4^{th} 4th layer activation. W [ 5 ] W^{[5]} W[5] and b [ 5 ] b^{[5]} b[5] are the 5 t h 5^{th} 5th layer parameters.
-
Superscript ( i ) (i) (i) denotes an object from the i t h i^{th} ith example.
- Example: x ( i ) x^{(i)} x(i) is the i t h i^{th} ith training example input.
-
Lowerscript i i i denotes the i t h i^{th} ith entry of a vector.
- Example: a i [ l ] a^{[l]}_i ai[l] denotes the i t h i^{th} ith entry of the activations in layer l l l, assuming this is a fully connected (FC) layer.
-
n H n_H nH, n W n_W nW and n C n_C nC denote respectively the height, width and number of channels of a given layer. If you want to reference a specific layer l l l, you can also write n H [ l ] n_H^{[l]} nH[l], n W [ l ] n_W^{[l]} nW[l], n C [ l ] n_C^{[l]} nC[l].
-
n H p r e v n_{H_{prev}} nHprev, n W p r e v n_{W_{prev}} nWprev and n C p r e v n_{C_{prev}} nCprev denote respectively the height, width and number of channels of the previous layer. If referencing a specific layer l l l, this could also be denoted n H [ l − 1 ] n_H^{[l-1]} nH[l−1], n W [ l − 1 ] n_W^{[l-1]} nW[l−1], n C [ l − 1 ] n_C^{[l-1]} nC[l−1].
We assume that you are already familiar with numpy
and/or have completed the previous courses of the specialization. Let’s get started!
1 - Packages
Let’s first import all the packages that you will need during this assignment.
- numpy is the fundamental package for scientific computing with Python.
- matplotlib is a library to plot graphs in Python.
- np.random.seed(1) is used to keep all the random function calls consistent. It will help us grade your work.
import numpy as np
import h5py
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
%load_ext autoreload
%autoreload 2
np.random.seed(1)
2 - Outline of the Assignment
You will be implementing the building blocks of a convolutional neural network! Each function you will implement will have detailed instructions that will walk you through the steps needed:
- Convolution functions, including:
- Zero Padding
- Convolve window
- Convolution forward
- Convolution backward (optional)
- Pooling functions, including:
- Pooling forward
- Create mask
- Distribute value
- Pooling backward (optional)
This notebook will ask you to implement these functions from scratch in numpy
. In the next notebook, you will use the TensorFlow equivalents of these functions to build the following model:
Note that for every forward function, there is its corresponding backward equivalent. Hence, at every step of your forward module you will store some parameters in a cache. These parameters are used to compute gradients during backpropagation.
3 - Convolutional Neural Networks
Although programming frameworks make convolutions easy to use, they remain one of the hardest concepts to understand in Deep Learning. A convolution layer transforms an input volume into an output volume of different size, as shown below.
In this part, you will build every step of the convolution layer. You will first implement two helper functions: one for zero padding and the other for computing the convolution function itself.
3.1 - Zero-Padding
Zero-padding adds zeros around the border of an image:
Image (3 channels, RGB) with a padding of 2.
The main benefits of padding are the following:
-
It allows you to use a CONV layer without necessarily shrinking the height and width of the volumes. This is important for building deeper networks, since otherwise the height/width would shrink as you go to deeper layers. An important special case is the “same” convolution, in which the height/width is exactly preserved after one layer.
-
It helps us keep more of the information at the border of an image. Without padding, very few values at the next layer would be affected by pixels as the edges of an image.
Exercise: Implement the following function, which pads all the images of a batch of examples X with zeros. Use np.pad. Note if you want to pad the array “a” of shape ( 5 , 5 , 5 , 5 , 5