TV Script Generation

In this project, you'll generate your own Simpsons TV scripts using RNNs. You'll be using part of the Simpsons dataset of scripts from 27 seasons. The Neural Network you'll build will generate a new TV script for a scene at Moe's Tavern.

Get the Data

The data is already provided for you. You'll be using a subset of the original dataset. It consists of only the scenes in Moe's Tavern. This doesn't include other versions of the tavern, like "Moe's Cavern", "Flaming Moe's", "Uncle Moe's Family Feed-Bag", etc..


In [1]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import helper

data_dir = './data/simpsons/moes_tavern_lines.txt'
text = helper.load_data(data_dir)
# Ignore notice, since we don't use it for analysing the data
text = text[81:]

In [2]:
text.split()[199:205]


Out[2]:
['Barney_Gumble:', 'So', 'the', 'next', 'time', 'somebody']

Explore the Data

Play around with view_sentence_range to view different parts of the data.


In [3]:
view_sentence_range = (0, 10)

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np

print('Dataset Stats')
print('Roughly the number of unique words: {}'.format(len({word: None for word in text.split()})))
scenes = text.split('\n\n')
print('Number of scenes: {}'.format(len(scenes)))
sentence_count_scene = [scene.count('\n') for scene in scenes]
print('Average number of sentences in each scene: {}'.format(np.average(sentence_count_scene)))

sentences = [sentence for scene in scenes for sentence in scene.split('\n')]
print('Number of lines: {}'.format(len(sentences)))
word_count_sentence = [len(sentence.split()) for sentence in sentences]
print('Average number of words in each line: {}'.format(np.average(word_count_sentence)))

print()
print('The sentences {} to {}:'.format(*view_sentence_range))
print('\n'.join(text.split('\n')[view_sentence_range[0]:view_sentence_range[1]]))


Dataset Stats
Roughly the number of unique words: 11492
Number of scenes: 262
Average number of sentences in each scene: 15.248091603053435
Number of lines: 4257
Average number of words in each line: 11.50434578341555

The sentences 0 to 10:
Moe_Szyslak: (INTO PHONE) Moe's Tavern. Where the elite meet to drink.
Bart_Simpson: Eh, yeah, hello, is Mike there? Last name, Rotch.
Moe_Szyslak: (INTO PHONE) Hold on, I'll check. (TO BARFLIES) Mike Rotch. Mike Rotch. Hey, has anybody seen Mike Rotch, lately?
Moe_Szyslak: (INTO PHONE) 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: What's 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.


Implement Preprocessing Functions

The first thing to do to any dataset is preprocessing. Implement the following preprocessing 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 vocab_to_int
  • Dictionary to go from the id to word, we'll call int_to_vocab

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


In [4]:
import numpy as np
import problem_unittests as tests
from collections import Counter 

def create_lookup_tables(text):
    """
    Create lookup tables for vocabulary
    :param text: The text of tv scripts split into words
    :return: A tuple of dicts (vocab_to_int, int_to_vocab)
    """
    # TODO: Implement Function
    words = Counter (text).most_common()
    word_freq = {t[0] : t[1] for t in words}
    vocab_to_int = {w[0] : i for i, w in enumerate(words, 0)}
    int_to_vocab = {i : w[0] for i, w in enumerate(words, 0)}
    
#    for i in sorted(int_to_vocab.keys(), reverse=False):
#        print (i, int_to_vocab[i], word_freq[int_to_vocab[i]])
        
    return vocab_to_int, int_to_vocab


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.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 make it hard for the neural network to distinguish between the word "bye" and "bye!".

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 token the symbols and add the delimiter (space) around it. This separates the symbols as it's own word, making it easier for the neural network to predict on the next word. Make sure you don't use a token that could be confused as a word. Instead of using the token "dash", try using something like "||dash||".


In [5]:
def token_lookup():
    """
    Generate a dict to turn punctuation into a token.
    :return: Tokenize dictionary where the key is the punctuation and the value is the token
    """
    # TODO: Implement Function
    return {
        '.'  : '||period||',
        ','  : '||comma||',
        '"'  : '||quotation||',
        ';'  : '||semicolon||',
        '!'  : '||exclamation||',
        '?'  : '||question||',
        '('  : '||left-parentheses||',
        ')'  : '||right-parentheses||',
        '--' : '||dash||',
        '\n' : '||return||',
    }

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_tokenize(token_lookup)


Tests Passed

Preprocess all the data and save it

Running the code cell below will preprocess all the data and save it to file.


In [6]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
# Preprocess Training, Validation, and Testing Data
helper.preprocess_and_save_data(data_dir, token_lookup, create_lookup_tables)

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.


In [7]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import helper
import numpy as np
import problem_unittests as tests

int_text, vocab_to_int, int_to_vocab, token_dict = helper.load_preprocess()

Build the Neural Network

You'll build the components necessary to build a RNN by implementing the following functions below:

  • get_inputs
  • get_init_cell
  • get_embed
  • build_rnn
  • build_nn
  • get_batches

Check the Version of TensorFlow and Access to GPU


In [8]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
from distutils.version import LooseVersion
import warnings
import tensorflow as tf

# Check TensorFlow Version
assert LooseVersion(tf.__version__) >= LooseVersion('1.0'), 'Please use TensorFlow version 1.0 or newer'
print('TensorFlow Version: {}'.format(tf.__version__))

# Check for a GPU
if not tf.test.gpu_device_name():
    warnings.warn('No GPU found. Please use a GPU to train your neural network.')
else:
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))


TensorFlow Version: 1.0.1
/Users/sneg/tools/miniconda2/envs/deeplearning/lib/python3.5/site-packages/ipykernel/__main__.py:14: UserWarning: No GPU found. Please use a GPU to train your neural network.

Input

Implement the get_inputs() function to create TF Placeholders for the Neural Network. It should create the following placeholders:

  • Input text placeholder named "input" using the TF Placeholder name parameter.
  • Targets placeholder
  • Learning Rate placeholder

Return the placeholders in the following tuple (Input, Targets, LearningRate)


In [9]:
def get_inputs():
    """
    Create TF Placeholders for input, targets, and learning rate.
    :return: Tuple (input, targets, learning rate)
    """
    # TODO: Implement Function
    input = tf.placeholder (dtype=tf.int32, shape=[None, None], name='input')
    targets = tf.placeholder (dtype=tf.int32, shape=[None, None], name='targets')
    learning_rate = tf.placeholder (dtype=tf.float32, name='learning_rate')
    return input, targets, learning_rate


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_get_inputs(get_inputs)


Tests Passed

Build RNN Cell and Initialize

Stack one or more BasicLSTMCells in a MultiRNNCell.

  • The Rnn size should be set using rnn_size
  • Initalize Cell State using the MultiRNNCell's zero_state() function
    • Apply the name "initial_state" to the initial state using tf.identity()

Return the cell and initial state in the following tuple (Cell, InitialState)


In [10]:
def get_init_cell(batch_size, rnn_size):
    """
    Create an RNN Cell and initialize it.
    :param batch_size: Size of batches
    :param rnn_size: Size of RNNs
    :return: Tuple (cell, initialize state)
    """
    # TODO: Implement Function
    lstm_layers = 1
    lstm = tf.contrib.rnn.BasicLSTMCell (rnn_size)
    cell = tf.contrib.rnn.MultiRNNCell ([lstm] * lstm_layers)
    initial_state = tf.identity (cell.zero_state (batch_size, dtype=tf.float32), name='initial_state')
    return cell, initial_state


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_get_init_cell(get_init_cell)


Tests Passed

Word Embedding

Apply embedding to input_data using TensorFlow. Return the embedded sequence.


In [11]:
def get_embed(input_data, vocab_size, embed_dim):
    """
    Create embedding for <input_data>.
    :param input_data: TF placeholder for text input.
    :param vocab_size: Number of words in vocabulary.
    :param embed_dim: Number of embedding dimensions
    :return: Embedded input.
    """
    # TODO: Implement Function
    embedding = tf.Variable (tf.random_uniform([vocab_size, embed_dim], -1, 1, dtype=tf.float32), name='embedding')
    embed = tf.nn.embedding_lookup (embedding, input_data)
    return embed


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_get_embed(get_embed)


