|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Vanishing and Exploding Gradients\n", |
| 8 | + "\n", |
| 9 | + "\n", |
| 10 | + "We just learned how backpropagation through time works and we saw how the gradient of\n", |
| 11 | + "loss can be computed with respect to all the weights in RNN. But here we encounter a\n", |
| 12 | + "problem called the vanishing and exploding gradients.\n", |
| 13 | + "While computing derivatives of loss with respect to $W$ and $U$ , we saw that we have to\n", |
| 14 | + "traverse all the way back to the first hidden state, as each hidden state at a time $t$ is\n", |
| 15 | + "dependent on its previous hidden state at a time $t-1$ .\n", |
| 16 | + "\n", |
| 17 | + "\n", |
| 18 | + "For instance, say we compute the gradient of loss $L_2$ with respect to $W$:\n", |
| 19 | + "\n", |
| 20 | + "$$ \\frac{\\partial L_{2}}{\\partial W}=\\frac{\\partial L_{2}}{\\partial y_{2}} \\frac{\\partial y_{2}}{\\partial h_{2}} \\frac{\\partial h_{2}}{\\partial W}$$" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "If you look at the term $\\frac{\\partial h_{2}}{\\partial W}$ from the above equation, we can't calculate derivative\n", |
| 28 | + "of $h_2$ with respect to $W$ directly. Because if you see $h_{2}=\\tanh \\left(U x_{2}+W h_{1}\\right)$ is a \n", |
| 29 | + "function which is dependent on $h_1$ and $W$. So we need to calculate derivative with respect\n", |
| 30 | + "to $h_1$ as well. But even $h_{1}=\\tanh \\left(U x_{2}+W h_{0}\\right)_{i}$ is a function which is dependent on $h_0$\n", |
| 31 | + " and $W$. Thus we need to calculate derivative with respect to $h_0$ as well. \n", |
| 32 | + " \n", |
| 33 | + " \n", |
| 34 | + "As shown in the following figure - to compute the derivative of $L_2$ we need to go back all\n", |
| 35 | + "the way to the initial hidden state $h_0$ as each hidden state is dependent on its previous\n", |
| 36 | + "hidden state: \n", |
| 37 | + "\n", |
| 38 | + "\n", |
| 39 | + "\n", |
| 40 | + "So to compute any loss $L_j$ we need to traverse all the way back to the initial hidden state\n", |
| 41 | + " $h_0$ as each hidden state is dependent on its previous hidden state. Say we have a deep\n", |
| 42 | + "recurrent network with 50 layers. To compute the loss $L_{50}$ we need to traverse all the way\n", |
| 43 | + "back to $h_0$ as shown in the below figure.\n", |
| 44 | + "\n", |
| 45 | + "" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "metadata": {}, |
| 51 | + "source": [ |
| 52 | + "So exactly what is the problem here? While backpropagating towards the initial hidden\n", |
| 53 | + "state we lose information and the RNN will not backpropagate perfectly. \n", |
| 54 | + "\n", |
| 55 | + "\n", |
| 56 | + "Because remember $h_{t}=\\tanh \\left(U x_{t}+W h_{t-1}\\right)$ , every time we move backward, we\n", |
| 57 | + "compute the derivative of $h_t$. A derivative of tanh is bounded to 1. We know that any two\n", |
| 58 | + "values between 0 to 1, when multiplied with each other gives us a small number. We\n", |
| 59 | + "usually initialize the weights of the network to a small number. When we multiply the\n", |
| 60 | + "derivatives and weights while backpropagating we are essentially multiplying smaller\n", |
| 61 | + "numbers.\n", |
| 62 | + "\n", |
| 63 | + "So when we multiply smaller numbers at every step while moving backward our gradient\n", |
| 64 | + "becomes infinitesimally small and leads to a number which the computer can't handle and\n", |
| 65 | + "this is called __vanishing gradient problem.__\n", |
| 66 | + "\n", |
| 67 | + "\n", |
| 68 | + "Recall the gradient of the loss with respect of $W$ equation we saw in the previous section:\n", |
| 69 | + "\n", |
| 70 | + "\n", |
| 71 | + "\n", |
| 72 | + "As you can see we are multiplying weights and derivative of the tanh function at every\n", |
| 73 | + "time step. Repeated multiplication of these two leads to a small number and causes\n", |
| 74 | + "vanishing gradients problem.\n" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "markdown", |
| 79 | + "metadata": {}, |
| 80 | + "source": [ |
| 81 | + "\n", |
| 82 | + "Vanishing gradients problem occurs not only in RNN but also in other deep networks\n", |
| 83 | + "where we use sigmoid or tanh as the activation function. So to overcome this we can use\n", |
| 84 | + "ReLu as an activation function instead of tanh.\n", |
| 85 | + "However, we have a variant of the RNN called LSTM network which can solve the\n", |
| 86 | + "vanishing gradient problem effectively. We will see how it works in the next chapter.\n", |
| 87 | + "Similarly, when we initialize the weights of the network to a very large number, gradients\n", |
| 88 | + "would become very large at every step. While backpropagating we multiply a large\n", |
| 89 | + "number together at every time step and it leads to infinity. This is called the __ Exploding\n", |
| 90 | + "Gradient Problem. __" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "markdown", |
| 95 | + "metadata": {}, |
| 96 | + "source": [ |
| 97 | + "In the next section, we will learn how can we use gradient clipping to avoid vanishing gradients problem. " |
| 98 | + ] |
| 99 | + } |
| 100 | + ], |
| 101 | + "metadata": { |
| 102 | + "kernelspec": { |
| 103 | + "display_name": "Python [conda root]", |
| 104 | + "language": "python", |
| 105 | + "name": "conda-root-py" |
| 106 | + }, |
| 107 | + "language_info": { |
| 108 | + "codemirror_mode": { |
| 109 | + "name": "ipython", |
| 110 | + "version": 2 |
| 111 | + }, |
| 112 | + "file_extension": ".py", |
| 113 | + "mimetype": "text/x-python", |
| 114 | + "name": "python", |
| 115 | + "nbconvert_exporter": "python", |
| 116 | + "pygments_lexer": "ipython2", |
| 117 | + "version": "2.7.11" |
| 118 | + } |
| 119 | + }, |
| 120 | + "nbformat": 4, |
| 121 | + "nbformat_minor": 2 |
| 122 | +} |
0 commit comments