TV Script Generation

Act I - The Call To Adventure

What is this about, then?

We want to create a model that can generate scripts for you. To do I'll use part of the Seinfeld dataset of scripts hosted on kaggle to create an RNN to create "fake" TV scripts that emulate the Seinfeld ones.

Set Up

Imports

  • Python
    from collections import Counter
    from functools import partial
    from pathlib import Path
    from typing import Collection
    import os
    import pickle
    
  • PyPi
    from dotenv import load_dotenv
    from tabulate import tabulate
    from torch import nn
    from torch.utils.data import TensorDataset, DataLoader
    import hvplot.pandas
    import numpy
    import pandas
    import torch
    
  • This Project
    from bartleby_the_penguin.tangles.embed_bokeh import EmbedBokeh
    
  • Support Code
    from udacity.project_tv_script_generation import helper
    import udacity.project_tv_script_generation.problem_unittests as unittests
    

Load Dotenv

load_dotenv()

The Folder Path

This is the path for saving files for this post.

FOLDER_PATH = Path("../../../files/posts/nano/tv-script-generation/"
                   "tv-script-generation/")
if not FOLDER_PATH.is_dir():
    FOLDER_PATH.mkdir(parents=True)

The Bokeh Embedder

This sets up the bokeh files and HTML.

Embed = partial(EmbedBokeh, folder_path=FOLDER_PATH)

Check CUDA

Make sure that we can use CUDA.

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
assert device.type == "cuda", 'No GPU found. Please use a GPU to train your neural network.'
print("Using {}".format(device))

Some Types

WordIndices = Collection[int]

Get the Data

Scripts

class Scripts:
    """Seinfeld Scripts

    Args:
     environment_key: environment variable with the source location
     dialog_only: remove descriptive columns
    """
    def __init__(self, environment_key: str="SCRIPTS", dialog_only: bool=True) -> None:
        self.environment_key = environment_key
        self.dialog_only = dialog_only
        self._script_blob = None
        self._path = None
        self._lines = None
        self._tokens = None
        self._line_tokens = None
        return

    @property
    def path(self) -> Path:
        """The path to the file"""
        if self._path is None:
            load_dotenv(".env")
            self._path = Path(os.environ.get("SCRIPTS")).expanduser()
            assert self._path.is_file()
        return self._path

    @property
    def script_blob(self) -> str:
        """The input file as a string"""
        if self._script_blob is None:
            with open(self.path) as reader:
                self._script_blob = reader.read()
        return self._script_blob

    @property
    def line_tokens(self) -> list:
        """list of tokens for each line"""
        if self._line_tokens is None:
            self._line_tokens = [line.split(" ") for line in self.lines]
        return self._line_tokens

    @property
    def lines(self) -> list:
        """The lines of the script"""
        if self._lines is None:
            lines = self.script_blob.split("\n")
            if self.dialog_only:
                lines = lines[1:]
                lines = [(",").join(line.split(",")[2:-3]) for line in lines]
            self._lines = lines
        return self._lines

    @property
    def tokens(self) -> Counter:
        """The tokens and their counts"""
        if self._tokens is None:
            self._tokens = Counter()
            for token in self.script_blob.split():
                self._tokens[token] += 1
        return self._tokens

Script Inspector

This is just to help with some preliminary exploratory data analysis.

