+#
+#
+
+# ### 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)$ (with $m=209$ examples) then:
+#
+#
+#
+#
+#
+#
+#
**Shape of W**
+#
**Shape of b**
+#
**Activation**
+#
**Shape of Activation**
+#
+#
+#
+#
**Layer 1**
+#
$(n^{[1]},12288)$
+#
$(n^{[1]},1)$
+#
$Z^{[1]} = W^{[1]} X + b^{[1]} $
+#
+#
$(n^{[1]},209)$
+#
+#
+#
+#
**Layer 2**
+#
$(n^{[2]}, n^{[1]})$
+#
$(n^{[2]},1)$
+#
$Z^{[2]} = W^{[2]} A^{[1]} + b^{[2]}$
+#
$(n^{[2]}, 209)$
+#
+#
+#
+#
$\vdots$
+#
$\vdots$
+#
$\vdots$
+#
$\vdots$
+#
$\vdots$
+#
+#
+#
+#
**Layer L-1**
+#
$(n^{[L-1]}, n^{[L-2]})$
+#
$(n^{[L-1]}, 1)$
+#
$Z^{[L-1]} = W^{[L-1]} A^{[L-2]} + b^{[L-1]}$
+#
$(n^{[L-1]}, 209)$
+#
+#
+#
+#
+#
**Layer L**
+#
$(n^{[L]}, n^{[L-1]})$
+#
$(n^{[L]}, 1)$
+#
$Z^{[L]} = W^{[L]} A^{[L-1]} + b^{[L]}$
+#
$(n^{[L]}, 209)$
+#
+#
+#
+#
+# Remember that when we compute $W X + b$ in python, it carries out broadcasting. For example, if:
+#
+# $$ W = \begin{bmatrix}
+# j & k & l\\
+# m & n & o \\
+# p & q & r
+# \end{bmatrix}\;\;\; X = \begin{bmatrix}
+# a & b & c\\
+# d & e & f \\
+# g & h & i
+# \end{bmatrix} \;\;\; b =\begin{bmatrix}
+# s \\
+# t \\
+# u
+# \end{bmatrix}\tag{2}$$
+#
+# Then $WX + b$ will be:
+#
+# $$ WX + b = \begin{bmatrix}
+# (ja + kd + lg) + s & (jb + ke + lh) + s & (jc + kf + li)+ s\\
+# (ma + nd + og) + t & (mb + ne + oh) + t & (mc + nf + oi) + t\\
+# (pa + qd + rg) + u & (pb + qe + rh) + u & (pc + qf + ri)+ u
+# \end{bmatrix}\tag{3} $$
+
+#
+# **Instructions**:
+# - The model's structure is *[LINEAR -> RELU] $ \times$ (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).
+# ```python
+# 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))
+# ```
+
+# In[4]:
+
+# GRADED FUNCTION: initialize_parameters_deep
+
+def initialize_parameters_deep(layer_dims):
+ """
+ Arguments:
+ layer_dims -- python array (list) containing the dimensions of each layer in our network
+
+ Returns:
+ parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL":
+ Wl -- weight matrix of shape (layer_dims[l], layer_dims[l-1])
+ bl -- bias vector of shape (layer_dims[l], 1)
+ """
+
+ np.random.seed(3)
+ parameters = {}
+ L = len(layer_dims) # number of layers in the network
+
+ for l in range(1, L):
+ ### START CODE HERE ### (≈ 2 lines of code)
+ parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l - 1]) * 0.01
+ parameters['b' + str(l)] = np.zeros((layer_dims[l], 1))
+ ### END CODE HERE ###
+
+ assert (parameters['W' + str(l)].shape == (layer_dims[l], layer_dims[l - 1]))
+ assert (parameters['b' + str(l)].shape == (layer_dims[l], 1))
+
+ return parameters
+
+
+# In[5]:
+
+parameters = initialize_parameters_deep([5, 4, 3])
+print("W1 = " + str(parameters["W1"]))
+print("b1 = " + str(parameters["b1"]))
+print("W2 = " + str(parameters["W2"]))
+print("b2 = " + str(parameters["b2"]))
+
+
+# **Expected output**:
+#
+#
+
+# ## 4 - Forward propagation module
+#
+# ### 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] $\times$ (L-1) -> LINEAR -> SIGMOID (whole model)
+#
+# The linear forward module (vectorized over all the examples) computes the following equations:
+#
+# $$Z^{[l]} = W^{[l]}A^{[l-1]} +b^{[l]}\tag{4}$$
+#
+# 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.
+
+# In[8]:
+
+# GRADED FUNCTION: linear_forward
+
+def linear_forward(A, W, b):
+ """
+ Implement the linear part of a layer's forward propagation.
+ Arguments:
+ A -- activations from previous layer (or input data): (size of previous layer, number of examples)
+ W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
+ b -- bias vector, numpy array of shape (size of the current layer, 1)
+ Returns:
+ Z -- the input of the activation function, also called pre-activation parameter
+ cache -- a python dictionary containing "A", "W" and "b" ; stored for computing the backward pass efficiently
+ """
+
+ ### START CODE HERE ### (≈ 1 line of code)
+ Z = np.dot(W, A) + b
+ ### END CODE HERE ###
+
+ assert (Z.shape == (W.shape[0], A.shape[1]))
+ cache = (A, W, b)
+
+ return Z, cache
+
+
+# In[9]:
+
+A, W, b = linear_forward_test_case()
+
+Z, linear_cache = linear_forward(A, W, b)
+print("Z = " + str(Z))
+
+
+# **Expected output**:
+#
+#
+#
+#
+#
**Z**
+#
[[ 3.26295337 -1.23429987]]
+#
+#
+#
+
+# ### 4.2 - Linear-Activation Forward
+#
+# In this notebook, you will use two activation functions:
+#
+# - **Sigmoid**: $\sigma(Z) = \sigma(W A + b) = \frac{1}{ 1 + e^{-(W A + 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:
+# ``` python
+# A, activation_cache = sigmoid(Z)
+# ```
+#
+# - **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:
+# ``` python
+# A, activation_cache = relu(Z)
+# ```
+
+# 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^{[l-1]} +b^{[l]})$ where the activation "g" can be sigmoid() or relu(). Use linear_forward() and the correct activation function.
+
+# In[10]:
+
+# GRADED FUNCTION: linear_activation_forward
+
+def linear_activation_forward(A_prev, W, b, activation):
+ """
+ Implement the forward propagation for the LINEAR->ACTIVATION layer
+ Arguments:
+ A_prev -- activations from previous layer (or input data): (size of previous layer, number of examples)
+ W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
+ b -- bias vector, numpy array of shape (size of the current layer, 1)
+ activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"
+ Returns:
+ A -- the output of the activation function, also called the post-activation value
+ cache -- a python dictionary containing "linear_cache" and "activation_cache";
+ stored for computing the backward pass efficiently
+ """
+
+ if activation == "sigmoid":
+ # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
+ ### START CODE HERE ### (≈ 2 lines of code)
+ Z, linear_cache = linear_forward(A_prev, W, b)
+ A, activation_cache = sigmoid(Z)
+ ### END CODE HERE ###
+
+ elif activation == "relu":
+ # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
+ ### START CODE HERE ### (≈ 2 lines of code)
+ Z, linear_cache = linear_forward(A_prev, W, b)
+ A, activation_cache = relu(Z)
+ ### END CODE HERE ###
+
+ assert (A.shape == (W.shape[0], A_prev.shape[1]))
+ cache = (linear_cache, activation_cache)
+
+ return A, cache
+
+
+# In[11]:
+
+A_prev, W, b = linear_activation_forward_test_case()
+
+A, linear_activation_cache = linear_activation_forward(A_prev, W, b, activation="sigmoid")
+print("With sigmoid: A = " + str(A))
+
+A, linear_activation_cache = linear_activation_forward(A_prev, W, b, activation="relu")
+print("With ReLU: A = " + str(A))
+
+# **Expected output**:
+#
+#
+#
+#
**With sigmoid: A **
+#
[[ 0.96890023 0.11013289]]
+#
+#
+#
**With ReLU: A **
+#
[[ 3.43896131 0. ]]
+#
+#
+#
+
+# **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
+#
+def L_model_forward(X, parameters):
+ """
+ Implement forward propagation for the [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID computation
+
+ Arguments:
+ X -- data, numpy array of shape (input size, number of examples)
+ parameters -- output of initialize_parameters_deep()
+
+ Returns:
+ AL -- last post-activation value
+ caches -- list of caches containing:
+ every cache of linear_relu_forward() (there are L-1 of them, indexed from 0 to L-2)
+ the cache of linear_sigmoid_forward() (there is one, indexed L-1)
+ """
+
+ caches = []
+ A = X
+ L = len(parameters) // 2 # number of layers in the neural network
+
+ # Implement [LINEAR -> RELU]*(L-1). Add "cache" to the "caches" list.
+ for l in range(1, L):
+ A_prev = A
+ ### START CODE HERE ### (≈ 2 lines of code)
+ A, cache = linear_activation_forward(A_prev, parameters['W' + str(l)], parameters['b' + str(l)],
+ activation="relu")
+ caches.append(cache)
+ ### END CODE HERE ###
+
+ # Implement LINEAR -> SIGMOID. Add "cache" to the "caches" list.
+ ### START CODE HERE ### (≈ 2 lines of code)
+ AL, cache = linear_activation_forward(A, parameters['W' + str(L)], parameters['b' + str(L)], activation="sigmoid")
+ caches.append(cache)
+ ### END CODE HERE ###
+
+ assert (AL.shape == (1, X.shape[1]))
+
+ return AL, caches
+
+
+# In[25]:
+
+X, parameters = L_model_forward_test_case()
+AL, caches = L_model_forward(X, parameters)
+print("AL = " + str(AL))
+print("Length of caches list = " + str(len(caches)))
+
+
+#
+#
+#
**AL**
+#
[[ 0.17007265 0.2524272 ]]
+#
+#
+#
**Length of caches list **
+#
2
+#
+#
+
+# GRADED FUNCTION: compute_cost
+
+def compute_cost(AL, Y):
+ """
+ Implement the cost function defined by equation (7).
+ Arguments:
+ AL -- probability vector corresponding to your label predictions, shape (1, number of examples)
+ Y -- true "label" vector (for example: containing 0 if non-cat, 1 if cat), shape (1, number of examples)
+ Returns:
+ cost -- cross-entropy cost
+ """
+
+ m = Y.shape[1]
+
+ # Compute loss from aL and y.
+ ### START CODE HERE ### (≈ 1 lines of code)
+ cost = -1 / m * (np.dot(Y, np.log(AL.T)) + np.dot(1 - Y, np.log(1 - AL).T))
+ ### END CODE HERE ###
+
+ cost = np.squeeze(cost) # To make sure your cost's shape is what we expect (e.g. this turns [[17]] into 17).
+ assert (cost.shape == ())
+
+ return cost
+
+
+# In[29]:
+
+Y, AL = compute_cost_test_case()
+
+print("cost = " + str(compute_cost(AL, Y)))
+
+
+# **Expected Output**:
+#
+#
+#
+#
+#
**cost**
+#
0.41493159961539694
+#
+#
+def linear_backward(dZ, cache):
+ """
+ Implement the linear portion of backward propagation for a single layer (layer l)
+ Arguments:
+ dZ -- Gradient of the cost with respect to the linear output (of current layer l)
+ cache -- tuple of values (A_prev, W, b) coming from the forward propagation in the current layer
+ Returns:
+ dA_prev -- Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev
+ dW -- Gradient of the cost with respect to W (current layer l), same shape as W
+ db -- Gradient of the cost with respect to b (current layer l), same shape as b
+ """
+ A_prev, W, b = cache
+ m = A_prev.shape[1]
+
+ ### START CODE HERE ### (≈ 3 lines of code)
+ dW = 1 / m * (np.dot(dZ, A_prev.T))
+ db = 1 / m * (np.sum(dZ, axis=1, keepdims=True))
+ dA_prev = np.dot(W.T, dZ)
+ ### END CODE HERE ###
+
+ assert (dA_prev.shape == A_prev.shape)
+ assert (dW.shape == W.shape)
+ assert (db.shape == b.shape)
+
+ return dA_prev, dW, db
+
+
+# In[33]:
+
+# Set up some test inputs
+dZ, linear_cache = linear_backward_test_case()
+
+dA_prev, dW, db = linear_backward(dZ, linear_cache)
+print("dA_prev = " + str(dA_prev))
+print("dW = " + str(dW))
+print("db = " + str(db))
+
+
+# **Expected Output**:
+#
+#
+#
+#
+def linear_activation_backward(dA, cache, activation):
+ """
+ Implement the backward propagation for the LINEAR->ACTIVATION layer.
+
+ Arguments:
+ dA -- post-activation gradient for current layer l
+ cache -- tuple of values (linear_cache, activation_cache) we store for computing backward propagation efficiently
+ activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"
+
+ Returns:
+ dA_prev -- Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev
+ dW -- Gradient of the cost with respect to W (current layer l), same shape as W
+ db -- Gradient of the cost with respect to b (current layer l), same shape as b
+ """
+ linear_cache, activation_cache = cache
+
+ if activation == "relu":
+ ### START CODE HERE ### (≈ 2 lines of code)
+ dZ = relu_backward(dA, activation_cache)
+ dA_prev, dW, db = linear_backward(dZ, linear_cache)
+ ### END CODE HERE ###
+
+ elif activation == "sigmoid":
+ ### START CODE HERE ### (≈ 2 lines of code)
+ dZ = sigmoid_backward(dA, activation_cache)
+ dA_prev, dW, db = linear_backward(dZ, linear_cache)
+ ### END CODE HERE ###
+
+ return dA_prev, dW, db
+
+
+# In[35]:
+
+AL, linear_activation_cache = linear_activation_backward_test_case()
+
+dA_prev, dW, db = linear_activation_backward(AL, linear_activation_cache, activation="sigmoid")
+print("sigmoid:")
+print("dA_prev = " + str(dA_prev))
+print("dW = " + str(dW))
+print("db = " + str(db) + "\n")
+
+dA_prev, dW, db = linear_activation_backward(AL, linear_activation_cache, activation="relu")
+print("relu:")
+print("dA_prev = " + str(dA_prev))
+print("dW = " + str(dW))
+print("db = " + str(db))
+
+
+# **Expected output with sigmoid:**
+#
+#