{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# For tips on running notebooks in Google Colab, see\n",
    "# https://docs.pytorch.org/tutorials/beginner/colab\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chatbot Tutorial\n",
    "\n",
    "**Author:** [Matthew Inkawhich](https://github.com/MatthewInkawhich)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this tutorial, we explore a fun and interesting use-case of recurrent\n",
    "sequence-to-sequence models. We will train a simple chatbot using movie\n",
    "scripts from the [Cornell Movie-Dialogs\n",
    "Corpus](https://www.cs.cornell.edu/~cristian/Cornell_Movie-Dialogs_Corpus.html).\n",
    "\n",
    "Conversational models are a hot topic in artificial intelligence\n",
    "research. Chatbots can be found in a variety of settings, including\n",
    "customer service applications and online helpdesks. These bots are often\n",
    "powered by retrieval-based models, which output predefined responses to\n",
    "questions of certain forms. In a highly restricted domain like a\n",
    "company's IT helpdesk, these models may be sufficient, however, they are\n",
    "not robust enough for more general use-cases. Teaching a machine to\n",
    "carry out a meaningful conversation with a human in multiple domains is\n",
    "a research question that is far from solved. Recently, the deep learning\n",
    "boom has allowed for powerful generative models like Google's [Neural\n",
    "Conversational Model](https://arxiv.org/abs/1506.05869), which marks a\n",
    "large step towards multi-domain generative conversational models. In\n",
    "this tutorial, we will implement this kind of model in PyTorch.\n",
    "\n",
    "![/\\_static/img/chatbot/bot.png](https://pytorch.org/tutorials/_static/img/chatbot/bot.png){.align-center}\n",
    "\n",
    "``` python\n",
    "> hello?\n",
    "Bot: hello .\n",
    "> where am I?\n",
    "Bot: you re in a hospital .\n",
    "> who are you?\n",
    "Bot: i m a lawyer .\n",
    "> how are you doing?\n",
    "Bot: i m fine .\n",
    "> are you my friend?\n",
    "Bot: no .\n",
    "> you're under arrest\n",
    "Bot: i m trying to help you !\n",
    "> i'm just kidding\n",
    "Bot: i m sorry .\n",
    "> where are you from?\n",
    "Bot: san francisco .\n",
    "> it's time for me to leave\n",
    "Bot: i know .\n",
    "> goodbye\n",
    "Bot: goodbye .\n",
    "```\n",
    "\n",
    "**Tutorial Highlights**\n",
    "\n",
    "-   Handle loading and preprocessing of [Cornell Movie-Dialogs\n",
    "    Corpus](https://www.cs.cornell.edu/~cristian/Cornell_Movie-Dialogs_Corpus.html)\n",
    "    dataset\n",
    "-   Implement a sequence-to-sequence model with [Luong attention\n",
    "    mechanism(s)](https://arxiv.org/abs/1508.04025)\n",
    "-   Jointly train encoder and decoder models using mini-batches\n",
    "-   Implement greedy-search decoding module\n",
    "-   Interact with trained chatbot\n",
    "\n",
    "**Acknowledgments**\n",
    "\n",
    "This tutorial borrows code from the following sources:\n",
    "\n",
    "1)  Yuan-Kuei Wu's pytorch-chatbot implementation:\n",
    "    <https://github.com/ywk991112/pytorch-chatbot>\n",
    "2)  Sean Robertson's practical-pytorch seq2seq-translation example:\n",
    "    <https://github.com/spro/practical-pytorch/tree/master/seq2seq-translation>\n",
    "3)  FloydHub Cornell Movie Corpus preprocessing code:\n",
    "    <https://github.com/floydhub/textutil-preprocess-cornell-movie-corpus>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Preparations\n",
    "\n",
    "To get started,\n",
    "[download](https://zissou.infosci.cornell.edu/convokit/datasets/movie-corpus/movie-corpus.zip)\n",
    "the Movie-Dialogs Corpus zip file.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# and put in a ``data/`` directory under the current directory.\n",
    "#\n",
    "# After that, let’s import some necessities.\n",
    "#\n",
    "\n",
    "import torch\n",
    "from torch.jit import script, trace\n",
    "import torch.nn as nn\n",
    "from torch import optim\n",
    "import torch.nn.functional as F\n",
    "import csv\n",
    "import random\n",
    "import re\n",
    "import os\n",
    "import unicodedata\n",
    "import codecs\n",
    "from io import open\n",
    "import itertools\n",
    "import math\n",
    "import json\n",
    "\n",
    "\n",
    "# If the current `accelerator <https://pytorch.org/docs/stable/torch.html#accelerators>`__ is available,\n",
    "# we will use it. Otherwise, we use the CPU.\n",
    "device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else \"cpu\"\n",
    "print(f\"Using {device} device\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load & Preprocess Data\n",
    "\n",
    "The next step is to reformat our data file and load the data into\n",
    "structures that we can work with.\n",
    "\n",
    "The [Cornell Movie-Dialogs\n",
    "Corpus](https://www.cs.cornell.edu/~cristian/Cornell_Movie-Dialogs_Corpus.html)\n",
    "is a rich dataset of movie character dialog:\n",
    "\n",
    "-   220,579 conversational exchanges between 10,292 pairs of movie\n",
    "    characters\n",
    "-   9,035 characters from 617 movies\n",
    "-   304,713 total utterances\n",
    "\n",
    "This dataset is large and diverse, and there is a great variation of\n",
    "language formality, time periods, sentiment, etc. Our hope is that this\n",
    "diversity makes our model robust to many forms of inputs and queries.\n",
    "\n",
    "First, we'll take a look at some lines of our datafile to see the\n",
    "original format.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "corpus_name = \"movie-corpus\"\n",
    "corpus = os.path.join(\"data\", corpus_name)\n",
    "\n",
    "def printLines(file, n=10):\n",
    "    with open(file, 'rb') as datafile:\n",
    "        lines = datafile.readlines()\n",
    "    for line in lines[:n]:\n",
    "        print(line)\n",
    "\n",
    "printLines(os.path.join(corpus, \"utterances.jsonl\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create formatted data file\n",
    "\n",
    "For convenience, we\\'ll create a nicely formatted data file in which\n",
    "each line contains a tab-separated *query sentence* and a *response\n",
    "sentence* pair.\n",
    "\n",
    "The following functions facilitate the parsing of the raw\n",
    "`utterances.jsonl` data file.\n",
    "\n",
    "-   `loadLinesAndConversations` splits each line of the file into a\n",
    "    dictionary of lines with fields: `lineID`, `characterID`, and text\n",
    "    and then groups them into conversations with fields:\n",
    "    `conversationID`, `movieID`, and lines.\n",
    "-   `extractSentencePairs` extracts pairs of sentences from\n",
    "    conversations\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Splits each line of the file to create lines and conversations\n",
    "def loadLinesAndConversations(fileName):\n",
    "    lines = {}\n",
    "    conversations = {}\n",
    "    with open(fileName, 'r', encoding='iso-8859-1') as f:\n",
    "        for line in f:\n",
    "            lineJson = json.loads(line)\n",
    "            # Extract fields for line object\n",
    "            lineObj = {}\n",
    "            lineObj[\"lineID\"] = lineJson[\"id\"]\n",
    "            lineObj[\"characterID\"] = lineJson[\"speaker\"]\n",
    "            lineObj[\"text\"] = lineJson[\"text\"]\n",
    "            lines[lineObj['lineID']] = lineObj\n",
    "\n",
    "            # Extract fields for conversation object\n",
    "            if lineJson[\"conversation_id\"] not in conversations:\n",
    "                convObj = {}\n",
    "                convObj[\"conversationID\"] = lineJson[\"conversation_id\"]\n",
    "                convObj[\"movieID\"] = lineJson[\"meta\"][\"movie_id\"]\n",
    "                convObj[\"lines\"] = [lineObj]\n",
    "            else:\n",
    "                convObj = conversations[lineJson[\"conversation_id\"]]\n",
    "                convObj[\"lines\"].insert(0, lineObj)\n",
    "            conversations[convObj[\"conversationID\"]] = convObj\n",
    "\n",
    "    return lines, conversations\n",
    "\n",
    "\n",
    "# Extracts pairs of sentences from conversations\n",
    "def extractSentencePairs(conversations):\n",
    "    qa_pairs = []\n",
    "    for conversation in conversations.values():\n",
    "        # Iterate over all the lines of the conversation\n",
    "        for i in range(len(conversation[\"lines\"]) - 1):  # We ignore the last line (no answer for it)\n",
    "            inputLine = conversation[\"lines\"][i][\"text\"].strip()\n",
    "            targetLine = conversation[\"lines\"][i+1][\"text\"].strip()\n",
    "            # Filter wrong samples (if one of the lists is empty)\n",
    "            if inputLine and targetLine:\n",
    "                qa_pairs.append([inputLine, targetLine])\n",
    "    return qa_pairs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we'll call these functions and create the file. We'll call it\n",
    "`formatted_movie_lines.txt`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Define path to new file\n",
    "datafile = os.path.join(corpus, \"formatted_movie_lines.txt\")\n",
    "\n",
    "delimiter = '\\t'\n",
    "# Unescape the delimiter\n",
    "delimiter = str(codecs.decode(delimiter, \"unicode_escape\"))\n",
    "\n",
    "# Initialize lines dict and conversations dict\n",
    "lines = {}\n",
    "conversations = {}\n",
    "# Load lines and conversations\n",
    "print(\"\\nProcessing corpus into lines and conversations...\")\n",
    "lines, conversations = loadLinesAndConversations(os.path.join(corpus, \"utterances.jsonl\"))\n",
    "\n",
    "# Write new csv file\n",
    "print(\"\\nWriting newly formatted file...\")\n",
    "with open(datafile, 'w', encoding='utf-8') as outputfile:\n",
    "    writer = csv.writer(outputfile, delimiter=delimiter, lineterminator='\\n')\n",
    "    for pair in extractSentencePairs(conversations):\n",
    "        writer.writerow(pair)\n",
    "\n",
    "# Print a sample of lines\n",
    "print(\"\\nSample lines from file:\")\n",
    "printLines(datafile)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load and trim data\n",
    "\n",
    "Our next order of business is to create a vocabulary and load\n",
    "query/response sentence pairs into memory.\n",
    "\n",
    "Note that we are dealing with sequences of **words**, which do not have\n",
    "an implicit mapping to a discrete numerical space. Thus, we must create\n",
    "one by mapping each unique word that we encounter in our dataset to an\n",
    "index value.\n",
    "\n",
    "For this we define a `Voc` class, which keeps a mapping from words to\n",
    "indexes, a reverse mapping of indexes to words, a count of each word and\n",
    "a total word count. The class provides methods for adding a word to the\n",
    "vocabulary (`addWord`), adding all words in a sentence (`addSentence`)\n",
    "and trimming infrequently seen words (`trim`). More on trimming later.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Default word tokens\n",
    "PAD_token = 0  # Used for padding short sentences\n",
    "SOS_token = 1  # Start-of-sentence token\n",
    "EOS_token = 2  # End-of-sentence token\n",
    "\n",
    "class Voc:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        self.trimmed = False\n",
    "        self.word2index = {}\n",
    "        self.word2count = {}\n",
    "        self.index2word = {PAD_token: \"PAD\", SOS_token: \"SOS\", EOS_token: \"EOS\"}\n",
    "        self.num_words = 3  # Count SOS, EOS, PAD\n",
    "\n",
    "    def addSentence(self, sentence):\n",
    "        for word in sentence.split(' '):\n",
    "            self.addWord(word)\n",
    "\n",
    "    def addWord(self, word):\n",
    "        if word not in self.word2index:\n",
    "            self.word2index[word] = self.num_words\n",
    "            self.word2count[word] = 1\n",
    "            self.index2word[self.num_words] = word\n",
    "            self.num_words += 1\n",
    "        else:\n",
    "            self.word2count[word] += 1\n",
    "\n",
    "    # Remove words below a certain count threshold\n",
    "    def trim(self, min_count):\n",
    "        if self.trimmed:\n",
    "            return\n",
    "        self.trimmed = True\n",
    "\n",
    "        keep_words = []\n",
    "\n",
    "        for k, v in self.word2count.items():\n",
    "            if v >= min_count:\n",
    "                keep_words.append(k)\n",
    "\n",
    "        print('keep_words {} / {} = {:.4f}'.format(\n",
    "            len(keep_words), len(self.word2index), len(keep_words) / len(self.word2index)\n",
    "        ))\n",
    "\n",
    "        # Reinitialize dictionaries\n",
    "        self.word2index = {}\n",
    "        self.word2count = {}\n",
    "        self.index2word = {PAD_token: \"PAD\", SOS_token: \"SOS\", EOS_token: \"EOS\"}\n",
    "        self.num_words = 3 # Count default tokens\n",
    "\n",
    "        for word in keep_words:\n",
    "            self.addWord(word)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can assemble our vocabulary and query/response sentence pairs.\n",
    "Before we are ready to use this data, we must perform some\n",
    "preprocessing.\n",
    "\n",
    "First, we must convert the Unicode strings to ASCII using\n",
    "`unicodeToAscii`. Next, we should convert all letters to lowercase and\n",
    "trim all non-letter characters except for basic punctuation\n",
    "(`normalizeString`). Finally, to aid in training convergence, we will\n",
    "filter out sentences with length greater than the `MAX_LENGTH` threshold\n",
    "(`filterPairs`).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "MAX_LENGTH = 10  # Maximum sentence length to consider\n",
    "\n",
    "# Turn a Unicode string to plain ASCII, thanks to\n",
    "# https://stackoverflow.com/a/518232/2809427\n",
    "def unicodeToAscii(s):\n",
    "    return ''.join(\n",
    "        c for c in unicodedata.normalize('NFD', s)\n",
    "        if unicodedata.category(c) != 'Mn'\n",
    "    )\n",
    "\n",
    "# Lowercase, trim, and remove non-letter characters\n",
    "def normalizeString(s):\n",
    "    s = unicodeToAscii(s.lower().strip())\n",
    "    s = re.sub(r\"([.!?])\", r\" \\1\", s)\n",
    "    s = re.sub(r\"[^a-zA-Z.!?]+\", r\" \", s)\n",
    "    s = re.sub(r\"\\s+\", r\" \", s).strip()\n",
    "    return s\n",
    "\n",
    "# Read query/response pairs and return a voc object\n",
    "def readVocs(datafile, corpus_name):\n",
    "    print(\"Reading lines...\")\n",
    "    # Read the file and split into lines\n",
    "    lines = open(datafile, encoding='utf-8').\\\n",
    "        read().strip().split('\\n')\n",
    "    # Split every line into pairs and normalize\n",
    "    pairs = [[normalizeString(s) for s in l.split('\\t')] for l in lines]\n",
    "    voc = Voc(corpus_name)\n",
    "    return voc, pairs\n",
    "\n",
    "# Returns True if both sentences in a pair 'p' are under the MAX_LENGTH threshold\n",
    "def filterPair(p):\n",
    "    # Input sequences need to preserve the last word for EOS token\n",
    "    return len(p[0].split(' ')) < MAX_LENGTH and len(p[1].split(' ')) < MAX_LENGTH\n",
    "\n",
    "# Filter pairs using the ``filterPair`` condition\n",
    "def filterPairs(pairs):\n",
    "    return [pair for pair in pairs if filterPair(pair)]\n",
    "\n",
    "# Using the functions defined above, return a populated voc object and pairs list\n",
    "def loadPrepareData(corpus, corpus_name, datafile, save_dir):\n",
    "    print(\"Start preparing training data ...\")\n",
    "    voc, pairs = readVocs(datafile, corpus_name)\n",
    "    print(\"Read {!s} sentence pairs\".format(len(pairs)))\n",
    "    pairs = filterPairs(pairs)\n",
    "    print(\"Trimmed to {!s} sentence pairs\".format(len(pairs)))\n",
    "    print(\"Counting words...\")\n",
    "    for pair in pairs:\n",
    "        voc.addSentence(pair[0])\n",
    "        voc.addSentence(pair[1])\n",
    "    print(\"Counted words:\", voc.num_words)\n",
    "    return voc, pairs\n",
    "\n",
    "\n",
    "# Load/Assemble voc and pairs\n",
    "save_dir = os.path.join(\"data\", \"save\")\n",
    "voc, pairs = loadPrepareData(corpus, corpus_name, datafile, save_dir)\n",
    "# Print some pairs to validate\n",
    "print(\"\\npairs:\")\n",
    "for pair in pairs[:10]:\n",
    "    print(pair)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Another tactic that is beneficial to achieving faster convergence during\n",
    "training is trimming rarely used words out of our vocabulary. Decreasing\n",
    "the feature space will also soften the difficulty of the function that\n",
    "the model must learn to approximate. We will do this as a two-step\n",
    "process:\n",
    "\n",
    "1)  Trim words used under `MIN_COUNT` threshold using the `voc.trim`\n",
    "    function.\n",
    "2)  Filter out pairs with trimmed words.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "MIN_COUNT = 3    # Minimum word count threshold for trimming\n",
    "\n",
    "def trimRareWords(voc, pairs, MIN_COUNT):\n",
    "    # Trim words used under the MIN_COUNT from the voc\n",
    "    voc.trim(MIN_COUNT)\n",
    "    # Filter out pairs with trimmed words\n",
    "    keep_pairs = []\n",
    "    for pair in pairs:\n",
    "        input_sentence = pair[0]\n",
    "        output_sentence = pair[1]\n",
    "        keep_input = True\n",
    "        keep_output = True\n",
    "        # Check input sentence\n",
    "        for word in input_sentence.split(' '):\n",
    "            if word not in voc.word2index:\n",
    "                keep_input = False\n",
    "                break\n",
    "        # Check output sentence\n",
    "        for word in output_sentence.split(' '):\n",
    "            if word not in voc.word2index:\n",
    "                keep_output = False\n",
    "                break\n",
    "\n",
    "        # Only keep pairs that do not contain trimmed word(s) in their input or output sentence\n",
    "        if keep_input and keep_output:\n",
    "            keep_pairs.append(pair)\n",
    "\n",
    "    print(\"Trimmed from {} pairs to {}, {:.4f} of total\".format(len(pairs), len(keep_pairs), len(keep_pairs) / len(pairs)))\n",
    "    return keep_pairs\n",
    "\n",
    "\n",
    "# Trim voc and pairs\n",
    "pairs = trimRareWords(voc, pairs, MIN_COUNT)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Prepare Data for Models\n",
    "\n",
    "Although we have put a great deal of effort into preparing and massaging\n",
    "our data into a nice vocabulary object and list of sentence pairs, our\n",
    "models will ultimately expect numerical torch tensors as inputs. One way\n",
    "to prepare the processed data for the models can be found in the\n",
    "[seq2seq translation\n",
    "tutorial](https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html).\n",
    "In that tutorial, we use a batch size of 1, meaning that all we have to\n",
    "do is convert the words in our sentence pairs to their corresponding\n",
    "indexes from the vocabulary and feed this to the models.\n",
    "\n",
    "However, if you're interested in speeding up training and/or would like\n",
    "to leverage GPU parallelization capabilities, you will need to train\n",
    "with mini-batches.\n",
    "\n",
    "Using mini-batches also means that we must be mindful of the variation\n",
    "of sentence length in our batches. To accommodate sentences of different\n",
    "sizes in the same batch, we will make our batched input tensor of shape\n",
    "*(max_length, batch_size)*, where sentences shorter than the\n",
    "*max_length* are zero padded after an *EOS_token*.\n",
    "\n",
    "If we simply convert our English sentences to tensors by converting\n",
    "words to their indexes(`indexesFromSentence`) and zero-pad, our tensor\n",
    "would have shape *(batch_size, max_length)* and indexing the first\n",
    "dimension would return a full sequence across all time-steps. However,\n",
    "we need to be able to index our batch along time, and across all\n",
    "sequences in the batch. Therefore, we transpose our input batch shape to\n",
    "*(max_length, batch_size)*, so that indexing across the first dimension\n",
    "returns a time step across all sentences in the batch. We handle this\n",
    "transpose implicitly in the `zeroPadding` function.\n",
    "\n",
    "![/\\_static/img/chatbot/seq2seq_batches.png](https://pytorch.org/tutorials/_static/img/chatbot/seq2seq_batches.png){.align-center}\n",
    "\n",
    "The `inputVar` function handles the process of converting sentences to\n",
    "tensor, ultimately creating a correctly shaped zero-padded tensor. It\n",
    "also returns a tensor of `lengths` for each of the sequences in the\n",
    "batch which will be passed to our decoder later.\n",
    "\n",
    "The `outputVar` function performs a similar function to `inputVar`, but\n",
    "instead of returning a `lengths` tensor, it returns a binary mask tensor\n",
    "and a maximum target sentence length. The binary mask tensor has the\n",
    "same shape as the output target tensor, but every element that is a\n",
    "*PAD_token* is 0 and all others are 1.\n",
    "\n",
    "`batch2TrainData` simply takes a bunch of pairs and returns the input\n",
    "and target tensors using the aforementioned functions.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def indexesFromSentence(voc, sentence):\n",
    "    return [voc.word2index[word] for word in sentence.split(' ')] + [EOS_token]\n",
    "\n",
    "\n",
    "def zeroPadding(l, fillvalue=PAD_token):\n",
    "    return list(itertools.zip_longest(*l, fillvalue=fillvalue))\n",
    "\n",
    "def binaryMatrix(l, value=PAD_token):\n",
    "    m = []\n",
    "    for i, seq in enumerate(l):\n",
    "        m.append([])\n",
    "        for token in seq:\n",
    "            if token == PAD_token:\n",
    "                m[i].append(0)\n",
    "            else:\n",
    "                m[i].append(1)\n",
    "    return m\n",
    "\n",
    "# Returns padded input sequence tensor and lengths\n",
    "def inputVar(l, voc):\n",
    "    indexes_batch = [indexesFromSentence(voc, sentence) for sentence in l]\n",
    "    lengths = torch.tensor([len(indexes) for indexes in indexes_batch])\n",
    "    padList = zeroPadding(indexes_batch)\n",
    "    padVar = torch.LongTensor(padList)\n",
    "    return padVar, lengths\n",
    "\n",
    "# Returns padded target sequence tensor, padding mask, and max target length\n",
    "def outputVar(l, voc):\n",
    "    indexes_batch = [indexesFromSentence(voc, sentence) for sentence in l]\n",
    "    max_target_len = max([len(indexes) for indexes in indexes_batch])\n",
    "    padList = zeroPadding(indexes_batch)\n",
    "    mask = binaryMatrix(padList)\n",
    "    mask = torch.BoolTensor(mask)\n",
    "    padVar = torch.LongTensor(padList)\n",
    "    return padVar, mask, max_target_len\n",
    "\n",
    "# Returns all items for a given batch of pairs\n",
    "def batch2TrainData(voc, pair_batch):\n",
    "    pair_batch.sort(key=lambda x: len(x[0].split(\" \")), reverse=True)\n",
    "    input_batch, output_batch = [], []\n",
    "    for pair in pair_batch:\n",
    "        input_batch.append(pair[0])\n",
    "        output_batch.append(pair[1])\n",
    "    inp, lengths = inputVar(input_batch, voc)\n",
    "    output, mask, max_target_len = outputVar(output_batch, voc)\n",
    "    return inp, lengths, output, mask, max_target_len\n",
    "\n",
    "\n",
    "# Example for validation\n",
    "small_batch_size = 5\n",
    "batches = batch2TrainData(voc, [random.choice(pairs) for _ in range(small_batch_size)])\n",
    "input_variable, lengths, target_variable, mask, max_target_len = batches\n",
    "\n",
    "print(\"input_variable:\", input_variable)\n",
    "print(\"lengths:\", lengths)\n",
    "print(\"target_variable:\", target_variable)\n",
    "print(\"mask:\", mask)\n",
    "print(\"max_target_len:\", max_target_len)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Define Models\n",
    "\n",
    "## Seq2Seq Model\n",
    "\n",
    "The brains of our chatbot is a sequence-to-sequence (seq2seq) model. The\n",
    "goal of a seq2seq model is to take a variable-length sequence as an\n",
    "input, and return a variable-length sequence as an output using a\n",
    "fixed-sized model.\n",
    "\n",
    "[Sutskever et al.](https://arxiv.org/abs/1409.3215) discovered that by\n",
    "using two separate recurrent neural nets together, we can accomplish\n",
    "this task. One RNN acts as an **encoder**, which encodes a variable\n",
    "length input sequence to a fixed-length context vector. In theory, this\n",
    "context vector (the final hidden layer of the RNN) will contain semantic\n",
    "information about the query sentence that is input to the bot. The\n",
    "second RNN is a **decoder**, which takes an input word and the context\n",
    "vector, and returns a guess for the next word in the sequence and a\n",
    "hidden state to use in the next iteration.\n",
    "\n",
    "![/\\_static/img/chatbot/seq2seq_ts.png](https://pytorch.org/tutorials/_static/img/chatbot/seq2seq_ts.png){.align-center}\n",
    "\n",
    "Image source:\n",
    "<https://jeddy92.github.io/JEddy92.github.io/ts_seq2seq_intro/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Encoder\n",
    "\n",
    "The encoder RNN iterates through the input sentence one token\n",
    "(e.g. word) at a time, at each time step outputting an \"output\" vector\n",
    "and a \"hidden state\" vector. The hidden state vector is then passed to\n",
    "the next time step, while the output vector is recorded. The encoder\n",
    "transforms the context it saw at each point in the sequence into a set\n",
    "of points in a high-dimensional space, which the decoder will use to\n",
    "generate a meaningful output for the given task.\n",
    "\n",
    "At the heart of our encoder is a multi-layered Gated Recurrent Unit,\n",
    "invented by [Cho et al.](https://arxiv.org/pdf/1406.1078v3.pdf) in 2014.\n",
    "We will use a bidirectional variant of the GRU, meaning that there are\n",
    "essentially two independent RNNs: one that is fed the input sequence in\n",
    "normal sequential order, and one that is fed the input sequence in\n",
    "reverse order. The outputs of each network are summed at each time step.\n",
    "Using a bidirectional GRU will give us the advantage of encoding both\n",
    "past and future contexts.\n",
    "\n",
    "Bidirectional RNN:\n",
    "\n",
    "![/\\_static/img/chatbot/RNN-bidirectional.png](https://pytorch.org/tutorials/_static/img/chatbot/RNN-bidirectional.png){.align-center\n",
    "width=\"70.0%\"}\n",
    "\n",
    "Image source: <https://colah.github.io/posts/2015-09-NN-Types-FP/>\n",
    "\n",
    "Note that an `embedding` layer is used to encode our word indices in an\n",
    "arbitrarily sized feature space. For our models, this layer will map\n",
    "each word to a feature space of size *hidden_size*. When trained, these\n",
    "values should encode semantic similarity between similar meaning words.\n",
    "\n",
    "Finally, if passing a padded batch of sequences to an RNN module, we\n",
    "must pack and unpack padding around the RNN pass using\n",
    "`nn.utils.rnn.pack_padded_sequence` and\n",
    "`nn.utils.rnn.pad_packed_sequence` respectively.\n",
    "\n",
    "**Computation Graph:**\n",
    "\n",
    "> 1)  Convert word indexes to embeddings.\n",
    "> 2)  Pack padded batch of sequences for RNN module.\n",
    "> 3)  Forward pass through GRU.\n",
    "> 4)  Unpack padding.\n",
    "> 5)  Sum bidirectional GRU outputs.\n",
    "> 6)  Return output and final hidden state.\n",
    "\n",
    "**Inputs:**\n",
    "\n",
    "-   `input_seq`: batch of input sentences; shape=*(max_length,\n",
    "    batch_size)*\n",
    "-   `input_lengths`: list of sentence lengths corresponding to each\n",
    "    sentence in the batch; shape=*(batch_size)*\n",
    "-   `hidden`: hidden state; shape=*(n_layers x num_directions,\n",
    "    batch_size, hidden_size)*\n",
    "\n",
    "**Outputs:**\n",
    "\n",
    "-   `outputs`: output features from the last hidden layer of the GRU\n",
    "    (sum of bidirectional outputs); shape=*(max_length, batch_size,\n",
    "    hidden_size)*\n",
    "-   `hidden`: updated hidden state from GRU; shape=*(n_layers x\n",
    "    num_directions, batch_size, hidden_size)*\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class EncoderRNN(nn.Module):\n",
    "    def __init__(self, hidden_size, embedding, n_layers=1, dropout=0):\n",
    "        super(EncoderRNN, self).__init__()\n",
    "        self.n_layers = n_layers\n",
    "        self.hidden_size = hidden_size\n",
    "        self.embedding = embedding\n",
    "\n",
    "        # Initialize GRU; the input_size and hidden_size parameters are both set to 'hidden_size'\n",
    "        #   because our input size is a word embedding with number of features == hidden_size\n",
    "        self.gru = nn.GRU(hidden_size, hidden_size, n_layers,\n",
    "                          dropout=(0 if n_layers == 1 else dropout), bidirectional=True)\n",
    "\n",
    "    def forward(self, input_seq, input_lengths, hidden=None):\n",
    "        # Convert word indexes to embeddings\n",
    "        embedded = self.embedding(input_seq)\n",
    "        # Pack padded batch of sequences for RNN module\n",
    "        packed = nn.utils.rnn.pack_padded_sequence(embedded, input_lengths)\n",
    "        # Forward pass through GRU\n",
    "        outputs, hidden = self.gru(packed, hidden)\n",
    "        # Unpack padding\n",
    "        outputs, _ = nn.utils.rnn.pad_packed_sequence(outputs)\n",
    "        # Sum bidirectional GRU outputs\n",
    "        outputs = outputs[:, :, :self.hidden_size] + outputs[:, : ,self.hidden_size:]\n",
    "        # Return output and final hidden state\n",
    "        return outputs, hidden"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Decoder\n",
    "\n",
    "The decoder RNN generates the response sentence in a token-by-token\n",
    "fashion. It uses the encoder's context vectors, and internal hidden\n",
    "states to generate the next word in the sequence. It continues\n",
    "generating words until it outputs an *EOS_token*, representing the end\n",
    "of the sentence. A common problem with a vanilla seq2seq decoder is that\n",
    "if we rely solely on the context vector to encode the entire input\n",
    "sequence's meaning, it is likely that we will have information loss.\n",
    "This is especially the case when dealing with long input sequences,\n",
    "greatly limiting the capability of our decoder.\n",
    "\n",
    "To combat this, [Bahdanau et al.](https://arxiv.org/abs/1409.0473)\n",
    "created an \"attention mechanism\" that allows the decoder to pay\n",
    "attention to certain parts of the input sequence, rather than using the\n",
    "entire fixed context at every step.\n",
    "\n",
    "At a high level, attention is calculated using the decoder's current\n",
    "hidden state and the encoder's outputs. The output attention weights\n",
    "have the same shape as the input sequence, allowing us to multiply them\n",
    "by the encoder outputs, giving us a weighted sum which indicates the\n",
    "parts of encoder output to pay attention to. [Sean\n",
    "Robertson's](https://github.com/spro) figure describes this very well:\n",
    "\n",
    "![/\\_static/img/chatbot/attn2.png](https://pytorch.org/tutorials/_static/img/chatbot/attn2.png){.align-center}\n",
    "\n",
    "[Luong et al.](https://arxiv.org/abs/1508.04025) improved upon Bahdanau\n",
    "et al.'s groundwork by creating \"Global attention\". The key difference\n",
    "is that with \"Global attention\", we consider all of the encoder's hidden\n",
    "states, as opposed to Bahdanau et al.'s \"Local attention\", which only\n",
    "considers the encoder's hidden state from the current time step. Another\n",
    "difference is that with \"Global attention\", we calculate attention\n",
    "weights, or energies, using the hidden state of the decoder from the\n",
    "current time step only. Bahdanau et al.'s attention calculation requires\n",
    "knowledge of the decoder's state from the previous time step. Also,\n",
    "Luong et al. provides various methods to calculate the attention\n",
    "energies between the encoder output and decoder output which are called\n",
    "\"score functions\":\n",
    "\n",
    "![/\\_static/img/chatbot/scores.png](https://pytorch.org/tutorials/_static/img/chatbot/scores.png){.align-center\n",
    "width=\"60.0%\"}\n",
    "\n",
    "where $h_t$ = current target decoder state and $\\bar{h}_s$ = all encoder\n",
    "states.\n",
    "\n",
    "Overall, the Global attention mechanism can be summarized by the\n",
    "following figure. Note that we will implement the \"Attention Layer\" as a\n",
    "separate `nn.Module` called `Attn`. The output of this module is a\n",
    "softmax normalized weights tensor of shape *(batch_size, 1,\n",
    "max_length)*.\n",
    "\n",
    "![/\\_static/img/chatbot/global_attn.png](https://pytorch.org/tutorials/_static/img/chatbot/global_attn.png){.align-center\n",
    "width=\"60.0%\"}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Luong attention layer\n",
    "class Attn(nn.Module):\n",
    "    def __init__(self, method, hidden_size):\n",
    "        super(Attn, self).__init__()\n",
    "        self.method = method\n",
    "        if self.method not in ['dot', 'general', 'concat']:\n",
    "            raise ValueError(self.method, \"is not an appropriate attention method.\")\n",
    "        self.hidden_size = hidden_size\n",
    "        if self.method == 'general':\n",
    "            self.attn = nn.Linear(self.hidden_size, hidden_size)\n",
    "        elif self.method == 'concat':\n",
    "            self.attn = nn.Linear(self.hidden_size * 2, hidden_size)\n",
    "            self.v = nn.Parameter(torch.FloatTensor(hidden_size))\n",
    "\n",
    "    def dot_score(self, hidden, encoder_output):\n",
    "        return torch.sum(hidden * encoder_output, dim=2)\n",
    "\n",
    "    def general_score(self, hidden, encoder_output):\n",
    "        energy = self.attn(encoder_output)\n",
    "        return torch.sum(hidden * energy, dim=2)\n",
    "\n",
    "    def concat_score(self, hidden, encoder_output):\n",
    "        energy = self.attn(torch.cat((hidden.expand(encoder_output.size(0), -1, -1), encoder_output), 2)).tanh()\n",
    "        return torch.sum(self.v * energy, dim=2)\n",
    "\n",
    "    def forward(self, hidden, encoder_outputs):\n",
    "        # Calculate the attention weights (energies) based on the given method\n",
    "        if self.method == 'general':\n",
    "            attn_energies = self.general_score(hidden, encoder_outputs)\n",
    "        elif self.method == 'concat':\n",
    "            attn_energies = self.concat_score(hidden, encoder_outputs)\n",
    "        elif self.method == 'dot':\n",
    "            attn_energies = self.dot_score(hidden, encoder_outputs)\n",
    "\n",
    "        # Transpose max_length and batch_size dimensions\n",
    "        attn_energies = attn_energies.t()\n",
    "\n",
    "        # Return the softmax normalized probability scores (with added dimension)\n",
    "        return F.softmax(attn_energies, dim=1).unsqueeze(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we have defined our attention submodule, we can implement the\n",
    "actual decoder model. For the decoder, we will manually feed our batch\n",
    "one time step at a time. This means that our embedded word tensor and\n",
    "GRU output will both have shape *(1, batch_size, hidden_size)*.\n",
    "\n",
    "**Computation Graph:**\n",
    "\n",
    "> 1)  Get embedding of current input word.\n",
    "> 2)  Forward through unidirectional GRU.\n",
    "> 3)  Calculate attention weights from the current GRU output from (2).\n",
    "> 4)  Multiply attention weights to encoder outputs to get new\n",
    ">     \\\"weighted sum\\\" context vector.\n",
    "> 5)  Concatenate weighted context vector and GRU output using Luong eq.\n",
    ">     5.\n",
    "> 6)  Predict next word using Luong eq. 6 (without softmax).\n",
    "> 7)  Return output and final hidden state.\n",
    "\n",
    "**Inputs:**\n",
    "\n",
    "-   `input_step`: one time step (one word) of input sequence batch;\n",
    "    shape=*(1, batch_size)*\n",
    "-   `last_hidden`: final hidden layer of GRU; shape=*(n_layers x\n",
    "    num_directions, batch_size, hidden_size)*\n",
    "-   `encoder_outputs`: encoder model's output; shape=*(max_length,\n",
    "    batch_size, hidden_size)*\n",
    "\n",
    "**Outputs:**\n",
    "\n",
    "-   `output`: softmax normalized tensor giving probabilities of each\n",
    "    word being the correct next word in the decoded sequence;\n",
    "    shape=*(batch_size, voc.num_words)*\n",
    "-   `hidden`: final hidden state of GRU; shape=*(n_layers x\n",
    "    num_directions, batch_size, hidden_size)*\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class LuongAttnDecoderRNN(nn.Module):\n",
    "    def __init__(self, attn_model, embedding, hidden_size, output_size, n_layers=1, dropout=0.1):\n",
    "        super(LuongAttnDecoderRNN, self).__init__()\n",
    "\n",
    "        # Keep for reference\n",
    "        self.attn_model = attn_model\n",
    "        self.hidden_size = hidden_size\n",
    "        self.output_size = output_size\n",
    "        self.n_layers = n_layers\n",
    "        self.dropout = dropout\n",
    "\n",
    "        # Define layers\n",
    "        self.embedding = embedding\n",
    "        self.embedding_dropout = nn.Dropout(dropout)\n",
    "        self.gru = nn.GRU(hidden_size, hidden_size, n_layers, dropout=(0 if n_layers == 1 else dropout))\n",
    "        self.concat = nn.Linear(hidden_size * 2, hidden_size)\n",
    "        self.out = nn.Linear(hidden_size, output_size)\n",
    "\n",
    "        self.attn = Attn(attn_model, hidden_size)\n",
    "\n",
    "    def forward(self, input_step, last_hidden, encoder_outputs):\n",
    "        # Note: we run this one step (word) at a time\n",
    "        # Get embedding of current input word\n",
    "        embedded = self.embedding(input_step)\n",
    "        embedded = self.embedding_dropout(embedded)\n",
    "        # Forward through unidirectional GRU\n",
    "        rnn_output, hidden = self.gru(embedded, last_hidden)\n",
    "        # Calculate attention weights from the current GRU output\n",
    "        attn_weights = self.attn(rnn_output, encoder_outputs)\n",
    "        # Multiply attention weights to encoder outputs to get new \"weighted sum\" context vector\n",
    "        context = attn_weights.bmm(encoder_outputs.transpose(0, 1))\n",
    "        # Concatenate weighted context vector and GRU output using Luong eq. 5\n",
    "        rnn_output = rnn_output.squeeze(0)\n",
    "        context = context.squeeze(1)\n",
    "        concat_input = torch.cat((rnn_output, context), 1)\n",
    "        concat_output = torch.tanh(self.concat(concat_input))\n",
    "        # Predict next word using Luong eq. 6\n",
    "        output = self.out(concat_output)\n",
    "        output = F.softmax(output, dim=1)\n",
    "        # Return output and final hidden state\n",
    "        return output, hidden"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Define Training Procedure\n",
    "\n",
    "## Masked loss\n",
    "\n",
    "Since we are dealing with batches of padded sequences, we cannot simply\n",
    "consider all elements of the tensor when calculating loss. We define\n",
    "`maskNLLLoss` to calculate our loss based on our decoder's output\n",
    "tensor, the target tensor, and a binary mask tensor describing the\n",
    "padding of the target tensor. This loss function calculates the average\n",
    "negative log likelihood of the elements that correspond to a *1* in the\n",
    "mask tensor.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def maskNLLLoss(inp, target, mask):\n",
    "    nTotal = mask.sum()\n",
    "    crossEntropy = -torch.log(torch.gather(inp, 1, target.view(-1, 1)).squeeze(1))\n",
    "    loss = crossEntropy.masked_select(mask).mean()\n",
    "    loss = loss.to(device)\n",
    "    return loss, nTotal.item()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Single training iteration\n",
    "\n",
    "The `train` function contains the algorithm for a single training\n",
    "iteration (a single batch of inputs).\n",
    "\n",
    "We will use a couple of clever tricks to aid in convergence:\n",
    "\n",
    "-   The first trick is using **teacher forcing**. This means that at\n",
    "    some probability, set by `teacher_forcing_ratio`, we use the current\n",
    "    target word as the decoder's next input rather than using the\n",
    "    decoder's current guess. This technique acts as training wheels for\n",
    "    the decoder, aiding in more efficient training. However, teacher\n",
    "    forcing can lead to model instability during inference, as the\n",
    "    decoder may not have a sufficient chance to truly craft its own\n",
    "    output sequences during training. Thus, we must be mindful of how we\n",
    "    are setting the `teacher_forcing_ratio`, and not be fooled by fast\n",
    "    convergence.\n",
    "-   The second trick that we implement is **gradient clipping**. This is\n",
    "    a commonly used technique for countering the \"exploding gradient\"\n",
    "    problem. In essence, by clipping or thresholding gradients to a\n",
    "    maximum value, we prevent the gradients from growing exponentially\n",
    "    and either overflow (NaN), or overshoot steep cliffs in the cost\n",
    "    function.\n",
    "\n",
    "![/\\_static/img/chatbot/grad_clip.png](https://pytorch.org/tutorials/_static/img/chatbot/grad_clip.png){.align-center\n",
    "width=\"60.0%\"}\n",
    "\n",
    "Image source: Goodfellow et al. *Deep Learning*. 2016.\n",
    "<https://www.deeplearningbook.org/>\n",
    "\n",
    "**Sequence of Operations:**\n",
    "\n",
    "> 1)  Forward pass entire input batch through encoder.\n",
    "> 2)  Initialize decoder inputs as SOS_token, and hidden state as the\n",
    ">     encoder\\'s final hidden state.\n",
    "> 3)  Forward input batch sequence through decoder one time step at a\n",
    ">     time.\n",
    "> 4)  If teacher forcing: set next decoder input as the current target;\n",
    ">     else: set next decoder input as current decoder output.\n",
    "> 5)  Calculate and accumulate loss.\n",
    "> 6)  Perform backpropagation.\n",
    "> 7)  Clip gradients.\n",
    "> 8)  Update encoder and decoder model parameters.\n",
    "\n",
    "> PyTorch's RNN modules (`RNN`, `LSTM`, `GRU`) can be used like any\n",
    "> other non-recurrent layers by simply passing them the entire input\n",
    "> sequence (or batch of sequences). We use the `GRU` layer like this in\n",
    "> the `encoder`. The reality is that under the hood, there is an\n",
    "> iterative process looping over each time step calculating hidden\n",
    "> states. Alternatively, you can run these modules one time-step at a\n",
    "> time. In this case, we manually loop over the sequences during the\n",
    "> training process like we must do for the `decoder` model. As long as\n",
    "> you maintain the correct conceptual model of these modules,\n",
    "> implementing sequential models can be very straightforward.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def train(input_variable, lengths, target_variable, mask, max_target_len, encoder, decoder, embedding,\n",
    "          encoder_optimizer, decoder_optimizer, batch_size, clip, max_length=MAX_LENGTH):\n",
    "\n",
    "    # Zero gradients\n",
    "    encoder_optimizer.zero_grad()\n",
    "    decoder_optimizer.zero_grad()\n",
    "\n",
    "    # Set device options\n",
    "    input_variable = input_variable.to(device)\n",
    "    target_variable = target_variable.to(device)\n",
    "    mask = mask.to(device)\n",
    "    # Lengths for RNN packing should always be on the CPU\n",
    "    lengths = lengths.to(\"cpu\")\n",
    "\n",
    "    # Initialize variables\n",
    "    loss = 0\n",
    "    print_losses = []\n",
    "    n_totals = 0\n",
    "\n",
    "    # Forward pass through encoder\n",
    "    encoder_outputs, encoder_hidden = encoder(input_variable, lengths)\n",
    "\n",
    "    # Create initial decoder input (start with SOS tokens for each sentence)\n",
    "    decoder_input = torch.LongTensor([[SOS_token for _ in range(batch_size)]])\n",
    "    decoder_input = decoder_input.to(device)\n",
    "\n",
    "    # Set initial decoder hidden state to the encoder's final hidden state\n",
    "    decoder_hidden = encoder_hidden[:decoder.n_layers]\n",
    "\n",
    "    # Determine if we are using teacher forcing this iteration\n",
    "    use_teacher_forcing = True if random.random() < teacher_forcing_ratio else False\n",
    "\n",
    "    # Forward batch of sequences through decoder one time step at a time\n",
    "    if use_teacher_forcing:\n",
    "        for t in range(max_target_len):\n",
    "            decoder_output, decoder_hidden = decoder(\n",
    "                decoder_input, decoder_hidden, encoder_outputs\n",
    "            )\n",
    "            # Teacher forcing: next input is current target\n",
    "            decoder_input = target_variable[t].view(1, -1)\n",
    "            # Calculate and accumulate loss\n",
    "            mask_loss, nTotal = maskNLLLoss(decoder_output, target_variable[t], mask[t])\n",
    "            loss += mask_loss\n",
    "            print_losses.append(mask_loss.item() * nTotal)\n",
    "            n_totals += nTotal\n",
    "    else:\n",
    "        for t in range(max_target_len):\n",
    "            decoder_output, decoder_hidden = decoder(\n",
    "                decoder_input, decoder_hidden, encoder_outputs\n",
    "            )\n",
    "            # No teacher forcing: next input is decoder's own current output\n",
    "            _, topi = decoder_output.topk(1)\n",
    "            decoder_input = torch.LongTensor([[topi[i][0] for i in range(batch_size)]])\n",
    "            decoder_input = decoder_input.to(device)\n",
    "            # Calculate and accumulate loss\n",
    "            mask_loss, nTotal = maskNLLLoss(decoder_output, target_variable[t], mask[t])\n",
    "            loss += mask_loss\n",
    "            print_losses.append(mask_loss.item() * nTotal)\n",
    "            n_totals += nTotal\n",
    "\n",
    "    # Perform backpropagation\n",
    "    loss.backward()\n",
    "\n",
    "    # Clip gradients: gradients are modified in place\n",
    "    _ = nn.utils.clip_grad_norm_(encoder.parameters(), clip)\n",
    "    _ = nn.utils.clip_grad_norm_(decoder.parameters(), clip)\n",
    "\n",
    "    # Adjust model weights\n",
    "    encoder_optimizer.step()\n",
    "    decoder_optimizer.step()\n",
    "\n",
    "    return sum(print_losses) / n_totals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Training iterations\n",
    "\n",
    "It is finally time to tie the full training procedure together with the\n",
    "data. The `trainIters` function is responsible for running\n",
    "`n_iterations` of training given the passed models, optimizers, data,\n",
    "etc. This function is quite self explanatory, as we have done the heavy\n",
    "lifting with the `train` function.\n",
    "\n",
    "One thing to note is that when we save our model, we save a tarball\n",
    "containing the encoder and decoder `state_dicts` (parameters), the\n",
    "optimizers' `state_dicts`, the loss, the iteration, etc. Saving the\n",
    "model in this way will give us the ultimate flexibility with the\n",
    "checkpoint. After loading a checkpoint, we will be able to use the model\n",
    "parameters to run inference, or we can continue training right where we\n",
    "left off.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def trainIters(model_name, voc, pairs, encoder, decoder, encoder_optimizer, decoder_optimizer, embedding, encoder_n_layers, decoder_n_layers, save_dir, n_iteration, batch_size, print_every, save_every, clip, corpus_name, loadFilename):\n",
    "\n",
    "    # Load batches for each iteration\n",
    "    training_batches = [batch2TrainData(voc, [random.choice(pairs) for _ in range(batch_size)])\n",
    "                      for _ in range(n_iteration)]\n",
    "\n",
    "    # Initializations\n",
    "    print('Initializing ...')\n",
    "    start_iteration = 1\n",
    "    print_loss = 0\n",
    "    if loadFilename:\n",
    "        start_iteration = checkpoint['iteration'] + 1\n",
    "\n",
    "    # Training loop\n",
    "    print(\"Training...\")\n",
    "    for iteration in range(start_iteration, n_iteration + 1):\n",
    "        training_batch = training_batches[iteration - 1]\n",
    "        # Extract fields from batch\n",
    "        input_variable, lengths, target_variable, mask, max_target_len = training_batch\n",
    "\n",
    "        # Run a training iteration with batch\n",
    "        loss = train(input_variable, lengths, target_variable, mask, max_target_len, encoder,\n",
    "                     decoder, embedding, encoder_optimizer, decoder_optimizer, batch_size, clip)\n",
    "        print_loss += loss\n",
    "\n",
    "        # Print progress\n",
    "        if iteration % print_every == 0:\n",
    "            print_loss_avg = print_loss / print_every\n",
    "            print(\"Iteration: {}; Percent complete: {:.1f}%; Average loss: {:.4f}\".format(iteration, iteration / n_iteration * 100, print_loss_avg))\n",
    "            print_loss = 0\n",
    "\n",
    "        # Save checkpoint\n",
    "        if (iteration % save_every == 0):\n",
    "            directory = os.path.join(save_dir, model_name, corpus_name, '{}-{}_{}'.format(encoder_n_layers, decoder_n_layers, hidden_size))\n",
    "            if not os.path.exists(directory):\n",
    "                os.makedirs(directory)\n",
    "            torch.save({\n",
    "                'iteration': iteration,\n",
    "                'en': encoder.state_dict(),\n",
    "                'de': decoder.state_dict(),\n",
    "                'en_opt': encoder_optimizer.state_dict(),\n",
    "                'de_opt': decoder_optimizer.state_dict(),\n",
    "                'loss': loss,\n",
    "                'voc_dict': voc.__dict__,\n",
    "                'embedding': embedding.state_dict()\n",
    "            }, os.path.join(directory, '{}_{}.tar'.format(iteration, 'checkpoint')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Define Evaluation\n",
    "\n",
    "After training a model, we want to be able to talk to the bot ourselves.\n",
    "First, we must define how we want the model to decode the encoded input.\n",
    "\n",
    "## Greedy decoding\n",
    "\n",
    "Greedy decoding is the decoding method that we use during training when\n",
    "we are **NOT** using teacher forcing. In other words, for each time\n",
    "step, we simply choose the word from `decoder_output` with the highest\n",
    "softmax value. This decoding method is optimal on a single time-step\n",
    "level.\n",
    "\n",
    "To facilitate the greedy decoding operation, we define a\n",
    "`GreedySearchDecoder` class. When run, an object of this class takes an\n",
    "input sequence (`input_seq`) of shape *(input_seq length, 1)*, a scalar\n",
    "input length (`input_length`) tensor, and a `max_length` to bound the\n",
    "response sentence length. The input sentence is evaluated using the\n",
    "following computational graph:\n",
    "\n",
    "**Computation Graph:**\n",
    "\n",
    "> 1)  Forward input through encoder model.\n",
    ">\n",
    "> 2)  Prepare encoder\\'s final hidden layer to be first hidden input to\n",
    ">     the decoder.\n",
    ">\n",
    "> 3)  Initialize decoder\\'s first input as SOS_token.\n",
    ">\n",
    "> 4)  Initialize tensors to append decoded words to.\n",
    ">\n",
    "> 5)  \n",
    ">\n",
    ">     Iteratively decode one word token at a time:\n",
    ">\n",
    ">     :   a)  Forward pass through decoder.\n",
    ">         b)  Obtain most likely word token and its softmax score.\n",
    ">         c)  Record token and score.\n",
    ">         d)  Prepare current token to be next decoder input.\n",
    ">\n",
    "> 6)  Return collections of word tokens and scores.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class GreedySearchDecoder(nn.Module):\n",
    "    def __init__(self, encoder, decoder):\n",
    "        super(GreedySearchDecoder, self).__init__()\n",
    "        self.encoder = encoder\n",
    "        self.decoder = decoder\n",
    "\n",
    "    def forward(self, input_seq, input_length, max_length):\n",
    "        # Forward input through encoder model\n",
    "        encoder_outputs, encoder_hidden = self.encoder(input_seq, input_length)\n",
    "        # Prepare encoder's final hidden layer to be first hidden input to the decoder\n",
    "        decoder_hidden = encoder_hidden[:self.decoder.n_layers]\n",
    "        # Initialize decoder input with SOS_token\n",
    "        decoder_input = torch.ones(1, 1, device=device, dtype=torch.long) * SOS_token\n",
    "        # Initialize tensors to append decoded words to\n",
    "        all_tokens = torch.zeros([0], device=device, dtype=torch.long)\n",
    "        all_scores = torch.zeros([0], device=device)\n",
    "        # Iteratively decode one word token at a time\n",
    "        for _ in range(max_length):\n",
    "            # Forward pass through decoder\n",
    "            decoder_output, decoder_hidden = self.decoder(decoder_input, decoder_hidden, encoder_outputs)\n",
    "            # Obtain most likely word token and its softmax score\n",
    "            decoder_scores, decoder_input = torch.max(decoder_output, dim=1)\n",
    "            # Record token and score\n",
    "            all_tokens = torch.cat((all_tokens, decoder_input), dim=0)\n",
    "            all_scores = torch.cat((all_scores, decoder_scores), dim=0)\n",
    "            # Prepare current token to be next decoder input (add a dimension)\n",
    "            decoder_input = torch.unsqueeze(decoder_input, 0)\n",
    "        # Return collections of word tokens and scores\n",
    "        return all_tokens, all_scores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Evaluate my text\n",
    "\n",
    "Now that we have our decoding method defined, we can write functions for\n",
    "evaluating a string input sentence. The `evaluate` function manages the\n",
    "low-level process of handling the input sentence. We first format the\n",
    "sentence as an input batch of word indexes with *batch_size==1*. We do\n",
    "this by converting the words of the sentence to their corresponding\n",
    "indexes, and transposing the dimensions to prepare the tensor for our\n",
    "models. We also create a `lengths` tensor which contains the length of\n",
    "our input sentence. In this case, `lengths` is scalar because we are\n",
    "only evaluating one sentence at a time (batch_size==1). Next, we obtain\n",
    "the decoded response sentence tensor using our `GreedySearchDecoder`\n",
    "object (`searcher`). Finally, we convert the response's indexes to words\n",
    "and return the list of decoded words.\n",
    "\n",
    "`evaluateInput` acts as the user interface for our chatbot. When called,\n",
    "an input text field will spawn in which we can enter our query sentence.\n",
    "After typing our input sentence and pressing *Enter*, our text is\n",
    "normalized in the same way as our training data, and is ultimately fed\n",
    "to the `evaluate` function to obtain a decoded output sentence. We loop\n",
    "this process, so we can keep chatting with our bot until we enter either\n",
    "\"q\" or \"quit\".\n",
    "\n",
    "Finally, if a sentence is entered that contains a word that is not in\n",
    "the vocabulary, we handle this gracefully by printing an error message\n",
    "and prompting the user to enter another sentence.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def evaluate(encoder, decoder, searcher, voc, sentence, max_length=MAX_LENGTH):\n",
    "    ### Format input sentence as a batch\n",
    "    # words -> indexes\n",
    "    indexes_batch = [indexesFromSentence(voc, sentence)]\n",
    "    # Create lengths tensor\n",
    "    lengths = torch.tensor([len(indexes) for indexes in indexes_batch])\n",
    "    # Transpose dimensions of batch to match models' expectations\n",
    "    input_batch = torch.LongTensor(indexes_batch).transpose(0, 1)\n",
    "    # Use appropriate device\n",
    "    input_batch = input_batch.to(device)\n",
    "    lengths = lengths.to(\"cpu\")\n",
    "    # Decode sentence with searcher\n",
    "    tokens, scores = searcher(input_batch, lengths, max_length)\n",
    "    # indexes -> words\n",
    "    decoded_words = [voc.index2word[token.item()] for token in tokens]\n",
    "    return decoded_words\n",
    "\n",
    "\n",
    "def evaluateInput(encoder, decoder, searcher, voc):\n",
    "    input_sentence = ''\n",
    "    while(1):\n",
    "        try:\n",
    "            # Get input sentence\n",
    "            input_sentence = input('> ')\n",
    "            # Check if it is quit case\n",
    "            if input_sentence == 'q' or input_sentence == 'quit': break\n",
    "            # Normalize sentence\n",
    "            input_sentence = normalizeString(input_sentence)\n",
    "            # Evaluate sentence\n",
    "            output_words = evaluate(encoder, decoder, searcher, voc, input_sentence)\n",
    "            # Format and print response sentence\n",
    "            output_words[:] = [x for x in output_words if not (x == 'EOS' or x == 'PAD')]\n",
    "            print('Bot:', ' '.join(output_words))\n",
    "\n",
    "        except KeyError:\n",
    "            print(\"Error: Encountered unknown word.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run Model\n",
    "\n",
    "Finally, it is time to run our model!\n",
    "\n",
    "Regardless of whether we want to train or test the chatbot model, we\n",
    "must initialize the individual encoder and decoder models. In the\n",
    "following block, we set our desired configurations, choose to start from\n",
    "scratch or set a checkpoint to load from, and build and initialize the\n",
    "models. Feel free to play with different model configurations to\n",
    "optimize performance.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Configure models\n",
    "model_name = 'cb_model'\n",
    "attn_model = 'dot'\n",
    "#``attn_model = 'general'``\n",
    "#``attn_model = 'concat'``\n",
    "hidden_size = 500\n",
    "encoder_n_layers = 2\n",
    "decoder_n_layers = 2\n",
    "dropout = 0.1\n",
    "batch_size = 64\n",
    "\n",
    "# Set checkpoint to load from; set to None if starting from scratch\n",
    "loadFilename = None\n",
    "checkpoint_iter = 4000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sample code to load from a checkpoint:\n",
    "\n",
    "``` python\n",
    "loadFilename = os.path.join(save_dir, model_name, corpus_name,\n",
    "                    '{}-{}_{}'.format(encoder_n_layers, decoder_n_layers, hidden_size),\n",
    "                    '{}_checkpoint.tar'.format(checkpoint_iter))\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Load model if a ``loadFilename`` is provided\n",
    "if loadFilename:\n",
    "    # If loading on same machine the model was trained on\n",
    "    checkpoint = torch.load(loadFilename)\n",
    "    # If loading a model trained on GPU to CPU\n",
    "    #checkpoint = torch.load(loadFilename, map_location=torch.device('cpu'))\n",
    "    encoder_sd = checkpoint['en']\n",
    "    decoder_sd = checkpoint['de']\n",
    "    encoder_optimizer_sd = checkpoint['en_opt']\n",
    "    decoder_optimizer_sd = checkpoint['de_opt']\n",
    "    embedding_sd = checkpoint['embedding']\n",
    "    voc.__dict__ = checkpoint['voc_dict']\n",
    "\n",
    "\n",
    "print('Building encoder and decoder ...')\n",
    "# Initialize word embeddings\n",
    "embedding = nn.Embedding(voc.num_words, hidden_size)\n",
    "if loadFilename:\n",
    "    embedding.load_state_dict(embedding_sd)\n",
    "# Initialize encoder & decoder models\n",
    "encoder = EncoderRNN(hidden_size, embedding, encoder_n_layers, dropout)\n",
    "decoder = LuongAttnDecoderRNN(attn_model, embedding, hidden_size, voc.num_words, decoder_n_layers, dropout)\n",
    "if loadFilename:\n",
    "    encoder.load_state_dict(encoder_sd)\n",
    "    decoder.load_state_dict(decoder_sd)\n",
    "# Use appropriate device\n",
    "encoder = encoder.to(device)\n",
    "decoder = decoder.to(device)\n",
    "print('Models built and ready to go!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run Training\n",
    "\n",
    "Run the following block if you want to train the model.\n",
    "\n",
    "First we set training parameters, then we initialize our optimizers, and\n",
    "finally we call the `trainIters` function to run our training\n",
    "iterations.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Configure training/optimization\n",
    "clip = 50.0\n",
    "teacher_forcing_ratio = 1.0\n",
    "learning_rate = 0.0001\n",
    "decoder_learning_ratio = 5.0\n",
    "n_iteration = 4000\n",
    "print_every = 1\n",
    "save_every = 500\n",
    "\n",
    "# Ensure dropout layers are in train mode\n",
    "encoder.train()\n",
    "decoder.train()\n",
    "\n",
    "# Initialize optimizers\n",
    "print('Building optimizers ...')\n",
    "encoder_optimizer = optim.Adam(encoder.parameters(), lr=learning_rate)\n",
    "decoder_optimizer = optim.Adam(decoder.parameters(), lr=learning_rate * decoder_learning_ratio)\n",
    "if loadFilename:\n",
    "    encoder_optimizer.load_state_dict(encoder_optimizer_sd)\n",
    "    decoder_optimizer.load_state_dict(decoder_optimizer_sd)\n",
    "\n",
    "# If you have an accelerator, configure it to call\n",
    "for state in encoder_optimizer.state.values():\n",
    "    for k, v in state.items():\n",
    "        if isinstance(v, torch.Tensor):\n",
    "            state[k] = v.to(device)\n",
    "\n",
    "for state in decoder_optimizer.state.values():\n",
    "    for k, v in state.items():\n",
    "        if isinstance(v, torch.Tensor):\n",
    "            state[k] = v.to(device)\n",
    "\n",
    "# Run training iterations\n",
    "print(\"Starting Training!\")\n",
    "trainIters(model_name, voc, pairs, encoder, decoder, encoder_optimizer, decoder_optimizer,\n",
    "           embedding, encoder_n_layers, decoder_n_layers, save_dir, n_iteration, batch_size,\n",
    "           print_every, save_every, clip, corpus_name, loadFilename)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run Evaluation\n",
    "\n",
    "To chat with your model, run the following block.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Set dropout layers to ``eval`` mode\n",
    "encoder.eval()\n",
    "decoder.eval()\n",
    "\n",
    "# Initialize search module\n",
    "searcher = GreedySearchDecoder(encoder, decoder)\n",
    "\n",
    "# Begin chatting (uncomment and run the following line to begin)\n",
    "# evaluateInput(encoder, decoder, searcher, voc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conclusion\n",
    "\n",
    "That's all for this one, folks. Congratulations, you now know the\n",
    "fundamentals to building a generative chatbot model! If you're\n",
    "interested, you can try tailoring the chatbot's behavior by tweaking the\n",
    "model and training parameters and customizing the data that you train\n",
    "the model on.\n",
    "\n",
    "Check out the other tutorials for more cool deep learning applications\n",
    "in PyTorch!\n"
   ]
  }
 ],
 "metadata": {
  "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