class ScriptInspector:
    """gets some basic counts

    Args:
     scripts: object with the scripts
    """
    def __init__(self, scripts: Scripts=None) -> None:
        self._scripts = scripts
        self._line_count = None
        self._count_per_line = None
        self._mean_words_per_line = None
        self._median_words_per_line = None
        self._max_words_per_line = None
        self._min_words_per_line = None
        self._token_count = None
        return

    @property
    def scripts(self) -> Scripts:
        """The scripts object"""
        if self._scripts is None:
            self._scripts = Scripts()
        return self._scripts

    @property
    def line_count(self) -> int:
        """Number of lines in the source"""
        if self._line_count is None:
            self._line_count = len(self.scripts.lines)
        return self._line_count

    @property
    def count_per_line(self) -> list:
        """tokens per line"""
        if self._count_per_line is None:
            self._count_per_line = [len(tokens)
                                    for tokens in self.scripts.line_tokens]
        return self._count_per_line

    @property
    def mean_words_per_line(self) -> float:
        """Average number of words per line"""
        if self._mean_words_per_line is None:
            self._mean_words_per_line = (sum(self.count_per_line)
                                         /self.line_count)
        return self._mean_words_per_line

    @property
    def median_words_per_line(self) -> float:
        """Median words per line in the scripts"""
        if self._median_words_per_line is None:
            self._median_words_per_line = numpy.median(self.count_per_line)
        return self._median_words_per_line

    @property
    def max_words_per_line(self) -> int:
        """Count of words in longest line"""
        if self._max_words_per_line is None:
            self._max_words_per_line = max(self.count_per_line)
        return self._max_words_per_line

    @property
    def min_words_per_line(self) -> int:
        """Count of words in shortest line"""
        if self._min_words_per_line is None:
            self._min_words_per_line = min(self.count_per_line)
        return self._min_words_per_line

    @property
    def token_count(self) -> int:
        """Number of tokens in the text"""
        if self._token_count is None:
            self._token_count = sum(self.scripts.tokens.values())
        return self._token_count

    def most_common_tokens(self, count: int=10) -> list:
        """token, count tuples in descending rank

        Args:
         count: number of tuples to return in the list
        """
        if count > 0:
            return self.scripts.tokens.most_common(count)
        return self.scripts.tokens.most_common()[count:]

    def line_range(self, start: int=0, stop: int=10) -> list:
        """lines within range

        Args:
         start: index of first line
         stop: upper bound for last line
        """
        return self.scripts.lines[start:stop]

The scripts aren't really in a format that is optimized for pandas, at least not for this initial look, so we'll just load it as text.

inspector = ScriptInspector()

Explore the Data

view_line_range = (0, 10)
words_per_line = pandas.DataFrame(inspector.count_per_line,
                                  columns=["line_counts"])
print(words_per_line.shape)
(54617, 1)

Dataset Statistics

lines = (("Number of unique tokens", "{:,}".format(inspector.token_count)),
         ("Number of lines", "{:,}".format(inspector.line_count)),
         ("Words in longest line", "{:,}".format(inspector.max_words_per_line)),
         ("Average number of words in each line", "{:.2f}".format(
             inspector.mean_words_per_line)),
         ("Median Words Per Line", "{:.2f}".format(
             inspector.median_words_per_line)),
         ("Words in shortest line", "{}".format(inspector.min_words_per_line))
)
print(tabulate(lines, headers="Statistic Value".split(), tablefmt="orgtbl"))
Statistic Value
Number of unique tokens 550,996
Number of lines 54,617
Words in longest line 363
Average number of words in each line 10.01
Median Words Per Line 7.00
Words in shortest line 1

Why would a line have 363 words?

index = words_per_line.line_counts.idxmax()
print(inspector.count_per_line[index])
print(inspector.scripts.lines[index])
363
"The dating world is not a fun world...its a pressure world, its a world of tension, its a world of pain...and you know, if a woman comes over to my house, I gotta get that bathroom ready, cause she needs things. Women need equipment. I dont know what they need. I know I dont have it, I know that- You know what they need, women seem to need a lot of cotton-balls. This is the one Im- always has been one of the amazing things to me...I have no cotton-balls, were all human beings, what is the story? Ive never had one...I never bought one, I never needed one, Ive never been in a situation, when I thought to myself I could use a cotton-ball right now. I can certainly get out of this mess. Women need them and they dont need one or two, they need thousands of them, they need bags, theyre like peat moss bags, have you ever seen these giant bags? Theyre huge and two days later, theyre out, theyre gone, the, the bag is empty, where are the cotton-balls, ladies? What are you doin with them? The only time I ever see em is in the bottom of your little waste basket, theres two or three, that look like theyve been through some horrible experience... tortured, interrogated, I dont know what happened to them. I once went out with a girl whos left a little zip-lock-baggy of cotton-balls over at my house. I dont know what to do with them, I took them out, I put them on my kitchen floor like little tumbleweeds. I thought maybe the cockroaches would see it, figure this is a dead town. Lets move on. The dating world is a world of pressure. Lets face it a date is a job interview that lasts all night. The only difference between a date and a job interview is not many job interviews is there a chance youll end up naked at the end of it. You know? Well, Bill, the boss thinks youre the man for the position, why dont you strip down and meet some of the people youll be workin with?"

