2018-02-02  1,443 views 评论

Part 1：Building your Deep Neural Network: Step by Step

1 - Packages

Let’s first import all the packages that you will need duri

ng 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.

2 – 大作业框架

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 L-layer neural network.（初始化2层和L层神经网络的参数）

• 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]).（完成一层前向传播的线性部分）

• 我们给你了激活函数(relu/sigmoid).

• 将之前的两步混合加入一个新的【线性->激活】前向传递函数。

• 堆叠【线性->激活】前向传递函数并且增加一个【线性->SIGMOID】在最后一层。这完成了一个新的L_model前向传播函数。

• 计算损失函数.

• 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.

3 – 初始化

3.1 - 2层神经网络

Exercise: Create and initialize the parameters of the 2-layer neural network.

Instructions:

• The model's structure is: LINEAR -> RELU -> LINEAR -> SIGMOID.

• Use random initialization for the weight matrices. Use np.random.randn(shape)*0.01 with the correct shape.

• Use zero initialization for the biases. Use np.zeros(shape).

3.2 - L层神经网络

The initialization for a deeper L-layer neural network is more complicated because there are many more weight matrices and bias vectors. When completing the initialize_parameters_deep, 你应当确保你的维度符合每一层的维度. 调用的 n[l]是l层中每一个节点（单元）的个数。 Thus for example if the size of our input Xis (12288,209) (with m=209 examples) then:

Exercise: Implement initialization for an L-layer Neural Network.

Instructions:

• The model's structure is [LINEAR -> RELU] ×(L-1) -> LINEAR -> SIGMOID. I.e., it has L−1 layers using a ReLU activation function followed by an output layer with a sigmoid activation function.

• Use random initialization for the weight matrices. Use np.random.rand(shape) * 0.01.

• Use zeros initialization for the biases. Use np.zeros(shape).

• We will store n[l], the number of units in different layers, in a variable layer_dims. For example, the layer_dims for the "Planar Data classification model" from last week would have been [2,4,1]: There were two inputs, one hidden layer with 4 hidden units, and an output layer with 1 output unit. Thus means W1's shape was (4,2), b1 was (4,1), W2 was (1,4) and b2 was (1,1). Now you will generalize this to L layers!

• Here is the implementation for L=1 (one layer neural network). It should inspire you to implement the general case (L-layer neural network).

4 – 前向传播模型

4.1 – 线性前向传播

Now that you have initialized your parameters, you will do the forward propagation module. You will start by implementing some basic functions that you will use later when implementing the model. You will complete three functions in this order:

• LINEAR

• LINEAR -> ACTIVATION where      ACTIVATION will be either ReLU or Sigmoid.

• [LINEAR -> RELU] ×× (L-1) -> LINEAR -> SIGMOID (whole model)

The linear forward module (vectorized over all the examples) computes the following equations:

Z[l]=W[l]A[l1]+b[l]

where A[0]=X

Exercise: Build the linear part of forward propagation.

Reminder: The mathematical representation of this unit is Z[l]=W[l]A[l−1]+b[l]. You may also find np.dot() useful. If your dimensions don't match, printing W.shape may help.

4.2 -线性激活函数前向传播

In this notebook, you will use two activation functions:

• Sigmoid: σ(Z)=σ(WA+b)=11+e−(WA+b).      We have provided you with the sigmoid function.      This function returns two items: the activation      value “a” and a “cache” that contains “Z” (it’s what we      will feed in to the corresponding backward function). To use it you could      just call:

• ReLU:（修正线性单元） The mathematical formula for ReLu is A=RELU(Z)=max(0,Z).      We have provided you with the relu function.      This function returns two items: the activation      value “A” and a “cache” that contains “Z” (it’s what we      will feed in to the corresponding backward function). To use it you could      just call:

For more convenience, you are going to group two functions (Linear and Activation) into one function (LINEAR->ACTIVATION). Hence, you will implement a function that does the LINEAR forward step followed by an ACTIVATION forward step.

Exercise: Implement the forward propagation of the LINEAR->ACTIVATION layer. Mathematical relation is: A[l]=g(Z[l])=g(W[l]A[l1]+b[l]) where the activation “g” can be sigmoid() or relu(). Use linear_forward() and the correct activation function.

d) L层模型

For even more convenience when implementing the LL-layer Neural Net, you will need a function that replicates the previous one (linear_activation_forward with RELU) L1L1 times, then follows that with one linear_activation_forward with SIGMOID.

Figure 2 : [LINEAR -> RELU] ×× (L-1) -> LINEAR -> SIGMOID model

Exercise: Implement the forward propagation of the above model.

Instruction: In the code below, the variable AL will denote A[L]=σ(Z[L])=σ(W[L]A[L−1]+b[L])

. (This is sometimes also called Yhat, i.e., this is Y^.)

Tips:

• Use the functions      you had previously written

• Use a for      loop to replicate [LINEAR->RELU] (L-1) times

• Don't forget      to keep track of the caches in the "caches" list. To add a new      value c to a list, you can use list.append(c).

5 – 代价函数

Now you will implement forward and backward propagation. You need to compute the cost, because you want to check if your model is actually learning.

Exercise: Compute the cross-entropy cost JJ, using the following formula:

1mi=1m(y(i)log(a[L](i))+(1y(i))log(1a[L](i)))

6 - 反向传播模块

Just like with forward propagation, you will implement helper functions for backpropagation. Remember that back propagation is used to calculate the gradient of the loss function with respect to the parameters.

Reminder:

Figure 3 : Forward and Backward propagation for LINEAR->RELU->LINEAR->SIGMOID
The purple blocks represent the forward propagation, and the red blocks represent the backward propagation.

Now, similar to forward propagation, you are going to build the backward propagation in three steps:

• LINEAR backward

• LINEAR -> ACTIVATION backward where ACTIVATION computes the derivative of either the ReLU or sigmoid activation

• [LINEAR ->RELU] × (L-1) -> LINEAR -> SIGMOID backward(whole model)

6.1 – 线性反馈

Exercise: Use the 3 formulas above to implement linear_backward().

6.2 – 线性激活函数反馈

Next, you will create a function that merges the two helper functions: linear_backward and the backward step for the activation linear_activation_backward.

To help you implement linear_activation_backward, we provided two backward functions:

• sigmoid_backward: Implements the backward propagation for SIGMOID unit. You can      call it as follows:

• relu_backward: Implements the backward propagation for RELU      unit. You can call it as follows:

6.3 – L层模型的反馈

Now you will implement the backward function for the whole network. Recall that when you implemented the L_model_forward function, at each iteration, you stored a cache which contains (X,W,b, and z). In the back propagation module, you will use those variables to compute the gradients. Therefore, in the L_model_backward function, you will iterate through all the hidden layers backward, starting from layer L. On each step, you will use the cached values for layer l to backpropagate through layerl. Figure 5 below shows the backward pass.

• 两层神经网络

• L层神经网络