{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Building your Recurrent Neural Network - Step by Step\n",
"\n",
"Welcome to Course 5's first assignment! In this assignment, you will implement key components of a Recurrent Neural Network in numpy.\n",
"\n",
"Recurrent Neural Networks (RNN) are very effective for Natural Language Processing and other sequence tasks because they have \"memory\". They can read inputs $x^{\\langle t \\rangle}$ (such as words) one at a time, and remember some information/context through the hidden layer activations that get passed from one time-step to the next. This allows a unidirectional RNN to take information from the past to process later inputs. A bidirectional RNN can take context from both the past and the future. \n",
"\n",
"**Notation**:\n",
"- Superscript $[l]$ denotes an object associated with the $l^{th}$ layer. \n",
"\n",
"- Superscript $(i)$ denotes an object associated with the $i^{th}$ example. \n",
"\n",
"- Superscript $\\langle t \\rangle$ denotes an object at the $t^{th}$ time-step. \n",
" \n",
"- **Sub**script $i$ denotes the $i^{th}$ entry of a vector.\n",
"\n",
"Example: \n",
"- $a^{(2)[3]<4>}_5$ denotes the activation of the 2nd training example (2), 3rd layer [3], 4th time step <4>, and 5th entry in the vector.\n",
"\n",
"#### Pre-requisites\n",
"* We assume that you are already familiar with `numpy`. \n",
"* To refresh your knowledge of numpy, you can review course 1 of this specialization \"Neural Networks and Deep Learning\". \n",
" * Specifically, review the week 2 assignment [\"Python Basics with numpy (optional)\"](https://www.coursera.org/learn/neural-networks-deep-learning/item/Zh0CU).\n",
" \n",
" \n",
"#### Be careful when modifying the starter code\n",
"* When working on graded functions, please remember to only modify the code that is between the\n",
"```Python\n",
"#### START CODE HERE\n",
"```\n",
"and\n",
"```Python\n",
"#### END CODE HERE\n",
"```\n",
"* In particular, Be careful to not modify the first line of graded routines. These start with:\n",
"```Python\n",
"# GRADED FUNCTION: routine_name\n",
"```\n",
"* The automatic grader (autograder) needs these to locate the function.\n",
"* Even a change in spacing will cause issues with the autograder. \n",
"* It will return 'failed' if these are modified or missing.\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Updates\n",
"\n",
"#### If you were working on the notebook before this update...\n",
"* The current notebook is version \"3a\".\n",
"* You can find your original work saved in the notebook with the previous version name (\"v3\") \n",
"* To view the file directory, go to the menu \"File->Open\", and this will open a new tab that shows the file directory.\n",
"\n",
"#### List of updates\n",
"* \"Forward propagation for the basic RNN\", added sections to clarify variable names and shapes:\n",
" - \"Dimensions of $x^{\\langle t \\rangle}$\"\n",
" - \"Hidden State $a$\", \n",
" - \"Dimensions of hidden state $a^{\\langle t \\rangle}$\"\n",
" - \"Dimensions of prediction $y^{\\langle t \\rangle}$\"\n",
"* `rnn_cell_forward`: \n",
" * Added additional hints.\n",
" * Updated figure 2.\n",
"* `rnn_forward`\n",
" - Set `xt` in a separate line of code to clarify what code is expected; added additional hints.\n",
" - Clarifies instructions to specify dimensions (2D or 3D), and clarifies variable names.\n",
" - Additional Hints\n",
" - Clarifies when the basic RNN works well.\n",
" - Updated figure 3.\n",
"* \"About the gates\" replaced with \"overview of gates and states\":\n",
" - Updated to include conceptual description of each gate's purpose, and an explanation of each equation. \n",
" - Added sections about the cell state, hidden state, and prediction.\n",
" - Lists variable names that are used in the code, and notes when they differ from the variables used in the equations.\n",
" - Lists shapes of the variables.\n",
" - Updated figure 4.\n",
"* `lstm_forward`\n",
" - Added instructions, noting the shapes of the variables.\n",
" - Added hints about `c` and `c_next` to help students avoid copy-by-reference mistakes.\n",
" - Set `xt` in a separate line to make this step explicit.\n",
"* Renamed global variables so that they do not conflict with local variables within the function.\n",
"* Spelling, grammar and wording corrections.\n",
"* For unit tests, updated print statements and \"expected output\" for easier comparisons.\n",
"* Many thanks to mentor Geoff Ladwig for suggested improvements and fixes in the assignments for course 5!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's first import all the packages that you will need during this assignment."
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from rnn_utils import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1 - Forward propagation for the basic Recurrent Neural Network\n",
"\n",
"Later this week, you will generate music using an RNN. The basic RNN that you will implement has the structure below. In this example, $T_x = T_y$. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
**Figure 1**: Basic RNN model "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dimensions of input $x$\n",
"\n",
"#### Input with $n_x$ number of units\n",
"* For a single input example, $x^{(i)}$ is a one-dimensional input vector.\n",
"* Using language as an example, a language with a 5000 word vocabulary could be one-hot encoded into a vector that has 5000 units. So $x^{(i)}$ would have the shape (5000,). \n",
"* We'll use the notation $n_x$ to denote the number of units in a single training example."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Batches of size $m$\n",
"* Let's say we have mini-batches, each with 20 training examples. \n",
"* To benefit from vectorization, we'll stack 20 columns of $x^{(i)}$ examples into a 2D array (a matrix).\n",
"* For example, this tensor has the shape (5000,20). \n",
"* We'll use $m$ to denote the number of training examples. \n",
"* So the shape of a mini-batch is $(n_x,m)$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Time steps of size $T_{x}$\n",
"* A recurrent neural network has multiple time steps, which we'll index with $t$.\n",
"* In the lessons, we saw a single training example $x^{(i)}$ (a vector) pass through multiple time steps $T_x$. For example, if there are 10 time steps, $T_{x} = 10$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3D Tensor of shape $(n_{x},m,T_{x})$\n",
"* The 3-dimensional tensor $x$ of shape $(n_x,m,T_x)$ represents the input $x$ that is fed into the RNN.\n",
"\n",
"#### Taking a 2D slice for each time step: $x^{\\langle t \\rangle}$\n",
"* At each time step, we'll use a mini-batches of training examples (not just a single example).\n",
"* So, for each time step $t$, we'll use a 2D slice of shape $(n_x,m)$.\n",
"* We're referring to this 2D slice as $x^{\\langle t \\rangle}$. The variable name in the code is `xt`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Definition of hidden state $a$\n",
"\n",
"* The activation $a^{\\langle t \\rangle}$ that is passed to the RNN from one time step to another is called a \"hidden state.\"\n",
"\n",
"### Dimensions of hidden state $a$\n",
"\n",
"* Similar to the input tensor $x$, the hidden state for a single training example is a vector of length $n_{a}$.\n",
"* If we include a mini-batch of $m$ training examples, the shape of a mini-batch is $(n_{a},m)$.\n",
"* When we include the time step dimension, the shape of the hidden state is $(n_{a}, m, T_x)$\n",
"* We will loop through the time steps with index $t$, and work with a 2D slice of the 3D tensor. \n",
"* We'll refer to this 2D slice as $a^{\\langle t \\rangle}$. \n",
"* In the code, the variable names we use are either `a_prev` or `a_next`, depending on the function that's being implemented.\n",
"* The shape of this 2D slice is $(n_{a}, m)$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dimensions of prediction $\\hat{y}$\n",
"* Similar to the inputs and hidden states, $\\hat{y}$ is a 3D tensor of shape $(n_{y}, m, T_{y})$.\n",
" * $n_{y}$: number of units in the vector representing the prediction.\n",
" * $m$: number of examples in a mini-batch.\n",
" * $T_{y}$: number of time steps in the prediction.\n",
"* For a single time step $t$, a 2D slice $\\hat{y}^{\\langle t \\rangle}$ has shape $(n_{y}, m)$.\n",
"* In the code, the variable names are:\n",
" - `y_pred`: $\\hat{y}$ \n",
" - `yt_pred`: $\\hat{y}^{\\langle t \\rangle}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's how you can implement an RNN: \n",
"\n",
"**Steps**:\n",
"1. Implement the calculations needed for one time-step of the RNN.\n",
"2. Implement a loop over $T_x$ time-steps in order to process all the inputs, one at a time. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.1 - RNN cell\n",
"\n",
"A recurrent neural network can be seen as the repeated use of a single cell. You are first going to implement the computations for a single time-step. The following figure describes the operations for a single time-step of an RNN cell. \n",
"\n",
"\n",
" **Figure 2**: Basic RNN cell. Takes as input $x^{\\langle t \\rangle}$ (current input) and $a^{\\langle t - 1\\rangle}$ (previous hidden state containing information from the past), and outputs $a^{\\langle t \\rangle}$ which is given to the next RNN cell and also used to predict $\\hat{y}^{\\langle t \\rangle}$ \n",
"\n",
"#### rnn cell versus rnn_cell_forward\n",
"* Note that an RNN cell outputs the hidden state $a^{\\langle t \\rangle}$. \n",
" * The rnn cell is shown in the figure as the inner box which has solid lines. \n",
"* The function that we will implement, `rnn_cell_forward`, also calculates the prediction $\\hat{y}^{\\langle t \\rangle}$\n",
" * The rnn_cell_forward is shown in the figure as the outer box that has dashed lines."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise**: Implement the RNN-cell described in Figure (2).\n",
"\n",
"**Instructions**:\n",
"1. Compute the hidden state with tanh activation: $a^{\\langle t \\rangle} = \\tanh(W_{aa} a^{\\langle t-1 \\rangle} + W_{ax} x^{\\langle t \\rangle} + b_a)$.\n",
"2. Using your new hidden state $a^{\\langle t \\rangle}$, compute the prediction $\\hat{y}^{\\langle t \\rangle} = softmax(W_{ya} a^{\\langle t \\rangle} + b_y)$. We provided the function `softmax`.\n",
"3. Store $(a^{\\langle t \\rangle}, a^{\\langle t-1 \\rangle}, x^{\\langle t \\rangle}, parameters)$ in a `cache`.\n",
"4. Return $a^{\\langle t \\rangle}$ , $\\hat{y}^{\\langle t \\rangle}$ and `cache`\n",
"\n",
"#### Additional Hints\n",
"* [numpy.tanh](https://www.google.com/search?q=numpy+tanh&rlz=1C5CHFA_enUS854US855&oq=numpy+tanh&aqs=chrome..69i57j0l5.1340j0j7&sourceid=chrome&ie=UTF-8)\n",
"* We've created a `softmax` function that you can use. It is located in the file 'rnn_utils.py' and has been imported.\n",
"* For matrix multiplication, use [numpy.dot](https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# GRADED FUNCTION: rnn_cell_forward\n",
"\n",
"def rnn_cell_forward(xt, a_prev, parameters):\n",
" \"\"\"\n",
" Implements a single forward step of the RNN-cell as described in Figure (2)\n",
"\n",
" Arguments:\n",
" xt -- your input data at timestep \"t\", numpy array of shape (n_x, m).\n",
" a_prev -- Hidden state at timestep \"t-1\", numpy array of shape (n_a, m)\n",
" parameters -- python dictionary containing:\n",
" Wax -- Weight matrix multiplying the input, numpy array of shape (n_a, n_x)\n",
" Waa -- Weight matrix multiplying the hidden state, numpy array of shape (n_a, n_a)\n",
" Wya -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a)\n",
" ba -- Bias, numpy array of shape (n_a, 1)\n",
" by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1)\n",
" Returns:\n",
" a_next -- next hidden state, of shape (n_a, m)\n",
" yt_pred -- prediction at timestep \"t\", numpy array of shape (n_y, m)\n",
" cache -- tuple of values needed for the backward pass, contains (a_next, a_prev, xt, parameters)\n",
" \"\"\"\n",
" \n",
" # Retrieve parameters from \"parameters\"\n",
" Wax = parameters[\"Wax\"]\n",
" Waa = parameters[\"Waa\"]\n",
" Wya = parameters[\"Wya\"]\n",
" ba = parameters[\"ba\"]\n",
" by = parameters[\"by\"]\n",
" \n",
" ### START CODE HERE ### (≈2 lines)\n",
" # compute next activation state using the formula given above\n",
" a_next = np.tanh(np.dot(Waa,a_prev) + np.dot(Wax,xt) + ba)\n",
" # compute output of the current cell using the formula given above\n",
" yt_pred = softmax(np.dot(Wya,a_next) + by) \n",
" ### END CODE HERE ###\n",
" \n",
" # store values you need for backward propagation in cache\n",
" cache = (a_next, a_prev, xt, parameters)\n",
" \n",
" return a_next, yt_pred, cache"
]
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a_next[4] = \n",
" [ 0.59584544 0.18141802 0.61311866 0.99808218 0.85016201 0.99980978\n",
" -0.18887155 0.99815551 0.6531151 0.82872037]\n",
"a_next.shape = \n",
" (5, 10)\n",
"yt_pred[1] =\n",
" [ 0.9888161 0.01682021 0.21140899 0.36817467 0.98988387 0.88945212\n",
" 0.36920224 0.9966312 0.9982559 0.17746526]\n",
"yt_pred.shape = \n",
" (2, 10)\n"
]
}
],
"source": [
"np.random.seed(1)\n",
"xt_tmp = np.random.randn(3,10)\n",
"a_prev_tmp = np.random.randn(5,10)\n",
"parameters_tmp = {}\n",
"parameters_tmp['Waa'] = np.random.randn(5,5)\n",
"parameters_tmp['Wax'] = np.random.randn(5,3)\n",
"parameters_tmp['Wya'] = np.random.randn(2,5)\n",
"parameters_tmp['ba'] = np.random.randn(5,1)\n",
"parameters_tmp['by'] = np.random.randn(2,1)\n",
"\n",
"a_next_tmp, yt_pred_tmp, cache_tmp = rnn_cell_forward(xt_tmp, a_prev_tmp, parameters_tmp)\n",
"print(\"a_next[4] = \\n\", a_next_tmp[4])\n",
"print(\"a_next.shape = \\n\", a_next_tmp.shape)\n",
"print(\"yt_pred[1] =\\n\", yt_pred_tmp[1])\n",
"print(\"yt_pred.shape = \\n\", yt_pred_tmp.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Expected Output**: \n",
"```Python\n",
"a_next[4] = \n",
" [ 0.59584544 0.18141802 0.61311866 0.99808218 0.85016201 0.99980978\n",
" -0.18887155 0.99815551 0.6531151 0.82872037]\n",
"a_next.shape = \n",
" (5, 10)\n",
"yt_pred[1] =\n",
" [ 0.9888161 0.01682021 0.21140899 0.36817467 0.98988387 0.88945212\n",
" 0.36920224 0.9966312 0.9982559 0.17746526]\n",
"yt_pred.shape = \n",
" (2, 10)\n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.2 - RNN forward pass \n",
"\n",
"- A recurrent neural network (RNN) is a repetition of the RNN cell that you've just built. \n",
" - If your input sequence of data is 10 time steps long, then you will re-use the RNN cell 10 times. \n",
"- Each cell takes two inputs at each time step:\n",
" - $a^{\\langle t-1 \\rangle}$: The hidden state from the previous cell.\n",
" - $x^{\\langle t \\rangle}$: The current time-step's input data.\n",
"- It has two outputs at each time step:\n",
" - A hidden state ($a^{\\langle t \\rangle}$)\n",
" - A prediction ($y^{\\langle t \\rangle}$)\n",
"- The weights and biases $(W_{aa}, b_{a}, W_{ax}, b_{x})$ are re-used each time step. \n",
" - They are maintained between calls to rnn_cell_forward in the 'parameters' dictionary.\n",
"\n",
"\n",
"\n",
" **Figure 3**: Basic RNN. The input sequence $x = (x^{\\langle 1 \\rangle}, x^{\\langle 2 \\rangle}, ..., x^{\\langle T_x \\rangle})$ is carried over $T_x$ time steps. The network outputs $y = (y^{\\langle 1 \\rangle}, y^{\\langle 2 \\rangle}, ..., y^{\\langle T_x \\rangle})$. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise**: Code the forward propagation of the RNN described in Figure (3).\n",
"\n",
"**Instructions**:\n",
"* Create a 3D array of zeros, $a$ of shape $(n_{a}, m, T_{x})$ that will store all the hidden states computed by the RNN.\n",
"* Create a 3D array of zeros, $\\hat{y}$, of shape $(n_{y}, m, T_{x})$ that will store the predictions. \n",
" - Note that in this case, $T_{y} = T_{x}$ (the prediction and input have the same number of time steps).\n",
"* Initialize the 2D hidden state `a_next` by setting it equal to the initial hidden state, $a_{0}$.\n",
"* At each time step $t$:\n",
" - Get $x^{\\langle t \\rangle}$, which is a 2D slice of $x$ for a single time step $t$.\n",
" - $x^{\\langle t \\rangle}$ has shape $(n_{x}, m)$\n",
" - $x$ has shape $(n_{x}, m, T_{x})$\n",
" - Update the 2D hidden state $a^{\\langle t \\rangle}$ (variable name `a_next`), the prediction $\\hat{y}^{\\langle t \\rangle}$ and the cache by running `rnn_cell_forward`.\n",
" - $a^{\\langle t \\rangle}$ has shape $(n_{a}, m)$\n",
" - Store the 2D hidden state in the 3D tensor $a$, at the $t^{th}$ position.\n",
" - $a$ has shape $(n_{a}, m, T_{x})$\n",
" - Store the 2D $\\hat{y}^{\\langle t \\rangle}$ prediction (variable name `yt_pred`) in the 3D tensor $\\hat{y}_{pred}$ at the $t^{th}$ position.\n",
" - $\\hat{y}^{\\langle t \\rangle}$ has shape $(n_{y}, m)$\n",
" - $\\hat{y}$ has shape $(n_{y}, m, T_x)$\n",
" - Append the cache to the list of caches.\n",
"* Return the 3D tensor $a$ and $\\hat{y}$, as well as the list of caches.\n",
"\n",
"#### Additional Hints\n",
"- [np.zeros](https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html)\n",
"- If you have a 3 dimensional numpy array and are indexing by its third dimension, you can use array slicing like this: `var_name[:,:,i]`."
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# GRADED FUNCTION: rnn_forward\n",
"\n",
"def rnn_forward(x, a0, parameters):\n",
" \"\"\"\n",
" Implement the forward propagation of the recurrent neural network described in Figure (3).\n",
"\n",
" Arguments:\n",
" x -- Input data for every time-step, of shape (n_x, m, T_x).\n",
" a0 -- Initial hidden state, of shape (n_a, m)\n",
" parameters -- python dictionary containing:\n",
" Waa -- Weight matrix multiplying the hidden state, numpy array of shape (n_a, n_a)\n",
" Wax -- Weight matrix multiplying the input, numpy array of shape (n_a, n_x)\n",
" Wya -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a)\n",
" ba -- Bias numpy array of shape (n_a, 1)\n",
" by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1)\n",
"\n",
" Returns:\n",
" a -- Hidden states for every time-step, numpy array of shape (n_a, m, T_x)\n",
" y_pred -- Predictions for every time-step, numpy array of shape (n_y, m, T_x)\n",
" caches -- tuple of values needed for the backward pass, contains (list of caches, x)\n",
" \"\"\"\n",
" \n",
" # Initialize \"caches\" which will contain the list of all caches\n",
" caches = []\n",
" \n",
" # Retrieve dimensions from shapes of x and parameters[\"Wya\"]\n",
" n_x, m, T_x = x.shape\n",
" n_y, n_a = parameters[\"Wya\"].shape\n",
" \n",
" ### START CODE HERE ###\n",
" \n",
" # initialize \"a\" and \"y\" with zeros (≈2 lines)\n",
" a = np.zeros([n_a,m,T_x])\n",
" y_pred = np.zeros([n_y,m,T_x])\n",
" \n",
" # Initialize a_next (≈1 line)\n",
" a_next = a0\n",
" \n",
" # loop over all time-steps\n",
" for t in range(T_x):\n",
" # Update next hidden state, compute the prediction, get the cache (≈1 line)\n",
" a_next, yt_pred, cache = rnn_cell_forward(x[:,:,t], a_next, parameters)\n",
" # Save the value of the new \"next\" hidden state in a (≈1 line)\n",
" a[:,:,t] = a_next\n",
" # Save the value of the prediction in y (≈1 line)\n",
" y_pred[:,:,t] = yt_pred\n",
" # Append \"cache\" to \"caches\" (≈1 line)\n",
" caches.append(cache)\n",
" \n",
" ### END CODE HERE ###\n",
" \n",
" # store values needed for backward propagation in cache\n",
" caches = (caches, x)\n",
" \n",
" return a, y_pred, caches"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a[4][1] = \n",
" [-0.99999375 0.77911235 -0.99861469 -0.99833267]\n",
"a.shape = \n",
" (5, 10, 4)\n",
"y_pred[1][3] =\n",
" [ 0.79560373 0.86224861 0.11118257 0.81515947]\n",
"y_pred.shape = \n",
" (2, 10, 4)\n",
"caches[1][1][3] =\n",
" [-1.1425182 -0.34934272 -0.20889423 0.58662319]\n",
"len(caches) = \n",
" 2\n"
]
}
],
"source": [
"np.random.seed(1)\n",
"x_tmp = np.random.randn(3,10,4)\n",
"a0_tmp = np.random.randn(5,10)\n",
"parameters_tmp = {}\n",
"parameters_tmp['Waa'] = np.random.randn(5,5)\n",
"parameters_tmp['Wax'] = np.random.randn(5,3)\n",
"parameters_tmp['Wya'] = np.random.randn(2,5)\n",
"parameters_tmp['ba'] = np.random.randn(5,1)\n",
"parameters_tmp['by'] = np.random.randn(2,1)\n",
"\n",
"a_tmp, y_pred_tmp, caches_tmp = rnn_forward(x_tmp, a0_tmp, parameters_tmp)\n",
"print(\"a[4][1] = \\n\", a_tmp[4][1])\n",
"print(\"a.shape = \\n\", a_tmp.shape)\n",
"print(\"y_pred[1][3] =\\n\", y_pred_tmp[1][3])\n",
"print(\"y_pred.shape = \\n\", y_pred_tmp.shape)\n",
"print(\"caches[1][1][3] =\\n\", caches_tmp[1][1][3])\n",
"print(\"len(caches) = \\n\", len(caches_tmp))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Expected Output**:\n",
"\n",
"```Python\n",
"a[4][1] = \n",
" [-0.99999375 0.77911235 -0.99861469 -0.99833267]\n",
"a.shape = \n",
" (5, 10, 4)\n",
"y_pred[1][3] =\n",
" [ 0.79560373 0.86224861 0.11118257 0.81515947]\n",
"y_pred.shape = \n",
" (2, 10, 4)\n",
"caches[1][1][3] =\n",
" [-1.1425182 -0.34934272 -0.20889423 0.58662319]\n",
"len(caches) = \n",
" 2\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Congratulations! You've successfully built the forward propagation of a recurrent neural network from scratch. \n",
"\n",
"#### Situations when this RNN will perform better:\n",
"- This will work well enough for some applications, but it suffers from the vanishing gradient problems. \n",
"- The RNN works best when each output $\\hat{y}^{\\langle t \\rangle}$ can be estimated using \"local\" context. \n",
"- \"Local\" context refers to information that is close to the prediction's time step $t$.\n",
"- More formally, local context refers to inputs $x^{\\langle t' \\rangle}$ and predictions $\\hat{y}^{\\langle t \\rangle}$ where $t'$ is close to $t$.\n",
"\n",
"In the next part, you will build a more complex LSTM model, which is better at addressing vanishing gradients. The LSTM will be better able to remember a piece of information and keep it saved for many timesteps. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2 - Long Short-Term Memory (LSTM) network\n",
"\n",
"The following figure shows the operations of an LSTM-cell.\n",
"\n",
"\n",
" **Figure 4**: LSTM-cell. This tracks and updates a \"cell state\" or memory variable $c^{\\langle t \\rangle}$ at every time-step, which can be different from $a^{\\langle t \\rangle}$. \n",
"\n",
"Similar to the RNN example above, you will start by implementing the LSTM cell for a single time-step. Then you can iteratively call it from inside a \"for-loop\" to have it process an input with $T_x$ time-steps. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Overview of gates and states\n",
"\n",
"#### - Forget gate $\\mathbf{\\Gamma}_{f}$\n",
"\n",
"* Let's assume we are reading words in a piece of text, and plan to use an LSTM to keep track of grammatical structures, such as whether the subject is singular (\"puppy\") or plural (\"puppies\"). \n",
"* If the subject changes its state (from a singular word to a plural word), the memory of the previous state becomes outdated, so we \"forget\" that outdated state.\n",
"* The \"forget gate\" is a tensor containing values that are between 0 and 1.\n",
" * If a unit in the forget gate has a value close to 0, the LSTM will \"forget\" the stored state in the corresponding unit of the previous cell state.\n",
" * If a unit in the forget gate has a value close to 1, the LSTM will mostly remember the corresponding value in the stored state.\n",
"\n",
"##### Equation\n",
"\n",
"$$\\mathbf{\\Gamma}_f^{\\langle t \\rangle} = \\sigma(\\mathbf{W}_f[\\mathbf{a}^{\\langle t-1 \\rangle}, \\mathbf{x}^{\\langle t \\rangle}] + \\mathbf{b}_f)\\tag{1} $$\n",
"\n",
"##### Explanation of the equation:\n",
"\n",
"* $\\mathbf{W_{f}}$ contains weights that govern the forget gate's behavior. \n",
"* The previous time step's hidden state $[a^{\\langle t-1 \\rangle}$ and current time step's input $x^{\\langle t \\rangle}]$ are concatenated together and multiplied by $\\mathbf{W_{f}}$. \n",
"* A sigmoid function is used to make each of the gate tensor's values $\\mathbf{\\Gamma}_f^{\\langle t \\rangle}$ range from 0 to 1.\n",
"* The forget gate $\\mathbf{\\Gamma}_f^{\\langle t \\rangle}$ has the same dimensions as the previous cell state $c^{\\langle t-1 \\rangle}$. \n",
"* This means that the two can be multiplied together, element-wise.\n",
"* Multiplying the tensors $\\mathbf{\\Gamma}_f^{\\langle t \\rangle} * \\mathbf{c}^{\\langle t-1 \\rangle}$ is like applying a mask over the previous cell state.\n",
"* If a single value in $\\mathbf{\\Gamma}_f^{\\langle t \\rangle}$ is 0 or close to 0, then the product is close to 0.\n",
" * This keeps the information stored in the corresponding unit in $\\mathbf{c}^{\\langle t-1 \\rangle}$ from being remembered for the next time step.\n",
"* Similarly, if one value is close to 1, the product is close to the original value in the previous cell state.\n",
" * The LSTM will keep the information from the corresponding unit of $\\mathbf{c}^{\\langle t-1 \\rangle}$, to be used in the next time step.\n",
" \n",
"##### Variable names in the code\n",
"The variable names in the code are similar to the equations, with slight differences. \n",
"* `Wf`: forget gate weight $\\mathbf{W}_{f}$\n",
"* `Wb`: forget gate bias $\\mathbf{W}_{b}$\n",
"* `ft`: forget gate $\\Gamma_f^{\\langle t \\rangle}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Candidate value $\\tilde{\\mathbf{c}}^{\\langle t \\rangle}$\n",
"* The candidate value is a tensor containing information from the current time step that **may** be stored in the current cell state $\\mathbf{c}^{\\langle t \\rangle}$.\n",
"* Which parts of the candidate value get passed on depends on the update gate.\n",
"* The candidate value is a tensor containing values that range from -1 to 1.\n",
"* The tilde \"~\" is used to differentiate the candidate $\\tilde{\\mathbf{c}}^{\\langle t \\rangle}$ from the cell state $\\mathbf{c}^{\\langle t \\rangle}$.\n",
"\n",
"##### Equation\n",
"$$\\mathbf{\\tilde{c}}^{\\langle t \\rangle} = \\tanh\\left( \\mathbf{W}_{c} [\\mathbf{a}^{\\langle t - 1 \\rangle}, \\mathbf{x}^{\\langle t \\rangle}] + \\mathbf{b}_{c} \\right) \\tag{3}$$\n",
"\n",
"##### Explanation of the equation\n",
"* The 'tanh' function produces values between -1 and +1.\n",
"\n",
"\n",
"##### Variable names in the code\n",
"* `cct`: candidate value $\\mathbf{\\tilde{c}}^{\\langle t \\rangle}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### - Update gate $\\mathbf{\\Gamma}_{i}$\n",
"\n",
"* We use the update gate to decide what aspects of the candidate $\\tilde{\\mathbf{c}}^{\\langle t \\rangle}$ to add to the cell state $c^{\\langle t \\rangle}$.\n",
"* The update gate decides what parts of a \"candidate\" tensor $\\tilde{\\mathbf{c}}^{\\langle t \\rangle}$ are passed onto the cell state $\\mathbf{c}^{\\langle t \\rangle}$.\n",
"* The update gate is a tensor containing values between 0 and 1.\n",
" * When a unit in the update gate is close to 1, it allows the value of the candidate $\\tilde{\\mathbf{c}}^{\\langle t \\rangle}$ to be passed onto the hidden state $\\mathbf{c}^{\\langle t \\rangle}$\n",
" * When a unit in the update gate is close to 0, it prevents the corresponding value in the candidate from being passed onto the hidden state.\n",
"* Notice that we use the subscript \"i\" and not \"u\", to follow the convention used in the literature.\n",
"\n",
"##### Equation\n",
"\n",
"$$\\mathbf{\\Gamma}_i^{\\langle t \\rangle} = \\sigma(\\mathbf{W}_i[a^{\\langle t-1 \\rangle}, \\mathbf{x}^{\\langle t \\rangle}] + \\mathbf{b}_i)\\tag{2} $$ \n",
"\n",
"##### Explanation of the equation\n",
"\n",
"* Similar to the forget gate, here $\\mathbf{\\Gamma}_i^{\\langle t \\rangle}$, the sigmoid produces values between 0 and 1.\n",
"* The update gate is multiplied element-wise with the candidate, and this product ($\\mathbf{\\Gamma}_{i}^{\\langle t \\rangle} * \\tilde{c}^{\\langle t \\rangle}$) is used in determining the cell state $\\mathbf{c}^{\\langle t \\rangle}$.\n",
"\n",
"##### Variable names in code (Please note that they're different than the equations)\n",
"In the code, we'll use the variable names found in the academic literature. These variables don't use \"u\" to denote \"update\".\n",
"* `Wi` is the update gate weight $\\mathbf{W}_i$ (not \"Wu\") \n",
"* `bi` is the update gate bias $\\mathbf{b}_i$ (not \"bu\")\n",
"* `it` is the forget gate $\\mathbf{\\Gamma}_i^{\\langle t \\rangle}$ (not \"ut\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### - Cell state $\\mathbf{c}^{\\langle t \\rangle}$\n",
"\n",
"* The cell state is the \"memory\" that gets passed onto future time steps.\n",
"* The new cell state $\\mathbf{c}^{\\langle t \\rangle}$ is a combination of the previous cell state and the candidate value.\n",
"\n",
"##### Equation\n",
"\n",
"$$ \\mathbf{c}^{\\langle t \\rangle} = \\mathbf{\\Gamma}_f^{\\langle t \\rangle}* \\mathbf{c}^{\\langle t-1 \\rangle} + \\mathbf{\\Gamma}_{i}^{\\langle t \\rangle} *\\mathbf{\\tilde{c}}^{\\langle t \\rangle} \\tag{4} $$\n",
"\n",
"##### Explanation of equation\n",
"* The previous cell state $\\mathbf{c}^{\\langle t-1 \\rangle}$ is adjusted (weighted) by the forget gate $\\mathbf{\\Gamma}_{f}^{\\langle t \\rangle}$\n",
"* and the candidate value $\\tilde{\\mathbf{c}}^{\\langle t \\rangle}$, adjusted (weighted) by the update gate $\\mathbf{\\Gamma}_{i}^{\\langle t \\rangle}$\n",
"\n",
"##### Variable names and shapes in the code\n",
"* `c`: cell state, including all time steps, $\\mathbf{c}$ shape $(n_{a}, m, T)$\n",
"* `c_next`: new (next) cell state, $\\mathbf{c}^{\\langle t \\rangle}$ shape $(n_{a}, m)$\n",
"* `c_prev`: previous cell state, $\\mathbf{c}^{\\langle t-1 \\rangle}$, shape $(n_{a}, m)$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### - Output gate $\\mathbf{\\Gamma}_{o}$\n",
"\n",
"* The output gate decides what gets sent as the prediction (output) of the time step.\n",
"* The output gate is like the other gates. It contains values that range from 0 to 1.\n",
"\n",
"##### Equation\n",
"\n",
"$$ \\mathbf{\\Gamma}_o^{\\langle t \\rangle}= \\sigma(\\mathbf{W}_o[\\mathbf{a}^{\\langle t-1 \\rangle}, \\mathbf{x}^{\\langle t \\rangle}] + \\mathbf{b}_{o})\\tag{5}$$ \n",
"\n",
"##### Explanation of the equation\n",
"* The output gate is determined by the previous hidden state $\\mathbf{a}^{\\langle t-1 \\rangle}$ and the current input $\\mathbf{x}^{\\langle t \\rangle}$\n",
"* The sigmoid makes the gate range from 0 to 1.\n",
"\n",
"\n",
"##### Variable names in the code\n",
"* `Wo`: output gate weight, $\\mathbf{W_o}$\n",
"* `bo`: output gate bias, $\\mathbf{b_o}$\n",
"* `ot`: output gate, $\\mathbf{\\Gamma}_{o}^{\\langle t \\rangle}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### - Hidden state $\\mathbf{a}^{\\langle t \\rangle}$\n",
"\n",
"* The hidden state gets passed to the LSTM cell's next time step.\n",
"* It is used to determine the three gates ($\\mathbf{\\Gamma}_{f}, \\mathbf{\\Gamma}_{u}, \\mathbf{\\Gamma}_{o}$) of the next time step.\n",
"* The hidden state is also used for the prediction $y^{\\langle t \\rangle}$.\n",
"\n",
"##### Equation\n",
"\n",
"$$ \\mathbf{a}^{\\langle t \\rangle} = \\mathbf{\\Gamma}_o^{\\langle t \\rangle} * \\tanh(\\mathbf{c}^{\\langle t \\rangle})\\tag{6} $$\n",
"\n",
"##### Explanation of equation\n",
"* The hidden state $\\mathbf{a}^{\\langle t \\rangle}$ is determined by the cell state $\\mathbf{c}^{\\langle t \\rangle}$ in combination with the output gate $\\mathbf{\\Gamma}_{o}$.\n",
"* The cell state state is passed through the \"tanh\" function to rescale values between -1 and +1.\n",
"* The output gate acts like a \"mask\" that either preserves the values of $\\tanh(\\mathbf{c}^{\\langle t \\rangle})$ or keeps those values from being included in the hidden state $\\mathbf{a}^{\\langle t \\rangle}$\n",
"\n",
"##### Variable names and shapes in the code\n",
"* `a`: hidden state, including time steps. $\\mathbf{a}$ has shape $(n_{a}, m, T_{x})$\n",
"* 'a_prev`: hidden state from previous time step. $\\mathbf{a}^{\\langle t-1 \\rangle}$ has shape $(n_{a}, m)$\n",
"* `a_next`: hidden state for next time step. $\\mathbf{a}^{\\langle t \\rangle}$ has shape $(n_{a}, m)$ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### - Prediction $\\mathbf{y}^{\\langle t \\rangle}_{pred}$\n",
"* The prediction in this use case is a classification, so we'll use a softmax.\n",
"\n",
"The equation is:\n",
"$$\\mathbf{y}^{\\langle t \\rangle}_{pred} = \\textrm{softmax}(\\mathbf{W}_{y} \\mathbf{a}^{\\langle t \\rangle} + \\mathbf{b}_{y})$$\n",
"\n",
"##### Variable names and shapes in the code\n",
"* `y_pred`: prediction, including all time steps. $\\mathbf{y}_{pred}$ has shape $(n_{y}, m, T_{x})$. Note that $(T_{y} = T_{x})$ for this example.\n",
"* `yt_pred`: prediction for the current time step $t$. $\\mathbf{y}^{\\langle t \\rangle}_{pred}$ has shape $(n_{y}, m)$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.1 - LSTM cell\n",
"\n",
"**Exercise**: Implement the LSTM cell described in the Figure (4).\n",
"\n",
"**Instructions**:\n",
"1. Concatenate the hidden state $a^{\\langle t-1 \\rangle}$ and input $x^{\\langle t \\rangle}$ into a single matrix: \n",
"\n",
"$$concat = \\begin{bmatrix} a^{\\langle t-1 \\rangle} \\\\ x^{\\langle t \\rangle} \\end{bmatrix}$$ \n",
"\n",
"2. Compute all the formulas 1 through 6 for the gates, hidden state, and cell state.\n",
"3. Compute the prediction $y^{\\langle t \\rangle}$.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Additional Hints\n",
"* You can use [numpy.concatenate](https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html). Check which value to use for the `axis` parameter.\n",
"* The functions `sigmoid()` and `softmax` are imported from `rnn_utils.py`.\n",
"* [numpy.tanh](https://docs.scipy.org/doc/numpy/reference/generated/numpy.tanh.html)\n",
"* Use [np.dot](https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html) for matrix multiplication.\n",
"* Notice that the variable names `Wi`, `bi` refer to the weights and biases of the **update** gate. There are no variables named \"Wu\" or \"bu\" in this function."
]
},
{
"cell_type": "code",
"execution_count": 186,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# GRADED FUNCTION: lstm_cell_forward\n",
"\n",
"def lstm_cell_forward(xt, a_prev, c_prev, parameters):\n",
" \"\"\"\n",
" Implement a single forward step of the LSTM-cell as described in Figure (4)\n",
"\n",
" Arguments:\n",
" xt -- your input data at timestep \"t\", numpy array of shape (n_x, m).\n",
" a_prev -- Hidden state at timestep \"t-1\", numpy array of shape (n_a, m)\n",
" c_prev -- Memory state at timestep \"t-1\", numpy array of shape (n_a, m)\n",
" parameters -- python dictionary containing:\n",
" Wf -- Weight matrix of the forget gate, numpy array of shape (n_a, n_a + n_x)\n",
" bf -- Bias of the forget gate, numpy array of shape (n_a, 1)\n",
" Wi -- Weight matrix of the update gate, numpy array of shape (n_a, n_a + n_x)\n",
" bi -- Bias of the update gate, numpy array of shape (n_a, 1)\n",
" Wc -- Weight matrix of the first \"tanh\", numpy array of shape (n_a, n_a + n_x)\n",
" bc -- Bias of the first \"tanh\", numpy array of shape (n_a, 1)\n",
" Wo -- Weight matrix of the output gate, numpy array of shape (n_a, n_a + n_x)\n",
" bo -- Bias of the output gate, numpy array of shape (n_a, 1)\n",
" Wy -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a)\n",
" by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1)\n",
" \n",
" Returns:\n",
" a_next -- next hidden state, of shape (n_a, m)\n",
" c_next -- next memory state, of shape (n_a, m)\n",
" yt_pred -- prediction at timestep \"t\", numpy array of shape (n_y, m)\n",
" cache -- tuple of values needed for the backward pass, contains (a_next, c_next, a_prev, c_prev, xt, parameters)\n",
" \n",
" Note: ft/it/ot stand for the forget/update/output gates, cct stands for the candidate value (c tilde),\n",
" c stands for the cell state (memory)\n",
" \"\"\"\n",
"\n",
" # Retrieve parameters from \"parameters\"\n",
" Wf = parameters[\"Wf\"] # forget gate weight\n",
" bf = parameters[\"bf\"]\n",
" Wi = parameters[\"Wi\"] # update gate weight (notice the variable name)\n",
" bi = parameters[\"bi\"] # (notice the variable name)\n",
" Wc = parameters[\"Wc\"] # candidate value weight\n",
" bc = parameters[\"bc\"]\n",
" Wo = parameters[\"Wo\"] # output gate weight\n",
" bo = parameters[\"bo\"]\n",
" Wy = parameters[\"Wy\"] # prediction weight\n",
" by = parameters[\"by\"]\n",
" \n",
" # Retrieve dimensions from shapes of xt and Wy\n",
" n_x, m = xt.shape\n",
" n_y, n_a = Wy.shape\n",
"\n",
" ### START CODE HERE ###\n",
" # Concatenate a_prev and xt (≈3 lines)\n",
" concat = np.zeros([n_x+n_a,m])\n",
" concat[: n_a, :] = a_prev\n",
" concat[n_a :, :] = xt\n",
"\n",
" # Compute values for ft, it, cct, c_next, ot, a_next using the formulas given figure (4) (≈6 lines)\n",
" ft = sigmoid(np.dot(Wf,concat)+bf)\n",
" it = sigmoid(np.dot(Wi,concat)+bi)\n",
" cct = np.tanh(np.dot(Wc,concat)+bc)\n",
" c_next = ft*c_prev +it*cct\n",
" ot = sigmoid(np.dot(Wo,concat)+bo)\n",
" a_next = ot*np.tanh(c_next)\n",
" \n",
" # Compute prediction of the LSTM cell (≈1 line)\n",
" yt_pred = softmax(np.dot(Wy,a_next)+by)\n",
" ### END CODE HERE ###\n",
"\n",
" # store values needed for backward propagation in cache\n",
" cache = (a_next, c_next, a_prev, c_prev, ft, it, cct, ot, xt, parameters)\n",
"\n",
" return a_next, c_next, yt_pred, cache"
]
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a_next[4] = \n",
" [-0.66408471 0.0036921 0.02088357 0.22834167 -0.85575339 0.00138482\n",
" 0.76566531 0.34631421 -0.00215674 0.43827275]\n",
"a_next.shape = (5, 10)\n",
"c_next[2] = \n",
" [ 0.63267805 1.00570849 0.35504474 0.20690913 -1.64566718 0.11832942\n",
" 0.76449811 -0.0981561 -0.74348425 -0.26810932]\n",
"c_next.shape = (5, 10)\n",
"yt[1] = [ 0.79913913 0.15986619 0.22412122 0.15606108 0.97057211 0.31146381\n",
" 0.00943007 0.12666353 0.39380172 0.07828381]\n",
"yt.shape = (2, 10)\n",
"cache[1][3] =\n",
" [-0.16263996 1.03729328 0.72938082 -0.54101719 0.02752074 -0.30821874\n",
" 0.07651101 -1.03752894 1.41219977 -0.37647422]\n",
"len(cache) = 10\n"
]
}
],
"source": [
"np.random.seed(1)\n",
"xt_tmp = np.random.randn(3,10)\n",
"a_prev_tmp = np.random.randn(5,10)\n",
"c_prev_tmp = np.random.randn(5,10)\n",
"parameters_tmp = {}\n",
"parameters_tmp['Wf'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bf'] = np.random.randn(5,1)\n",
"parameters_tmp['Wi'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bi'] = np.random.randn(5,1)\n",
"parameters_tmp['Wo'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bo'] = np.random.randn(5,1)\n",
"parameters_tmp['Wc'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bc'] = np.random.randn(5,1)\n",
"parameters_tmp['Wy'] = np.random.randn(2,5)\n",
"parameters_tmp['by'] = np.random.randn(2,1)\n",
"\n",
"a_next_tmp, c_next_tmp, yt_tmp, cache_tmp = lstm_cell_forward(xt_tmp, a_prev_tmp, c_prev_tmp, parameters_tmp)\n",
"print(\"a_next[4] = \\n\", a_next_tmp[4])\n",
"print(\"a_next.shape = \", c_next_tmp.shape)\n",
"print(\"c_next[2] = \\n\", c_next_tmp[2])\n",
"print(\"c_next.shape = \", c_next_tmp.shape)\n",
"print(\"yt[1] =\", yt_tmp[1])\n",
"print(\"yt.shape = \", yt_tmp.shape)\n",
"print(\"cache[1][3] =\\n\", cache_tmp[1][3])\n",
"print(\"len(cache) = \", len(cache_tmp))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Expected Output**:\n",
"\n",
"```Python\n",
"a_next[4] = \n",
" [-0.66408471 0.0036921 0.02088357 0.22834167 -0.85575339 0.00138482\n",
" 0.76566531 0.34631421 -0.00215674 0.43827275]\n",
"a_next.shape = (5, 10)\n",
"c_next[2] = \n",
" [ 0.63267805 1.00570849 0.35504474 0.20690913 -1.64566718 0.11832942\n",
" 0.76449811 -0.0981561 -0.74348425 -0.26810932]\n",
"c_next.shape = (5, 10)\n",
"yt[1] = [ 0.79913913 0.15986619 0.22412122 0.15606108 0.97057211 0.31146381\n",
" 0.00943007 0.12666353 0.39380172 0.07828381]\n",
"yt.shape = (2, 10)\n",
"cache[1][3] =\n",
" [-0.16263996 1.03729328 0.72938082 -0.54101719 0.02752074 -0.30821874\n",
" 0.07651101 -1.03752894 1.41219977 -0.37647422]\n",
"len(cache) = 10\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.2 - Forward pass for LSTM\n",
"\n",
"Now that you have implemented one step of an LSTM, you can now iterate this over this using a for-loop to process a sequence of $T_x$ inputs. \n",
"\n",
"\n",
" **Figure 5**: LSTM over multiple time-steps. \n",
"\n",
"**Exercise:** Implement `lstm_forward()` to run an LSTM over $T_x$ time-steps. \n",
"\n",
"**Instructions**\n",
"* Get the dimensions $n_x, n_a, n_y, m, T_x$ from the shape of the variables: `x` and `parameters`.\n",
"* Initialize the 3D tensors $a$, $c$ and $y$.\n",
" - $a$: hidden state, shape $(n_{a}, m, T_{x})$\n",
" - $c$: cell state, shape $(n_{a}, m, T_{x})$\n",
" - $y$: prediction, shape $(n_{y}, m, T_{x})$ (Note that $T_{y} = T_{x}$ in this example).\n",
" - **Note** Setting one variable equal to the other is a \"copy by reference\". In other words, don't do `c = a', otherwise both these variables point to the same underlying variable.\n",
"* Initialize the 2D tensor $a^{\\langle t \\rangle}$ \n",
" - $a^{\\langle t \\rangle}$ stores the hidden state for time step $t$. The variable name is `a_next`.\n",
" - $a^{\\langle 0 \\rangle}$, the initial hidden state at time step 0, is passed in when calling the function. The variable name is `a0`.\n",
" - $a^{\\langle t \\rangle}$ and $a^{\\langle 0 \\rangle}$ represent a single time step, so they both have the shape $(n_{a}, m)$ \n",
" - Initialize $a^{\\langle t \\rangle}$ by setting it to the initial hidden state ($a^{\\langle 0 \\rangle}$) that is passed into the function.\n",
"* Initialize $c^{\\langle t \\rangle}$ with zeros. \n",
" - The variable name is `c_next`. \n",
" - $c^{\\langle t \\rangle}$ represents a single time step, so its shape is $(n_{a}, m)$\n",
" - **Note**: create `c_next` as its own variable with its own location in memory. Do not initialize it as a slice of the 3D tensor $c$. In other words, **don't** do `c_next = c[:,:,0]`.\n",
"* For each time step, do the following:\n",
" - From the 3D tensor $x$, get a 2D slice $x^{\\langle t \\rangle}$ at time step $t$.\n",
" - Call the `lstm_cell_forward` function that you defined previously, to get the hidden state, cell state, prediction, and cache.\n",
" - Store the hidden state, cell state and prediction (the 2D tensors) inside the 3D tensors.\n",
" - Also append the cache to the list of caches."
]
},
{
"cell_type": "code",
"execution_count": 188,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# GRADED FUNCTION: lstm_forward\n",
"\n",
"def lstm_forward(x, a0, parameters):\n",
" \"\"\"\n",
" Implement the forward propagation of the recurrent neural network using an LSTM-cell described in Figure (4).\n",
"\n",
" Arguments:\n",
" x -- Input data for every time-step, of shape (n_x, m, T_x).\n",
" a0 -- Initial hidden state, of shape (n_a, m)\n",
" parameters -- python dictionary containing:\n",
" Wf -- Weight matrix of the forget gate, numpy array of shape (n_a, n_a + n_x)\n",
" bf -- Bias of the forget gate, numpy array of shape (n_a, 1)\n",
" Wi -- Weight matrix of the update gate, numpy array of shape (n_a, n_a + n_x)\n",
" bi -- Bias of the update gate, numpy array of shape (n_a, 1)\n",
" Wc -- Weight matrix of the first \"tanh\", numpy array of shape (n_a, n_a + n_x)\n",
" bc -- Bias of the first \"tanh\", numpy array of shape (n_a, 1)\n",
" Wo -- Weight matrix of the output gate, numpy array of shape (n_a, n_a + n_x)\n",
" bo -- Bias of the output gate, numpy array of shape (n_a, 1)\n",
" Wy -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a)\n",
" by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1)\n",
" \n",
" Returns:\n",
" a -- Hidden states for every time-step, numpy array of shape (n_a, m, T_x)\n",
" y -- Predictions for every time-step, numpy array of shape (n_y, m, T_x)\n",
" c -- The value of the cell state, numpy array of shape (n_a, m, T_x)\n",
" caches -- tuple of values needed for the backward pass, contains (list of all the caches, x)\n",
" \"\"\"\n",
"\n",
" # Initialize \"caches\", which will track the list of all the caches\n",
" caches = []\n",
" \n",
" ### START CODE HERE ###\n",
" Wy = parameters['Wy'] # saving parameters['Wy'] in a local variable in case students use Wy instead of parameters['Wy']\n",
" # Retrieve dimensions from shapes of x and parameters['Wy'] (≈2 lines)\n",
" n_x, m, T_x = x.shape\n",
" n_y, n_a = Wy.shape\n",
" \n",
" # initialize \"a\", \"c\" and \"y\" with zeros (≈3 lines)\n",
" a = np.zeros([n_a, m, T_x])\n",
" c = np.zeros([n_a, m, T_x])\n",
" y = np.zeros([n_y, m, T_x])\n",
" \n",
" # Initialize a_next and c_next (≈2 lines)\n",
" a_next = a0\n",
" c_next = np.zeros([n_a,m])\n",
" \n",
" # loop over all time-steps\n",
" for t in range(T_x):\n",
" # Update next hidden state, next memory state, compute the prediction, get the cache (≈1 line)\n",
" a_next, c_next, yt, cache = lstm_cell_forward(x[:,:,t], a_next, c_next, parameters)\n",
" # Save the value of the new \"next\" hidden state in a (≈1 line)\n",
" a[:,:,t] = a_next\n",
" # Save the value of the prediction in y (≈1 line)\n",
" y[:,:,t] = yt\n",
" # Save the value of the next cell state (≈1 line)\n",
" c[:,:,t] = c_next\n",
" # Append the cache into caches (≈1 line)\n",
" caches.append(cache)\n",
" \n",
" ### END CODE HERE ###\n",
" \n",
" # store values needed for backward propagation in cache\n",
" caches = (caches, x)\n",
"\n",
" return a, y, c, caches"
]
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a[4][3][6] = 0.172117767533\n",
"a.shape = (5, 10, 7)\n",
"y[1][4][3] = 0.95087346185\n",
"y.shape = (2, 10, 7)\n",
"caches[1][1][1] =\n",
" [ 0.82797464 0.23009474 0.76201118 -0.22232814 -0.20075807 0.18656139\n",
" 0.41005165]\n",
"c[1][2][1] -0.855544916718\n",
"len(caches) = 2\n"
]
}
],
"source": [
"np.random.seed(1)\n",
"x_tmp = np.random.randn(3,10,7)\n",
"a0_tmp = np.random.randn(5,10)\n",
"parameters_tmp = {}\n",
"parameters_tmp['Wf'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bf'] = np.random.randn(5,1)\n",
"parameters_tmp['Wi'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bi']= np.random.randn(5,1)\n",
"parameters_tmp['Wo'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bo'] = np.random.randn(5,1)\n",
"parameters_tmp['Wc'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bc'] = np.random.randn(5,1)\n",
"parameters_tmp['Wy'] = np.random.randn(2,5)\n",
"parameters_tmp['by'] = np.random.randn(2,1)\n",
"\n",
"a_tmp, y_tmp, c_tmp, caches_tmp = lstm_forward(x_tmp, a0_tmp, parameters_tmp)\n",
"print(\"a[4][3][6] = \", a_tmp[4][3][6])\n",
"print(\"a.shape = \", a_tmp.shape)\n",
"print(\"y[1][4][3] =\", y_tmp[1][4][3])\n",
"print(\"y.shape = \", y_tmp.shape)\n",
"print(\"caches[1][1][1] =\\n\", caches_tmp[1][1][1])\n",
"print(\"c[1][2][1]\", c_tmp[1][2][1])\n",
"print(\"len(caches) = \", len(caches_tmp))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Expected Output**:\n",
"\n",
"```Python\n",
"a[4][3][6] = 0.172117767533\n",
"a.shape = (5, 10, 7)\n",
"y[1][4][3] = 0.95087346185\n",
"y.shape = (2, 10, 7)\n",
"caches[1][1][1] =\n",
" [ 0.82797464 0.23009474 0.76201118 -0.22232814 -0.20075807 0.18656139\n",
" 0.41005165]\n",
"c[1][2][1] -0.855544916718\n",
"len(caches) = 2\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Congratulations! You have now implemented the forward passes for the basic RNN and the LSTM. When using a deep learning framework, implementing the forward pass is sufficient to build systems that achieve great performance. \n",
"\n",
"The rest of this notebook is optional, and will not be graded."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3 - Backpropagation in recurrent neural networks (OPTIONAL / UNGRADED)\n",
"\n",
"In modern deep learning frameworks, you only have to implement the forward pass, and the framework takes care of the backward pass, so most deep learning engineers do not need to bother with the details of the backward pass. If however you are an expert in calculus and want to see the details of backprop in RNNs, you can work through this optional portion of the notebook. \n",
"\n",
"When in an earlier course you implemented a simple (fully connected) neural network, you used backpropagation to compute the derivatives with respect to the cost to update the parameters. Similarly, in recurrent neural networks you can calculate the derivatives with respect to the cost in order to update the parameters. The backprop equations are quite complicated and we did not derive them in lecture. However, we will briefly present them below. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.1 - Basic RNN backward pass\n",
"\n",
"We will start by computing the backward pass for the basic RNN-cell.\n",
"\n",
"
\n",
" **Figure 6**: RNN-cell's backward pass. Just like in a fully-connected neural network, the derivative of the cost function $J$ backpropagates through the RNN by following the chain-rule from calculus. The chain-rule is also used to calculate $(\\frac{\\partial J}{\\partial W_{ax}},\\frac{\\partial J}{\\partial W_{aa}},\\frac{\\partial J}{\\partial b})$ to update the parameters $(W_{ax}, W_{aa}, b_a)$. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Deriving the one step backward functions: \n",
"\n",
"To compute the `rnn_cell_backward` you need to compute the following equations. It is a good exercise to derive them by hand. \n",
"\n",
"The derivative of $\\tanh$ is $1-\\tanh(x)^2$. You can find the complete proof [here](https://www.wyzant.com/resources/lessons/math/calculus/derivative_proofs/tanx). Note that: $ \\text{sech}(x)^2 = 1 - \\tanh(x)^2$\n",
"\n",
"Similarly for $\\frac{ \\partial a^{\\langle t \\rangle} } {\\partial W_{ax}}, \\frac{ \\partial a^{\\langle t \\rangle} } {\\partial W_{aa}}, \\frac{ \\partial a^{\\langle t \\rangle} } {\\partial b}$, the derivative of $\\tanh(u)$ is $(1-\\tanh(u)^2)du$. \n",
"\n",
"The final two equations also follow the same rule and are derived using the $\\tanh$ derivative. Note that the arrangement is done in a way to get the same dimensions to match."
]
},
{
"cell_type": "code",
"execution_count": 190,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def rnn_cell_backward(da_next, cache):\n",
" \"\"\"\n",
" Implements the backward pass for the RNN-cell (single time-step).\n",
"\n",
" Arguments:\n",
" da_next -- Gradient of loss with respect to next hidden state\n",
" cache -- python dictionary containing useful values (output of rnn_cell_forward())\n",
"\n",
" Returns:\n",
" gradients -- python dictionary containing:\n",
" dx -- Gradients of input data, of shape (n_x, m)\n",
" da_prev -- Gradients of previous hidden state, of shape (n_a, m)\n",
" dWax -- Gradients of input-to-hidden weights, of shape (n_a, n_x)\n",
" dWaa -- Gradients of hidden-to-hidden weights, of shape (n_a, n_a)\n",
" dba -- Gradients of bias vector, of shape (n_a, 1)\n",
" \"\"\"\n",
" \n",
" # Retrieve values from cache\n",
" (a_next, a_prev, xt, parameters) = cache\n",
" \n",
" # Retrieve values from parameters\n",
" Wax = parameters[\"Wax\"]\n",
" Waa = parameters[\"Waa\"]\n",
" Wya = parameters[\"Wya\"]\n",
" ba = parameters[\"ba\"]\n",
" by = parameters[\"by\"]\n",
"\n",
" ### START CODE HERE ###\n",
" # compute the gradient of tanh with respect to a_next (≈1 line)\n",
" dtanh = (1-a_next*a_next)*da_next\n",
"\n",
" # compute the gradient of the loss with respect to Wax (≈2 lines)\n",
" dxt = np.dot(Wax.T, dtanh)\n",
" dWax = np.dot(dtanh,xt.T)\n",
"\n",
" # compute the gradient with respect to Waa (≈2 lines)\n",
" da_prev = np.dot(Waa.T, dtanh) \n",
" dWaa = np.dot( dtanh,a_prev.T)\n",
"\n",
" # compute the gradient with respect to b (≈1 line)\n",
" dba = np.sum( dtanh,keepdims=True,axis=-1)\n",
"\n",
" ### END CODE HERE ###\n",
" \n",
" # Store the gradients in a python dictionary\n",
" gradients = {\"dxt\": dxt, \"da_prev\": da_prev, \"dWax\": dWax, \"dWaa\": dWaa, \"dba\": dba}\n",
" \n",
" return gradients"
]
},
{
"cell_type": "code",
"execution_count": 191,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"gradients[\"dxt\"][1][2] = -1.3872130506\n",
"gradients[\"dxt\"].shape = (3, 10)\n",
"gradients[\"da_prev\"][2][3] = -0.152399493774\n",
"gradients[\"da_prev\"].shape = (5, 10)\n",
"gradients[\"dWax\"][3][1] = 0.410772824935\n",
"gradients[\"dWax\"].shape = (5, 3)\n",
"gradients[\"dWaa\"][1][2] = 1.15034506685\n",
"gradients[\"dWaa\"].shape = (5, 5)\n",
"gradients[\"dba\"][4] = [ 0.20023491]\n",
"gradients[\"dba\"].shape = (5, 1)\n"
]
}
],
"source": [
"np.random.seed(1)\n",
"xt_tmp = np.random.randn(3,10)\n",
"a_prev_tmp = np.random.randn(5,10)\n",
"parameters_tmp = {}\n",
"parameters_tmp['Wax'] = np.random.randn(5,3)\n",
"parameters_tmp['Waa'] = np.random.randn(5,5)\n",
"parameters_tmp['Wya'] = np.random.randn(2,5)\n",
"parameters_tmp['ba'] = np.random.randn(5,1)\n",
"parameters_tmp['by'] = np.random.randn(2,1)\n",
"\n",
"a_next_tmp, yt_tmp, cache_tmp = rnn_cell_forward(xt_tmp, a_prev_tmp, parameters_tmp)\n",
"\n",
"da_next_tmp = np.random.randn(5,10)\n",
"gradients_tmp = rnn_cell_backward(da_next_tmp, cache_tmp)\n",
"print(\"gradients[\\\"dxt\\\"][1][2] =\", gradients_tmp[\"dxt\"][1][2])\n",
"print(\"gradients[\\\"dxt\\\"].shape =\", gradients_tmp[\"dxt\"].shape)\n",
"print(\"gradients[\\\"da_prev\\\"][2][3] =\", gradients_tmp[\"da_prev\"][2][3])\n",
"print(\"gradients[\\\"da_prev\\\"].shape =\", gradients_tmp[\"da_prev\"].shape)\n",
"print(\"gradients[\\\"dWax\\\"][3][1] =\", gradients_tmp[\"dWax\"][3][1])\n",
"print(\"gradients[\\\"dWax\\\"].shape =\", gradients_tmp[\"dWax\"].shape)\n",
"print(\"gradients[\\\"dWaa\\\"][1][2] =\", gradients_tmp[\"dWaa\"][1][2])\n",
"print(\"gradients[\\\"dWaa\\\"].shape =\", gradients_tmp[\"dWaa\"].shape)\n",
"print(\"gradients[\\\"dba\\\"][4] =\", gradients_tmp[\"dba\"][4])\n",
"print(\"gradients[\\\"dba\\\"].shape =\", gradients_tmp[\"dba\"].shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Expected Output**:\n",
"\n",
"\n",
" \n",
" \n",
" **gradients[\"dxt\"][1][2]** =\n",
" | \n",
" \n",
" -0.460564103059\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dxt\"].shape** =\n",
" | \n",
" \n",
" (3, 10)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"da_prev\"][2][3]** =\n",
" | \n",
" \n",
" 0.0842968653807\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"da_prev\"].shape** =\n",
" | \n",
" \n",
" (5, 10)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWax\"][3][1]** =\n",
" | \n",
" \n",
" 0.393081873922\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWax\"].shape** =\n",
" | \n",
" \n",
" (5, 3)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWaa\"][1][2]** = \n",
" | \n",
" \n",
" -0.28483955787\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWaa\"].shape** =\n",
" | \n",
" \n",
" (5, 5)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dba\"][4]** = \n",
" | \n",
" \n",
" [ 0.80517166]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dba\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Backward pass through the RNN\n",
"\n",
"Computing the gradients of the cost with respect to $a^{\\langle t \\rangle}$ at every time-step $t$ is useful because it is what helps the gradient backpropagate to the previous RNN-cell. To do so, you need to iterate through all the time steps starting at the end, and at each step, you increment the overall $db_a$, $dW_{aa}$, $dW_{ax}$ and you store $dx$.\n",
"\n",
"**Instructions**:\n",
"\n",
"Implement the `rnn_backward` function. Initialize the return variables with zeros first and then loop through all the time steps while calling the `rnn_cell_backward` at each time timestep, update the other variables accordingly."
]
},
{
"cell_type": "code",
"execution_count": 192,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def rnn_backward(da, caches):\n",
" \"\"\"\n",
" Implement the backward pass for a RNN over an entire sequence of input data.\n",
"\n",
" Arguments:\n",
" da -- Upstream gradients of all hidden states, of shape (n_a, m, T_x)\n",
" caches -- tuple containing information from the forward pass (rnn_forward)\n",
" \n",
" Returns:\n",
" gradients -- python dictionary containing:\n",
" dx -- Gradient w.r.t. the input data, numpy-array of shape (n_x, m, T_x)\n",
" da0 -- Gradient w.r.t the initial hidden state, numpy-array of shape (n_a, m)\n",
" dWax -- Gradient w.r.t the input's weight matrix, numpy-array of shape (n_a, n_x)\n",
" dWaa -- Gradient w.r.t the hidden state's weight matrix, numpy array of shape (n_a, n_a)\n",
" dba -- Gradient w.r.t the bias, of shape (n_a, 1)\n",
" \"\"\"\n",
" \n",
" ### START CODE HERE ###\n",
" \n",
" # Retrieve values from the first cache (t=1) of caches (≈2 lines)\n",
" (caches, x) = caches\n",
" (a1, a0, x1, parameters) = caches[0]\n",
" \n",
" # Retrieve dimensions from da's and x1's shapes (≈2 lines)\n",
" n_a, m, T_x = da.shape\n",
" n_x, m = x1.shape \n",
" \n",
" # initialize the gradients with the right sizes (≈6 lines)\n",
" dx = np.zeros((n_x, m, T_x)) \n",
" dWax = np.zeros((n_a, n_x))\n",
" dWaa = np.zeros((n_a, n_a))\n",
" dba = np.zeros((n_a, 1)) \n",
" da0 = np.zeros((n_a, m))\n",
" da_prevt = np.zeros((n_a, m)) \n",
" \n",
" # Loop through all the time steps\n",
" for t in reversed(range(T_x)):\n",
" # Compute gradients at time step t. Choose wisely the \"da_next\" and the \"cache\" to use in the backward propagation step. (≈1 line)\n",
" gradients = rnn_cell_backward(da[:, :, t] + da_prevt, caches[t])\n",
" # Retrieve derivatives from gradients (≈ 1 line)\n",
" dxt, da_prevt, dWaxt, dWaat, dbat = gradients[\"dxt\"], gradients[\"da_prev\"], gradients[\"dWax\"], gradients[\"dWaa\"], gradients[\"dba\"]\n",
" # Increment global derivatives w.r.t parameters by adding their derivative at time-step t (≈4 lines)\n",
" dx[:, :, t] = dxt \n",
" dWax += dWaxt \n",
" dWaa += dWaat \n",
" dba += dbat \n",
" \n",
" # Set da0 to the gradient of a which has been backpropagated through all time-steps (≈1 line) \n",
" da0 = da_prevt\n",
" ### END CODE HERE ###\n",
"\n",
" # Store the gradients in a python dictionary\n",
" gradients = {\"dx\": dx, \"da0\": da0, \"dWax\": dWax, \"dWaa\": dWaa,\"dba\": dba}\n",
" \n",
" return gradients"
]
},
{
"cell_type": "code",
"execution_count": 193,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"gradients[\"dx\"][1][2] = [-2.07101689 -0.59255627 0.02466855 0.01483317]\n",
"gradients[\"dx\"].shape = (3, 10, 4)\n",
"gradients[\"da0\"][2][3] = -0.314942375127\n",
"gradients[\"da0\"].shape = (5, 10)\n",
"gradients[\"dWax\"][3][1] = 11.2641044965\n",
"gradients[\"dWax\"].shape = (5, 3)\n",
"gradients[\"dWaa\"][1][2] = 2.30333312658\n",
"gradients[\"dWaa\"].shape = (5, 5)\n",
"gradients[\"dba\"][4] = [-0.74747722]\n",
"gradients[\"dba\"].shape = (5, 1)\n"
]
}
],
"source": [
"np.random.seed(1)\n",
"x_tmp = np.random.randn(3,10,4)\n",
"a0_tmp = np.random.randn(5,10)\n",
"parameters_tmp = {}\n",
"parameters_tmp['Wax'] = np.random.randn(5,3)\n",
"parameters_tmp['Waa'] = np.random.randn(5,5)\n",
"parameters_tmp['Wya'] = np.random.randn(2,5)\n",
"parameters_tmp['ba'] = np.random.randn(5,1)\n",
"parameters_tmp['by'] = np.random.randn(2,1)\n",
"\n",
"a_tmp, y_tmp, caches_tmp = rnn_forward(x_tmp, a0_tmp, parameters_tmp)\n",
"da_tmp = np.random.randn(5, 10, 4)\n",
"gradients_tmp = rnn_backward(da_tmp, caches_tmp)\n",
"\n",
"print(\"gradients[\\\"dx\\\"][1][2] =\", gradients_tmp[\"dx\"][1][2])\n",
"print(\"gradients[\\\"dx\\\"].shape =\", gradients_tmp[\"dx\"].shape)\n",
"print(\"gradients[\\\"da0\\\"][2][3] =\", gradients_tmp[\"da0\"][2][3])\n",
"print(\"gradients[\\\"da0\\\"].shape =\", gradients_tmp[\"da0\"].shape)\n",
"print(\"gradients[\\\"dWax\\\"][3][1] =\", gradients_tmp[\"dWax\"][3][1])\n",
"print(\"gradients[\\\"dWax\\\"].shape =\", gradients_tmp[\"dWax\"].shape)\n",
"print(\"gradients[\\\"dWaa\\\"][1][2] =\", gradients_tmp[\"dWaa\"][1][2])\n",
"print(\"gradients[\\\"dWaa\\\"].shape =\", gradients_tmp[\"dWaa\"].shape)\n",
"print(\"gradients[\\\"dba\\\"][4] =\", gradients_tmp[\"dba\"][4])\n",
"print(\"gradients[\\\"dba\\\"].shape =\", gradients_tmp[\"dba\"].shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Expected Output**:\n",
"\n",
"\n",
" \n",
" \n",
" **gradients[\"dx\"][1][2]** =\n",
" | \n",
" \n",
" [-2.07101689 -0.59255627 0.02466855 0.01483317]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dx\"].shape** =\n",
" | \n",
" \n",
" (3, 10, 4)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"da0\"][2][3]** =\n",
" | \n",
" \n",
" -0.314942375127\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"da0\"].shape** =\n",
" | \n",
" \n",
" (5, 10)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWax\"][3][1]** =\n",
" | \n",
" \n",
" 11.2641044965\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWax\"].shape** =\n",
" | \n",
" \n",
" (5, 3)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWaa\"][1][2]** = \n",
" | \n",
" \n",
" 2.30333312658\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWaa\"].shape** =\n",
" | \n",
" \n",
" (5, 5)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dba\"][4]** = \n",
" | \n",
" \n",
" [-0.74747722]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dba\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.2 - LSTM backward pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.2.1 One Step backward\n",
"\n",
"The LSTM backward pass is slightly more complicated than the forward one. We have provided you with all the equations for the LSTM backward pass below. (If you enjoy calculus exercises feel free to try deriving these from scratch yourself.) \n",
"\n",
"### 3.2.2 gate derivatives\n",
"\n",
"$$d \\Gamma_o^{\\langle t \\rangle} = da_{next}*\\tanh(c_{next}) * \\Gamma_o^{\\langle t \\rangle}*(1-\\Gamma_o^{\\langle t \\rangle})\\tag{7}$$\n",
"\n",
"$$d\\tilde c^{\\langle t \\rangle} = dc_{next}*\\Gamma_u^{\\langle t \\rangle}+ \\Gamma_o^{\\langle t \\rangle} (1-\\tanh(c_{next})^2) * i_t * da_{next} * \\tilde c^{\\langle t \\rangle} * (1-\\tanh(\\tilde c)^2) \\tag{8}$$\n",
"\n",
"$$d\\Gamma_u^{\\langle t \\rangle} = dc_{next}*\\tilde c^{\\langle t \\rangle} + \\Gamma_o^{\\langle t \\rangle} (1-\\tanh(c_{next})^2) * \\tilde c^{\\langle t \\rangle} * da_{next}*\\Gamma_u^{\\langle t \\rangle}*(1-\\Gamma_u^{\\langle t \\rangle})\\tag{9}$$\n",
"\n",
"$$d\\Gamma_f^{\\langle t \\rangle} = dc_{next}*\\tilde c_{prev} + \\Gamma_o^{\\langle t \\rangle} (1-\\tanh(c_{next})^2) * c_{prev} * da_{next}*\\Gamma_f^{\\langle t \\rangle}*(1-\\Gamma_f^{\\langle t \\rangle})\\tag{10}$$\n",
"\n",
"### 3.2.3 parameter derivatives \n",
"\n",
"$$ dW_f = d\\Gamma_f^{\\langle t \\rangle} * \\begin{pmatrix} a_{prev} \\\\ x_t\\end{pmatrix}^T \\tag{11} $$\n",
"$$ dW_u = d\\Gamma_u^{\\langle t \\rangle} * \\begin{pmatrix} a_{prev} \\\\ x_t\\end{pmatrix}^T \\tag{12} $$\n",
"$$ dW_c = d\\tilde c^{\\langle t \\rangle} * \\begin{pmatrix} a_{prev} \\\\ x_t\\end{pmatrix}^T \\tag{13} $$\n",
"$$ dW_o = d\\Gamma_o^{\\langle t \\rangle} * \\begin{pmatrix} a_{prev} \\\\ x_t\\end{pmatrix}^T \\tag{14}$$\n",
"\n",
"To calculate $db_f, db_u, db_c, db_o$ you just need to sum across the horizontal (axis= 1) axis on $d\\Gamma_f^{\\langle t \\rangle}, d\\Gamma_u^{\\langle t \\rangle}, d\\tilde c^{\\langle t \\rangle}, d\\Gamma_o^{\\langle t \\rangle}$ respectively. Note that you should have the `keep_dims = True` option.\n",
"\n",
"Finally, you will compute the derivative with respect to the previous hidden state, previous memory state, and input.\n",
"\n",
"$$ da_{prev} = W_f^T*d\\Gamma_f^{\\langle t \\rangle} + W_u^T * d\\Gamma_u^{\\langle t \\rangle}+ W_c^T * d\\tilde c^{\\langle t \\rangle} + W_o^T * d\\Gamma_o^{\\langle t \\rangle} \\tag{15}$$\n",
"Here, the weights for equations 13 are the first n_a, (i.e. $W_f = W_f[:n_a,:]$ etc...)\n",
"\n",
"$$ dc_{prev} = dc_{next}\\Gamma_f^{\\langle t \\rangle} + \\Gamma_o^{\\langle t \\rangle} * (1- \\tanh(c_{next})^2)*\\Gamma_f^{\\langle t \\rangle}*da_{next} \\tag{16}$$\n",
"$$ dx^{\\langle t \\rangle} = W_f^T*d\\Gamma_f^{\\langle t \\rangle} + W_u^T * d\\Gamma_u^{\\langle t \\rangle}+ W_c^T * d\\tilde c_t + W_o^T * d\\Gamma_o^{\\langle t \\rangle}\\tag{17} $$\n",
"where the weights for equation 15 are from n_a to the end, (i.e. $W_f = W_f[n_a:,:]$ etc...)\n",
"\n",
"**Exercise:** Implement `lstm_cell_backward` by implementing equations $7-17$ below. Good luck! :)"
]
},
{
"cell_type": "code",
"execution_count": 194,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def lstm_cell_backward(da_next, dc_next, cache):\n",
" \"\"\"\n",
" Implement the backward pass for the LSTM-cell (single time-step).\n",
"\n",
" Arguments:\n",
" da_next -- Gradients of next hidden state, of shape (n_a, m)\n",
" dc_next -- Gradients of next cell state, of shape (n_a, m)\n",
" cache -- cache storing information from the forward pass\n",
"\n",
" Returns:\n",
" gradients -- python dictionary containing:\n",
" dxt -- Gradient of input data at time-step t, of shape (n_x, m)\n",
" da_prev -- Gradient w.r.t. the previous hidden state, numpy array of shape (n_a, m)\n",
" dc_prev -- Gradient w.r.t. the previous memory state, of shape (n_a, m, T_x)\n",
" dWf -- Gradient w.r.t. the weight matrix of the forget gate, numpy array of shape (n_a, n_a + n_x)\n",
" dWi -- Gradient w.r.t. the weight matrix of the update gate, numpy array of shape (n_a, n_a + n_x)\n",
" dWc -- Gradient w.r.t. the weight matrix of the memory gate, numpy array of shape (n_a, n_a + n_x)\n",
" dWo -- Gradient w.r.t. the weight matrix of the output gate, numpy array of shape (n_a, n_a + n_x)\n",
" dbf -- Gradient w.r.t. biases of the forget gate, of shape (n_a, 1)\n",
" dbi -- Gradient w.r.t. biases of the update gate, of shape (n_a, 1)\n",
" dbc -- Gradient w.r.t. biases of the memory gate, of shape (n_a, 1)\n",
" dbo -- Gradient w.r.t. biases of the output gate, of shape (n_a, 1)\n",
" \"\"\"\n",
"\n",
" # Retrieve information from \"cache\"\n",
" (a_next, c_next, a_prev, c_prev, ft, it, cct, ot, xt, parameters) = cache\n",
" \n",
" ### START CODE HERE ###\n",
" # Retrieve dimensions from xt's and a_next's shape (≈2 lines)\n",
" n_x, m = xt.shape \n",
" n_a, m = a_next.shape \n",
" \n",
" # Compute gates related derivatives, you can find their values can be found by looking carefully at equations (7) to (10) (≈4 lines)\n",
" # Code equations (7) to (10) (≈4 lines)\n",
" dit = (da_next * ot * (1 - np.tanh(c_next) ** 2) + dc_next) * cct * (1 - it) * it\n",
" dft = (da_next * ot * (1 - np.tanh(c_next) ** 2) + dc_next) * c_prev * ft * (1 - ft)\n",
" dot = da_next * np.tanh(c_next) * ot * (1 - ot)\n",
" dcct = (da_next * ot * (1 - np.tanh(c_next) ** 2) + dc_next) * it * (1 - cct ** 2)\n",
"\n",
" # Compute parameters related derivatives. Use equations (11)-(14) (≈8 lines)\n",
" dWf = np.dot(dft,np.concatenate((a_prev, xt), axis=0).T) # or use np.dot(dft, np.hstack([a_prev.T, xt.T]))\n",
" dWi = np.dot(dit,np.concatenate((a_prev, xt), axis=0).T)\n",
" dWc = np.dot(dcct,np.concatenate((a_prev, xt), axis=0).T)\n",
" dWo = np.dot(dot,np.concatenate((a_prev, xt), axis=0).T)\n",
" dbf = np.sum(dft,axis=1,keepdims=True)\n",
" dbi = np.sum(dit,axis=1,keepdims=True) \n",
" dbc = np.sum(dcct,axis=1,keepdims=True) \n",
" dbo = np.sum(dot,axis=1,keepdims=True) \n",
"\n",
" # Compute derivatives w.r.t previous hidden state, previous memory state and input. Use equations (15)-(17). (≈3 lines)\n",
" da_prev = np.dot(parameters['Wf'][:,:n_a].T,dft)+np.dot(parameters['Wi'][:,:n_a].T,dit)+np.dot(parameters['Wc'][:,:n_a].T,dcct)+np.dot(parameters['Wo'][:,:n_a].T,dot) \n",
" dc_prev = dc_next*ft+ot*(1-np.square(np.tanh(c_next)))*ft*da_next \n",
" dxt = np.dot(parameters['Wf'][:,n_a:].T,dft)+np.dot(parameters['Wi'][:,n_a:].T,dit)+np.dot(parameters['Wc'][:,n_a:].T,dcct)+np.dot(parameters['Wo'][:,n_a:].T,dot) \n",
" ### END CODE HERE ###\n",
" \n",
" # Save gradients in dictionary\n",
" gradients = {\"dxt\": dxt, \"da_prev\": da_prev, \"dc_prev\": dc_prev, \"dWf\": dWf,\"dbf\": dbf, \"dWi\": dWi,\"dbi\": dbi,\n",
" \"dWc\": dWc,\"dbc\": dbc, \"dWo\": dWo,\"dbo\": dbo}\n",
"\n",
" return gradients"
]
},
{
"cell_type": "code",
"execution_count": 195,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"gradients[\"dxt\"][1][2] = 3.23055911511\n",
"gradients[\"dxt\"].shape = (3, 10)\n",
"gradients[\"da_prev\"][2][3] = -0.0639621419711\n",
"gradients[\"da_prev\"].shape = (5, 10)\n",
"gradients[\"dc_prev\"][2][3] = 0.797522038797\n",
"gradients[\"dc_prev\"].shape = (5, 10)\n",
"gradients[\"dWf\"][3][1] = -0.147954838164\n",
"gradients[\"dWf\"].shape = (5, 8)\n",
"gradients[\"dWi\"][1][2] = 1.05749805523\n",
"gradients[\"dWi\"].shape = (5, 8)\n",
"gradients[\"dWc\"][3][1] = 2.30456216369\n",
"gradients[\"dWc\"].shape = (5, 8)\n",
"gradients[\"dWo\"][1][2] = 0.331311595289\n",
"gradients[\"dWo\"].shape = (5, 8)\n",
"gradients[\"dbf\"][4] = [ 0.18864637]\n",
"gradients[\"dbf\"].shape = (5, 1)\n",
"gradients[\"dbi\"][4] = [-0.40142491]\n",
"gradients[\"dbi\"].shape = (5, 1)\n",
"gradients[\"dbc\"][4] = [ 0.25587763]\n",
"gradients[\"dbc\"].shape = (5, 1)\n",
"gradients[\"dbo\"][4] = [ 0.13893342]\n",
"gradients[\"dbo\"].shape = (5, 1)\n"
]
}
],
"source": [
"np.random.seed(1)\n",
"xt_tmp = np.random.randn(3,10)\n",
"a_prev_tmp = np.random.randn(5,10)\n",
"c_prev_tmp = np.random.randn(5,10)\n",
"parameters_tmp = {}\n",
"parameters_tmp['Wf'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bf'] = np.random.randn(5,1)\n",
"parameters_tmp['Wi'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bi'] = np.random.randn(5,1)\n",
"parameters_tmp['Wo'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bo'] = np.random.randn(5,1)\n",
"parameters_tmp['Wc'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bc'] = np.random.randn(5,1)\n",
"parameters_tmp['Wy'] = np.random.randn(2,5)\n",
"parameters_tmp['by'] = np.random.randn(2,1)\n",
"\n",
"a_next_tmp, c_next_tmp, yt_tmp, cache_tmp = lstm_cell_forward(xt_tmp, a_prev_tmp, c_prev_tmp, parameters_tmp)\n",
"\n",
"da_next_tmp = np.random.randn(5,10)\n",
"dc_next_tmp = np.random.randn(5,10)\n",
"gradients_tmp = lstm_cell_backward(da_next_tmp, dc_next_tmp, cache_tmp)\n",
"print(\"gradients[\\\"dxt\\\"][1][2] =\", gradients_tmp[\"dxt\"][1][2])\n",
"print(\"gradients[\\\"dxt\\\"].shape =\", gradients_tmp[\"dxt\"].shape)\n",
"print(\"gradients[\\\"da_prev\\\"][2][3] =\", gradients_tmp[\"da_prev\"][2][3])\n",
"print(\"gradients[\\\"da_prev\\\"].shape =\", gradients_tmp[\"da_prev\"].shape)\n",
"print(\"gradients[\\\"dc_prev\\\"][2][3] =\", gradients_tmp[\"dc_prev\"][2][3])\n",
"print(\"gradients[\\\"dc_prev\\\"].shape =\", gradients_tmp[\"dc_prev\"].shape)\n",
"print(\"gradients[\\\"dWf\\\"][3][1] =\", gradients_tmp[\"dWf\"][3][1])\n",
"print(\"gradients[\\\"dWf\\\"].shape =\", gradients_tmp[\"dWf\"].shape)\n",
"print(\"gradients[\\\"dWi\\\"][1][2] =\", gradients_tmp[\"dWi\"][1][2])\n",
"print(\"gradients[\\\"dWi\\\"].shape =\", gradients_tmp[\"dWi\"].shape)\n",
"print(\"gradients[\\\"dWc\\\"][3][1] =\", gradients_tmp[\"dWc\"][3][1])\n",
"print(\"gradients[\\\"dWc\\\"].shape =\", gradients_tmp[\"dWc\"].shape)\n",
"print(\"gradients[\\\"dWo\\\"][1][2] =\", gradients_tmp[\"dWo\"][1][2])\n",
"print(\"gradients[\\\"dWo\\\"].shape =\", gradients_tmp[\"dWo\"].shape)\n",
"print(\"gradients[\\\"dbf\\\"][4] =\", gradients_tmp[\"dbf\"][4])\n",
"print(\"gradients[\\\"dbf\\\"].shape =\", gradients_tmp[\"dbf\"].shape)\n",
"print(\"gradients[\\\"dbi\\\"][4] =\", gradients_tmp[\"dbi\"][4])\n",
"print(\"gradients[\\\"dbi\\\"].shape =\", gradients_tmp[\"dbi\"].shape)\n",
"print(\"gradients[\\\"dbc\\\"][4] =\", gradients_tmp[\"dbc\"][4])\n",
"print(\"gradients[\\\"dbc\\\"].shape =\", gradients_tmp[\"dbc\"].shape)\n",
"print(\"gradients[\\\"dbo\\\"][4] =\", gradients_tmp[\"dbo\"][4])\n",
"print(\"gradients[\\\"dbo\\\"].shape =\", gradients_tmp[\"dbo\"].shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Expected Output**:\n",
"\n",
"\n",
" \n",
" \n",
" **gradients[\"dxt\"][1][2]** =\n",
" | \n",
" \n",
" 3.23055911511\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dxt\"].shape** =\n",
" | \n",
" \n",
" (3, 10)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"da_prev\"][2][3]** =\n",
" | \n",
" \n",
" -0.0639621419711\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"da_prev\"].shape** =\n",
" | \n",
" \n",
" (5, 10)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dc_prev\"][2][3]** =\n",
" | \n",
" \n",
" 0.797522038797\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dc_prev\"].shape** =\n",
" | \n",
" \n",
" (5, 10)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWf\"][3][1]** = \n",
" | \n",
" \n",
" -0.147954838164\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWf\"].shape** =\n",
" | \n",
" \n",
" (5, 8)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWi\"][1][2]** = \n",
" | \n",
" \n",
" 1.05749805523\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWi\"].shape** = \n",
" | \n",
" \n",
" (5, 8)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWc\"][3][1]** = \n",
" | \n",
" \n",
" 2.30456216369\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWc\"].shape** = \n",
" | \n",
" \n",
" (5, 8)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWo\"][1][2]** = \n",
" | \n",
" \n",
" 0.331311595289\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWo\"].shape** = \n",
" | \n",
" \n",
" (5, 8)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbf\"][4]** = \n",
" | \n",
" \n",
" [ 0.18864637]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbf\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbi\"][4]** = \n",
" | \n",
" \n",
" [-0.40142491]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbi\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbc\"][4]** = \n",
" | \n",
" \n",
" [ 0.25587763]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbc\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbo\"][4]** = \n",
" | \n",
" \n",
" [ 0.13893342]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbo\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.3 Backward pass through the LSTM RNN\n",
"\n",
"This part is very similar to the `rnn_backward` function you implemented above. You will first create variables of the same dimension as your return variables. You will then iterate over all the time steps starting from the end and call the one step function you implemented for LSTM at each iteration. You will then update the parameters by summing them individually. Finally return a dictionary with the new gradients. \n",
"\n",
"**Instructions**: Implement the `lstm_backward` function. Create a for loop starting from $T_x$ and going backward. For each step call `lstm_cell_backward` and update the your old gradients by adding the new gradients to them. Note that `dxt` is not updated but is stored."
]
},
{
"cell_type": "code",
"execution_count": 196,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def lstm_backward(da, caches):\n",
" \n",
" \"\"\"\n",
" Implement the backward pass for the RNN with LSTM-cell (over a whole sequence).\n",
"\n",
" Arguments:\n",
" da -- Gradients w.r.t the hidden states, numpy-array of shape (n_a, m, T_x)\n",
" caches -- cache storing information from the forward pass (lstm_forward)\n",
"\n",
" Returns:\n",
" gradients -- python dictionary containing:\n",
" dx -- Gradient of inputs, of shape (n_x, m, T_x)\n",
" da0 -- Gradient w.r.t. the previous hidden state, numpy array of shape (n_a, m)\n",
" dWf -- Gradient w.r.t. the weight matrix of the forget gate, numpy array of shape (n_a, n_a + n_x)\n",
" dWi -- Gradient w.r.t. the weight matrix of the update gate, numpy array of shape (n_a, n_a + n_x)\n",
" dWc -- Gradient w.r.t. the weight matrix of the memory gate, numpy array of shape (n_a, n_a + n_x)\n",
" dWo -- Gradient w.r.t. the weight matrix of the save gate, numpy array of shape (n_a, n_a + n_x)\n",
" dbf -- Gradient w.r.t. biases of the forget gate, of shape (n_a, 1)\n",
" dbi -- Gradient w.r.t. biases of the update gate, of shape (n_a, 1)\n",
" dbc -- Gradient w.r.t. biases of the memory gate, of shape (n_a, 1)\n",
" dbo -- Gradient w.r.t. biases of the save gate, of shape (n_a, 1)\n",
" \"\"\"\n",
"\n",
" # Retrieve values from the first cache (t=1) of caches.\n",
" (caches, x) = caches\n",
" (a1, c1, a0, c0, f1, i1, cc1, o1, x1, parameters) = caches[0]\n",
" \n",
" ### START CODE HERE ###\n",
" # Retrieve dimensions from da's and x1's shapes (≈2 lines)\n",
" n_a, m, T_x = da.shape\n",
" n_x, m = x1.shape\n",
" \n",
" # initialize the gradients with the right sizes (≈12 lines)\n",
" dx = np.zeros((n_x, m, T_x))\n",
" da0 = np.zeros((n_a, m))\n",
" da_prevt = np.zeros((n_a, m))\n",
" dc_prevt = np.zeros((n_a, m))\n",
" dWf = np.zeros((n_a, n_a + n_x))\n",
" dWi = np.zeros((n_a, n_a + n_x))\n",
" dWc = np.zeros((n_a, n_a + n_x))\n",
" dWo = np.zeros((n_a, n_a + n_x))\n",
" dbf = np.zeros((n_a, 1))\n",
" dbi = np.zeros((n_a, 1))\n",
" dbc = np.zeros((n_a, 1))\n",
" dbo = np.zeros((n_a, 1))\n",
" \n",
" # loop back over the whole sequence\n",
" for t in reversed(range(T_x)):\n",
" # Compute all gradients using lstm_cell_backward\n",
" gradients = lstm_cell_backward(da[:,:,t] + da_prevt, dc_prevt, caches[t])\n",
" # Store or add the gradient to the parameters' previous step's gradient\n",
" dx[:,:,t] = gradients[\"dxt\"]\n",
" dWf += gradients[\"dWf\"]\n",
" dWi += gradients[\"dWi\"]\n",
" dWc += gradients[\"dWc\"]\n",
" dWo += gradients[\"dWo\"]\n",
" dbf += gradients[\"dbf\"]\n",
" dbi += gradients[\"dbi\"]\n",
" dbc += gradients[\"dbc\"]\n",
" dbo += gradients[\"dbo\"]\n",
" # Set the first activation's gradient to the backpropagated gradient da_prev.\n",
" da0 = gradients[\"da_prev\"]\n",
" \n",
" ### END CODE HERE ###\n",
"\n",
" # Store the gradients in a python dictionary\n",
" gradients = {\"dx\": dx, \"da0\": da0, \"dWf\": dWf,\"dbf\": dbf, \"dWi\": dWi,\"dbi\": dbi,\n",
" \"dWc\": dWc,\"dbc\": dbc, \"dWo\": dWo,\"dbo\": dbo}\n",
" \n",
" return gradients"
]
},
{
"cell_type": "code",
"execution_count": 197,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"gradients[\"dx\"][1][2] = [ 0.01980463 -0.02745056 -0.31327706 0.53886581]\n",
"gradients[\"dx\"].shape = (3, 10, 4)\n",
"gradients[\"da0\"][2][3] = -0.000284495289749\n",
"gradients[\"da0\"].shape = (5, 10)\n",
"gradients[\"dWf\"][3][1] = -0.0153890043327\n",
"gradients[\"dWf\"].shape = (5, 8)\n",
"gradients[\"dWi\"][1][2] = -0.10924217935\n",
"gradients[\"dWi\"].shape = (5, 8)\n",
"gradients[\"dWc\"][3][1] = 0.0793905844933\n",
"gradients[\"dWc\"].shape = (5, 8)\n",
"gradients[\"dWo\"][1][2] = -0.0810144543621\n",
"gradients[\"dWo\"].shape = (5, 8)\n",
"gradients[\"dbf\"][4] = [-0.24148921]\n",
"gradients[\"dbf\"].shape = (5, 1)\n",
"gradients[\"dbi\"][4] = [-0.08824333]\n",
"gradients[\"dbi\"].shape = (5, 1)\n",
"gradients[\"dbc\"][4] = [ 0.14411048]\n",
"gradients[\"dbc\"].shape = (5, 1)\n",
"gradients[\"dbo\"][4] = [-0.45977321]\n",
"gradients[\"dbo\"].shape = (5, 1)\n"
]
}
],
"source": [
"np.random.seed(1)\n",
"x_tmp = np.random.randn(3,10,7)\n",
"a0_tmp = np.random.randn(5,10)\n",
"\n",
"parameters_tmp = {}\n",
"parameters_tmp['Wf'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bf'] = np.random.randn(5,1)\n",
"parameters_tmp['Wi'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bi'] = np.random.randn(5,1)\n",
"parameters_tmp['Wo'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bo'] = np.random.randn(5,1)\n",
"parameters_tmp['Wc'] = np.random.randn(5, 5+3)\n",
"parameters_tmp['bc'] = np.random.randn(5,1)\n",
"parameters_tmp['Wy'] = np.random.randn(2,5)\n",
"parameters_tmp['by'] = np.random.randn(2,1)\n",
"\n",
"a_tmp, y_tmp, c_tmp, caches_tmp = lstm_forward(x_tmp, a0_tmp, parameters_tmp)\n",
"\n",
"da_tmp = np.random.randn(5, 10, 4)\n",
"gradients_tmp = lstm_backward(da_tmp, caches_tmp)\n",
"\n",
"print(\"gradients[\\\"dx\\\"][1][2] =\", gradients_tmp[\"dx\"][1][2])\n",
"print(\"gradients[\\\"dx\\\"].shape =\", gradients_tmp[\"dx\"].shape)\n",
"print(\"gradients[\\\"da0\\\"][2][3] =\", gradients_tmp[\"da0\"][2][3])\n",
"print(\"gradients[\\\"da0\\\"].shape =\", gradients_tmp[\"da0\"].shape)\n",
"print(\"gradients[\\\"dWf\\\"][3][1] =\", gradients_tmp[\"dWf\"][3][1])\n",
"print(\"gradients[\\\"dWf\\\"].shape =\", gradients_tmp[\"dWf\"].shape)\n",
"print(\"gradients[\\\"dWi\\\"][1][2] =\", gradients_tmp[\"dWi\"][1][2])\n",
"print(\"gradients[\\\"dWi\\\"].shape =\", gradients_tmp[\"dWi\"].shape)\n",
"print(\"gradients[\\\"dWc\\\"][3][1] =\", gradients_tmp[\"dWc\"][3][1])\n",
"print(\"gradients[\\\"dWc\\\"].shape =\", gradients_tmp[\"dWc\"].shape)\n",
"print(\"gradients[\\\"dWo\\\"][1][2] =\", gradients_tmp[\"dWo\"][1][2])\n",
"print(\"gradients[\\\"dWo\\\"].shape =\", gradients_tmp[\"dWo\"].shape)\n",
"print(\"gradients[\\\"dbf\\\"][4] =\", gradients_tmp[\"dbf\"][4])\n",
"print(\"gradients[\\\"dbf\\\"].shape =\", gradients_tmp[\"dbf\"].shape)\n",
"print(\"gradients[\\\"dbi\\\"][4] =\", gradients_tmp[\"dbi\"][4])\n",
"print(\"gradients[\\\"dbi\\\"].shape =\", gradients_tmp[\"dbi\"].shape)\n",
"print(\"gradients[\\\"dbc\\\"][4] =\", gradients_tmp[\"dbc\"][4])\n",
"print(\"gradients[\\\"dbc\\\"].shape =\", gradients_tmp[\"dbc\"].shape)\n",
"print(\"gradients[\\\"dbo\\\"][4] =\", gradients_tmp[\"dbo\"][4])\n",
"print(\"gradients[\\\"dbo\\\"].shape =\", gradients_tmp[\"dbo\"].shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Expected Output**:\n",
"\n",
"\n",
" \n",
" \n",
" **gradients[\"dx\"][1][2]** =\n",
" | \n",
" \n",
" [-0.00173313 0.08287442 -0.30545663 -0.43281115]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dx\"].shape** =\n",
" | \n",
" \n",
" (3, 10, 4)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"da0\"][2][3]** =\n",
" | \n",
" \n",
" -0.095911501954\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"da0\"].shape** =\n",
" | \n",
" \n",
" (5, 10)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWf\"][3][1]** = \n",
" | \n",
" \n",
" -0.0698198561274\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWf\"].shape** =\n",
" | \n",
" \n",
" (5, 8)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWi\"][1][2]** = \n",
" | \n",
" \n",
" 0.102371820249\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWi\"].shape** = \n",
" | \n",
" \n",
" (5, 8)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWc\"][3][1]** = \n",
" | \n",
" \n",
" -0.0624983794927\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWc\"].shape** = \n",
" | \n",
" \n",
" (5, 8)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWo\"][1][2]** = \n",
" | \n",
" \n",
" 0.0484389131444\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dWo\"].shape** = \n",
" | \n",
" \n",
" (5, 8)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbf\"][4]** = \n",
" | \n",
" \n",
" [-0.0565788]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbf\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbi\"][4]** = \n",
" | \n",
" \n",
" [-0.06997391]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbi\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbc\"][4]** = \n",
" | \n",
" \n",
" [-0.27441821]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbc\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbo\"][4]** = \n",
" | \n",
" \n",
" [ 0.16532821]\n",
" | \n",
"
\n",
" \n",
" \n",
" **gradients[\"dbo\"].shape** = \n",
" | \n",
" \n",
" (5, 1)\n",
" | \n",
"
\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Congratulations !\n",
"\n",
"Congratulations on completing this assignment. You now understand how recurrent neural networks work! \n",
"\n",
"Let's go on to the next exercise, where you'll use an RNN to build a character-level language model.\n"
]
}
],
"metadata": {
"coursera": {
"course_slug": "nlp-sequence-models",
"graded_item_id": "xxuVc",
"launcher_item_id": "X20PE"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}