This is one of Seinfeld's stand up routines, so I don't think it's, strictly speaking, a line, or at least not a line of dialog.

What about one word?

print(inspector.scripts.lines[words_per_line.line_counts.idxmin()])
Ha.

There's probably a lot of one word lines ("Yes", "No", etc.).

Plot the Words Per Line

plot = words_per_line.line_counts.hvplot.kde(title="Word Counts Per Line Distribution")
plotter = plot.opts(width=600, height=600, tools=["hover"])
Embed(plotter, "line_counts.js")()
plot = words_per_line.line_counts.hvplot.box(title="Words Per Line")
plot = plot.opts(tools=["hover"])
Embed(plot, "line_counts_boxplot.js")()

Most Used Words

>>>>>>> d51aea0b1ff0725156523a28363e1f7bc18d91e0

lines = ((token, "{:,}".format(count))
         for token, count in inspector.most_common_tokens())
print(tabulate(lines,
               tablefmt="orgtbl", headers=["Token", "Count"]))
Token Count
the 16,373
I 13,911
you 12,831
a 12,096
to 11,594
of 5,490
and 5,210
in 4,741
is 4,283
that 4,047

So it looks like the stop words are the most common, as you might expect.

words, counts = zip(*inspector.most_common_tokens(20))
top_twenty = pandas.DataFrame([counts], columns=words).T.reset_index()
top_twenty.columns = ["Word", "Count"]
layout = top_twenty.hvplot.bar(x="Word", y="Count",
                               title="Twenty Most Used Words",
                               colormap="Category20")
layout.opts(height=500, width=600)
Embed(layout, "top_twenty.js")()

The First five Lines

for line in inspector.line_range(stop=5):
    print(line)
"Do you know what this is all about? Do you know, why were here? To be out, this is out...and out is one of the single most enjoyable experiences of life. People...did you ever hear people talking about We should go out? This is what theyre talking about...this whole thing, were all out now, no one is home. Not one person here is home, were all out! There are people tryin to find us, they dont know where we are. (on an imaginary phone) Did you ring?, I cant find him. Where did he go? He didnt tell me where he was going. He must have gone out. You wanna go out you get ready, you pick out the clothes, right? You take the shower, you get all ready, get the cash, get your friends, the car, the spot, the reservation...Then youre standing around, whatta you do? You go We gotta be getting back. Once youre out, you wanna get back! You wanna go to sleep, you wanna get up, you wanna go out again tomorrow, right? Where ever you are in life, its my feeling, youve gotta go."
"(pointing at Georges shirt) See, to me, that button is in the worst possible spot. The second button literally makes or breaks the shirt, look at it. Its too high! Its in no-mans-land. You look like you live with your mother."
Are you through?
"You do of course try on, when you buy?"
"Yes, it was purple, I liked it, I dont actually recall considering the buttons."

I took out the header and the identifying columns so this is just the dialog part of the data. It looks like they left in all the punctuation except for apostrophes for some reason.

Pre-Processing the Text

The first thing to do to any dataset is pre-processing. Implement the following pre-processing functions below:

  • Lookup Table
  • Tokenize Punctuation

Lookup Table

To create a word embedding, you first need to transform the words to ids. In this function, create two dictionaries:

  • Dictionary to go from the words to an ID, we'll call it vocab_to_int
  • Dictionary to go from the ID to word, we'll call it int_to_vocab

Return these dictionaries in the following tuple (vocab_to_int, int_to_vocab)

def create_lookup_tables(text: list) -> tuple:
    """
    Create lookup tables for vocabulary

    Args:
     text The text of tv scripts split into words

    Returns: 
     A tuple of dicts (vocab_to_int, int_to_vocab)
    """
    text = set(text)
    vocabulary_to_index = {token: index for index, token in enumerate(text)}
    index_to_vocabulary = {index: token for index, token in enumerate(text)}
    return vocabulary_to_index, index_to_vocabulary
