Learn practical skills, build real-world projects, and advance your career

Building your Deep Neural Network: Step by Step

Welcome to your week 4 assignment (part 1 of 2)! You have previously trained a 2-layer Neural Network (with a single hidden layer). This week, you will build a deep neural network, with as many layers as you want!

  • In this notebook, you will implement all the functions required to build a deep neural network.
  • In the next assignment, you will use these functions to build a deep neural network for image classification.

After this assignment you will be able to:

  • Use non-linear units like ReLU to improve your model
  • Build a deeper neural network (with more than 1 hidden layer)
  • Implement an easy-to-use neural network class

Notation:

  • Superscript [l][l] denotes a quantity associated with the lthl^{th} layer.
    • Example: a[L]a^{[L]} is the LthL^{th} layer activation. W[L]W^{[L]} and b[L]b^{[L]} are the LthL^{th} layer parameters.
  • Superscript (i)(i) denotes a quantity associated with the ithi^{th} example.
    • Example: x(i)x^{(i)} is the ithi^{th} training example.
  • Lowerscript ii denotes the ithi^{th} entry of a vector.
    • Example: ai[l]a^{[l]}_i denotes the ithi^{th} entry of the lthl^{th} layer's activations).

Let's get started!

Updates to Assignment

If you were working on a previous version
  • The current notebook filename is version "4a".
  • You can find your work in the file directory as version "4".
  • To see the file directory, click on the Coursera logo at the top left of the notebook.
List of Updates
  • compute_cost unit test now includes tests for Y = 0 as well as Y = 1. This catches a possible bug before students get graded.
  • linear_backward unit test now has a more complete unit test that catches a possible bug before students get graded.

1 - Packages

Let's first import all the packages that you will need during this assignment.

  • numpy is the main package for scientific computing with Python.
  • matplotlib is a library to plot graphs in Python.
  • dnn_utils provides some necessary functions for this notebook.
  • testCases provides some test cases to assess the correctness of your functions
  • np.random.seed(1) is used to keep all the random function calls consistent. It will help us grade your work. Please don't change the seed.
import numpy as np
import h5py
import matplotlib.pyplot as plt
from testCases_v4a import *
from dnn_utils_v2 import sigmoid, sigmoid_backward, relu, relu_backward

%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

To build your neural network, you will be implementing several "helper functions". These helper functions will be used in the next assignment to build a two-layer neural network and an L-layer neural network. Each small helper function you will implement will have detailed instructions that will walk you through the necessary steps. Here is an outline of this assignment, you will:

  • Initialize the parameters for a two-layer network and for an LL-layer neural network.
  • Implement the forward propagation module (shown in purple in the figure below).
    • Complete the LINEAR part of a layer's forward propagation step (resulting in Z[l]Z^{[l]}).
    • We give you the ACTIVATION function (relu/sigmoid).
    • Combine the previous two steps into a new [LINEAR->ACTIVATION] forward function.
    • Stack the [LINEAR->RELU] forward function L-1 time (for layers 1 through L-1) and add a [LINEAR->SIGMOID] at the end (for the final layer LL). This gives you a new L_model_forward function.
  • Compute the loss.
  • Implement the backward propagation module (denoted in red in the figure below).
    • Complete the LINEAR part of a layer's backward propagation step.
    • We give you the gradient of the ACTIVATE function (relu_backward/sigmoid_backward)
    • Combine the previous two steps into a new [LINEAR->ACTIVATION] backward function.
    • Stack [LINEAR->RELU] backward L-1 times and add [LINEAR->SIGMOID] backward in a new L_model_backward function
  • Finally update the parameters.
alt
**Figure 1**

Note that for every forward function, there is a corresponding backward function. That is why at every step of your forward module you will be storing some values in a cache. The cached values are useful for computing gradients. In the backpropagation module you will then use the cache to calculate the gradients. This assignment will show you exactly how to carry out each of these steps.