|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Gradient Checking" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 10, |
| 13 | + "metadata": { |
| 14 | + "collapsed": true |
| 15 | + }, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "# Packages\n", |
| 19 | + "import numpy as np\n", |
| 20 | + "from test import *\n", |
| 21 | + "from Supporting_func import sigmoid, relu, dictionary_to_vector, vector_to_dictionary, gradients_to_vector" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "## N-dimensional gradient checking" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "metadata": { |
| 34 | + "collapsed": true |
| 35 | + }, |
| 36 | + "source": [ |
| 37 | + "The following figure describes the forward and backward propagation of your fraud detection model.\n", |
| 38 | + "\n", |
| 39 | + "<img src=\"images/NDgrad_kiank.png\" style=\"width:600px;height:400px;\">\n", |
| 40 | + "<caption><center> <u> **Figure** </u><br>*LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID*</center></caption>" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 17, |
| 46 | + "metadata": { |
| 47 | + "collapsed": true |
| 48 | + }, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "def forward_propagation_n(X, Y, parameters):\n", |
| 52 | + " \"\"\"\n", |
| 53 | + " Implements the forward propagation (and computes the cost) presented in above Figure.\n", |
| 54 | + " \n", |
| 55 | + " Arguments:\n", |
| 56 | + " X -- training set for m examples\n", |
| 57 | + " Y -- labels for m examples \n", |
| 58 | + " parameters -- python dictionary containing your parameters \"W1\", \"b1\", \"W2\", \"b2\", \"W3\", \"b3\":\n", |
| 59 | + " W1 -- weight matrix of shape (5, 4)\n", |
| 60 | + " b1 -- bias vector of shape (5, 1)\n", |
| 61 | + " W2 -- weight matrix of shape (3, 5)\n", |
| 62 | + " b2 -- bias vector of shape (3, 1)\n", |
| 63 | + " W3 -- weight matrix of shape (1, 3)\n", |
| 64 | + " b3 -- bias vector of shape (1, 1)\n", |
| 65 | + " \n", |
| 66 | + " Returns:\n", |
| 67 | + " cost -- the cost function (logistic cost for one example)\n", |
| 68 | + " \"\"\"\n", |
| 69 | + " \n", |
| 70 | + " # retrieve parameters\n", |
| 71 | + " m = X.shape[1]\n", |
| 72 | + " W1 = parameters[\"W1\"]\n", |
| 73 | + " b1 = parameters[\"b1\"]\n", |
| 74 | + " W2 = parameters[\"W2\"]\n", |
| 75 | + " b2 = parameters[\"b2\"]\n", |
| 76 | + " W3 = parameters[\"W3\"]\n", |
| 77 | + " b3 = parameters[\"b3\"]\n", |
| 78 | + "\n", |
| 79 | + " # LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID\n", |
| 80 | + " Z1 = np.dot(W1, X) + b1\n", |
| 81 | + " A1 = relu(Z1)\n", |
| 82 | + " Z2 = np.dot(W2, A1) + b2\n", |
| 83 | + " A2 = relu(Z2)\n", |
| 84 | + " Z3 = np.dot(W3, A2) + b3\n", |
| 85 | + " A3 = sigmoid(Z3)\n", |
| 86 | + "\n", |
| 87 | + " # Cost\n", |
| 88 | + " logprobs = np.multiply(-np.log(A3),Y) + np.multiply(-np.log(1 - A3), 1 - Y)\n", |
| 89 | + " cost = 1./m * np.sum(logprobs)\n", |
| 90 | + " \n", |
| 91 | + " cache = (Z1, A1, W1, b1, Z2, A2, W2, b2, Z3, A3, W3, b3)\n", |
| 92 | + " \n", |
| 93 | + " return cost, cache" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": 30, |
| 99 | + "metadata": { |
| 100 | + "collapsed": true |
| 101 | + }, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "def backward_propagation_n(X, Y, cache):\n", |
| 105 | + " \"\"\"\n", |
| 106 | + " Implement the backward propagation presented in above figure.\n", |
| 107 | + " \n", |
| 108 | + " Arguments:\n", |
| 109 | + " X -- input datapoint, of shape (input size, 1)\n", |
| 110 | + " Y -- true \"label\"\n", |
| 111 | + " cache -- cache output from forward_propagation_n()\n", |
| 112 | + " \n", |
| 113 | + " Returns:\n", |
| 114 | + " gradients -- A dictionary with the gradients of the cost with respect to each parameter, activation and pre-activation variables.\n", |
| 115 | + " \"\"\"\n", |
| 116 | + " \n", |
| 117 | + " m = X.shape[1]\n", |
| 118 | + " (Z1, A1, W1, b1, Z2, A2, W2, b2, Z3, A3, W3, b3) = cache\n", |
| 119 | + " \n", |
| 120 | + " dZ3 = A3 - Y\n", |
| 121 | + " dW3 = 1./m * np.dot(dZ3, A2.T)\n", |
| 122 | + " db3 = 1./m * np.sum(dZ3, axis=1, keepdims = True)\n", |
| 123 | + " \n", |
| 124 | + " dA2 = np.dot(W3.T, dZ3)\n", |
| 125 | + " dZ2 = np.multiply(dA2, np.int64(A2 > 0))\n", |
| 126 | + " dW2 = 1./m * np.dot(dZ2, A1.T)*2\n", |
| 127 | + " db2 = 1./m * np.sum(dZ2, axis=1, keepdims = True)\n", |
| 128 | + " \n", |
| 129 | + " dA1 = np.dot(W2.T, dZ2)\n", |
| 130 | + " dZ1 = np.multiply(dA1, np.int64(A1 > 0))\n", |
| 131 | + " dW1 = 1./m * np.dot(dZ1, X.T)\n", |
| 132 | + " db1 = 4./m * np.sum(dZ1, axis=1, keepdims = True)\n", |
| 133 | + " \n", |
| 134 | + " gradients = {\"dZ3\": dZ3, \"dW3\": dW3, \"db3\": db3,\n", |
| 135 | + " \"dA2\": dA2, \"dZ2\": dZ2, \"dW2\": dW2, \"db2\": db2,\n", |
| 136 | + " \"dA1\": dA1, \"dZ1\": dZ1, \"dW1\": dW1, \"db1\": db1}\n", |
| 137 | + " \n", |
| 138 | + " return gradients" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "markdown", |
| 143 | + "metadata": {}, |
| 144 | + "source": [ |
| 145 | + "**How does gradient checking work?**.\n", |
| 146 | + "\n", |
| 147 | + "As you want to compare \"gradapprox\" to the gradient computed by backpropagation. The formula is still:\n", |
| 148 | + "\n", |
| 149 | + "$$ \\frac{\\partial J}{\\partial \\theta} = \\lim_{\\varepsilon \\to 0} \\frac{J(\\theta + \\varepsilon) - J(\\theta - \\varepsilon)}{2 \\varepsilon} \\tag{1}$$\n", |
| 150 | + "\n", |
| 151 | + "However, $\\theta$ is not a scalar anymore. It is a dictionary called \"parameters\". We implemented a function \"`dictionary_to_vector()`\" for you. It converts the \"parameters\" dictionary into a vector called \"values\", obtained by reshaping all parameters (W1, b1, W2, b2, W3, b3) into vectors and concatenating them.\n", |
| 152 | + "\n", |
| 153 | + "The inverse function is \"`vector_to_dictionary`\" which outputs back the \"parameters\" dictionary.\n", |
| 154 | + "\n", |
| 155 | + "<img src=\"images/dictionary_to_vector.png\" style=\"width:600px;height:400px;\">\n", |
| 156 | + "<caption><center> <u> **Figure 2** </u>: **dictionary_to_vector() and vector_to_dictionary()**<br> You will need these functions in gradient_check_n()</center></caption>\n", |
| 157 | + "\n", |
| 158 | + "I have also converted the \"gradients\" dictionary into a vector \"grad\" using gradients_to_vector().\n", |
| 159 | + "\n", |
| 160 | + "Here is pseudo-code that will help to implement the gradient check.\n", |
| 161 | + "\n", |
| 162 | + "For each i in num_parameters:\n", |
| 163 | + "- To compute `J_plus[i]`:\n", |
| 164 | + " 1. Set $\\theta^{+}$ to `np.copy(parameters_values)`\n", |
| 165 | + " 2. Set $\\theta^{+}_i$ to $\\theta^{+}_i + \\varepsilon$\n", |
| 166 | + " 3. Calculate $J^{+}_i$ using to `forward_propagation_n(x, y, vector_to_dictionary(`$\\theta^{+}$ `))`. \n", |
| 167 | + "- To compute `J_minus[i]`: do the same thing with $\\theta^{-}$\n", |
| 168 | + "- Compute $gradapprox[i] = \\frac{J^{+}_i - J^{-}_i}{2 \\varepsilon}$\n", |
| 169 | + "\n", |
| 170 | + "Thus, you get a vector gradapprox, where gradapprox[i] is an approximation of the gradient with respect to `parameter_values[i]`. You can now compare this gradapprox vector to the gradients vector from backpropagation. Just like for the 1D case (Steps 1', 2', 3'), compute: \n", |
| 171 | + "$$ difference = \\frac {\\| grad - gradapprox \\|_2}{\\| grad \\|_2 + \\| gradapprox \\|_2 } \\tag{3}$$" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": 31, |
| 177 | + "metadata": { |
| 178 | + "collapsed": true |
| 179 | + }, |
| 180 | + "outputs": [], |
| 181 | + "source": [ |
| 182 | + "# GRADED FUNCTION: gradient_check_n\n", |
| 183 | + "\n", |
| 184 | + "def gradient_check_n(parameters, gradients, X, Y, epsilon = 1e-7):\n", |
| 185 | + " \"\"\"\n", |
| 186 | + " Checks if backward_propagation_n computes correctly the gradient of the cost output by forward_propagation_n\n", |
| 187 | + " \n", |
| 188 | + " Arguments:\n", |
| 189 | + " parameters -- python dictionary containing your parameters \"W1\", \"b1\", \"W2\", \"b2\", \"W3\", \"b3\":\n", |
| 190 | + " grad -- output of backward_propagation_n, contains gradients of the cost with respect to the parameters. \n", |
| 191 | + " x -- input datapoint, of shape (input size, 1)\n", |
| 192 | + " y -- true \"label\"\n", |
| 193 | + " epsilon -- tiny shift to the input to compute approximated gradient with formula(1)\n", |
| 194 | + " \n", |
| 195 | + " Returns:\n", |
| 196 | + " difference -- difference (2) between the approximated gradient and the backward propagation gradient\n", |
| 197 | + " \"\"\"\n", |
| 198 | + " \n", |
| 199 | + " # Set-up variables\n", |
| 200 | + " parameters_values, _ = dictionary_to_vector(parameters)\n", |
| 201 | + " grad = gradients_to_vector(gradients)\n", |
| 202 | + " num_parameters = parameters_values.shape[0]\n", |
| 203 | + " J_plus = np.zeros((num_parameters, 1))\n", |
| 204 | + " J_minus = np.zeros((num_parameters, 1))\n", |
| 205 | + " gradapprox = np.zeros((num_parameters, 1))\n", |
| 206 | + " \n", |
| 207 | + " # Compute gradapprox\n", |
| 208 | + " for i in range(num_parameters):\n", |
| 209 | + " \n", |
| 210 | + " # Compute J_plus[i]. Inputs: \"parameters_values, epsilon\". Output = \"J_plus[i]\".\n", |
| 211 | + " # \"_\" is used because the function you have to outputs two parameters but we only care about the first one\n", |
| 212 | + " \n", |
| 213 | + " thetaplus = np.copy(parameters_values) # Step 1\n", |
| 214 | + " thetaplus[i][0] += epsilon # Step 2\n", |
| 215 | + " J_plus[i], _ = forward_propagation_n(X, Y, vector_to_dictionary(thetaplus)) # Step 3\n", |
| 216 | + " \n", |
| 217 | + " \n", |
| 218 | + " # Compute J_minus[i]. Inputs: \"parameters_values, epsilon\". Output = \"J_minus[i]\".\n", |
| 219 | + " \n", |
| 220 | + " thetaminus = np.copy(parameters_values) # Step 1\n", |
| 221 | + " thetaminus[i][0] -= epsilon # Step 2 \n", |
| 222 | + " J_minus[i], _ = forward_propagation_n(X, Y, vector_to_dictionary(thetaminus)) # Step 3\n", |
| 223 | + " \n", |
| 224 | + " \n", |
| 225 | + " # Compute gradapprox[i]\n", |
| 226 | + " \n", |
| 227 | + " gradapprox[i] = (J_plus[i]-J_minus[i])/(2*epsilon)\n", |
| 228 | + " \n", |
| 229 | + " \n", |
| 230 | + " # Compare gradapprox to backward propagation gradients by computing difference.\n", |
| 231 | + " \n", |
| 232 | + " numerator = np.linalg.norm(grad-gradapprox) # Step 1'\n", |
| 233 | + " denominator = np.linalg.norm(grad) + np.linalg.norm(gradapprox) # Step 2'\n", |
| 234 | + " difference = numerator/denominator # Step 3'\n", |
| 235 | + " \n", |
| 236 | + "\n", |
| 237 | + " if difference > 2e-7:\n", |
| 238 | + " print (\"\\033[93m\" + \"There is a mistake in the backward propagation! difference = \" + str(difference) + \"\\033[0m\")\n", |
| 239 | + " else:\n", |
| 240 | + " print (\"\\033[92m\" + \"Your backward propagation works perfectly fine! difference = \" + str(difference) + \"\\033[0m\")\n", |
| 241 | + " \n", |
| 242 | + " return difference" |
| 243 | + ] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "code", |
| 247 | + "execution_count": 32, |
| 248 | + "metadata": { |
| 249 | + "scrolled": false |
| 250 | + }, |
| 251 | + "outputs": [ |
| 252 | + { |
| 253 | + "name": "stdout", |
| 254 | + "output_type": "stream", |
| 255 | + "text": [ |
| 256 | + "\u001b[93mThere is a mistake in the backward propagation! difference = 0.285093156781\u001b[0m\n" |
| 257 | + ] |
| 258 | + } |
| 259 | + ], |
| 260 | + "source": [ |
| 261 | + "X, Y, parameters = gradient_check_n_test_case()\n", |
| 262 | + "\n", |
| 263 | + "cost, cache = forward_propagation_n(X, Y, parameters)\n", |
| 264 | + "gradients = backward_propagation_n(X, Y, cache)\n", |
| 265 | + "difference = gradient_check_n(parameters, gradients, X, Y)" |
| 266 | + ] |
| 267 | + }, |
| 268 | + { |
| 269 | + "cell_type": "markdown", |
| 270 | + "metadata": {}, |
| 271 | + "source": [ |
| 272 | + "**Expected output**:\n", |
| 273 | + "\n", |
| 274 | + "<table>\n", |
| 275 | + " <tr>\n", |
| 276 | + " <td> ** There is a mistake in the backward propagation!** </td>\n", |
| 277 | + " <td> difference = 0.285093156781 </td>\n", |
| 278 | + " </tr>\n", |
| 279 | + "</table>" |
| 280 | + ] |
| 281 | + }, |
| 282 | + { |
| 283 | + "cell_type": "markdown", |
| 284 | + "metadata": {}, |
| 285 | + "source": [ |
| 286 | + "It seems that there were errors in the `backward_propagation_n` code we gave you! Good that you've implemented the gradient check. Go back to `backward_propagation` and try to find/correct the errors *(Hint: check dW2 and db1)*. Rerun the gradient check when you think you've fixed it. Remember you'll need to re-execute the cell defining `backward_propagation_n()` if you modify the code. \n", |
| 287 | + "\n", |
| 288 | + "Can you get gradient check to declare your derivative computation correct? Even though this part of the assignment isn't graded, we strongly urge you to try to find the bug and re-run gradient check until you're convinced backprop is now correctly implemented. \n", |
| 289 | + "\n", |
| 290 | + "**Note** \n", |
| 291 | + "- Gradient Checking is slow! Approximating the gradient with $\\frac{\\partial J}{\\partial \\theta} \\approx \\frac{J(\\theta + \\varepsilon) - J(\\theta - \\varepsilon)}{2 \\varepsilon}$ is computationally costly. For this reason, we don't run gradient checking at every iteration during training. Just a few times to check if the gradient is correct. \n", |
| 292 | + "- Gradient Checking, at least as we've presented it, doesn't work with dropout. You would usually run the gradient check algorithm without dropout to make sure your backprop is correct, then add dropout. " |
| 293 | + ] |
| 294 | + }, |
| 295 | + { |
| 296 | + "cell_type": "code", |
| 297 | + "execution_count": null, |
| 298 | + "metadata": { |
| 299 | + "collapsed": true |
| 300 | + }, |
| 301 | + "outputs": [], |
| 302 | + "source": [] |
| 303 | + } |
| 304 | + ], |
| 305 | + "metadata": { |
| 306 | + "coursera": { |
| 307 | + "course_slug": "deep-neural-network", |
| 308 | + "graded_item_id": "n6NBD", |
| 309 | + "launcher_item_id": "yfOsE" |
| 310 | + }, |
| 311 | + "kernelspec": { |
| 312 | + "display_name": "Python 3", |
| 313 | + "language": "python", |
| 314 | + "name": "python3" |
| 315 | + }, |
| 316 | + "language_info": { |
| 317 | + "codemirror_mode": { |
| 318 | + "name": "ipython", |
| 319 | + "version": 3 |
| 320 | + }, |
| 321 | + "file_extension": ".py", |
| 322 | + "mimetype": "text/x-python", |
| 323 | + "name": "python", |
| 324 | + "nbconvert_exporter": "python", |
| 325 | + "pygments_lexer": "ipython3", |
| 326 | + "version": "3.7.1" |
| 327 | + } |
| 328 | + }, |
| 329 | + "nbformat": 4, |
| 330 | + "nbformat_minor": 1 |
| 331 | +} |
0 commit comments