test_text = '''
Moe_Szyslak Moe's Tavern Where the elite meet to drink
Bart_Simpson Eh yeah hello is Mike there Last name Rotch
Moe_Szyslak Hold on I'll check Mike Rotch Mike Rotch Hey has anybody seen Mike Rotch lately
Moe_Szyslak Listen you little puke One of these days I'm gonna catch you and I'm gonna carve my name on your back with an ice pick
Moe_Szyslak Whats the matter Homer You're not your normal effervescent self
Homer_Simpson I got my problems Moe Give me another one
Moe_Szyslak Homer hey you should not drink to forget your problems
Barney_Gumble Yeah you should only drink to enhance your social skills'''
unittests.test_create_lookup_tables(create_lookup_tables)
Tests Passed

Tokenize Punctuation

We'll be splitting the script into a word array using spaces as delimiters. However, punctuations like periods and exclamation marks can create multiple ids for the same word. For example, "bye" and "bye!" would generate two different word ids.

Implement the function token_lookup to return a dict that will be used to tokenize symbols like "!" into "||Exclamation_Mark||". Create a dictionary for the following symbols where the symbol is the key and value is the token:

  • Period ( . )
  • Comma ( , )
  • Quotation Mark ( " )
  • Semicolon ( ; )
  • Exclamation mark ( ! )
  • Question mark ( ? )
  • Left Parentheses ( ( )
  • Right Parentheses ( ) )
  • Dash ( - )
  • Return ( \n )

This dictionary will be used to tokenize the symbols and add the delimiter (space) around it. This separates each symbols as its own word, making it easier for the neural network to predict the next word. Make sure you don't use a value that could be confused as a word; for example, instead of using the value "dash", try using something like "||dash||".

def token_lookup():
    """
    Generate a dict to turn punctuation into a token.

    Returns:
     Tokenized dictionary where the key is the punctuation and the value is the token
    """
    tokens = {'.': "period",
              ',': 'comma',
              '"': 'quotation',
              ';': 'semicolon',
              '!': 'exclamation',
              '?': 'question',
              '(': 'leftparenthesis',
              ')': 'rightparenthesis',
              '-': 'dash',
              '\n': 'newline'}
    return {token: '**{}**'.format(coded) for token,coded in tokens.items()}
unittests.test_tokenize(token_lookup)

Pre-process all the data and save it

Running the code cell below will pre-process all the data and save it to file. You're encouraged to look at the code for preprocess_and_save_data in the helpers.py file to see what it's doing in detail, but you do not need to change this code.

text = helper.load_data(inspector.scripts.path)
text = text[81:]
token_dict = token_lookup()
for key, token in token_dict.items():
    text = text.replace(key, ' {} '.format(token))
text = text.lower()
text = text.split()
vocab_to_int, int_to_vocab = create_lookup_tables(text + list(helper.SPECIAL_WORDS.values()))
int_text = [vocab_to_int[word] for word in text]
pre_processed = inspector.scripts.path.parent.joinpath('preprocess.pkl')
with pre_processed.open("wb") as writer:
    pickle.dump((int_text, vocab_to_int, int_to_vocab, token_dict), writer)

Check Point

This is your first checkpoint. If you ever decide to come back to this notebook or have to restart the notebook, you can start from here. The preprocessed data has been saved to disk.

pre_processed = inspector.scripts.path.parent.joinpath('preprocess.pkl')
with pre_processed.open("rb") as reader:
    int_text, vocab_to_int, int_to_vocab, token_dict = pickle.load(reader)

Act II - The Departure

Build the Neural Network

In this section, you'll build the components necessary to build an RNN by implementing the RNN Module and forward and backpropagation functions.

Input

Let's start with the preprocessed input data. We'll use TensorDataset to provide a known format to our dataset; in combination with DataLoader, it will handle batching, shuffling, and other dataset iteration functions.

You can create data with TensorDataset by passing in feature and target tensors. Then create a DataLoader as usual.

data = TensorDataset(feature_tensors, target_tensors)
data_loader = torch.utils.data.DataLoader(data, 
                                          batch_size=batch_size)

Batching

Implement the batch_data function to batch words data into chunks of size batch_size using the TensorDataset and DataLoader classes.

You can batch words using the DataLoader, but it will be up to you to create feature_tensors and target_tensors of the correct size and content for a given sequence_length.

For example, say we have these as input:

words = [1, 2, 3, 4, 5, 6, 7]
sequence_length = 4

Your first feature_tensor should contain the values:

[1, 2, 3, 4]

And the corresponding target_tensor should just be the next "word"/tokenized word value:

5

This should continue with the second feature_tensor, target_tensor being:

[2, 3, 4, 5]  # features
6             # target
def train_test_split(words: WordIndices, sequence_length: int) -> tuple:
    """Breaks the words into a training and a test set

    Args:
     words: the IDs of the TV scripts
     sequence_length: the sequence length of each training instance

    Returns:
     tuple of training tensors, target tensors
    """
    training, testing = [], []
    for start in range(len(words) - sequence_length):
        training.append(words[start:start+sequence_length])
        testing.append(words[start + sequence_length])
    return torch.Tensor(training), torch.Tensor(testing)
words = list(range(1, 8))
sequence_length = 4
training, testing = train_test_split(words, sequence_length)
assert training[0] == torch.Tensor([1, 2, 3, 4])
assert testing[0] == torch.Tensor(5)
assert training[1] == torch.Tensor([2, 3, 4, 5])
assert testing[1] == torch.Tensor(6)
assert training[2] == torch.Tensor([3, 4, 5, 6])
assert testing[2] == torch.Tensor(7)
assert len(training) == torch.Tensor(3)
assert len(testing) == torch.Tensor(3)
def batch_data(words: WordIndices, sequence_length: int, batch_size: int) -> DataLoader:
    """
    Batch the neural network data using DataLoader

    Args:
     - words: The word ids of the TV scripts
     - sequence_length: The sequence length of each batch
     - batch_size: The size of each batch; the number of sequences in a batch
    Returns: 
     DataLoader with batched data
    """
    training, target = train_test_split(words, sequence_length)
    data = TensorDataset(training, target)
    return DataLoader(data)

There is no test for this function, but you are encouraged to create tests of your own.

Test your dataloader

You'll have to modify this code to test a batching function, but it should look fairly similar.

Below, we're generating some test text data and defining a dataloader using the function you defined, above. Then, we are getting some sample batch of inputs `sample_x` and targets `sample_y` from our dataloader.

Your code should return something like the following (likely in a different order, if you shuffled your data):

torch.Size([10, 5])
tensor([[ 28,  29,  30,  31,  32],
        [ 21,  22,  23,  24,  25],
        [ 17,  18,  19,  20,  21],
        [ 34,  35,  36,  37,  38],
        [ 11,  12,  13,  14,  15],
        [ 23,  24,  25,  26,  27],
        [  6,   7,   8,   9,  10],
        [ 38,  39,  40,  41,  42],
        [ 25,  26,  27,  28,  29],
        [  7,   8,   9,  10,  11]])

torch.Size([10])
tensor([ 33,  26,  22,  39,  16,  28,  11,  43,  30,  12])

Sizes

Your sample_x should be of size `(batch_size, sequence_length)` or (10, 5) in this case and sample_y should just have one dimension: batch_size (10).

Values

You should also notice that the targets, sample_y, are the next value in the ordered test_text data. So, for an input sequence `[ 28, 29, 30, 31, 32]` that ends with the value `32`, the corresponding output should be `33`.

test_text = range(50)
t_loader = batch_data(test_text, sequence_length=5, batch_size=10)

data_iter = iter(t_loader)
sample_x, sample_y = data_iter.next()

print(sample_x.shape)
print(sample_x)
print()
print(sample_y.shape)
print(sample_y)

Build the Neural Network

Implement an RNN using PyTorch's [Module class](http://pytorch.org/docs/master/nn.html#torch.nn.Module). You may choose to use a GRU or an LSTM. To complete the RNN, you'll have to implement the following functions for the class:

  • `__init__` - The initialize function.
  • `init_hidden` - The initialization function for an LSTM/GRU hidden state
  • `forward` - Forward propagation function.

The initialize function should create the layers of the neural network and save them to the class. The forward propagation function will use these layers to run forward propagation and generate an output and a hidden state.

*The output of this model should be the *last batch of word scores** after a complete sequence has been processed. That is, for each input sequence of words, we only want to output the word scores for a single, most likely, next word.

Hints

  1. Make sure to stack the outputs of the lstm to pass to your fully-connected layer, you can do this with `lstm_output = lstm_output.contiguous().view(-1, self.hidden_dim)`
  2. You can get the last batch of word scores by shaping the output of the final, fully-connected layer like so:
# reshape into (batch_size, seq_length, output_size)
output = output.view(batch_size, -1, self.output_size)
# get last batch
out = output[:, -1]
import torch.nn as nn

class RNN(nn.Module):

    def __init__(self, vocab_size, output_size, embedding_dim, hidden_dim, n_layers, dropout=0.5):
        """
        Initialize the PyTorch RNN Module
        :param vocab_size: The number of input dimensions of the neural network (the size of the vocabulary)
        :param output_size: The number of output dimensions of the neural network
        :param embedding_dim: The size of embeddings, should you choose to use them        
        :param hidden_dim: The size of the hidden layer outputs
        :param dropout: dropout to add in between LSTM/GRU layers
        """
        super(RNN, self).__init__()
        # TODO: Implement function

        # set class variables

        # define model layers


    def forward(self, nn_input, hidden):
        """
        Forward propagation of the neural network
        :param nn_input: The input to the neural network
        :param hidden: The hidden state        
        :return: Two Tensors, the output of the neural network and the latest hidden state
        """
        # TODO: Implement function   

        # return one batch of output word scores and the hidden state
        return None, None


    def init_hidden(self, batch_size):
        '''
        Initialize the hidden state of an LSTM/GRU
        :param batch_size: The batch_size of the hidden state
        :return: hidden state of dims (n_layers, batch_size, hidden_dim)
        '''
        # Implement function

        # initialize hidden state with zero weights, and move to GPU if available

        return None

tests.test_rnn(RNN, train_on_gpu)

Define forward and backpropagation

Use the RNN class you implemented to apply forward and back propagation. This function will be called, iteratively, in the training loop as follows:

loss = forward_back_prop(decoder, decoder_optimizer, criterion, inp, target)

And it should return the average loss over a batch and the hidden state returned by a call to `RNN(inp, hidden)`. Recall that you can get this loss by computing it, as usual, and calling `loss.item()`.

If a GPU is available, you should move your data to that GPU device, here.

def forward_back_prop(rnn, optimizer, criterion, inp, target, hidden):
    """
    Forward and backward propagation on the neural network
    :param decoder: The PyTorch Module that holds the neural network
    :param decoder_optimizer: The PyTorch optimizer for the neural network
    :param criterion: The PyTorch loss function
    :param inp: A batch of input to the neural network
    :param target: The target output for the batch of input
    :return: The loss and the latest hidden state Tensor
    """

    # TODO: Implement Function

    # move data to GPU, if available

    # perform backpropagation and optimization

    # return the loss over a batch and the hidden state produced by our model
    return None, None

# Note that these tests aren't completely extensive.
# they are here to act as general checks on the expected outputs of your functions
"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_forward_back_prop(RNN, forward_back_prop, train_on_gpu)

Neural Network Training

With the structure of the network complete and data ready to be fed in the neural network, it's time to train it.

  • Train Loop

    The training loop is implemented for you in the `train_decoder` function. This function will train the network over all the batches for the number of epochs given. The model progress will be shown every number of batches. This number is set with the `show_every_n_batches` parameter. You'll set this parameter along with other parameters in the next section.

    def train_rnn(rnn, batch_size, optimizer, criterion, n_epochs, show_every_n_batches=100):
        batch_losses = []
    
        rnn.train()
    
        print("Training for %d epoch(s)..." % n_epochs)
        for epoch_i in range(1, n_epochs + 1):
    
            # initialize hidden state
            hidden = rnn.init_hidden(batch_size)
    
            for batch_i, (inputs, labels) in enumerate(train_loader, 1):
    
                # make sure you iterate over completely full batches, only
                n_batches = len(train_loader.dataset)//batch_size
                if(batch_i > n_batches):
                    break
    
                # forward, back prop
                loss, hidden = forward_back_prop(rnn, optimizer, criterion, inputs, labels, hidden)          
                # record loss
                batch_losses.append(loss)
    
                # printing loss stats
                if batch_i % show_every_n_batches == 0:
                    print('Epoch: {:>4}/{:<4}  Loss: {}\n'.format(
                        epoch_i, n_epochs, np.average(batch_losses)))
                    batch_losses = []
    
        # returns a trained rnn
        return rnn
    

Hyperparameters

Set and train the neural network with the following parameters:

  • Set `sequence_length` to the length of a sequence.
  • Set `batch_size` to the batch size.
  • Set `num_epochs` to the number of epochs to train for.
  • Set `learning_rate` to the learning rate for an Adam optimizer.
  • Set `vocab_size` to the number of unique tokens in our vocabulary.
  • Set `output_size` to the desired size of the output.
  • Set `embedding_dim` to the embedding dimension; smaller than the vocab_size.
  • Set `hidden_dim` to the hidden dimension of your RNN.
  • Set `n_layers` to the number of layers/cells in your RNN.
  • Set `show_every_n_batches` to the number of batches at which the neural network should print progress.

If the network isn't getting the desired results, tweak these parameters and/or the layers in the `RNN` class.

# Data params
# Sequence Length
sequence_length =   # of words in a sequence
# Batch Size
batch_size = 

# data loader - do not change
train_loader = batch_data(int_text, sequence_length, batch_size)

Training parameters

# Number of Epochs
num_epochs = 
# Learning Rate
learning_rate = 

# Model parameters
# Vocab size
vocab_size = 
# Output size
output_size = 
# Embedding Dimension
embedding_dim = 
# Hidden Dimension
hidden_dim = 
# Number of RNN Layers
n_layers = 

# Show stats for every n number of batches
show_every_n_batches = 500

Train

In the next cell, you'll train the neural network on the pre-processed data. If you have a hard time getting a good loss, you may consider changing your hyperparameters. In general, you may get better results with larger hidden and n_layer dimensions, but larger models take a longer time to train. > You should aim for a loss less than 3.5.

You should also experiment with different sequence lengths, which determine the size of the long range dependencies that a model can learn.

# create model and move to gpu if available
rnn = RNN(vocab_size, output_size, embedding_dim, hidden_dim, n_layers, dropout=0.5)
if train_on_gpu:
    rnn.cuda()

# defining loss and optimization functions for training
optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate)
criterion = nn.CrossEntropyLoss()