Tests Passed

Build RNN

You created a RNN Cell in the get_init_cell() function. Time to use the cell to create a RNN.

Return the outputs and final_state state in the following tuple (Outputs, FinalState)


In [12]:
def build_rnn(cell, inputs):
    """
    Create a RNN using a RNN Cell
    :param cell: RNN Cell
    :param inputs: Input text data
    :return: Tuple (Outputs, Final State)
    """
    # TODO: Implement Function
    outputs, final_state = tf.nn.dynamic_rnn (cell, inputs, dtype=tf.float32)
    final_state = tf.identity (final_state, name='final_state')
    return outputs, final_state


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_build_rnn(build_rnn)


Tests Passed

Build the Neural Network

Apply the functions you implemented above to:

  • Apply embedding to input_data using your get_embed(input_data, vocab_size, embed_dim) function.
  • Build RNN using cell and your build_rnn(cell, inputs) function.
  • Apply a fully connected layer with a linear activation and vocab_size as the number of outputs.

Return the logits and final state in the following tuple (Logits, FinalState)


In [13]:
def build_nn(cell, rnn_size, input_data, vocab_size, embed_dim):
    """
    Build part of the neural network
    :param cell: RNN cell
    :param rnn_size: Size of rnns
    :param input_data: Input data
    :param vocab_size: Vocabulary size
    :param embed_dim: Number of embedding dimensions
    :return: Tuple (Logits, FinalState)
    """
    # TODO: Implement Function
    embedding = get_embed (input_data, vocab_size, embed_dim)
    outputs, final_state = build_rnn (cell, embedding)
    logits = tf.contrib.layers.fully_connected (outputs, vocab_size, activation_fn=None)
    return logits, final_state


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_build_nn(build_nn)


Tests Passed

Batches

Implement get_batches to create batches of input and targets using int_text. The batches should be a Numpy array with the shape (number of batches, 2, batch size, sequence length). Each batch contains two elements:

  • The first element is a single batch of input with the shape [batch size, sequence length]
  • The second element is a single batch of targets with the shape [batch size, sequence length]

If you can't fill the last batch with enough data, drop the last batch.

For exmple, get_batches([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 3, 2) would return a Numpy array of the following:

[
  # First Batch
  [
    # Batch of Input
    [[ 1  2], [ 7  8], [13 14]]
    # Batch of targets
    [[ 2  3], [ 8  9], [14 15]]
  ]

  # Second Batch
  [
    # Batch of Input
    [[ 3  4], [ 9 10], [15 16]]
    # Batch of targets
    [[ 4  5], [10 11], [16 17]]
  ]

  # Third Batch
  [
    # Batch of Input
    [[ 5  6], [11 12], [17 18]]
    # Batch of targets
    [[ 6  7], [12 13], [18  1]]
  ]
]

Notice that the last target value in the last batch is the first input value of the first batch. In this case, 1. This is a common technique used when creating sequence batches, although it is rather unintuitive.


In [14]:
def get_batches(int_text, batch_size, seq_length):
    """
    Return batches of input and target
    :param int_text: Text with the words replaced by their ids
    :param batch_size: The size of batch
    :param seq_length: The length of sequence
    :return: Batches as a Numpy array
    """
    # TODO: Implement Function
    words_in_batch = batch_size * seq_length
    num_batches = (len(int_text) - 1) // words_in_batch
    
    print ('Text length = {}\nBatch size = {}\nNumber of batches = {}\nBatch clipping loss = {}'.format (len(int_text), words_in_batch, num_batches, len(int_text) - num_batches*words_in_batch))
    
    x, y = int_text[:num_batches*words_in_batch], int_text[1:num_batches*words_in_batch+1]
    
    batches = np.empty ([num_batches, 2, batch_size, seq_length], dtype=np.int32)
    
    for i in range(num_batches):
        batches[i][0] = np.reshape (int_text[i*words_in_batch:(i+1)*words_in_batch], (batch_size, seq_length))
        batches[i][1] = np.reshape (int_text[i*words_in_batch+1:(i+1)*words_in_batch+1], (batch_size, seq_length))
        
    return batches


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_get_batches(get_batches)


Text length = 5000
Batch size = 640
Number of batches = 7
Batch clipping loss = 520
Tests Passed

Neural Network Training

Hyperparameters

Tune the following parameters:

  • Set num_epochs to the number of epochs.
  • Set batch_size to the batch size.
  • Set rnn_size to the size of the RNNs.
  • Set embed_dim to the size of the embedding.
  • Set seq_length to the length of sequence.
  • Set learning_rate to the learning rate.
  • Set show_every_n_batches to the number of batches the neural network should print progress.

In [15]:
# Number of Epochs
num_epochs = 800
# Batch Size
batch_size = 512
# RNN Size
rnn_size = 256
# Embedding Dimension Size
embed_dim = 300
# Sequence Length
seq_length = 13
# Learning Rate
learning_rate = 0.001
# Show stats for every n number of batches
show_every_n_batches = 11

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
save_dir = './save'

Build the Graph

Build the graph using the neural network you implemented.


In [16]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
from tensorflow.contrib import seq2seq

train_graph = tf.Graph()
with train_graph.as_default():
    vocab_size = len(int_to_vocab)
    input_text, targets, lr = get_inputs()
    input_data_shape = tf.shape(input_text)
    cell, initial_state = get_init_cell(input_data_shape[0], rnn_size)
    logits, final_state = build_nn(cell, rnn_size, input_text, vocab_size, embed_dim)

    # Probabilities for generating words
    probs = tf.nn.softmax(logits, name='probs')

    # Loss function
    cost = seq2seq.sequence_loss(
        logits,
        targets,
        tf.ones([input_data_shape[0], input_data_shape[1]]))

    # Optimizer
    optimizer = tf.train.AdamOptimizer(lr)

    # Gradient Clipping
    gradients = optimizer.compute_gradients(cost)
    capped_gradients = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gradients if grad is not None]
    train_op = optimizer.apply_gradients(capped_gradients)

Train

Train the neural network on the preprocessed data. If you have a hard time getting a good loss, check the forms to see if anyone is having the same problem.


In [17]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
batches = get_batches(int_text, batch_size, seq_length)
#print ('Max in text = {}'.format (max(set(int_text))))
#print ('Max word = {}'.format (int_to_vocab[6778]))
#print ('{}'.format ([(i,w) for i,w in int_to_vocab.items()][:20]))

with tf.Session(graph=train_graph) as sess:
    sess.run(tf.global_variables_initializer())

    for epoch_i in range(num_epochs):
        state = sess.run(initial_state, {input_text: batches[0][0]})

        for batch_i, (x, y) in enumerate(batches):
            feed = {
                input_text: x,
                targets: y,
                initial_state: state,
                lr: learning_rate}
            train_loss, state, _ = sess.run([cost, final_state, train_op], feed)

            # Show every <show_every_n_batches> batches
            if (epoch_i * len(batches) + batch_i) % show_every_n_batches == 0:
                print('Epoch {:>3} Batch {:>4}/{}   train_loss = {:.3f}'.format(
                    epoch_i,
                    batch_i,
                    len(batches),
                    train_loss))

    # Save Model
    saver = tf.train.Saver()
    saver.save(sess, save_dir)
    print('Model Trained and Saved')


