๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Coursera/Deep Learning Specialization

[Deep-Special] [Lec1] Week4. Deep Neural Network - Programming Assignment1. Building your Deep Neural Network

by Steve-Lee 2020. 11. 10.

๐Ÿ“” [Lec1] Neural Networks & Deep Learning

Building your Deep Neural Network: Step by Step

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” deep neural network๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ํ•„์š”ํ•œ ํ•จ์ˆ˜๋“ค์„ ๊ตฌํ˜„ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๊ณผ์ œ์—์„œ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค์Œ ๊ณผ์ œ์—์„œ๋Š” 'Cat vs Non-Cat ' Image Classification Task๋ฅผ ํ•ด๊ฒฐํ•ด ๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

 

Coursera Rule๋กœ ์ธํ•ด ์ •๋‹ต ์ฝ”๋“œ๋ฅผ ๊ณต๊ฐœํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ณผ์ œ๋ฅผ ํ•˜์‹œ๋ฉด์„œ ๊ถ๊ธˆํ•œ ์ ์ด ์žˆ์œผ์‹œ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ ์—ฐ๋ฝ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค!

๐Ÿ“ฉ leeyj0511@naver.com

๋Œ“๊ธ€๋„ ํ™˜์˜์ž…๋‹ˆ๋‹ค!๐Ÿ˜ƒ

 

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

Superscript[l], Superscript(i), Lowerscript(i)

Let's get started!

 

๊ทธ๋Ÿผ ์‹œ์ž‘ํ•ด๋ณด์ž! - Again, -20.11.10.Tue. pm4:00-

 

2. Outline of the Assignment

To buid your neural network, you'll be implementing several 'helper functions'. These helper functions will be used in the next assignment to build a two-layer neural network and L-layer neural network.

Specifics

  • Initialize the parameters for a two-layer network and for an L-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]).
    • 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 L-layer ). 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.

์ด๋ฒˆ ์‹œ๊ฐ„์— ๊ตฌํ˜„ํ•  ๊ณผ์ œ์ž…๋‹ˆ๋‹ค

 

  • ์˜ˆ๋ฅผ ๋“ค์–ด 3๊ฐœ์˜ layer๋กœ ๊ตฌ์„ฑ๋œ Neural Network๋ผ๋ฉด n-1์ธ 2๊ฐœ์˜ layer๋Š” Linear ReLU Forward๋กœ ๊ตฌํ˜„ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰ layer๋Š” Linear Sigmoid Forward๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

 

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.

⇒ Forward Function์— ๋Œ€์‘ํ•˜๋Š” Backward Function์ด ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ Forward๋งˆ๋‹ค cache์— ๊ฐ’์„ ์ž˜ ์ €์žฅํ•ด ์ค˜์•ผ ํ•œ๋‹ค. (๊ฐ•์˜์—์„œ ๊ณ„์†ํ•ด์„œ ๊ฐ•์กฐํ–ˆ๋˜ ๋ถ€๋ถ„๊ณผ ๊ฐ™๋‹ค)

3. Initialization

๋‘๊ฐœ์˜ helper function์„ ์ž‘์„ฑํ•  ๊ฒƒ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ•จ์ˆ˜๋Š” 2-layer ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ดˆ๊ธฐํ™” ์‹œํ‚ค๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ํ•จ์ˆ˜๋Š” L-layer์˜ ์ดˆ๊ธฐํ™” ๊ณผ์ •์„ ์ผ๋ฐ˜ํ™” ํ•ฉ๋‹ˆ๋‹ค.

(๊ณผ์ œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •์—์„œ ํ•จ์ˆ˜์˜ ์—ญํ• ์„ ๋ณด๋‹ค ์ž˜ ์ดํ•ดํ•˜์‹ค ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค!)

3.1 - 2-layer Neural Network

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-layer Neural Network

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, you should make sure that your dimensions match between each layer. Recall that n[l] is the number of units in layer l. Thus for example if the size of our input X is (12288,209)(12288,209) (with m=209m=209 examples) then:

Instructions:

  • L-1๋ฒˆ์˜ [LINEAR → RELU] ๋ธ”๋Ÿญ๊ณผ 1๋ฒˆ์˜ [LINEAR→SIGMOID]๋ธ”๋Ÿญ์ด ์žˆ๋‹ค

  • Use random initialization for the weight matrices. Use np.random.randn(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. This 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  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).

    if L == 1: parameters["W" + str(L)] = np.random.randn(layer_dims[1], layer_dims[0]) * 0.01 parameters["b" + str(L)] = np.zeros((layer_dims[1], 1))

    ⇒ ์š”๊ตฌ์‚ฌํ•ญ์ด ๋งŽ์œผ๋ฉด ์œผ์—‘...๐Ÿ˜ฌ์ธ๋ฐ... ๊ฒŒ๋‹ค๊ฐ€ ์˜์–ด๋‹ค... ๐Ÿคช -> ๊ณผ์ œ๋ฅผ ๋งˆ์น˜๊ณ  ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ณผ์ œ๋ฅผ ๋ฆฌ๋ทฐํ•˜๋‹ˆ๊น ์ดํ•ด๊ฐ€ ๊ฐ€๋”๊ตฐ์š”...ใ…Žใ…Ž