# training the model
trained_rnn = train_rnn(rnn, batch_size, optimizer, criterion, num_epochs, show_every_n_batches)

# saving the trained model
helper.save_model('./save/trained_rnn', trained_rnn)
print('Model Trained and Saved')

Question: How did you decide on your model hyperparameters?

For example, did you try different sequence_lengths and find that one size made the model converge faster? What about your hidden_dim and n_layers; how did you decide on those?

Answer: (Write answer, here)

Checkpoint

After running the above training cell, your model will be saved by name, `trained_rnn`, and if you save your notebook progress, you can pause here and come back to this code at another time. You can resume your progress by running the next cell, which will load in our word:id dictionaries and load in your saved model by name!

import torch
import helper
import problem_unittests as tests

_, vocab_to_int, int_to_vocab, token_dict = helper.load_preprocess()
trained_rnn = helper.load_model('./save/trained_rnn')

Act III - The Final Battle

Generate TV Script

With the network trained and saved, you'll use it to generate a new, "fake" Seinfeld TV script in this section.

Generate Text

To generate the text, the network needs to start with a single word and repeat its predictions until it reaches a set length. You'll be using the `generate` function to do this. It takes a word id to start with, `prime_id`, and generates a set length of text, `predict_len`. Also note that it uses topk sampling to introduce some randomness in choosing the most likely next word, given an output set of word scores!

