{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "adTDe2CTh3MU" }, "source": [ "# Neural Machine Translation\n", "\n", "Welcome to your first programming assignment for this week! \n", "\n", "* You will build a Neural Machine Translation (NMT) model to translate human-readable dates (\"25th of June, 2009\") into machine-readable dates (\"2009-06-25\"). \n", "* You will do this using an attention model, one of the most sophisticated sequence-to-sequence models. \n", "\n", "This notebook was produced together with NVIDIA's Deep Learning Institute. " ] }, { "cell_type": "markdown", "metadata": { "id": "0LCkjDBFh3Md" }, "source": [ "## Table of Contents\n", "\n", "- [Packages](#0)\n", "- [1 - Translating Human Readable Dates Into Machine Readable Dates](#1)\n", " - [1.1 - Dataset](#1-1)\n", "- [2 - Neural Machine Translation with Attention](#2)\n", " - [2.1 - Attention Mechanism](#2-1)\n", " - [Exercise 1 - one_step_attention](#ex-1)\n", " - [Exercise 2 - modelf](#ex-2)\n", " - [Exercise 3 - Compile the Model](#ex-3)\n", "- [3 - Visualizing Attention (Optional / Ungraded)](#3)\n", " - [3.1 - Getting the Attention Weights From the Network](#3-1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 14508, "status": "ok", "timestamp": 1612468511651, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "RcBRMzPiiMmp", "outputId": "17e9a429-5bb6-4401-a23a-f8f756d6113d" }, "outputs": [], "source": [ "from tensorflow.keras.layers import Bidirectional, Concatenate, Permute, Dot, Input, LSTM, Multiply\n", "from tensorflow.keras.layers import RepeatVector, Dense, Activation, Lambda\n", "from tensorflow.keras.optimizers import Adam\n", "from tensorflow.keras.utils import to_categorical\n", "from tensorflow.keras.models import load_model, Model\n", "import tensorflow.keras.backend as K\n", "import tensorflow as tf\n", "import numpy as np\n", "\n", "from faker import Faker\n", "import random\n", "from tqdm import tqdm\n", "from babel.dates import format_date\n", "from nmt_utils import *\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": { "id": "J0pkH-k0h3Mf" }, "source": [ "\n", "## 1 - Translating Human Readable Dates Into Machine Readable Dates\n", "\n", "* The model you will build here could be used to translate from one language to another, such as translating from English to Hindi. \n", "* However, language translation requires massive datasets and usually takes days of training on GPUs. \n", "* To give you a place to experiment with these models without using massive datasets, we will perform a simpler \"date translation\" task. \n", "* The network will input a date written in a variety of possible formats (*e.g. \"the 29th of August 1958\", \"03/30/1968\", \"24 JUNE 1987\"*) \n", "* The network will translate them into standardized, machine readable dates (*e.g. \"1958-08-29\", \"1968-03-30\", \"1987-06-24\"*). \n", "* We will have the network learn to output dates in the common machine-readable format YYYY-MM-DD. \n", "\n", " " ] }, { "cell_type": "markdown", "metadata": { "id": "8BhEaJvph3Mf" }, "source": [ "\n", "### 1.1 - Dataset\n", "\n", "We will train the model on a dataset of 10,000 human readable dates and their equivalent, standardized, machine readable dates. Let's run the following cells to load the dataset and print some examples. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 16981, "status": "ok", "timestamp": 1612468514155, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "gwIf5l17h3Mg", "outputId": "1fca5fb8-3a9b-4a78-f726-7aef8e14ee41" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 10000/10000 [00:00<00:00, 23782.56it/s]\n" ] } ], "source": [ "m = 10000\n", "dataset, human_vocab, machine_vocab, inv_machine_vocab = load_dataset(m)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 16972, "status": "ok", "timestamp": 1612468514156, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "zCTqMyPch3Mg", "outputId": "42c9d8aa-d07b-4618-ab8a-4db4e1b971e2", "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "[('9 may 1998', '1998-05-09'),\n", " ('10.11.19', '2019-11-10'),\n", " ('9/10/70', '1970-09-10'),\n", " ('saturday april 28 1990', '1990-04-28'),\n", " ('thursday january 26 1995', '1995-01-26'),\n", " ('monday march 7 1983', '1983-03-07'),\n", " ('sunday may 22 1988', '1988-05-22'),\n", " ('08 jul 2008', '2008-07-08'),\n", " ('8 sep 1999', '1999-09-08'),\n", " ('thursday january 1 1981', '1981-01-01')]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset[:10]" ] }, { "cell_type": "markdown", "metadata": { "id": "ao4Ffrkxh3Mg" }, "source": [ "You've loaded:\n", "- `dataset`: a list of tuples of (human readable date, machine readable date).\n", "- `human_vocab`: a python dictionary mapping all characters used in the human readable dates to an integer-valued index.\n", "- `machine_vocab`: a python dictionary mapping all characters used in machine readable dates to an integer-valued index. \n", " - **Note**: These indices are not necessarily consistent with `human_vocab`. \n", "- `inv_machine_vocab`: the inverse dictionary of `machine_vocab`, mapping from indices back to characters. \n", "\n", "Let's preprocess the data and map the raw text data into the index values. \n", "- We will set Tx=30 \n", " - We assume Tx is the maximum length of the human readable date.\n", " - If we get a longer input, we would have to truncate it.\n", "- We will set Ty=10\n", " - \"YYYY-MM-DD\" is 10 characters long." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{' ': 0,\n", " '.': 1,\n", " '/': 2,\n", " '0': 3,\n", " '1': 4,\n", " '2': 5,\n", " '3': 6,\n", " '4': 7,\n", " '5': 8,\n", " '6': 9,\n", " '7': 10,\n", " '8': 11,\n", " '9': 12,\n", " 'a': 13,\n", " 'b': 14,\n", " 'c': 15,\n", " 'd': 16,\n", " 'e': 17,\n", " 'f': 18,\n", " 'g': 19,\n", " 'h': 20,\n", " 'i': 21,\n", " 'j': 22,\n", " 'l': 23,\n", " 'm': 24,\n", " 'n': 25,\n", " 'o': 26,\n", " 'p': 27,\n", " 'r': 28,\n", " 's': 29,\n", " 't': 30,\n", " 'u': 31,\n", " 'v': 32,\n", " 'w': 33,\n", " 'y': 34,\n", " '': 35,\n", " '': 36}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "human_vocab" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),\n", " PhysicalDevice(name='/physical_device:XLA_CPU:0', device_type='XLA_CPU')]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.config.list_physical_devices()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 16962, "status": "ok", "timestamp": 1612468514157, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "Qdso90EBh3Mg", "outputId": "0a364ad8-8b25-4de3-f036-d5d8e40bdf8c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X.shape: (10000, 30)\n", "Y.shape: (10000, 10)\n", "Xoh.shape: (10000, 30, 37)\n", "Yoh.shape: (10000, 10, 11)\n" ] } ], "source": [ "Tx = 30\n", "Ty = 10\n", "X, Y, Xoh, Yoh = preprocess_data(dataset, human_vocab, machine_vocab, Tx, Ty)\n", "\n", "print(\"X.shape:\", X.shape)\n", "print(\"Y.shape:\", Y.shape)\n", "print(\"Xoh.shape:\", Xoh.shape)\n", "print(\"Yoh.shape:\", Yoh.shape)" ] }, { "cell_type": "markdown", "metadata": { "id": "q9C0UY25h3Mh" }, "source": [ "You now have:\n", "- `X`: a processed version of the human readable dates in the training set.\n", " - Each character in X is replaced by an index (integer) mapped to the character using `human_vocab`. \n", " - Each date is padded to ensure a length of $T_x$ using a special character (< pad >). \n", " - `X.shape = (m, Tx)` where m is the number of training examples in a batch.\n", "- `Y`: a processed version of the machine readable dates in the training set.\n", " - Each character is replaced by the index (integer) it is mapped to in `machine_vocab`. \n", " - `Y.shape = (m, Ty)`. \n", "- `Xoh`: one-hot version of `X`\n", " - Each index in `X` is converted to the one-hot representation (if the index is 2, the one-hot version has the index position 2 set to 1, and the remaining positions are 0.\n", " - `Xoh.shape = (m, Tx, len(human_vocab))`\n", "- `Yoh`: one-hot version of `Y`\n", " - Each index in `Y` is converted to the one-hot representation. \n", " - `Yoh.shape = (m, Ty, len(machine_vocab))`. \n", " - `len(machine_vocab) = 11` since there are 10 numeric digits (0 to 9) and the `-` symbol." ] }, { "cell_type": "markdown", "metadata": { "id": "N7qKvWrTh3Mh" }, "source": [ "* Let's also look at some examples of preprocessed training examples. \n", "* Feel free to play with `index` in the cell below to navigate the dataset and see how source/target dates are preprocessed. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 16952, "status": "ok", "timestamp": 1612468514158, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "kUOayR4gh3Mh", "outputId": "d20994de-bbea-4cc7-ffaf-38a05974c9db" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Source date: 9 may 1998\n", "Target date: 1998-05-09\n", "\n", "Source after preprocessing (indices): [12 0 24 13 34 0 4 12 12 11 36 36 36 36 36 36 36 36 36 36 36 36 36 36\n", " 36 36 36 36 36 36]\n", "Target after preprocessing (indices): [ 2 10 10 9 0 1 6 0 1 10]\n", "\n", "Source after preprocessing (one-hot): [[0. 0. 0. ... 0. 0. 0.]\n", " [1. 0. 0. ... 0. 0. 0.]\n", " [0. 0. 0. ... 0. 0. 0.]\n", " ...\n", " [0. 0. 0. ... 0. 0. 1.]\n", " [0. 0. 0. ... 0. 0. 1.]\n", " [0. 0. 0. ... 0. 0. 1.]]\n", "Target after preprocessing (one-hot): [[0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]\n" ] } ], "source": [ "index = 0\n", "print(\"Source date:\", dataset[index][0])\n", "print(\"Target date:\", dataset[index][1])\n", "print()\n", "print(\"Source after preprocessing (indices):\", X[index])\n", "print(\"Target after preprocessing (indices):\", Y[index])\n", "print()\n", "print(\"Source after preprocessing (one-hot):\", Xoh[index])\n", "print(\"Target after preprocessing (one-hot):\", Yoh[index])" ] }, { "cell_type": "markdown", "metadata": { "id": "94o4RYbOh3Mi" }, "source": [ "\n", "## 2 - Neural Machine Translation with Attention\n", "\n", "* If you had to translate a book's paragraph from French to English, you would not read the whole paragraph, then close the book and translate. \n", "* Even during the translation process, you would read/re-read and focus on the parts of the French paragraph corresponding to the parts of the English you are writing down. \n", "* The attention mechanism tells a Neural Machine Translation model where it should pay attention to at any step. \n", "\n", "\n", "### 2.1 - Attention Mechanism\n", "\n", "In this part, you will implement the attention mechanism presented in the lecture videos. \n", "* Here is a figure to remind you how the model works. \n", " * The diagram on the left shows the attention model. \n", " * The diagram on the right shows what one \"attention\" step does to calculate the attention variables $\\alpha^{\\langle t, t' \\rangle}$.\n", " * The attention variables $\\alpha^{\\langle t, t' \\rangle}$ are used to compute the context variable $context^{\\langle t \\rangle}$ for each timestep in the output ($t=1, \\ldots, T_y$). \n", "\n", "\n", " \n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
**Figure 1**: Neural machine translation with attention
\n" ] }, { "cell_type": "markdown", "metadata": { "id": "b2TkQnykh3Mi" }, "source": [ "Here are some properties of the model that you may notice: \n", "\n", "#### Pre-attention and Post-attention LSTMs on both sides of the attention mechanism\n", "- There are two separate LSTMs in this model (see diagram on the left): pre-attention and post-attention LSTMs.\n", "- *Pre-attention* Bi-LSTM is the one at the bottom of the picture is a Bi-directional LSTM and comes *before* the attention mechanism.\n", " - The attention mechanism is shown in the middle of the left-hand diagram.\n", " - The pre-attention Bi-LSTM goes through $T_x$ time steps\n", "- *Post-attention* LSTM: at the top of the diagram comes *after* the attention mechanism. \n", " - The post-attention LSTM goes through $T_y$ time steps. \n", "\n", "- The post-attention LSTM passes the hidden state $s^{\\langle t \\rangle}$ and cell state $c^{\\langle t \\rangle}$ from one time step to the next. " ] }, { "cell_type": "markdown", "metadata": { "id": "JpznWuWqh3Mi" }, "source": [ "#### An LSTM has both a hidden state and cell state\n", "* In the lecture videos, we were using only a basic RNN for the post-attention sequence model\n", " * This means that the state captured by the RNN was outputting only the hidden state $s^{\\langle t\\rangle}$. \n", "* In this assignment, we are using an LSTM instead of a basic RNN.\n", " * So the LSTM has both the hidden state $s^{\\langle t\\rangle}$ and the cell state $c^{\\langle t\\rangle}$. " ] }, { "cell_type": "markdown", "metadata": { "id": "85btUzl4h3Mj" }, "source": [ "#### Each time step does not use predictions from the previous time step\n", "* Unlike previous text generation examples earlier in the course, in this model, the post-attention LSTM at time $t$ does not take the previous time step's prediction $y^{\\langle t-1 \\rangle}$ as input.\n", "* The post-attention LSTM at time 't' only takes the hidden state $s^{\\langle t\\rangle}$ and cell state $c^{\\langle t\\rangle}$ as input. \n", "* We have designed the model this way because unlike language generation (where adjacent characters are highly correlated) there isn't as strong a dependency between the previous character and the next character in a YYYY-MM-DD date." ] }, { "cell_type": "markdown", "metadata": { "id": "NYT3v7rUh3Mk" }, "source": [ "#### Concatenation of hidden states from the forward and backward pre-attention LSTMs\n", "- $\\overrightarrow{a}^{\\langle t \\rangle}$: hidden state of the forward-direction, pre-attention LSTM.\n", "- $\\overleftarrow{a}^{\\langle t \\rangle}$: hidden state of the backward-direction, pre-attention LSTM.\n", "- $a^{\\langle t \\rangle} = [\\overrightarrow{a}^{\\langle t \\rangle}, \\overleftarrow{a}^{\\langle t \\rangle}]$: the concatenation of the activations of both the forward-direction $\\overrightarrow{a}^{\\langle t \\rangle}$ and backward-directions $\\overleftarrow{a}^{\\langle t \\rangle}$ of the pre-attention Bi-LSTM. " ] }, { "cell_type": "markdown", "metadata": { "id": "97GUKCqwh3Mk" }, "source": [ "#### Computing \"energies\" $e^{\\langle t, t' \\rangle}$ as a function of $s^{\\langle t-1 \\rangle}$ and $a^{\\langle t' \\rangle}$\n", "- Recall in the lesson videos \"Attention Model\", at time 6:45 to 8:16, the definition of \"e\" as a function of $s^{\\langle t-1 \\rangle}$ and $a^{\\langle t \\rangle}$.\n", " - \"e\" is called the \"energies\" variable.\n", " - $s^{\\langle t-1 \\rangle}$ is the hidden state of the post-attention LSTM\n", " - $a^{\\langle t' \\rangle}$ is the hidden state of the pre-attention LSTM.\n", " - $s^{\\langle t-1 \\rangle}$ and $a^{\\langle t \\rangle}$ are fed into a simple neural network, which learns the function to output $e^{\\langle t, t' \\rangle}$.\n", " - $e^{\\langle t, t' \\rangle}$ is then used when computing the attention $a^{\\langle t, t' \\rangle}$ that $y^{\\langle t \\rangle}$ should pay to $a^{\\langle t' \\rangle}$." ] }, { "cell_type": "markdown", "metadata": { "id": "scu_HnPNh3Mk" }, "source": [ "- The diagram on the right of figure 1 uses a `RepeatVector` node to copy $s^{\\langle t-1 \\rangle}$'s value $T_x$ times.\n", "- Then it uses `Concatenation` to concatenate $s^{\\langle t-1 \\rangle}$ and $a^{\\langle t \\rangle}$.\n", "- The concatenation of $s^{\\langle t-1 \\rangle}$ and $a^{\\langle t \\rangle}$ is fed into a \"Dense\" layer, which computes $e^{\\langle t, t' \\rangle}$. \n", "- $e^{\\langle t, t' \\rangle}$ is then passed through a softmax to compute $\\alpha^{\\langle t, t' \\rangle}$.\n", "- Note that the diagram doesn't explicitly show variable $e^{\\langle t, t' \\rangle}$, but $e^{\\langle t, t' \\rangle}$ is above the Dense layer and below the Softmax layer in the diagram in the right half of figure 1.\n", "- We'll explain how to use `RepeatVector` and `Concatenation` in Keras below. " ] }, { "cell_type": "markdown", "metadata": { "id": "_ukmqe_Yh3Ml" }, "source": [ "#### Implementation Details\n", " \n", "Let's implement this neural translator. You will start by implementing two functions: `one_step_attention()` and `model()`.\n", "\n", "#### one_step_attention\n", "* The inputs to the one_step_attention at time step $t$ are:\n", " - $[a^{<1>},a^{<2>}, ..., a^{}]$: all hidden states of the pre-attention Bi-LSTM.\n", " - $s^{}$: the previous hidden state of the post-attention LSTM \n", "* one_step_attention computes:\n", " - $[\\alpha^{},\\alpha^{}, ..., \\alpha^{}]$: the attention weights\n", " - $context^{ \\langle t \\rangle }$: the context vector:\n", " \n", "$$context^{} = \\sum_{t' = 1}^{T_x} \\alpha^{}a^{}\\tag{1}$$ \n", "\n", "##### Clarifying 'context' and 'c'\n", "- In the lecture videos, the context was denoted $c^{\\langle t \\rangle}$\n", "- In the assignment, we are calling the context $context^{\\langle t \\rangle}$.\n", " - This is to avoid confusion with the post-attention LSTM's internal memory cell variable, which is also denoted $c^{\\langle t \\rangle}$." ] }, { "cell_type": "markdown", "metadata": { "id": "LIfLKkwoh3Ml" }, "source": [ "\n", "### Exercise 1 - one_step_attention \n", "\n", "Implement `one_step_attention()`. \n", "\n", "* The function `model()` will call the layers in `one_step_attention()` $T_y$ times using a for-loop.\n", "* It is important that all $T_y$ copies have the same weights. \n", " * It should not reinitialize the weights every time. \n", " * In other words, all $T_y$ steps should have shared weights. \n", "* Here's how you can implement layers with shareable weights in Keras:\n", " 1. Define the layer objects in a variable scope that is outside of the `one_step_attention` function. For example, defining the objects as global variables would work.\n", " - Note that defining these variables inside the scope of the function `model` would technically work, since `model` will then call the `one_step_attention` function. For the purposes of making grading and troubleshooting easier, we are defining these as global variables. Note that the automatic grader will expect these to be global variables as well.\n", " 2. Call these objects when propagating the input.\n", "* We have defined the layers you need as global variables. \n", " * Please run the following cells to create them. \n", " * Please note that the automatic grader expects these global variables with the given variable names. For grading purposes, please do not rename the global variables.\n", "* Please check the Keras documentation to learn more about these layers. The layers are functions. Below are examples of how to call these functions.\n", " * [RepeatVector()](https://www.tensorflow.org/api_docs/python/tf/keras/layers/RepeatVector)\n", "```Python\n", "var_repeated = repeat_layer(var1)\n", "```\n", " * [Concatenate()](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Concatenate) \n", "```Python\n", "concatenated_vars = concatenate_layer([var1,var2,var3])\n", "```\n", " * [Dense()](https://keras.io/layers/core/#dense) \n", "```Python\n", "var_out = dense_layer(var_in)\n", "```\n", " * [Activation()](https://keras.io/layers/core/#activation) \n", "```Python\n", "activation = activation_layer(var_in) \n", "```\n", " * [Dot()](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dot) \n", "```Python\n", "dot_product = dot_layer([var1,var2])\n", "```" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "executionInfo": { "elapsed": 16950, "status": "ok", "timestamp": 1612468514158, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "Cvop5Apyh3Mm" }, "outputs": [], "source": [ "# Defined shared layers as global variables\n", "repeator = RepeatVector(Tx)\n", "concatenator = Concatenate(axis=-1)\n", "densor1 = Dense(10, activation = \"tanh\")\n", "densor2 = Dense(1, activation = \"relu\")\n", "activator = Activation(softmax, name='attention_weights') # We are using a custom softmax(axis = 1) loaded in this notebook\n", "dotor = Dot(axes = 1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "executionInfo": { "elapsed": 16950, "status": "ok", "timestamp": 1612468514159, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "mZuMOnTDh3Mn" }, "outputs": [], "source": [ "# UNQ_C1 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)\n", "# GRADED FUNCTION: one_step_attention\n", "\n", "def one_step_attention(a, s_prev):\n", " \"\"\"\n", " Performs one step of attention: Outputs a context vector computed as a dot product of the attention weights\n", " \"alphas\" and the hidden states \"a\" of the Bi-LSTM.\n", " \n", " Arguments:\n", " a -- hidden state output of the Bi-LSTM, numpy-array of shape (m, Tx, 2*n_a)\n", " s_prev -- previous hidden state of the (post-attention) LSTM, numpy-array of shape (m, n_s)\n", " \n", " Returns:\n", " context -- context vector, input of the next (post-attention) LSTM cell\n", " \"\"\"\n", " \n", " ### START CODE HERE ###\n", " # Use repeator to repeat s_prev to be of shape (m, Tx, n_s) so that you can concatenate it with all hidden states \"a\" (≈ 1 line)\n", " s_prev = repeator(s_prev)\n", " # Use concatenator to concatenate a and s_prev on the last axis (≈ 1 line)\n", " # For grading purposes, please list 'a' first and 's_prev' second, in this order.\n", " concat = concatenator([a,s_prev])\n", " # Use densor1 to propagate concat through a small fully-connected neural network to compute the \"intermediate energies\" variable e. (≈1 lines)\n", " e = densor1(concat)\n", " # Use densor2 to propagate e through a small fully-connected neural network to compute the \"energies\" variable energies. (≈1 lines)\n", " energies = densor2(e)\n", " # Use \"activator\" on \"energies\" to compute the attention weights \"alphas\" (≈ 1 line)\n", " alphas = activator(energies)\n", " # Use dotor together with \"alphas\" and \"a\", in this order, to compute the context vector to be given to the next (post-attention) LSTM-cell (≈ 1 line)\n", " context = dotor([alphas,a])\n", " ### END CODE HERE ###\n", " \n", " return context" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[92mAll tests passed!\n" ] } ], "source": [ "# UNIT TEST\n", "def one_step_attention_test(target):\n", "\n", " m = 10\n", " Tx = 30\n", " n_a = 32\n", " n_s = 64\n", " #np.random.seed(10)\n", " a = np.random.uniform(1, 0, (m, Tx, 2 * n_a)).astype(np.float32)\n", " s_prev =np.random.uniform(1, 0, (m, n_s)).astype(np.float32) * 1\n", " context = target(a, s_prev)\n", " \n", " assert type(context) == tf.python.framework.ops.EagerTensor, \"Unexpected type. It should be a Tensor\"\n", " assert tuple(context.shape) == (m, 1, n_s), \"Unexpected output shape\"\n", " assert np.all(context.numpy() > 0), \"All output values must be > 0 in this example\"\n", " assert np.all(context.numpy() < 1), \"All output values must be < 1 in this example\"\n", "\n", " #assert np.allclose(context[0][0][0:5].numpy(), [0.50877404, 0.57160693, 0.45448175, 0.50074816, 0.53651875]), \"Unexpected values in the result\"\n", " print(\"\\033[92mAll tests passed!\")\n", " \n", "one_step_attention_test(one_step_attention)" ] }, { "cell_type": "markdown", "metadata": { "id": "vcmC3WcQh3Mn" }, "source": [ "\n", "### Exercise 2 - modelf\n", "\n", "Implement `modelf()` as explained in figure 1 and the instructions:\n", "\n", "* `modelf` first runs the input through a Bi-LSTM to get $[a^{<1>},a^{<2>}, ..., a^{}]$. \n", "* Then, `modelf` calls `one_step_attention()` $T_y$ times using a `for` loop. At each iteration of this loop:\n", " - It gives the computed context vector $context^{}$ to the post-attention LSTM.\n", " - It runs the output of the post-attention LSTM through a dense layer with softmax activation.\n", " - The softmax generates a prediction $\\hat{y}^{}$.\n", " \n", "Again, we have defined global layers that will share weights to be used in `modelf()`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "executionInfo": { "elapsed": 16949, "status": "ok", "timestamp": 1612468514159, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "5RHgmZrVh3Mo" }, "outputs": [], "source": [ "n_a = 32 # number of units for the pre-attention, bi-directional LSTM's hidden state 'a'\n", "n_s = 64 # number of units for the post-attention LSTM's hidden state \"s\"\n", "\n", "# Please note, this is the post attention LSTM cell. \n", "post_activation_LSTM_cell = LSTM(n_s, return_state = True) # Please do not modify this global variable.\n", "output_layer = Dense(len(machine_vocab), activation=softmax)" ] }, { "cell_type": "markdown", "metadata": { "id": "lGkKpb1Nh3Mo" }, "source": [ "Now you can use these layers $T_y$ times in a `for` loop to generate the outputs, and their parameters will not be reinitialized. You will have to carry out the following steps: \n", "\n", "1. Propagate the input `X` into a bi-directional LSTM.\n", " * [Bidirectional](https://keras.io/layers/wrappers/#bidirectional) \n", " * [LSTM](https://keras.io/layers/recurrent/#lstm)\n", " * Remember that we want the LSTM to return a full sequence instead of just the last hidden state. \n", " \n", "Sample code:\n", "\n", "```Python\n", "sequence_of_hidden_states = Bidirectional(LSTM(units=..., return_sequences=...))(the_input_X)\n", "```\n", " \n", "2. Iterate for $t = 0, \\cdots, T_y-1$: \n", " 1. Call `one_step_attention()`, passing in the sequence of hidden states $[a^{\\langle 1 \\rangle},a^{\\langle 2 \\rangle}, ..., a^{ \\langle T_x \\rangle}]$ from the pre-attention bi-directional LSTM, and the previous hidden state $s^{}$ from the post-attention LSTM to calculate the context vector $context^{}$.\n", " 2. Give $context^{}$ to the post-attention LSTM cell. \n", " - Remember to pass in the previous hidden-state $s^{\\langle t-1\\rangle}$ and cell-states $c^{\\langle t-1\\rangle}$ of this LSTM \n", " * This outputs the new hidden state $s^{}$ and the new cell state $c^{}$. \n", "\n", " Sample code:\n", " ```Python\n", " next_hidden_state, _ , next_cell_state = \n", " post_activation_LSTM_cell(inputs=..., initial_state=[prev_hidden_state, prev_cell_state])\n", " ``` \n", " Please note that the layer is actually the \"post attention LSTM cell\". For the purposes of passing the automatic grader, please do not modify the naming of this global variable. This will be fixed when we deploy updates to the automatic grader.\n", " 3. Apply a dense, softmax layer to $s^{}$, get the output. \n", " Sample code:\n", " ```Python\n", " output = output_layer(inputs=...)\n", " ```\n", " 4. Save the output by adding it to the list of outputs.\n", "\n", "3. Create your Keras model instance.\n", " * It should have three inputs:\n", " * `X`, the one-hot encoded inputs to the model, of shape ($T_{x}, humanVocabSize)$\n", " * $s^{\\langle 0 \\rangle}$, the initial hidden state of the post-attention LSTM\n", " * $c^{\\langle 0 \\rangle}$, the initial cell state of the post-attention LSTM\n", " * The output is the list of outputs. \n", " Sample code\n", " ```Python\n", " model = Model(inputs=[...,...,...], outputs=...)\n", " ```" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "executionInfo": { "elapsed": 16948, "status": "ok", "timestamp": 1612468514160, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "qeKbeDOvh3Mo" }, "outputs": [], "source": [ "# UNQ_C2 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)\n", "# GRADED FUNCTION: model\n", "\n", "def modelf(Tx, Ty, n_a, n_s, human_vocab_size, machine_vocab_size):\n", " \"\"\"\n", " Arguments:\n", " Tx -- length of the input sequence\n", " Ty -- length of the output sequence\n", " n_a -- hidden state size of the Bi-LSTM\n", " n_s -- hidden state size of the post-attention LSTM\n", " human_vocab_size -- size of the python dictionary \"human_vocab\"\n", " machine_vocab_size -- size of the python dictionary \"machine_vocab\"\n", "\n", " Returns:\n", " model -- Keras model instance\n", " \"\"\"\n", " \n", " # Define the inputs of your model with a shape (Tx,)\n", " # Define s0 (initial hidden state) and c0 (initial cell state)\n", " # for the decoder LSTM with shape (n_s,)\n", " X = Input(shape=(Tx, human_vocab_size))\n", " s0 = Input(shape=(n_s,), name='s0')\n", " c0 = Input(shape=(n_s,), name='c0')\n", " s = s0\n", " c = c0\n", " \n", " # Initialize empty list of outputs\n", " outputs = []\n", " \n", " ### START CODE HERE ###\n", " \n", " # Step 1: Define your pre-attention Bi-LSTM. (≈ 1 line)\n", " a = Bidirectional(LSTM(n_a, return_sequences=True))(X)\n", " \n", " # Step 2: Iterate for Ty steps\n", " for t in range(Ty):\n", " \n", " # Step 2.A: Perform one step of the attention mechanism to get back the context vector at step t (≈ 1 line)\n", " context = one_step_attention(a, s)\n", " \n", " # Step 2.B: Apply the post-attention LSTM cell to the \"context\" vector.\n", " # Don't forget to pass: initial_state = [hidden state, cell state] (≈ 1 line)\n", " s, _, c = post_activation_LSTM_cell(context,initial_state=[s, c])\n", " \n", " # Step 2.C: Apply Dense layer to the hidden state output of the post-attention LSTM (≈ 1 line)\n", " out = output_layer(s)\n", " \n", " # Step 2.D: Append \"out\" to the \"outputs\" list (≈ 1 line)\n", " outputs.append(out)\n", " \n", " # Step 3: Create model instance taking three inputs and returning the list of outputs. (≈ 1 line)\n", " model = Model(inputs=[X, s0, c0],outputs=outputs)\n", " \n", " ### END CODE HERE ###\n", " \n", " return model" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[['InputLayer', [(None, 30, 37)], 0], ['InputLayer', [(None, 64)], 0], ['Bidirectional', (None, 30, 64), 17920], ['RepeatVector', (None, 30, 64), 0, 30], ['Concatenate', (None, 30, 128), 0], ['Dense', (None, 30, 10), 1290, 'tanh'], ['Dense', (None, 30, 1), 11, 'relu'], ['Activation', (None, 30, 1), 0], ['Dot', (None, 1, 64), 0], ['InputLayer', [(None, 64)], 0], ['LSTM', [(None, 64), (None, 64), (None, 64)], 33024, [(None, 1, 64), (None, 64), (None, 64)], 'tanh'], ['Dense', (None, 11), 715, 'softmax']]\n", "\u001b[32mAll tests passed!\u001b[0m\n" ] } ], "source": [ "# UNIT TEST\n", "from test_utils import *\n", "\n", "def modelf_test(target):\n", " m = 10\n", " Tx = 30\n", " n_a = 32\n", " n_s = 64\n", " len_human_vocab = 37\n", " len_machine_vocab = 11\n", " \n", " \n", " model = target(Tx, Ty, n_a, n_s, len_human_vocab, len_machine_vocab)\n", " \n", " print(summary(model))\n", "\n", " \n", " expected_summary = [['InputLayer', [(None, 30, 37)], 0],\n", " ['InputLayer', [(None, 64)], 0],\n", " ['Bidirectional', (None, 30, 64), 17920],\n", " ['RepeatVector', (None, 30, 64), 0, 30],\n", " ['Concatenate', (None, 30, 128), 0],\n", " ['Dense', (None, 30, 10), 1290, 'tanh'],\n", " ['Dense', (None, 30, 1), 11, 'relu'],\n", " ['Activation', (None, 30, 1), 0],\n", " ['Dot', (None, 1, 64), 0],\n", " ['InputLayer', [(None, 64)], 0],\n", " ['LSTM',[(None, 64), (None, 64), (None, 64)], 33024,[(None, 1, 64), (None, 64), (None, 64)],'tanh'],\n", " ['Dense', (None, 11), 715, 'softmax']]\n", "\n", " comparator(summary(model), expected_summary)\n", " \n", "\n", "modelf_test(modelf)" ] }, { "cell_type": "markdown", "metadata": { "id": "--RX7hSsh3Mo" }, "source": [ "Run the following cell to create your model." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "executionInfo": { "elapsed": 20837, "status": "ok", "timestamp": 1612468518050, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "psdd-Ac6h3Mp" }, "outputs": [], "source": [ "model = modelf(Tx, Ty, n_a, n_s, len(human_vocab), len(machine_vocab))" ] }, { "cell_type": "markdown", "metadata": { "id": "nUJw7Xohh3Mp" }, "source": [ "#### Troubleshooting Note\n", "* If you are getting repeated errors after an initially incorrect implementation of \"model\", but believe that you have corrected the error, you may still see error messages when building your model. \n", "* A solution is to save and restart your kernel (or shutdown then restart your notebook), and re-run the cells." ] }, { "cell_type": "markdown", "metadata": { "id": "VgeU_I9_h3Mp" }, "source": [ "Let's get a summary of the model to check if it matches the expected output." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 20835, "status": "ok", "timestamp": 1612468518050, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "tX0vaYmPh3Mq", "outputId": "336b9248-70b0-4379-be95-95366874c02a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"functional_3\"\n", "__________________________________________________________________________________________________\n", "Layer (type) Output Shape Param # Connected to \n", "==================================================================================================\n", "input_2 (InputLayer) [(None, 30, 37)] 0 \n", "__________________________________________________________________________________________________\n", "s0 (InputLayer) [(None, 64)] 0 \n", "__________________________________________________________________________________________________\n", "bidirectional_1 (Bidirectional) (None, 30, 64) 17920 input_2[0][0] \n", "__________________________________________________________________________________________________\n", "repeat_vector (RepeatVector) (None, 30, 64) 0 s0[0][0] \n", " lstm[10][0] \n", " lstm[11][0] \n", " lstm[12][0] \n", " lstm[13][0] \n", " lstm[14][0] \n", " lstm[15][0] \n", " lstm[16][0] \n", " lstm[17][0] \n", " lstm[18][0] \n", "__________________________________________________________________________________________________\n", "concatenate (Concatenate) (None, 30, 128) 0 bidirectional_1[0][0] \n", " repeat_vector[10][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[11][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[12][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[13][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[14][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[15][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[16][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[17][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[18][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[19][0] \n", "__________________________________________________________________________________________________\n", "dense (Dense) (None, 30, 10) 1290 concatenate[10][0] \n", " concatenate[11][0] \n", " concatenate[12][0] \n", " concatenate[13][0] \n", " concatenate[14][0] \n", " concatenate[15][0] \n", " concatenate[16][0] \n", " concatenate[17][0] \n", " concatenate[18][0] \n", " concatenate[19][0] \n", "__________________________________________________________________________________________________\n", "dense_1 (Dense) (None, 30, 1) 11 dense[10][0] \n", " dense[11][0] \n", " dense[12][0] \n", " dense[13][0] \n", " dense[14][0] \n", " dense[15][0] \n", " dense[16][0] \n", " dense[17][0] \n", " dense[18][0] \n", " dense[19][0] \n", "__________________________________________________________________________________________________\n", "attention_weights (Activation) (None, 30, 1) 0 dense_1[10][0] \n", " dense_1[11][0] \n", " dense_1[12][0] \n", " dense_1[13][0] \n", " dense_1[14][0] \n", " dense_1[15][0] \n", " dense_1[16][0] \n", " dense_1[17][0] \n", " dense_1[18][0] \n", " dense_1[19][0] \n", "__________________________________________________________________________________________________\n", "dot (Dot) (None, 1, 64) 0 attention_weights[10][0] \n", " bidirectional_1[0][0] \n", " attention_weights[11][0] \n", " bidirectional_1[0][0] \n", " attention_weights[12][0] \n", " bidirectional_1[0][0] \n", " attention_weights[13][0] \n", " bidirectional_1[0][0] \n", " attention_weights[14][0] \n", " bidirectional_1[0][0] \n", " attention_weights[15][0] \n", " bidirectional_1[0][0] \n", " attention_weights[16][0] \n", " bidirectional_1[0][0] \n", " attention_weights[17][0] \n", " bidirectional_1[0][0] \n", " attention_weights[18][0] \n", " bidirectional_1[0][0] \n", " attention_weights[19][0] \n", " bidirectional_1[0][0] \n", "__________________________________________________________________________________________________\n", "c0 (InputLayer) [(None, 64)] 0 \n", "__________________________________________________________________________________________________\n", "lstm (LSTM) [(None, 64), (None, 33024 dot[10][0] \n", " s0[0][0] \n", " c0[0][0] \n", " dot[11][0] \n", " lstm[10][0] \n", " lstm[10][2] \n", " dot[12][0] \n", " lstm[11][0] \n", " lstm[11][2] \n", " dot[13][0] \n", " lstm[12][0] \n", " lstm[12][2] \n", " dot[14][0] \n", " lstm[13][0] \n", " lstm[13][2] \n", " dot[15][0] \n", " lstm[14][0] \n", " lstm[14][2] \n", " dot[16][0] \n", " lstm[15][0] \n", " lstm[15][2] \n", " dot[17][0] \n", " lstm[16][0] \n", " lstm[16][2] \n", " dot[18][0] \n", " lstm[17][0] \n", " lstm[17][2] \n", " dot[19][0] \n", " lstm[18][0] \n", " lstm[18][2] \n", "__________________________________________________________________________________________________\n", "dense_2 (Dense) (None, 11) 715 lstm[10][0] \n", " lstm[11][0] \n", " lstm[12][0] \n", " lstm[13][0] \n", " lstm[14][0] \n", " lstm[15][0] \n", " lstm[16][0] \n", " lstm[17][0] \n", " lstm[18][0] \n", " lstm[19][0] \n", "==================================================================================================\n", "Total params: 52,960\n", "Trainable params: 52,960\n", "Non-trainable params: 0\n", "__________________________________________________________________________________________________\n" ] } ], "source": [ "model.summary()" ] }, { "cell_type": "markdown", "metadata": { "id": "uiqCePt5h3Mr" }, "source": [ "**Expected Output**:\n", "\n", "Here is the summary you should see\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " **Total params:**\n", " \n", " 52,960\n", "
\n", " **Trainable params:**\n", " \n", " 52,960\n", "
\n", " **Non-trainable params:**\n", " \n", " 0\n", "
\n", " **bidirectional_1's output shape **\n", " \n", " (None, 30, 64) \n", "
\n", " **repeat_vector_1's output shape **\n", " \n", " (None, 30, 64) \n", "
\n", " **concatenate_1's output shape **\n", " \n", " (None, 30, 128) \n", "
\n", " **attention_weights's output shape **\n", " \n", " (None, 30, 1) \n", "
\n", " **dot_1's output shape **\n", " \n", " (None, 1, 64)\n", "
\n", " **dense_3's output shape **\n", " \n", " (None, 11) \n", "
\n" ] }, { "cell_type": "markdown", "metadata": { "id": "8u3D9Odhh3Ms" }, "source": [ "\n", "### Exercise 3 - Compile the Model\n", "\n", "* After creating your model in Keras, you need to compile it and define the loss function, optimizer and metrics you want to use. \n", " * Loss function: 'categorical_crossentropy'.\n", " * Optimizer: [Adam](https://keras.io/optimizers/#adam) [optimizer](https://keras.io/optimizers/#usage-of-optimizers)\n", " - learning rate = 0.005 \n", " - $\\beta_1 = 0.9$\n", " - $\\beta_2 = 0.999$\n", " - decay = 0.01 \n", " * metric: 'accuracy'\n", " \n", "Sample code\n", "```Python\n", "optimizer = Adam(lr=..., beta_1=..., beta_2=..., decay=...)\n", "model.compile(optimizer=..., loss=..., metrics=[...])\n", "```" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "executionInfo": { "elapsed": 20835, "status": "ok", "timestamp": 1612468518051, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "sBFRJ49rh3Ms" }, "outputs": [], "source": [ "### START CODE HERE ### (≈2 lines)\n", "opt = Adam(lr=0.005, beta_1=0.9, beta_2=0.999, decay=0.01)\n", "model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])\n", "### END CODE HERE ###" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[92mAll tests passed!\n" ] } ], "source": [ "# UNIT TESTS\n", "assert opt.lr == 0.005, \"Set the lr parameter to 0.005\"\n", "assert opt.beta_1 == 0.9, \"Set the beta_1 parameter to 0.9\"\n", "assert opt.beta_2 == 0.999, \"Set the beta_2 parameter to 0.999\"\n", "assert opt.decay == 0.01, \"Set the decay parameter to 0.01\"\n", "assert model.loss == \"categorical_crossentropy\", \"Wrong loss. Use 'categorical_crossentropy'\"\n", "assert model.optimizer == opt, \"Use the optimizer that you have instantiated\"\n", "assert model.compiled_metrics._user_metrics[0] == 'accuracy', \"set metrics to ['accuracy']\"\n", "\n", "print(\"\\033[92mAll tests passed!\")" ] }, { "cell_type": "markdown", "metadata": { "id": "Qz71nM3oh3Ms" }, "source": [ "#### Define inputs and outputs, and fit the model\n", "The last step is to define all your inputs and outputs to fit the model:\n", "- You have input X of shape $(m = 10000, T_x = 30)$ containing the training examples.\n", "- You need to create `s0` and `c0` to initialize your `post_attention_LSTM_cell` with zeros.\n", "- Given the `model()` you coded, you need the \"outputs\" to be a list of 10 elements of shape (m, T_y). \n", " - The list `outputs[i][0], ..., outputs[i][Ty]` represents the true labels (characters) corresponding to the $i^{th}$ training example (`X[i]`). \n", " - `outputs[i][j]` is the true label of the $j^{th}$ character in the $i^{th}$ training example." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "executionInfo": { "elapsed": 20833, "status": "ok", "timestamp": 1612468518051, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "USFiNKYhh3Mt" }, "outputs": [], "source": [ "s0 = np.zeros((m, n_s))\n", "c0 = np.zeros((m, n_s))\n", "outputs = list(Yoh.swapaxes(0,1))" ] }, { "cell_type": "markdown", "metadata": { "id": "FVkITGi3h3Mt" }, "source": [ "Let's now fit the model and run it for one epoch." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 47944, "status": "ok", "timestamp": 1612468545172, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "tPuwY45bh3Mt", "outputId": "ec9dfc4c-1dcb-4577-d872-474f79c60d5f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100/100 [==============================] - 11s 110ms/step - loss: 16.6132 - dense_2_loss: 1.1322 - dense_2_1_loss: 0.9705 - dense_2_2_loss: 1.7746 - dense_2_3_loss: 2.6731 - dense_2_4_loss: 0.8185 - dense_2_5_loss: 1.3019 - dense_2_6_loss: 2.7432 - dense_2_7_loss: 0.9294 - dense_2_8_loss: 1.7146 - dense_2_9_loss: 2.5552 - dense_2_accuracy: 0.5503 - dense_2_1_accuracy: 0.6973 - dense_2_2_accuracy: 0.2949 - dense_2_3_accuracy: 0.0819 - dense_2_4_accuracy: 0.9235 - dense_2_5_accuracy: 0.3400 - dense_2_6_accuracy: 0.0508 - dense_2_7_accuracy: 0.9261 - dense_2_8_accuracy: 0.2437 - dense_2_9_accuracy: 0.1016\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit([Xoh, s0, c0], outputs, epochs=1, batch_size=100)" ] }, { "cell_type": "markdown", "metadata": { "id": "SUikskCoh3Mt" }, "source": [ "While training you can see the loss as well as the accuracy on each of the 10 positions of the output. The table below gives you an example of what the accuracies could be if the batch had 2 examples: \n", "\n", "
\n", "
Thus, `dense_2_acc_8: 0.89` means that you are predicting the 7th character of the output correctly 89% of the time in the current batch of data.
\n", "\n", "\n", "We have run this model for longer, and saved the weights. Run the next cell to load our weights. (By training a model for several minutes, you should be able to obtain a model of similar accuracy, but loading our model will save you time.) " ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "executionInfo": { "elapsed": 47942, "status": "ok", "timestamp": 1612468545173, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "ooiZCOx0h3Mu" }, "outputs": [], "source": [ "model.load_weights('models/model.h5')" ] }, { "cell_type": "markdown", "metadata": { "id": "yUUD9yXxh3Mu" }, "source": [ "You can now see the results on new examples." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 53835, "status": "ok", "timestamp": 1612468551077, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "rQ8sd_cuh3Mv", "outputId": "c37e92ac-5c60-4caf-b843-6aaeaa37be25" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "source: 3 May 1979\n", "output: 1979-05-33 \n", "\n", "source: 5 April 09\n", "output: 2009-04-05 \n", "\n", "source: 21th of August 2016\n", "output: 2016-08-20 \n", "\n", "source: Tue 10 Jul 2007\n", "output: 2007-07-10 \n", "\n", "source: Saturday May 9 2018\n", "output: 2018-05-09 \n", "\n", "source: March 3 2001\n", "output: 2001-03-03 \n", "\n", "source: March 3rd 2001\n", "output: 2001-03-03 \n", "\n", "source: 1 March 2001\n", "output: 2001-03-01 \n", "\n" ] } ], "source": [ "EXAMPLES = ['3 May 1979', '5 April 09', '21th of August 2016', 'Tue 10 Jul 2007', 'Saturday May 9 2018', 'March 3 2001', 'March 3rd 2001', '1 March 2001']\n", "s00 = np.zeros((1, n_s))\n", "c00 = np.zeros((1, n_s))\n", "for example in EXAMPLES:\n", " source = string_to_int(example, Tx, human_vocab)\n", " #print(source)\n", " source = np.array(list(map(lambda x: to_categorical(x, num_classes=len(human_vocab)), source))).swapaxes(0,1)\n", " source = np.swapaxes(source, 0, 1)\n", " source = np.expand_dims(source, axis=0)\n", " prediction = model.predict([source, s00, c00])\n", " prediction = np.argmax(prediction, axis = -1)\n", " output = [inv_machine_vocab[int(i)] for i in prediction]\n", " print(\"source:\", example)\n", " print(\"output:\", ''.join(output),\"\\n\")" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "source: 4th of july 2001\n", "output: 2001-07-04 \n", "\n" ] } ], "source": [ "def translate_date(sentence):\n", " source = string_to_int(sentence, Tx, human_vocab)\n", " source = np.array(list(map(lambda x: to_categorical(x, num_classes=len(human_vocab)), source))).swapaxes(0,1)\n", " source = np.swapaxes(source, 0, 1)\n", " source = np.expand_dims(source, axis=0)\n", " prediction = model.predict([source, s00, c00])\n", " prediction = np.argmax(prediction, axis = -1)\n", " output = [inv_machine_vocab[int(i)] for i in prediction]\n", " print(\"source:\", sentence)\n", " print(\"output:\", ''.join(output),\"\\n\")\n", "example = \"4th of july 2001\"\n", "translate_date(example)" ] }, { "cell_type": "markdown", "metadata": { "id": "vjdEQiIDh3Mv" }, "source": [ "You can also change these examples to test with your own examples. The next part will give you a better sense of what the attention mechanism is doing--i.e., what part of the input the network is paying attention to when generating a particular output character. " ] }, { "cell_type": "markdown", "metadata": { "id": "1XIxtN4xh3Mv" }, "source": [ "\n", "## 3 - Visualizing Attention (Optional / Ungraded)\n", "\n", "Since the problem has a fixed output length of 10, it is also possible to carry out this task using 10 different softmax units to generate the 10 characters of the output. But one advantage of the attention model is that each part of the output (such as the month) knows it needs to depend only on a small part of the input (the characters in the input giving the month). We can visualize what each part of the output is looking at which part of the input.\n", "\n", "Consider the task of translating \"Saturday 9 May 2018\" to \"2018-05-09\". If we visualize the computed $\\alpha^{\\langle t, t' \\rangle}$ we get this: \n", "\n", "
\n", "
**Figure 8**: Full Attention Map
\n", "\n", "Notice how the output ignores the \"Saturday\" portion of the input. None of the output timesteps are paying much attention to that portion of the input. We also see that 9 has been translated as 09 and May has been correctly translated into 05, with the output paying attention to the parts of the input it needs to to make the translation. The year mostly requires it to pay attention to the input's \"18\" in order to generate \"2018.\" " ] }, { "cell_type": "markdown", "metadata": { "id": "FrP893IFh3Mv" }, "source": [ "\n", "### 3.1 - Getting the Attention Weights From the Network\n", "\n", "Lets now visualize the attention values in your network. We'll propagate an example through the network, then visualize the values of $\\alpha^{\\langle t, t' \\rangle}$. \n", "\n", "To figure out where the attention values are located, let's start by printing a summary of the model ." ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 53826, "status": "ok", "timestamp": 1612468551078, "user": { "displayName": "Mubsi K", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gip7OjOkdNkKxKDyWEQAq1o8ccGN_HrBTGdqjgQ=s64", "userId": "08094225471505108399" }, "user_tz": -300 }, "id": "RfiLrfKIh3Mv", "outputId": "b6690603-209c-40d7-f352-235a689d1aea" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"functional_3\"\n", "__________________________________________________________________________________________________\n", "Layer (type) Output Shape Param # Connected to \n", "==================================================================================================\n", "input_2 (InputLayer) [(None, 30, 37)] 0 \n", "__________________________________________________________________________________________________\n", "s0 (InputLayer) [(None, 64)] 0 \n", "__________________________________________________________________________________________________\n", "bidirectional_1 (Bidirectional) (None, 30, 64) 17920 input_2[0][0] \n", "__________________________________________________________________________________________________\n", "repeat_vector (RepeatVector) (None, 30, 64) 0 s0[0][0] \n", " lstm[10][0] \n", " lstm[11][0] \n", " lstm[12][0] \n", " lstm[13][0] \n", " lstm[14][0] \n", " lstm[15][0] \n", " lstm[16][0] \n", " lstm[17][0] \n", " lstm[18][0] \n", "__________________________________________________________________________________________________\n", "concatenate (Concatenate) (None, 30, 128) 0 bidirectional_1[0][0] \n", " repeat_vector[10][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[11][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[12][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[13][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[14][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[15][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[16][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[17][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[18][0] \n", " bidirectional_1[0][0] \n", " repeat_vector[19][0] \n", "__________________________________________________________________________________________________\n", "dense (Dense) (None, 30, 10) 1290 concatenate[10][0] \n", " concatenate[11][0] \n", " concatenate[12][0] \n", " concatenate[13][0] \n", " concatenate[14][0] \n", " concatenate[15][0] \n", " concatenate[16][0] \n", " concatenate[17][0] \n", " concatenate[18][0] \n", " concatenate[19][0] \n", "__________________________________________________________________________________________________\n", "dense_1 (Dense) (None, 30, 1) 11 dense[10][0] \n", " dense[11][0] \n", " dense[12][0] \n", " dense[13][0] \n", " dense[14][0] \n", " dense[15][0] \n", " dense[16][0] \n", " dense[17][0] \n", " dense[18][0] \n", " dense[19][0] \n", "__________________________________________________________________________________________________\n", "attention_weights (Activation) (None, 30, 1) 0 dense_1[10][0] \n", " dense_1[11][0] \n", " dense_1[12][0] \n", " dense_1[13][0] \n", " dense_1[14][0] \n", " dense_1[15][0] \n", " dense_1[16][0] \n", " dense_1[17][0] \n", " dense_1[18][0] \n", " dense_1[19][0] \n", "__________________________________________________________________________________________________\n", "dot (Dot) (None, 1, 64) 0 attention_weights[10][0] \n", " bidirectional_1[0][0] \n", " attention_weights[11][0] \n", " bidirectional_1[0][0] \n", " attention_weights[12][0] \n", " bidirectional_1[0][0] \n", " attention_weights[13][0] \n", " bidirectional_1[0][0] \n", " attention_weights[14][0] \n", " bidirectional_1[0][0] \n", " attention_weights[15][0] \n", " bidirectional_1[0][0] \n", " attention_weights[16][0] \n", " bidirectional_1[0][0] \n", " attention_weights[17][0] \n", " bidirectional_1[0][0] \n", " attention_weights[18][0] \n", " bidirectional_1[0][0] \n", " attention_weights[19][0] \n", " bidirectional_1[0][0] \n", "__________________________________________________________________________________________________\n", "c0 (InputLayer) [(None, 64)] 0 \n", "__________________________________________________________________________________________________\n", "lstm (LSTM) [(None, 64), (None, 33024 dot[10][0] \n", " s0[0][0] \n", " c0[0][0] \n", " dot[11][0] \n", " lstm[10][0] \n", " lstm[10][2] \n", " dot[12][0] \n", " lstm[11][0] \n", " lstm[11][2] \n", " dot[13][0] \n", " lstm[12][0] \n", " lstm[12][2] \n", " dot[14][0] \n", " lstm[13][0] \n", " lstm[13][2] \n", " dot[15][0] \n", " lstm[14][0] \n", " lstm[14][2] \n", " dot[16][0] \n", " lstm[15][0] \n", " lstm[15][2] \n", " dot[17][0] \n", " lstm[16][0] \n", " lstm[16][2] \n", " dot[18][0] \n", " lstm[17][0] \n", " lstm[17][2] \n", " dot[19][0] \n", " lstm[18][0] \n", " lstm[18][2] \n", "__________________________________________________________________________________________________\n", "dense_2 (Dense) (None, 11) 715 lstm[10][0] \n", " lstm[11][0] \n", " lstm[12][0] \n", " lstm[13][0] \n", " lstm[14][0] \n", " lstm[15][0] \n", " lstm[16][0] \n", " lstm[17][0] \n", " lstm[18][0] \n", " lstm[19][0] \n", "==================================================================================================\n", "Total params: 52,960\n", "Trainable params: 52,960\n", "Non-trainable params: 0\n", "__________________________________________________________________________________________________\n" ] } ], "source": [ "model.summary()" ] }, { "cell_type": "markdown", "metadata": { "id": "zbcprBCPh3Mv" }, "source": [ "Navigate through the output of `model.summary()` above. You can see that the layer named `attention_weights` outputs the `alphas` of shape (m, 30, 1) before `dot_2` computes the context vector for every time step $t = 0, \\ldots, T_y-1$. Let's get the attention weights from this layer.\n", "\n", "The function `attention_map()` pulls out the attention values from your model and plots them.\n", "\n", "**Note**: We are aware that you might run into an error running the cell below despite a valid implementation for Exercise 2 - `modelf` above. If you get the error kindly report it on this [Topic](https://discourse.deeplearning.ai/t/error-in-optional-ungraded-part-of-neural-machine-translation-w3a1/1096) on [Discourse](https://discourse.deeplearning.ai) as it'll help us improve our content. \n", "\n", "If you haven’t joined our Discourse community you can do so by clicking on the link: http://bit.ly/dls-discourse\n", "\n", "And don’t worry about the error, it will not affect the grading for this assignment." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAegAAAGpCAYAAABGVKXFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nO3deZwcZbX/8c+ZLTOZkIQkEEKABJAtYU2AsIssiiiboogIIpuKoijxqsQrXO8N6nVX8KeAyEUQxB0QRGQ1kEBYAgl7gIBJIJCEJEyS2c/vj6oJzaSfqpru9Exl5vt+vQLT/fRT9XRVd5+u6jrPMXdHRERE8qWqrwcgIiIi61OAFhERySEFaBERkRxSgBYREckhBWgREZEcUoAWERHJoZq+HkChUaNG+bhx44u2rV69msbGxpKWO5D6bmzjzdK3ua0z2NbWsobaQYOD7fW14e+glRxzUvLimtVNDG4c0uN1ltovS9+WMrbx/CVvBds2a3DeWGvhZb+1Mti2xcjBvLZsTbB9yKgRwbbhNR2saK8Oto9orAu2pakKP51URhmdy1HWmHtfOessaxv3wZNdsujfrHxzWdE15ypAjxs3nvsffLho28wZ97D/QYeWtNyB1HdjG2+Wvs8uDgeAxc/MZsud9wm277TlJiWvN0la37b2cMCbPfM+9tn/kGC7BT4kHnrgPvY9INwPoCrQedb997Lfge8O9nv+taZgW9o2Pv5H9wXbvjy5nR8+Ev6Yee2e24JtU8+ZwrTLHwy27/2pjwfbPrbFm9zw2qbB9o9PGRtsS9NYGw78aWqqSj9pWVbQCr2oKrjecr7IVFvp26m2jG1c3QfnlD/3kSODbTrFLSIikkMK0CIiIjlUsQBtZleZ2etmNq9S6xAREemvKnkEfTVwVAWXLyIi0m9VLEC7+33A8kotX0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FzY3He3p82hKKb5+/Ex0tPUE01/KsgmWtBiaa2SNEZzG+lWUA8ZeY++OLlr4X37eEqNjErwPd/kw07/XjwF3Af7j7a+7+b6LrBp4gCgiPFfS5HLit6yKxhPF+nyggPkD0G3SXu4mC/3oXiZG8HUNuAL4SX1S2fcLjrgSeAh61KAXwl2Q7Y3gSMC8+Nb4z0RfXR4m+/DxE9Fq+0t0fK9I369gKFX1tmNm7iX5++K67Xwe0mlnSPNTvIXotSz+iubgls/gU9d7u/vm+HsuGYmZjiD6Ej+zrsZQrPiU6l+gCv5UVWkeTuw+pxLKlNGY2iOh6goOKXHcgGzEdQcuA5lHpuissnqhkY2XRpCbPAD+rVHCW3NoG+JqCc/+jI2gREZEc0hG0iIhIDilAi4iI5JACtIiISA4pQIuIiOSQArSIiEgOKUCLiIjk0P8Hhl/jp/oLUD8AAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "attention_map = plot_attention_map(model, human_vocab, inv_machine_vocab, \"Tuesday 09 Oct 1993\", num = 7, n_s = 64);" ] }, { "cell_type": "markdown", "metadata": { "id": "pQ3qbIjqh3Mx" }, "source": [ "On the generated plot you can observe the values of the attention weights for each character of the predicted output. Examine this plot and check that the places where the network is paying attention makes sense to you.\n", "\n", "In the date translation application, you will observe that most of the time attention helps predict the year, and doesn't have much impact on predicting the day or month." ] }, { "cell_type": "markdown", "metadata": { "id": "IkpGu1Jkh3Mx" }, "source": [ "### Congratulations!\n", "\n", "\n", "You have come to the end of this assignment \n", "\n", "#### Here's what you should remember\n", "\n", "- Machine translation models can be used to map from one sequence to another. They are useful not just for translating human languages (like French->English) but also for tasks like date format translation. \n", "- An attention mechanism allows a network to focus on the most relevant parts of the input when producing a specific part of the output. \n", "- A network using an attention mechanism can translate from inputs of length $T_x$ to outputs of length $T_y$, where $T_x$ and $T_y$ can be different. \n", "- You can visualize attention weights $\\alpha^{\\langle t,t' \\rangle}$ to see what the network is paying attention to while generating each output." ] }, { "cell_type": "markdown", "metadata": { "id": "ZaKA2u4uh3My" }, "source": [ "Congratulations on finishing this assignment! You are now able to implement an attention model and use it to learn complex mappings from one sequence to another. " ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "Solution_Neural_machine_translation_with_attention_v4a.ipynb", "provenance": [] }, "coursera": { "schema_names": [ "DLSC5W3-1A" ] }, "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.7.6" } }, "nbformat": 4, "nbformat_minor": 1 }