Text length = 69100
Batch size = 6656
Number of batches = 10
Batch clipping loss = 2540
Epoch   0 Batch    0/10   train_loss = 8.824
Epoch   1 Batch    1/10   train_loss = 7.409
Epoch   2 Batch    2/10   train_loss = 6.289
Epoch   3 Batch    3/10   train_loss = 6.048
Epoch   4 Batch    4/10   train_loss = 5.978
Epoch   5 Batch    5/10   train_loss = 5.994
Epoch   6 Batch    6/10   train_loss = 5.881
Epoch   7 Batch    7/10   train_loss = 5.832
Epoch   8 Batch    8/10   train_loss = 5.773
Epoch   9 Batch    9/10   train_loss = 5.622
Epoch  11 Batch    0/10   train_loss = 5.395
Epoch  12 Batch    1/10   train_loss = 5.295
Epoch  13 Batch    2/10   train_loss = 5.275
Epoch  14 Batch    3/10   train_loss = 5.084
Epoch  15 Batch    4/10   train_loss = 5.032
Epoch  16 Batch    5/10   train_loss = 5.123
Epoch  17 Batch    6/10   train_loss = 5.038
Epoch  18 Batch    7/10   train_loss = 5.069
Epoch  19 Batch    8/10   train_loss = 5.033
Epoch  20 Batch    9/10   train_loss = 4.910
Epoch  22 Batch    0/10   train_loss = 4.713
Epoch  23 Batch    1/10   train_loss = 4.646
Epoch  24 Batch    2/10   train_loss = 4.666
Epoch  25 Batch    3/10   train_loss = 4.504
Epoch  26 Batch    4/10   train_loss = 4.485
Epoch  27 Batch    5/10   train_loss = 4.587
Epoch  28 Batch    6/10   train_loss = 4.525
Epoch  29 Batch    7/10   train_loss = 4.583
Epoch  30 Batch    8/10   train_loss = 4.560
Epoch  31 Batch    9/10   train_loss = 4.458
Epoch  33 Batch    0/10   train_loss = 4.284
Epoch  34 Batch    1/10   train_loss = 4.238
Epoch  35 Batch    2/10   train_loss = 4.275
Epoch  36 Batch    3/10   train_loss = 4.123
Epoch  37 Batch    4/10   train_loss = 4.116
Epoch  38 Batch    5/10   train_loss = 4.206
Epoch  39 Batch    6/10   train_loss = 4.150
Epoch  40 Batch    7/10   train_loss = 4.207
Epoch  41 Batch    8/10   train_loss = 4.190
Epoch  42 Batch    9/10   train_loss = 4.112
Epoch  44 Batch    0/10   train_loss = 3.955
Epoch  45 Batch    1/10   train_loss = 3.920
Epoch  46 Batch    2/10   train_loss = 3.956
Epoch  47 Batch    3/10   train_loss = 3.819
Epoch  48 Batch    4/10   train_loss = 3.820
Epoch  49 Batch    5/10   train_loss = 3.892
Epoch  50 Batch    6/10   train_loss = 3.839
Epoch  51 Batch    7/10   train_loss = 3.888
Epoch  52 Batch    8/10   train_loss = 3.871
Epoch  53 Batch    9/10   train_loss = 3.813
Epoch  55 Batch    0/10   train_loss = 3.676
Epoch  56 Batch    1/10   train_loss = 3.649
Epoch  57 Batch    2/10   train_loss = 3.674
Epoch  58 Batch    3/10   train_loss = 3.560
Epoch  59 Batch    4/10   train_loss = 3.564
Epoch  60 Batch    5/10   train_loss = 3.621
Epoch  61 Batch    6/10   train_loss = 3.567
Epoch  62 Batch    7/10   train_loss = 3.611
Epoch  63 Batch    8/10   train_loss = 3.587
Epoch  64 Batch    9/10   train_loss = 3.546
Epoch  66 Batch    0/10   train_loss = 3.424
Epoch  67 Batch    1/10   train_loss = 3.410
Epoch  68 Batch    2/10   train_loss = 3.419
Epoch  69 Batch    3/10   train_loss = 3.326
Epoch  70 Batch    4/10   train_loss = 3.333
Epoch  71 Batch    5/10   train_loss = 3.377
Epoch  72 Batch    6/10   train_loss = 3.318
Epoch  73 Batch    7/10   train_loss = 3.361
Epoch  74 Batch    8/10   train_loss = 3.328
Epoch  75 Batch    9/10   train_loss = 3.303
Epoch  77 Batch    0/10   train_loss = 3.201
Epoch  78 Batch    1/10   train_loss = 3.195
Epoch  79 Batch    2/10   train_loss = 3.196
Epoch  80 Batch    3/10   train_loss = 3.126
Epoch  81 Batch    4/10   train_loss = 3.128
Epoch  82 Batch    5/10   train_loss = 3.166
Epoch  83 Batch    6/10   train_loss = 3.102
Epoch  84 Batch    7/10   train_loss = 3.144
Epoch  85 Batch    8/10   train_loss = 3.103
Epoch  86 Batch    9/10   train_loss = 3.088
Epoch  88 Batch    0/10   train_loss = 3.002
Epoch  89 Batch    1/10   train_loss = 2.997
Epoch  90 Batch    2/10   train_loss = 2.985
Epoch  91 Batch    3/10   train_loss = 2.936
Epoch  92 Batch    4/10   train_loss = 2.939
Epoch  93 Batch    5/10   train_loss = 2.966
Epoch  94 Batch    6/10   train_loss = 2.903
Epoch  95 Batch    7/10   train_loss = 2.944
Epoch  96 Batch    8/10   train_loss = 2.900
Epoch  97 Batch    9/10   train_loss = 2.897
Epoch  99 Batch    0/10   train_loss = 2.828
Epoch 100 Batch    1/10   train_loss = 2.831
Epoch 101 Batch    2/10   train_loss = 2.808
Epoch 102 Batch    3/10   train_loss = 2.776
Epoch 103 Batch    4/10   train_loss = 2.779
Epoch 104 Batch    5/10   train_loss = 2.794
Epoch 105 Batch    6/10   train_loss = 2.728
Epoch 106 Batch    7/10   train_loss = 2.764
Epoch 107 Batch    8/10   train_loss = 2.717
Epoch 108 Batch    9/10   train_loss = 2.718
Epoch 110 Batch    0/10   train_loss = 2.664
Epoch 111 Batch    1/10   train_loss = 2.671
Epoch 112 Batch    2/10   train_loss = 2.647
Epoch 113 Batch    3/10   train_loss = 2.628
Epoch 114 Batch    4/10   train_loss = 2.626
Epoch 115 Batch    5/10   train_loss = 2.635
Epoch 116 Batch    6/10   train_loss = 2.568
Epoch 117 Batch    7/10   train_loss = 2.602
Epoch 118 Batch    8/10   train_loss = 2.551
Epoch 119 Batch    9/10   train_loss = 2.555
Epoch 121 Batch    0/10   train_loss = 2.512
Epoch 122 Batch    1/10   train_loss = 2.523
Epoch 123 Batch    2/10   train_loss = 2.488
Epoch 124 Batch    3/10   train_loss = 2.482
Epoch 125 Batch    4/10   train_loss = 2.484
Epoch 126 Batch    5/10   train_loss = 2.481
Epoch 127 Batch    6/10   train_loss = 2.418
Epoch 128 Batch    7/10   train_loss = 2.449
Epoch 129 Batch    8/10   train_loss = 2.397
Epoch 130 Batch    9/10   train_loss = 2.406
Epoch 132 Batch    0/10   train_loss = 2.373
Epoch 133 Batch    1/10   train_loss = 2.386
Epoch 134 Batch    2/10   train_loss = 2.345
Epoch 135 Batch    3/10   train_loss = 2.347
Epoch 136 Batch    4/10   train_loss = 2.346
Epoch 137 Batch    5/10   train_loss = 2.338
Epoch 138 Batch    6/10   train_loss = 2.275
Epoch 139 Batch    7/10   train_loss = 2.301
Epoch 140 Batch    8/10   train_loss = 2.246
Epoch 141 Batch    9/10   train_loss = 2.257
Epoch 143 Batch    0/10   train_loss = 2.240
Epoch 144 Batch    1/10   train_loss = 2.253
Epoch 145 Batch    2/10   train_loss = 2.207
Epoch 146 Batch    3/10   train_loss = 2.218
Epoch 147 Batch    4/10   train_loss = 2.217
Epoch 148 Batch    5/10   train_loss = 2.196
Epoch 149 Batch    6/10   train_loss = 2.136
Epoch 150 Batch    7/10   train_loss = 2.161
Epoch 151 Batch    8/10   train_loss = 2.105
Epoch 152 Batch    9/10   train_loss = 2.125
Epoch 154 Batch    0/10   train_loss = 2.118
Epoch 155 Batch    1/10   train_loss = 2.131
Epoch 156 Batch    2/10   train_loss = 2.075
Epoch 157 Batch    3/10   train_loss = 2.090
Epoch 158 Batch    4/10   train_loss = 2.087
Epoch 159 Batch    5/10   train_loss = 2.062
Epoch 160 Batch    6/10   train_loss = 2.006
Epoch 161 Batch    7/10   train_loss = 2.031
Epoch 162 Batch    8/10   train_loss = 1.975
Epoch 163 Batch    9/10   train_loss = 1.994
Epoch 165 Batch    0/10   train_loss = 1.998
Epoch 166 Batch    1/10   train_loss = 2.006
Epoch 167 Batch    2/10   train_loss = 1.956
Epoch 168 Batch    3/10   train_loss = 1.982
Epoch 169 Batch    4/10   train_loss = 1.979
Epoch 170 Batch    5/10   train_loss = 1.938
Epoch 171 Batch    6/10   train_loss = 1.880
Epoch 172 Batch    7/10   train_loss = 1.903
Epoch 173 Batch    8/10   train_loss = 1.853
Epoch 174 Batch    9/10   train_loss = 1.879
Epoch 176 Batch    0/10   train_loss = 1.891
Epoch 177 Batch    1/10   train_loss = 1.891
Epoch 178 Batch    2/10   train_loss = 1.831
Epoch 179 Batch    3/10   train_loss = 1.861
Epoch 180 Batch    4/10   train_loss = 1.861
Epoch 181 Batch    5/10   train_loss = 1.818
Epoch 182 Batch    6/10   train_loss = 1.761
Epoch 183 Batch    7/10   train_loss = 1.777
Epoch 184 Batch    8/10   train_loss = 1.724
Epoch 185 Batch    9/10   train_loss = 1.755
Epoch 187 Batch    0/10   train_loss = 1.786
Epoch 188 Batch    1/10   train_loss = 1.784
Epoch 189 Batch    2/10   train_loss = 1.737
Epoch 190 Batch    3/10   train_loss = 1.765
Epoch 191 Batch    4/10   train_loss = 1.757
Epoch 192 Batch    5/10   train_loss = 1.706
Epoch 193 Batch    6/10   train_loss = 1.655
Epoch 194 Batch    7/10   train_loss = 1.674
Epoch 195 Batch    8/10   train_loss = 1.621
Epoch 196 Batch    9/10   train_loss = 1.648
Epoch 198 Batch    0/10   train_loss = 1.675
Epoch 199 Batch    1/10   train_loss = 1.669
Epoch 200 Batch    2/10   train_loss = 1.620
Epoch 201 Batch    3/10   train_loss = 1.658
Epoch 202 Batch    4/10   train_loss = 1.651
Epoch 203 Batch    5/10   train_loss = 1.599
Epoch 204 Batch    6/10   train_loss = 1.555
Epoch 205 Batch    7/10   train_loss = 1.569
Epoch 206 Batch    8/10   train_loss = 1.520
Epoch 207 Batch    9/10   train_loss = 1.552
Epoch 209 Batch    0/10   train_loss = 1.592
Epoch 210 Batch    1/10   train_loss = 1.585
Epoch 211 Batch    2/10   train_loss = 1.528
Epoch 212 Batch    3/10   train_loss = 1.563
Epoch 213 Batch    4/10   train_loss = 1.564
Epoch 214 Batch    5/10   train_loss = 1.509
Epoch 215 Batch    6/10   train_loss = 1.459
Epoch 216 Batch    7/10   train_loss = 1.464
Epoch 217 Batch    8/10   train_loss = 1.414
Epoch 218 Batch    9/10   train_loss = 1.441
Epoch 220 Batch    0/10   train_loss = 1.482
Epoch 221 Batch    1/10   train_loss = 1.472
Epoch 222 Batch    2/10   train_loss = 1.428
Epoch 223 Batch    3/10   train_loss = 1.476
Epoch 224 Batch    4/10   train_loss = 1.468
Epoch 225 Batch    5/10   train_loss = 1.405
Epoch 226 Batch    6/10   train_loss = 1.363
Epoch 227 Batch    7/10   train_loss = 1.369
Epoch 228 Batch    8/10   train_loss = 1.329
Epoch 229 Batch    9/10   train_loss = 1.360
Epoch 231 Batch    0/10   train_loss = 1.404
Epoch 232 Batch    1/10   train_loss = 1.396
Epoch 233 Batch    2/10   train_loss = 1.347
Epoch 234 Batch    3/10   train_loss = 1.396
Epoch 235 Batch    4/10   train_loss = 1.392
Epoch 236 Batch    5/10   train_loss = 1.335
Epoch 237 Batch    6/10   train_loss = 1.291
Epoch 238 Batch    7/10   train_loss = 1.287
Epoch 239 Batch    8/10   train_loss = 1.240
Epoch 240 Batch    9/10   train_loss = 1.269
Epoch 242 Batch    0/10   train_loss = 1.317
Epoch 243 Batch    1/10   train_loss = 1.305
Epoch 244 Batch    2/10   train_loss = 1.261
Epoch 245 Batch    3/10   train_loss = 1.311
Epoch 246 Batch    4/10   train_loss = 1.301
Epoch 247 Batch    5/10   train_loss = 1.240
Epoch 248 Batch    6/10   train_loss = 1.206
Epoch 249 Batch    7/10   train_loss = 1.208
Epoch 250 Batch    8/10   train_loss = 1.164
Epoch 251 Batch    9/10   train_loss = 1.197
Epoch 253 Batch    0/10   train_loss = 1.255
Epoch 254 Batch    1/10   train_loss = 1.250
Epoch 255 Batch    2/10   train_loss = 1.194
Epoch 256 Batch    3/10   train_loss = 1.236
Epoch 257 Batch    4/10   train_loss = 1.224
Epoch 258 Batch    5/10   train_loss = 1.163
Epoch 259 Batch    6/10   train_loss = 1.133
Epoch 260 Batch    7/10   train_loss = 1.132
Epoch 261 Batch    8/10   train_loss = 1.091
Epoch 262 Batch    9/10   train_loss = 1.122
Epoch 264 Batch    0/10   train_loss = 1.166
Epoch 265 Batch    1/10   train_loss = 1.147
Epoch 266 Batch    2/10   train_loss = 1.105
Epoch 267 Batch    3/10   train_loss = 1.163
Epoch 268 Batch    4/10   train_loss = 1.162
Epoch 269 Batch    5/10   train_loss = 1.094
Epoch 270 Batch    6/10   train_loss = 1.065
Epoch 271 Batch    7/10   train_loss = 1.061
Epoch 272 Batch    8/10   train_loss = 1.023
Epoch 273 Batch    9/10   train_loss = 1.052
Epoch 275 Batch    0/10   train_loss = 1.103
Epoch 276 Batch    1/10   train_loss = 1.089
Epoch 277 Batch    2/10   train_loss = 1.044
Epoch 278 Batch    3/10   train_loss = 1.094
Epoch 279 Batch    4/10   train_loss = 1.089
Epoch 280 Batch    5/10   train_loss = 1.023
Epoch 281 Batch    6/10   train_loss = 0.999
Epoch 282 Batch    7/10   train_loss = 0.991
Epoch 283 Batch    8/10   train_loss = 0.958
Epoch 284 Batch    9/10   train_loss = 0.987
Epoch 286 Batch    0/10   train_loss = 1.039
Epoch 287 Batch    1/10   train_loss = 1.025
Epoch 288 Batch    2/10   train_loss = 0.987
Epoch 289 Batch    3/10   train_loss = 1.045
Epoch 290 Batch    4/10   train_loss = 1.042
Epoch 291 Batch    5/10   train_loss = 0.970
Epoch 292 Batch    6/10   train_loss = 0.942
Epoch 293 Batch    7/10   train_loss = 0.934
Epoch 294 Batch    8/10   train_loss = 0.899
Epoch 295 Batch    9/10   train_loss = 0.928
Epoch 297 Batch    0/10   train_loss = 0.974
Epoch 298 Batch    1/10   train_loss = 0.959
Epoch 299 Batch    2/10   train_loss = 0.923
Epoch 300 Batch    3/10   train_loss = 0.975
Epoch 301 Batch    4/10   train_loss = 0.964
Epoch 302 Batch    5/10   train_loss = 0.897
Epoch 303 Batch    6/10   train_loss = 0.877
Epoch 304 Batch    7/10   train_loss = 0.874
Epoch 305 Batch    8/10   train_loss = 0.847
Epoch 306 Batch    9/10   train_loss = 0.873
Epoch 308 Batch    0/10   train_loss = 0.919
Epoch 309 Batch    1/10   train_loss = 0.907
Epoch 310 Batch    2/10   train_loss = 0.871
Epoch 311 Batch    3/10   train_loss = 0.924
Epoch 312 Batch    4/10   train_loss = 0.915
Epoch 313 Batch    5/10   train_loss = 0.848
Epoch 314 Batch    6/10   train_loss = 0.828
Epoch 315 Batch    7/10   train_loss = 0.822
Epoch 316 Batch    8/10   train_loss = 0.795
Epoch 317 Batch    9/10   train_loss = 0.823
Epoch 319 Batch    0/10   train_loss = 0.870
Epoch 320 Batch    1/10   train_loss = 0.859
Epoch 321 Batch    2/10   train_loss = 0.821
Epoch 322 Batch    3/10   train_loss = 0.869
Epoch 323 Batch    4/10   train_loss = 0.854
Epoch 324 Batch    5/10   train_loss = 0.789
Epoch 325 Batch    6/10   train_loss = 0.774
Epoch 326 Batch    7/10   train_loss = 0.771
Epoch 327 Batch    8/10   train_loss = 0.746
Epoch 328 Batch    9/10   train_loss = 0.773
Epoch 330 Batch    0/10   train_loss = 0.816
Epoch 331 Batch    1/10   train_loss = 0.802
Epoch 332 Batch    2/10   train_loss = 0.772
Epoch 333 Batch    3/10   train_loss = 0.824
Epoch 334 Batch    4/10   train_loss = 0.809
Epoch 335 Batch    5/10   train_loss = 0.748
Epoch 336 Batch    6/10   train_loss = 0.739
Epoch 337 Batch    7/10   train_loss = 0.735
Epoch 338 Batch    8/10   train_loss = 0.706
Epoch 339 Batch    9/10   train_loss = 0.728
Epoch 341 Batch    0/10   train_loss = 0.763
Epoch 342 Batch    1/10   train_loss = 0.755
Epoch 343 Batch    2/10   train_loss = 0.725
Epoch 344 Batch    3/10   train_loss = 0.776
Epoch 345 Batch    4/10   train_loss = 0.759
Epoch 346 Batch    5/10   train_loss = 0.700
Epoch 347 Batch    6/10   train_loss = 0.695
Epoch 348 Batch    7/10   train_loss = 0.686
Epoch 349 Batch    8/10   train_loss = 0.658
Epoch 350 Batch    9/10   train_loss = 0.687
Epoch 352 Batch    0/10   train_loss = 0.730
Epoch 353 Batch    1/10   train_loss = 0.719
Epoch 354 Batch    2/10   train_loss = 0.686
Epoch 355 Batch    3/10   train_loss = 0.741
Epoch 356 Batch    4/10   train_loss = 0.719
Epoch 357 Batch    5/10   train_loss = 0.660
Epoch 358 Batch    6/10   train_loss = 0.650
Epoch 359 Batch    7/10   train_loss = 0.644
Epoch 360 Batch    8/10   train_loss = 0.622
Epoch 361 Batch    9/10   train_loss = 0.654
Epoch 363 Batch    0/10   train_loss = 0.692
Epoch 364 Batch    1/10   train_loss = 0.677
Epoch 365 Batch    2/10   train_loss = 0.647
Epoch 366 Batch    3/10   train_loss = 0.699
Epoch 367 Batch    4/10   train_loss = 0.680
Epoch 368 Batch    5/10   train_loss = 0.622
Epoch 369 Batch    6/10   train_loss = 0.618
Epoch 370 Batch    7/10   train_loss = 0.611
Epoch 371 Batch    8/10   train_loss = 0.589
Epoch 372 Batch    9/10   train_loss = 0.612
Epoch 374 Batch    0/10   train_loss = 0.643
Epoch 375 Batch    1/10   train_loss = 0.637
Epoch 376 Batch    2/10   train_loss = 0.612
Epoch 377 Batch    3/10   train_loss = 0.663
Epoch 378 Batch    4/10   train_loss = 0.642
Epoch 379 Batch    5/10   train_loss = 0.591
Epoch 380 Batch    6/10   train_loss = 0.589
Epoch 381 Batch    7/10   train_loss = 0.579
Epoch 382 Batch    8/10   train_loss = 0.555
Epoch 383 Batch    9/10   train_loss = 0.577
Epoch 385 Batch    0/10   train_loss = 0.615
Epoch 386 Batch    1/10   train_loss = 0.610
Epoch 387 Batch    2/10   train_loss = 0.581
Epoch 388 Batch    3/10   train_loss = 0.630
Epoch 389 Batch    4/10   train_loss = 0.612
Epoch 390 Batch    5/10   train_loss = 0.563
Epoch 391 Batch    6/10   train_loss = 0.556
Epoch 392 Batch    7/10   train_loss = 0.546
Epoch 393 Batch    8/10   train_loss = 0.539
Epoch 394 Batch    9/10   train_loss = 0.560
Epoch 396 Batch    0/10   train_loss = 0.582
Epoch 397 Batch    1/10   train_loss = 0.575
Epoch 398 Batch    2/10   train_loss = 0.551
Epoch 399 Batch    3/10   train_loss = 0.595
Epoch 400 Batch    4/10   train_loss = 0.577
Epoch 401 Batch    5/10   train_loss = 0.536
Epoch 402 Batch    6/10   train_loss = 0.533
Epoch 403 Batch    7/10   train_loss = 0.519
Epoch 404 Batch    8/10   train_loss = 0.502
Epoch 405 Batch    9/10   train_loss = 0.518
Epoch 407 Batch    0/10   train_loss = 0.550
Epoch 408 Batch    1/10   train_loss = 0.548
Epoch 409 Batch    2/10   train_loss = 0.528
Epoch 410 Batch    3/10   train_loss = 0.573
Epoch 411 Batch    4/10   train_loss = 0.556
Epoch 412 Batch    5/10   train_loss = 0.514
Epoch 413 Batch    6/10   train_loss = 0.507
Epoch 414 Batch    7/10   train_loss = 0.492
Epoch 415 Batch    8/10   train_loss = 0.481
Epoch 416 Batch    9/10   train_loss = 0.495
Epoch 418 Batch    0/10   train_loss = 0.527
Epoch 419 Batch    1/10   train_loss = 0.521
Epoch 420 Batch    2/10   train_loss = 0.498
Epoch 421 Batch    3/10   train_loss = 0.541
Epoch 422 Batch    4/10   train_loss = 0.524
Epoch 423 Batch    5/10   train_loss = 0.479
Epoch 424 Batch    6/10   train_loss = 0.474
Epoch 425 Batch    7/10   train_loss = 0.467
Epoch 426 Batch    8/10   train_loss = 0.459
Epoch 427 Batch    9/10   train_loss = 0.473
Epoch 429 Batch    0/10   train_loss = 0.497
Epoch 430 Batch    1/10   train_loss = 0.494
Epoch 431 Batch    2/10   train_loss = 0.475
Epoch 432 Batch    3/10   train_loss = 0.516
Epoch 433 Batch    4/10   train_loss = 0.499
Epoch 434 Batch    5/10   train_loss = 0.460
Epoch 435 Batch    6/10   train_loss = 0.456
Epoch 436 Batch    7/10   train_loss = 0.446
Epoch 437 Batch    8/10   train_loss = 0.438
Epoch 438 Batch    9/10   train_loss = 0.449
Epoch 440 Batch    0/10   train_loss = 0.483
Epoch 441 Batch    1/10   train_loss = 0.483
Epoch 442 Batch    2/10   train_loss = 0.463
Epoch 443 Batch    3/10   train_loss = 0.499
Epoch 444 Batch    4/10   train_loss = 0.481
Epoch 445 Batch    5/10   train_loss = 0.442
Epoch 446 Batch    6/10   train_loss = 0.436
Epoch 447 Batch    7/10   train_loss = 0.429
Epoch 448 Batch    8/10   train_loss = 0.419
Epoch 449 Batch    9/10   train_loss = 0.429
Epoch 451 Batch    0/10   train_loss = 0.455
Epoch 452 Batch    1/10   train_loss = 0.452
Epoch 453 Batch    2/10   train_loss = 0.436
Epoch 454 Batch    3/10   train_loss = 0.473
Epoch 455 Batch    4/10   train_loss = 0.455
Epoch 456 Batch    5/10   train_loss = 0.418
Epoch 457 Batch    6/10   train_loss = 0.414
Epoch 458 Batch    7/10   train_loss = 0.409
Epoch 459 Batch    8/10   train_loss = 0.404
Epoch 460 Batch    9/10   train_loss = 0.418
Epoch 462 Batch    0/10   train_loss = 0.443
Epoch 463 Batch    1/10   train_loss = 0.444
Epoch 464 Batch    2/10   train_loss = 0.424
Epoch 465 Batch    3/10   train_loss = 0.459
Epoch 466 Batch    4/10   train_loss = 0.442
Epoch 467 Batch    5/10   train_loss = 0.407
Epoch 468 Batch    6/10   train_loss = 0.408
Epoch 469 Batch    7/10   train_loss = 0.398
Epoch 470 Batch    8/10   train_loss = 0.389
Epoch 471 Batch    9/10   train_loss = 0.394
Epoch 473 Batch    0/10   train_loss = 0.416
Epoch 474 Batch    1/10   train_loss = 0.417
Epoch 475 Batch    2/10   train_loss = 0.403
Epoch 476 Batch    3/10   train_loss = 0.437
Epoch 477 Batch    4/10   train_loss = 0.418
Epoch 478 Batch    5/10   train_loss = 0.386
Epoch 479 Batch    6/10   train_loss = 0.384
Epoch 480 Batch    7/10   train_loss = 0.376
Epoch 481 Batch    8/10   train_loss = 0.372
Epoch 482 Batch    9/10   train_loss = 0.380
Epoch 484 Batch    0/10   train_loss = 0.402
Epoch 485 Batch    1/10   train_loss = 0.401
Epoch 486 Batch    2/10   train_loss = 0.389
Epoch 487 Batch    3/10   train_loss = 0.424
Epoch 488 Batch    4/10   train_loss = 0.409
Epoch 489 Batch    5/10   train_loss = 0.376
Epoch 490 Batch    6/10   train_loss = 0.377
Epoch 491 Batch    7/10   train_loss = 0.368
Epoch 492 Batch    8/10   train_loss = 0.360
Epoch 493 Batch    9/10   train_loss = 0.366
Epoch 495 Batch    0/10   train_loss = 0.388
Epoch 496 Batch    1/10   train_loss = 0.387
Epoch 497 Batch    2/10   train_loss = 0.374
Epoch 498 Batch    3/10   train_loss = 0.407
Epoch 499 Batch    4/10   train_loss = 0.392
Epoch 500 Batch    5/10   train_loss = 0.361
Epoch 501 Batch    6/10   train_loss = 0.357
Epoch 502 Batch    7/10   train_loss = 0.352
Epoch 503 Batch    8/10   train_loss = 0.348
Epoch 504 Batch    9/10   train_loss = 0.358
Epoch 506 Batch    0/10   train_loss = 0.375
Epoch 507 Batch    1/10   train_loss = 0.373
Epoch 508 Batch    2/10   train_loss = 0.362
Epoch 509 Batch    3/10   train_loss = 0.395
Epoch 510 Batch    4/10   train_loss = 0.383
Epoch 511 Batch    5/10   train_loss = 0.355
Epoch 512 Batch    6/10   train_loss = 0.352
Epoch 513 Batch    7/10   train_loss = 0.345
Epoch 514 Batch    8/10   train_loss = 0.340
Epoch 515 Batch    9/10   train_loss = 0.345
Epoch 517 Batch    0/10   train_loss = 0.362
Epoch 518 Batch    1/10   train_loss = 0.363
Epoch 519 Batch    2/10   train_loss = 0.352
Epoch 520 Batch    3/10   train_loss = 0.382
Epoch 521 Batch    4/10   train_loss = 0.367
Epoch 522 Batch    5/10   train_loss = 0.342
Epoch 523 Batch    6/10   train_loss = 0.339
Epoch 524 Batch    7/10   train_loss = 0.334
Epoch 525 Batch    8/10   train_loss = 0.326
Epoch 526 Batch    9/10   train_loss = 0.332
Epoch 528 Batch    0/10   train_loss = 0.350
Epoch 529 Batch    1/10   train_loss = 0.350
Epoch 530 Batch    2/10   train_loss = 0.339
Epoch 531 Batch    3/10   train_loss = 0.368
Epoch 532 Batch    4/10   train_loss = 0.355
Epoch 533 Batch    5/10   train_loss = 0.332
Epoch 534 Batch    6/10   train_loss = 0.327
Epoch 535 Batch    7/10   train_loss = 0.322
Epoch 536 Batch    8/10   train_loss = 0.317
Epoch 537 Batch    9/10   train_loss = 0.323
Epoch 539 Batch    0/10   train_loss = 0.340
Epoch 540 Batch    1/10   train_loss = 0.341
Epoch 541 Batch    2/10   train_loss = 0.331
Epoch 542 Batch    3/10   train_loss = 0.359
Epoch 543 Batch    4/10   train_loss = 0.347
Epoch 544 Batch    5/10   train_loss = 0.323
Epoch 545 Batch    6/10   train_loss = 0.317
Epoch 546 Batch    7/10   train_loss = 0.313
Epoch 547 Batch    8/10   train_loss = 0.311
Epoch 548 Batch    9/10   train_loss = 0.317
Epoch 550 Batch    0/10   train_loss = 0.329
Epoch 551 Batch    1/10   train_loss = 0.331
Epoch 552 Batch    2/10   train_loss = 0.324
Epoch 553 Batch    3/10   train_loss = 0.353
Epoch 554 Batch    4/10   train_loss = 0.339
Epoch 555 Batch    5/10   train_loss = 0.317
Epoch 556 Batch    6/10   train_loss = 0.312
Epoch 557 Batch    7/10   train_loss = 0.307
Epoch 558 Batch    8/10   train_loss = 0.302
Epoch 559 Batch    9/10   train_loss = 0.306
Epoch 561 Batch    0/10   train_loss = 0.319
Epoch 562 Batch    1/10   train_loss = 0.324
Epoch 563 Batch    2/10   train_loss = 0.313
Epoch 564 Batch    3/10   train_loss = 0.341
Epoch 565 Batch    4/10   train_loss = 0.329
Epoch 566 Batch    5/10   train_loss = 0.310
Epoch 567 Batch    6/10   train_loss = 0.303
Epoch 568 Batch    7/10   train_loss = 0.300
Epoch 569 Batch    8/10   train_loss = 0.296
Epoch 570 Batch    9/10   train_loss = 0.300
Epoch 572 Batch    0/10   train_loss = 0.311
Epoch 573 Batch    1/10   train_loss = 0.315
Epoch 574 Batch    2/10   train_loss = 0.307
Epoch 575 Batch    3/10   train_loss = 0.335
Epoch 576 Batch    4/10   train_loss = 0.325
Epoch 577 Batch    5/10   train_loss = 0.306
Epoch 578 Batch    6/10   train_loss = 0.301
Epoch 579 Batch    7/10   train_loss = 0.300
Epoch 580 Batch    8/10   train_loss = 0.295
Epoch 581 Batch    9/10   train_loss = 0.297
Epoch 583 Batch    0/10   train_loss = 0.310
Epoch 584 Batch    1/10   train_loss = 0.315
Epoch 585 Batch    2/10   train_loss = 0.312
Epoch 586 Batch    3/10   train_loss = 0.340
Epoch 587 Batch    4/10   train_loss = 0.330
Epoch 588 Batch    5/10   train_loss = 0.317
Epoch 589 Batch    6/10   train_loss = 0.328
Epoch 590 Batch    7/10   train_loss = 0.314
Epoch 591 Batch    8/10   train_loss = 0.296
Epoch 592 Batch    9/10   train_loss = 0.295
Epoch 594 Batch    0/10   train_loss = 0.306
Epoch 595 Batch    1/10   train_loss = 0.306
Epoch 596 Batch    2/10   train_loss = 0.295
Epoch 597 Batch    3/10   train_loss = 0.321
Epoch 598 Batch    4/10   train_loss = 0.309
Epoch 599 Batch    5/10   train_loss = 0.292
Epoch 600 Batch    6/10   train_loss = 0.285
Epoch 601 Batch    7/10   train_loss = 0.282
Epoch 602 Batch    8/10   train_loss = 0.279
Epoch 603 Batch    9/10   train_loss = 0.281
Epoch 605 Batch    0/10   train_loss = 0.292
Epoch 606 Batch    1/10   train_loss = 0.295
Epoch 607 Batch    2/10   train_loss = 0.288
Epoch 608 Batch    3/10   train_loss = 0.314
Epoch 609 Batch    4/10   train_loss = 0.303
Epoch 610 Batch    5/10   train_loss = 0.286
Epoch 611 Batch    6/10   train_loss = 0.280
Epoch 612 Batch    7/10   train_loss = 0.278
Epoch 613 Batch    8/10   train_loss = 0.274
Epoch 614 Batch    9/10   train_loss = 0.276
Epoch 616 Batch    0/10   train_loss = 0.287
Epoch 617 Batch    1/10   train_loss = 0.291
Epoch 618 Batch    2/10   train_loss = 0.284
Epoch 619 Batch    3/10   train_loss = 0.309
Epoch 620 Batch    4/10   train_loss = 0.298
Epoch 621 Batch    5/10   train_loss = 0.283
Epoch 622 Batch    6/10   train_loss = 0.277
Epoch 623 Batch    7/10   train_loss = 0.274
Epoch 624 Batch    8/10   train_loss = 0.271
Epoch 625 Batch    9/10   train_loss = 0.273
Epoch 627 Batch    0/10   train_loss = 0.282
Epoch 628 Batch    1/10   train_loss = 0.286
Epoch 629 Batch    2/10   train_loss = 0.280
Epoch 630 Batch    3/10   train_loss = 0.305
Epoch 631 Batch    4/10   train_loss = 0.293
Epoch 632 Batch    5/10   train_loss = 0.279
Epoch 633 Batch    6/10   train_loss = 0.274
Epoch 634 Batch    7/10   train_loss = 0.271
Epoch 635 Batch    8/10   train_loss = 0.268
Epoch 636 Batch    9/10   train_loss = 0.269
Epoch 638 Batch    0/10   train_loss = 0.279
Epoch 639 Batch    1/10   train_loss = 0.283
Epoch 640 Batch    2/10   train_loss = 0.276
Epoch 641 Batch    3/10   train_loss = 0.300
Epoch 642 Batch    4/10   train_loss = 0.290
Epoch 643 Batch    5/10   train_loss = 0.275
Epoch 644 Batch    6/10   train_loss = 0.269
Epoch 645 Batch    7/10   train_loss = 0.268
Epoch 646 Batch    8/10   train_loss = 0.265
Epoch 647 Batch    9/10   train_loss = 0.265
Epoch 649 Batch    0/10   train_loss = 0.275
Epoch 650 Batch    1/10   train_loss = 0.279
Epoch 651 Batch    2/10   train_loss = 0.273
Epoch 652 Batch    3/10   train_loss = 0.296
Epoch 653 Batch    4/10   train_loss = 0.286
Epoch 654 Batch    5/10   train_loss = 0.273
Epoch 655 Batch    6/10   train_loss = 0.267
Epoch 656 Batch    7/10   train_loss = 0.265
Epoch 657 Batch    8/10   train_loss = 0.261
Epoch 658 Batch    9/10   train_loss = 0.262
Epoch 660 Batch    0/10   train_loss = 0.272
Epoch 661 Batch    1/10   train_loss = 0.275
Epoch 662 Batch    2/10   train_loss = 0.271
Epoch 663 Batch    3/10   train_loss = 0.294
Epoch 664 Batch    4/10   train_loss = 0.283
Epoch 665 Batch    5/10   train_loss = 0.270
Epoch 666 Batch    6/10   train_loss = 0.264
Epoch 667 Batch    7/10   train_loss = 0.263
Epoch 668 Batch    8/10   train_loss = 0.260
Epoch 669 Batch    9/10   train_loss = 0.260
Epoch 671 Batch    0/10   train_loss = 0.269
Epoch 672 Batch    1/10   train_loss = 0.273
Epoch 673 Batch    2/10   train_loss = 0.268
Epoch 674 Batch    3/10   train_loss = 0.290
Epoch 675 Batch    4/10   train_loss = 0.280
Epoch 676 Batch    5/10   train_loss = 0.267
Epoch 677 Batch    6/10   train_loss = 0.262
Epoch 678 Batch    7/10   train_loss = 0.260
Epoch 679 Batch    8/10   train_loss = 0.257
Epoch 680 Batch    9/10   train_loss = 0.257
Epoch 682 Batch    0/10   train_loss = 0.267
Epoch 683 Batch    1/10   train_loss = 0.271
Epoch 684 Batch    2/10   train_loss = 0.266
Epoch 685 Batch    3/10   train_loss = 0.288
Epoch 686 Batch    4/10   train_loss = 0.278
Epoch 687 Batch    5/10   train_loss = 0.266
Epoch 688 Batch    6/10   train_loss = 0.260
Epoch 689 Batch    7/10   train_loss = 0.258
Epoch 690 Batch    8/10   train_loss = 0.255
Epoch 691 Batch    9/10   train_loss = 0.255
Epoch 693 Batch    0/10   train_loss = 0.264
Epoch 694 Batch    1/10   train_loss = 0.267
Epoch 695 Batch    2/10   train_loss = 0.262
Epoch 696 Batch    3/10   train_loss = 0.283
Epoch 697 Batch    4/10   train_loss = 0.274
Epoch 698 Batch    5/10   train_loss = 0.263
Epoch 699 Batch    6/10   train_loss = 0.257
Epoch 700 Batch    7/10   train_loss = 0.255
Epoch 701 Batch    8/10   train_loss = 0.252
Epoch 702 Batch    9/10   train_loss = 0.252
Epoch 704 Batch    0/10   train_loss = 0.262
Epoch 705 Batch    1/10   train_loss = 0.265
Epoch 706 Batch    2/10   train_loss = 0.260
Epoch 707 Batch    3/10   train_loss = 0.282
Epoch 708 Batch    4/10   train_loss = 0.273
Epoch 709 Batch    5/10   train_loss = 0.261
Epoch 710 Batch    6/10   train_loss = 0.257
Epoch 711 Batch    7/10   train_loss = 0.255
Epoch 712 Batch    8/10   train_loss = 0.252
Epoch 713 Batch    9/10   train_loss = 0.251
Epoch 715 Batch    0/10   train_loss = 0.260
Epoch 716 Batch    1/10   train_loss = 0.264
Epoch 717 Batch    2/10   train_loss = 0.259
Epoch 718 Batch    3/10   train_loss = 0.279
Epoch 719 Batch    4/10   train_loss = 0.271
Epoch 720 Batch    5/10   train_loss = 0.260
Epoch 721 Batch    6/10   train_loss = 0.254
Epoch 722 Batch    7/10   train_loss = 0.252
Epoch 723 Batch    8/10   train_loss = 0.249
Epoch 724 Batch    9/10   train_loss = 0.249
Epoch 726 Batch    0/10   train_loss = 0.258
Epoch 727 Batch    1/10   train_loss = 0.261
Epoch 728 Batch    2/10   train_loss = 0.256
Epoch 729 Batch    3/10   train_loss = 0.277
Epoch 730 Batch    4/10   train_loss = 0.268
Epoch 731 Batch    5/10   train_loss = 0.257
Epoch 732 Batch    6/10   train_loss = 0.252
Epoch 733 Batch    7/10   train_loss = 0.250
Epoch 734 Batch    8/10   train_loss = 0.248
Epoch 735 Batch    9/10   train_loss = 0.247
Epoch 737 Batch    0/10   train_loss = 0.256
Epoch 738 Batch    1/10   train_loss = 0.259
Epoch 739 Batch    2/10   train_loss = 0.255
Epoch 740 Batch    3/10   train_loss = 0.275
Epoch 741 Batch    4/10   train_loss = 0.266
Epoch 742 Batch    5/10   train_loss = 0.256
Epoch 743 Batch    6/10   train_loss = 0.251
Epoch 744 Batch    7/10   train_loss = 0.249
Epoch 745 Batch    8/10   train_loss = 0.247
Epoch 746 Batch    9/10   train_loss = 0.245
Epoch 748 Batch    0/10   train_loss = 0.254
Epoch 749 Batch    1/10   train_loss = 0.258
Epoch 750 Batch    2/10   train_loss = 0.254
Epoch 751 Batch    3/10   train_loss = 0.275
Epoch 752 Batch    4/10   train_loss = 0.266
Epoch 753 Batch    5/10   train_loss = 0.256
Epoch 754 Batch    6/10   train_loss = 0.250
Epoch 755 Batch    7/10   train_loss = 0.249
Epoch 756 Batch    8/10   train_loss = 0.247
Epoch 757 Batch    9/10   train_loss = 0.245
Epoch 759 Batch    0/10   train_loss = 0.254
Epoch 760 Batch    1/10   train_loss = 0.257
Epoch 761 Batch    2/10   train_loss = 0.251
Epoch 762 Batch    3/10   train_loss = 0.272
Epoch 763 Batch    4/10   train_loss = 0.263
Epoch 764 Batch    5/10   train_loss = 0.253
Epoch 765 Batch    6/10   train_loss = 0.248
Epoch 766 Batch    7/10   train_loss = 0.247
Epoch 767 Batch    8/10   train_loss = 0.244
Epoch 768 Batch    9/10   train_loss = 0.243
Epoch 770 Batch    0/10   train_loss = 0.251
Epoch 771 Batch    1/10   train_loss = 0.254
Epoch 772 Batch    2/10   train_loss = 0.250
Epoch 773 Batch    3/10   train_loss = 0.270
Epoch 774 Batch    4/10   train_loss = 0.262
Epoch 775 Batch    5/10   train_loss = 0.252
Epoch 776 Batch    6/10   train_loss = 0.247
Epoch 777 Batch    7/10   train_loss = 0.246
Epoch 778 Batch    8/10   train_loss = 0.243
Epoch 779 Batch    9/10   train_loss = 0.242
Epoch 781 Batch    0/10   train_loss = 0.250
Epoch 782 Batch    1/10   train_loss = 0.253
Epoch 783 Batch    2/10   train_loss = 0.249
Epoch 784 Batch    3/10   train_loss = 0.269
Epoch 785 Batch    4/10   train_loss = 0.261
Epoch 786 Batch    5/10   train_loss = 0.252
Epoch 787 Batch    6/10   train_loss = 0.246
Epoch 788 Batch    7/10   train_loss = 0.245
Epoch 789 Batch    8/10   train_loss = 0.242
Epoch 790 Batch    9/10   train_loss = 0.241
Epoch 792 Batch    0/10   train_loss = 0.249
Epoch 793 Batch    1/10   train_loss = 0.253
Epoch 794 Batch    2/10   train_loss = 0.248
Epoch 795 Batch    3/10   train_loss = 0.268
Epoch 796 Batch    4/10   train_loss = 0.260
Epoch 797 Batch    5/10   train_loss = 0.251
Epoch 798 Batch    6/10   train_loss = 0.245
Epoch 799 Batch    7/10   train_loss = 0.245
Model Trained and Saved