import torch.nn.functional as F

def generate(rnn, prime_id, int_to_vocab, token_dict, pad_value, predict_len=100):
    """
    Generate text using the neural network
    :param decoder: The PyTorch Module that holds the trained neural network
    :param prime_id: The word id to start the first prediction
    :param int_to_vocab: Dict of word id keys to word values
    :param token_dict: Dict of puncuation tokens keys to puncuation values
    :param pad_value: The value used to pad a sequence
    :param predict_len: The length of text to generate
    :return: The generated text
    """
    rnn.eval()

    # create a sequence (batch_size=1) with the prime_id
    current_seq = np.full((1, sequence_length), pad_value)
    current_seq[-1][-1] = prime_id
    predicted = [int_to_vocab[prime_id]]

    for _ in range(predict_len):
        if train_on_gpu:
            current_seq = torch.LongTensor(current_seq).cuda()
        else:
            current_seq = torch.LongTensor(current_seq)

        # initialize the hidden state
        hidden = rnn.init_hidden(current_seq.size(0))

        # get the output of the rnn
        output, _ = rnn(current_seq, hidden)

        # get the next word probabilities
        p = F.softmax(output, dim=1).data
        if(train_on_gpu):
            p = p.cpu() # move to cpu

        # use top_k sampling to get the index of the next word
        top_k = 5
        p, top_i = p.topk(top_k)
        top_i = top_i.numpy().squeeze()

        # select the likely next word index with some element of randomness
        p = p.numpy().squeeze()
        word_i = np.random.choice(top_i, p=p/p.sum())

        # retrieve that word from the dictionary
        word = int_to_vocab[word_i]
        predicted.append(word)     

        # the generated word becomes the next "current sequence" and the cycle can continue
        current_seq = np.roll(current_seq, -1, 1)
        current_seq[-1][-1] = word_i

    gen_sentences = ' '.join(predicted)

    # Replace punctuation tokens
    for key, token in token_dict.items():
        ending = ' ' if key in ['\n', '(', '"'] else ''
        gen_sentences = gen_sentences.replace(' ' + token.lower(), key)
    gen_sentences = gen_sentences.replace('\n ', '\n')
    gen_sentences = gen_sentences.replace('( ', '(')

    # return all the sentences
    return gen_sentences