4 - Forward propagation module

โžก๏ธ Process

  • Weight Initialization → Linear Forward

4.1 - Linear Forward

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:

⇒ โญ ๋•กํ ๋ฌธ์ œ๋‹ค.!

4.2 - Linear-Activation Forward

In this notebook, you will use two activation functions:

  • Sigmoid: σ(Z)=σ(WA+b)=11+e−(WA+b)σ(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:

    A, activation_cache = sigmoid(Z)
  • ReLU: The mathematical formula for ReLu is A=RELU(Z)=max(0,Z)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:

    A, activation_cache = relu(Z)

Note: In deep learning, the "[LINEAR->ACTIVATION]" computation is counted as a single layer in the neural network, not two layers.

d) L-Layer Model

For even more convenience when implementing the LLlayer Neural Net, you will need a function that replicates the previous one (linear_activation_forward with RELU) L−1L−1 times, then follows that with one linear_activation_forward with SIGMOID.

⇒ L-1๋ฒˆ์จฐ layer์™€ L๋ฒˆ์จฐ layer๋ฅผ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” Function์„ ๊ตฌํ˜„ํ•ด๋ณด์ž!

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 - Cost function

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:


์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ Backprop์ž…๋‹ˆ๋‹ค. ์ˆจ์„ ํฌ๊ฒŒ ๋‚ด์‰ฌ๊ณ  ์ง‘์ค‘ํ•ฉ์‹œ๋‹ค! - 20.11.10.Tue. pm4:30-


6 - Backward propagation module

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.

 

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]X (L-1) -> LINEAR -> SIGMOID backward (whole model)

6.1 - Linear backward

For layer l, the linear part is: Z[l]=W[l]A[l−1]+b[l]Z[l]=W[l]A[l−1]+b[l] (followed by an activation).

Suppose you have already calculated the derivative dZ[l]=∂๎ˆธ∂Z[l]dZ[l]=∂L∂Z[l]. You want to get (dW[l],db[l],dA[l−1])(dW[l],db[l],dA[l−1]).

6-2. Linear-Ac

tivation backward

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:

dZ = sigmoid_backward(dA, activation_cache)

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

dZ = relu_backward(dA, activation_cache)

If g(.)g(.) is the activation function, sigmoid_backward and relu_backward compute

6.3 - L-Model Backward

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 LL . On each step, you will use the cached values for layer ll to backpropagate through layer ll . Figure 5 below shows the backward pass.

์ด๊ฑฐ ์–ด๋ ต๋‹ค... 6.3!! - 20.11.10.Tue

๐Ÿ™‹‍โ™‚๏ธ๐Ÿ™‹‍โ™‚๏ธ๐Ÿ™‹‍โ™‚๏ธ ๐Ÿ™‡‍โ™‚๏ธ๐Ÿ™‡‍โ™‚๏ธ๐Ÿ™‡‍โ™‚๏ธ

 

Summary

  • Neural Networks and Deep Learning์˜ ๋งˆ์ง€๋ง‰ ์ฃผ ๊ณผ์ œ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค
  • ํ•ด๋‹น ๊ณผ์ œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ทธ๋™์•ˆ ๋ฐฐ์› ๋˜ ์ง€์‹๋“ค์„ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋กœ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ๊ฒƒ์„ ๊ฒฝํ—˜ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค
  • ํŠนํžˆ Shallow Network(2-layer) ๋ฟ๋งŒ์•„๋‹ˆ๋ผ L-layer(L-๊ฐœ์˜ layer)๋ฅผ ํ•™์Šต์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ์š”. ํ–ฅํ›„ Advancedํ•œ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ณ  ๊ตฌํ˜„ํ•  ๋•Œ ์ด๋Ÿฌํ•œ ๊ฒฝํ—˜์ด ๋„์›€์ด ๋  ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค
  • ์ง์ ‘ ๊ณ ๋ฏผํ•˜๊ณ  ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •์„ ํ†ตํ•ด ํ•œ ๋‹จ๊ณ„ ์„ฑ์žฅํ•  ์ˆ˜ ์žˆ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค

๋Œ“๊ธ€