Save Parameters

Save seq_length and save_dir for generating a new TV script.


In [23]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
# Save parameters for checkpoint
helper.save_params((seq_length, save_dir))

Checkpoint


In [24]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import tensorflow as tf
import numpy as np
import helper
import problem_unittests as tests

_, vocab_to_int, int_to_vocab, token_dict = helper.load_preprocess()
seq_length, load_dir = helper.load_params()

Implement Generate Functions

Get Tensors

Get tensors from loaded_graph using the function get_tensor_by_name(). Get the tensors using the following names:

  • "input:0"
  • "initial_state:0"
  • "final_state:0"
  • "probs:0"

Return the tensors in the following tuple (InputTensor, InitialStateTensor, FinalStateTensor, ProbsTensor)


In [25]:
def get_tensors(loaded_graph):
    """
    Get input, initial state, final state, and probabilities tensor from <loaded_graph>
    :param loaded_graph: TensorFlow graph loaded from file
    :return: Tuple (InputTensor, InitialStateTensor, FinalStateTensor, ProbsTensor)
    """
    # TODO: Implement Function
    input = loaded_graph.get_tensor_by_name ('input:0')
    initial_state = loaded_graph.get_tensor_by_name ('initial_state:0')
    final_state = loaded_graph.get_tensor_by_name ('final_state:0')
    probs = loaded_graph.get_tensor_by_name ('probs:0')
    
    return input, initial_state, final_state, probs


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_get_tensors(get_tensors)


