吳恩達深度學習學習筆記——C2W1——神經網絡優化基礎及正則化——作業2——正則化

這裏主要梳理一下作業的主要內容和思路,完整作業文件可參考:

https://github.com/pandenghuang/Andrew-Ng-Deep-Learning-notes/tree/master/assignments/C2W1

作業完整截圖,參考本文結尾:作業完整截圖。

 

Regularization (正則化)

Welcome to the second assignment of this week. Deep Learning models have so much flexibility and capacity that overfitting can be a serious problem, if the training dataset is not big enough. Sure it does well on the training set, but the learned network doesn't generalize to new examples that it has never seen!

You will learn to: Use regularization in your deep learning models.

...

 

1 - Non-regularized model (未使用正則化的模型)

You will use the following neural network (already implemented for you below). This model can be used:

  • in regularization mode -- by setting the lambd input to a non-zero value. We use "lambd" instead of "lambda" because "lambda" is a reserved keyword in Python.
  • in dropout mode -- by setting the keep_prob to a value less than one

You will first try the model without any regularization. Then, you will implement:

  • L2 regularization -- functions: "compute_cost_with_regularization()" and "backward_propagation_with_regularization()"
  • Dropout -- functions: "forward_propagation_with_dropout()" and "backward_propagation_with_dropout()"

...

 

2 - L2 Regularization (L2範數正則化)

...

Observations:

  • The value of 𝜆 is a hyperparameter that you can tune using a dev set.
  • L2 regularization makes your decision boundary smoother. If 𝜆 is too large, it is also possible to "oversmooth", resulting in a model with high bias.

What is L2-regularization actually doing?:

L2-regularization relies on the assumption that a model with small weights is simpler than a model with large weights. Thus, by penalizing the square values of the weights in the cost function you drive all the weights to smaller values. It becomes too costly for the cost to have large weights! This leads to a smoother model in which the output changes more slowly as the input changes.

What you should remember
-- the implications of L2-regularization on:
- The cost computation: A regularization term is added to the cost
- The backpropagation function: There are extra terms in the gradients with respect to weight matrices
- Weights end up smaller ("weight decay"): Weights are pushed to smaller values.



...

 

3 - Dropout (隨機失活)

Finally, dropout is a widely used regularization technique that is specific to deep learning. It randomly shuts down some neurons in each iteration. Watch these two videos to see what this means!

At each iteration, you shut down (= set to zero) each neuron of a layer with probability 1−𝑘𝑒𝑒𝑝_𝑝𝑟𝑜𝑏 or keep it with probability 𝑘𝑒𝑒𝑝_𝑝𝑟𝑜𝑏 (50% here). The dropped neurons don't contribute to the training in both the forward and backward propagations of the iteration.

When you shut some neurons down, you actually modify your model. The idea behind drop-out is that at each iteration, you train a different model that uses only a subset of your neurons. With dropout, your neurons thus become less sensitive to the activation of one other specific neuron, because that other neuron might be shut down at any time.

...

3.1 - Forward propagation with dropout (使用隨機失活的前向傳播)

Exercise: Implement the forward propagation with dropout. You are using a 3 layer neural network, and will add dropout to the first and second hidden layers. We will not apply dropout to the input layer or output layer.

Instructions: You would like to shut down some neurons in the first and second layers. To do that, you are going to carry out 4 Steps:

  1. In lecture, we dicussed creating a variable 𝑑[1]d[1] with the same shape as 𝑎[1]a[1] using np.random.rand() to randomly get numbers between 0 and 1. Here, you will use a vectorized implementation, so create a random matrix 𝐷[1]=[𝑑[1](1)𝑑[1](2)...𝑑[1](𝑚)] of the same dimension as 𝐴[1].
  2. Set each entry of 𝐷[1] to be 0 with probability (1-keep_prob) or 1 with probability (keep_prob), by thresholding values in 𝐷[1] appropriately. Hint: to set all the entries of a matrix X to 0 (if entry is less than 0.5) or 1 (if entry is more than 0.5) you would do: X = (X < 0.5). Note that 0 and 1 are respectively equivalent to False and True.
  3. Set 𝐴[1]to 𝐴[1]∗𝐷[1] (You are shutting down some neurons). You can think of 𝐷[1] as a mask, so that when it is multiplied with another matrix, it shuts down some of the values.
  4. Divide 𝐴[1] by keep_prob. By doing this you are assuring that the result of the cost will still have the same expected value as without drop-out. (This technique is also called inverted dropout.)

...

 

3.2 - Backward propagation with dropout (使用隨機失活的反向傳播)

Exercise: Implement the backward propagation with dropout. As before, you are training a 3 layer network. Add dropout to the first and second hidden layers, using the masks 𝐷[1] and 𝐷[2] stored in the cache.

Instruction: Backpropagation with dropout is actually quite easy. You will have to carry out 2 Steps:

  1. You had previously shut down some neurons during forward propagation, by applying a mask 𝐷[1] to A1. In backpropagation, you will have to shut down the same neurons, by reapplying the same mask 𝐷[1] to dA1.
  2. During forward propagation, you had divided A1 by keep_prob. In backpropagation, you'll therefore have to divide dA1 by keep_prob again (the calculus interpretation is that if 𝐴[1] is scaled by keep_prob, then its derivative 𝑑𝐴[1] is also scaled by the same keep_prob).

...

 

Note:

  • common mistake when using dropout is to use it both in training and testing. You should use dropout (randomly eliminate nodes) only in training.
  • Deep learning frameworks like tensorflowPaddlePaddlekeras or caffe come with a dropout layer implementation. Don't stress - you will soon learn some of these frameworks.

What you should remember about dropout:
- Dropout is a regularization technique.
- You only use dropout during training. Don't use dropout (randomly eliminate nodes) during test time.
- Apply dropout both during forward and backward propagation.
- During training time, divide each dropout layer by keep_prob to keep the same expected value for the activations. For example, if keep_prob is 0.5, then we will on average shut down half the nodes, so the output will be scaled by 0.5 since only the remaining half are contributing to the solution. Dividing by 0.5 is equivalent to multiplying by 2. Hence, the output now has the same expected value. You can check that this works even when keep_prob is other values than 0.5.



...

 

4 - Conclusions (結論)

 

Here are the results of our three models:

model train accuracy test accuracy
3-layer NN without regularization 95% 91.5%
3-layer NN with L2-regularization 94% 93%
3-layer NN with dropout 93% 95%
 

Note that regularization hurts training set performance! This is because it limits the ability of the network to overfit to the training set. But since it ultimately gives better test accuracy, it is helping your system.

 

Congratulations for finishing this assignment! And also for revolutionizing French football. :-)

 
What we want you to remember from this notebook:
- Regularization will help you reduce overfitting.
- Regularization will drive your weights to lower values.
- L2 regularization and Dropout are two very effective regularization techniques.


 
 

作業完整截圖:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章