Generate a New Script

It's time to generate the text. Set `gen_length` to the length of TV script you want to generate and set `prime_word` to one of the following to start the prediction:

  • "jerry"
  • "elaine"
  • "george"
  • "kramer"

You can set the prime word to any word in our dictionary, but it's best to start with a name for generating a TV script. (You can also start with any other names you find in the original text file!)

# run the cell multiple times to get different results!
gen_length = 400 # modify the length to your preference
prime_word = 'jerry' # name for starting the script

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
pad_word = helper.SPECIAL_WORDS['PADDING']
generated_script = generate(trained_rnn, vocab_to_int[prime_word + ':'], int_to_vocab, token_dict, vocab_to_int[pad_word], gen_length)
print(generated_script)

Save your favorite scripts

Once you have a script that you like (or find interesting), save it to a text file!

# save script to a text file
f =  open("generated_script_1.txt","w")
f.write(generated_script)
f.close()

The TV Script is Not Perfect

It's ok if the TV script doesn't make perfect sense. It should look like alternating lines of dialogue, here is one such example of a few generated lines.

Example generated script

>jerry: what about me? > >jerry: i don't have to wait. > >kramer:(to the sales table) > >elaine:(to jerry) hey, look at this, i'm a good doctor. > >newman:(to elaine) you think i have no idea of this… > >elaine: oh, you better take the phone, and he was a little nervous. > >kramer:(to the phone) hey, hey, jerry, i don't want to be a little bit.(to kramer and jerry) you can't. > >jerry: oh, yeah. i don't even know, i know. > >jerry:(to the phone) oh, i know. > >kramer:(laughing) you know…(to jerry) you don't know.

You can see that there are multiple characters that say (somewhat) complete sentences, but it doesn't have to be perfect! It takes quite a while to get good results, and often, you'll have to use a smaller vocabulary (and discard uncommon words), or get more data. The Seinfeld dataset is about 3.4 MB, which is big enough for our purposes; for script generation you'll want more than 1 MB of text, generally.

Submitting This Project

When submitting this project, make sure to run all the cells before saving the notebook. Save the notebook file as "dlnd_tv_script_generation.ipynb" and save another copy as an HTML file by clicking "File" -> "Download as.."->"html". Include the "helper.py" and "problem_unittests.py" files in your submission. Once you download these files, compress them into one zip file for submission.