Tests Passed

Choose Word

Implement the pick_word() function to select the next word using probabilities.


In [26]:
from numpy import random

def pick_word(probabilities, int_to_vocab):
    """
    Pick the next word in the generated text
    :param probabilities: Probabilites of the next word
    :param int_to_vocab: Dictionary of word ids as the keys and words as the values
    :return: String of the predicted word
    """
    # TODO: Implement Function
    return random.choice (list(int_to_vocab.values()), p=probabilities)


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_pick_word(pick_word)


Tests Passed

Generate TV Script

This will generate the TV script for you. Set gen_length to the length of TV script you want to generate.


In [27]:
gen_length = 200
# homer_simpson, moe_szyslak, or Barney_Gumble
prime_word = 'moe_szyslak'

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
    # Load saved model
    loader = tf.train.import_meta_graph(load_dir + '.meta')
    loader.restore(sess, load_dir)

    # Get Tensors from loaded model
    input_text, initial_state, final_state, probs = get_tensors(loaded_graph)

    # Sentences generation setup
    gen_sentences = [prime_word + ':']
    prev_state = sess.run(initial_state, {input_text: np.array([[1]])})

    # Generate sentences
    for n in range(gen_length):
        # Dynamic Input
        dyn_input = [[vocab_to_int[word] for word in gen_sentences[-seq_length:]]]
        dyn_seq_length = len(dyn_input[0])

        # Get Prediction
        probabilities, prev_state = sess.run(
            [probs, final_state],
            {input_text: dyn_input, initial_state: prev_state})
        
        pred_word = pick_word(probabilities[dyn_seq_length-1], int_to_vocab)

        gen_sentences.append(pred_word)
    
    # Remove tokens
    tv_script = ' '.join(gen_sentences)
    for key, token in token_dict.items():
        ending = ' ' if key in ['\n', '(', '"'] else ''
        tv_script = tv_script.replace(' ' + token.lower(), key)
    tv_script = tv_script.replace('\n ', '\n')
    tv_script = tv_script.replace('( ', '(')
        
    print(tv_script)


moe_szyslak: what'sa matter, homer? do you still miss the upn?
moe_szyslak: oh... beer me, krusty.
moe_szyslak: don't eat those eggs! we don't have an anniversary coming home and spend the rest of the...
lenny_leonard: we are not staying at me.
lenny_leonard: you really think i could use my money. where you know, i just had a thing?
marge_simpson: quit playing dumb.
homer_simpson:(dreamily) homer, we all wanna know all we should ever.
homer_simpson: you nuclear workers have a great m(beat) you don't play. he's our guy in our love testing?
carl_carlson: hey, go. now since the-- where's the inflated sense of news won't play out there and buy you go to the texas cheesecake depository.
moe_szyslak: sorry homer?
homer_simpson: yeah.
thought_bubble_homer:(sobbing) oh marge, if there's one a guy stand here, twenty-two and sixty-nine.(dramatic) well, if it could put it up for a drink on, what's uh,

The TV Script is Nonsensical

It's ok if the TV script doesn't make any sense. We trained on less than a megabyte of text. In order to get good results, you'll have to use a smaller vocabulary or get more data. Luckly there's more data! As we mentioned in the begging of this project, this is a subset of another dataset. We didn't have you train on all the data, because that would take too long. However, you are free to train your neural network on all the data. After you complete the project, of course.

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 it as a HTML file under "File" -> "Download as". Include the "helper.py" and "problem_unittests.py" files in your submission.