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:]

Explore the Data

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


In [2]:
view_sentence_range = (80, 100)

"""
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 80 to 100:
Moe_Szyslak: (UNFAMILIAR) Gin and... tonic? Do they mix?
Homer_Simpson: (BRIGHTENING) Hey, I know a good drink. Really hits the spot. I invented it myself...
Moe_Szyslak: Sorry, Harv.
Moe_Szyslak: Whoa, sounds like one hell of a drink. What do you call it?
Homer_Simpson: A "Flaming Homer".
Moe_Szyslak: Okay, why don't you make us up a couple of "Flaming Homers"?
Homer_Simpson: Hey Moe, you got any cough syrup?
Moe_Szyslak: Uh, let me check the lost and found.
Moe_Szyslak: What do we got here, Bowie knife, troll doll, glass eye...
Moe_Szyslak: Oh. Here we are.
Moe_Szyslak: It's not without its charm.
Homer_Simpson: Try lighting it on fire.
Moe_Szyslak: (SMILING) Whoa! Homer, it's like there's a party in my mouth and everyone's invited.
Larry: Hey, your Love Tester's busted. I want my nickel back. (COUGHS)
Moe_Szyslak: Hey, buddy. Have one on the house.
Larry: Hey, hey, this drink is delicious! And my phlegm feels looser. What do you call it?
Homer_Simpson: Well, it's called a "Flaming...
Moe_Szyslak: Moe! It's called a "Flaming Moe"! That's right, a "Flaming Moe". My name is Moe, and I invented it. That's why it's called a Flaming Moe. What? What are you lookin' at, Homer? It's a Flaming Moe I'm Moe.
Barney_Gumble: Hey, what's this?
Moe_Szyslak: A sneeze guard.

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 [3]:
import numpy as np
import problem_unittests as tests

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)
    """
    # create a set and sort to get a list
    sorted_vocabs = sorted(set(text))
    vocab_to_int = { vocab:idx for idx,vocab in enumerate(sorted_vocabs)}
    int_to_vocab = {idx:vocab for vocab,idx in vocab_to_int.items()}
    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 [4]:
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_mark||",
           ';':"||semicolon||",'!':"||exclamation_mark||",'?':"||question_mark||",
           '(':"||left_parenthese||",')':"||right_parenthese||",'--':"||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 [5]:
"""
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 [1]:
"""
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 [2]:
"""
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
Default GPU Device: /gpu:0

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 [3]:
def get_inputs():
    """
    Create TF Placeholders for input, targets, and learning rate.
    :return: Tuple (input, targets, learning rate)
    """
    # TODO: Implement Function
    #with tf.name_scope("inputs"):
    inputs = 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 inputs, 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 [4]:
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
    # just 2 lstm layer
    num_layer = 2
    lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
    # Add dropout to the cell
    drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=0.5)
    
    cell = tf.contrib.rnn.MultiRNNCell([drop]* num_layer)
    # no dropout at first
    initial_state = cell.zero_state(batch_size, tf.float32)
    initial_state = tf.identity(initial_state, 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 [5]:
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))
    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 [6]:
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 [7]:
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
    embed_dim = 300
    embed = get_embed(input_data,vocab_size,embed_dim)
    outputs,final_state = build_rnn(cell,embed)
    logits = tf.contrib.layers.fully_connected(outputs,num_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], 2, 3) would return a Numpy array of the following:

[
  # First Batch
  [
    # Batch of Input
    [[ 1  2  3], [ 7  8  9]],
    # Batch of targets
    [[ 2  3  4], [ 8  9 10]]
  ],

  # Second Batch
  [
    # Batch of Input
    [[ 4  5  6], [10 11 12]],
    # Batch of targets
    [[ 5  6  7], [11 12 13]]
  ]
]

In [8]:
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
    n_batches = len(int_text)//(batch_size*seq_length)
    inputs = int_text[:n_batches*batch_size*seq_length]
    # assuming the last word has index >n_batches*batch_size*seq_length
    targets = int_text[1:n_batches*batch_size*seq_length+1]
    inputs = np.array(inputs).reshape([n_batches,1,batch_size,seq_length])
    targets = np.array(targets).reshape([n_batches,1,batch_size,seq_length])
    
    return np.concatenate([inputs,targets],axis=1)


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


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 [9]:
# Number of Epochs
num_epochs = 100
# Batch Size
batch_size = 32
# RNN Size
# Embedding Dimension Size
# Sequence Length
seq_length = 16
# Learning Rate
learning_rate = 0.001
# Show stats for every n number of batches
show_every_n_batches = 20

"""
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 [10]:
"""
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 [11]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
batches = get_batches(int_text, batch_size, seq_length)

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')


Epoch   0 Batch    0/134   train_loss = 8.823
Epoch   0 Batch   20/134   train_loss = 6.429
Epoch   0 Batch   40/134   train_loss = 6.612
Epoch   0 Batch   60/134   train_loss = 6.385
Epoch   0 Batch   80/134   train_loss = 6.240
Epoch   0 Batch  100/134   train_loss = 6.141
Epoch   0 Batch  120/134   train_loss = 6.600
Epoch   1 Batch    6/134   train_loss = 6.123
Epoch   1 Batch   26/134   train_loss = 6.181
Epoch   1 Batch   46/134   train_loss = 5.846
Epoch   1 Batch   66/134   train_loss = 5.875
Epoch   1 Batch   86/134   train_loss = 6.093
Epoch   1 Batch  106/134   train_loss = 6.113
Epoch   1 Batch  126/134   train_loss = 5.759
Epoch   2 Batch   12/134   train_loss = 5.375
Epoch   2 Batch   32/134   train_loss = 5.850
Epoch   2 Batch   52/134   train_loss = 5.380
Epoch   2 Batch   72/134   train_loss = 5.437
Epoch   2 Batch   92/134   train_loss = 5.169
Epoch   2 Batch  112/134   train_loss = 5.658
Epoch   2 Batch  132/134   train_loss = 4.906
Epoch   3 Batch   18/134   train_loss = 5.326
Epoch   3 Batch   38/134   train_loss = 5.193
Epoch   3 Batch   58/134   train_loss = 5.047
Epoch   3 Batch   78/134   train_loss = 5.096
Epoch   3 Batch   98/134   train_loss = 5.141
Epoch   3 Batch  118/134   train_loss = 5.170
Epoch   4 Batch    4/134   train_loss = 4.897
Epoch   4 Batch   24/134   train_loss = 5.064
Epoch   4 Batch   44/134   train_loss = 4.994
Epoch   4 Batch   64/134   train_loss = 5.070
Epoch   4 Batch   84/134   train_loss = 5.045
Epoch   4 Batch  104/134   train_loss = 4.986
Epoch   4 Batch  124/134   train_loss = 5.079
Epoch   5 Batch   10/134   train_loss = 4.859
Epoch   5 Batch   30/134   train_loss = 4.714
Epoch   5 Batch   50/134   train_loss = 4.361
Epoch   5 Batch   70/134   train_loss = 4.830
Epoch   5 Batch   90/134   train_loss = 4.878
Epoch   5 Batch  110/134   train_loss = 5.087
Epoch   5 Batch  130/134   train_loss = 4.648
Epoch   6 Batch   16/134   train_loss = 4.438
Epoch   6 Batch   36/134   train_loss = 4.397
Epoch   6 Batch   56/134   train_loss = 4.748
Epoch   6 Batch   76/134   train_loss = 4.666
Epoch   6 Batch   96/134   train_loss = 4.841
Epoch   6 Batch  116/134   train_loss = 4.772
Epoch   7 Batch    2/134   train_loss = 4.394
Epoch   7 Batch   22/134   train_loss = 4.601
Epoch   7 Batch   42/134   train_loss = 4.382
Epoch   7 Batch   62/134   train_loss = 3.989
Epoch   7 Batch   82/134   train_loss = 4.720
Epoch   7 Batch  102/134   train_loss = 4.513
Epoch   7 Batch  122/134   train_loss = 4.769
Epoch   8 Batch    8/134   train_loss = 4.330
Epoch   8 Batch   28/134   train_loss = 4.590
Epoch   8 Batch   48/134   train_loss = 4.074
Epoch   8 Batch   68/134   train_loss = 4.803
Epoch   8 Batch   88/134   train_loss = 4.534
Epoch   8 Batch  108/134   train_loss = 4.140
Epoch   8 Batch  128/134   train_loss = 4.390
Epoch   9 Batch   14/134   train_loss = 4.255
Epoch   9 Batch   34/134   train_loss = 4.082
Epoch   9 Batch   54/134   train_loss = 4.211
Epoch   9 Batch   74/134   train_loss = 4.000
Epoch   9 Batch   94/134   train_loss = 4.291
Epoch   9 Batch  114/134   train_loss = 4.464
Epoch  10 Batch    0/134   train_loss = 4.162
Epoch  10 Batch   20/134   train_loss = 4.124
Epoch  10 Batch   40/134   train_loss = 4.255
Epoch  10 Batch   60/134   train_loss = 4.059
Epoch  10 Batch   80/134   train_loss = 4.177
Epoch  10 Batch  100/134   train_loss = 4.145
Epoch  10 Batch  120/134   train_loss = 4.302
Epoch  11 Batch    6/134   train_loss = 3.953
Epoch  11 Batch   26/134   train_loss = 4.166
Epoch  11 Batch   46/134   train_loss = 3.972
Epoch  11 Batch   66/134   train_loss = 4.157
Epoch  11 Batch   86/134   train_loss = 4.226
Epoch  11 Batch  106/134   train_loss = 4.191
Epoch  11 Batch  126/134   train_loss = 4.087
Epoch  12 Batch   12/134   train_loss = 3.828
Epoch  12 Batch   32/134   train_loss = 4.191
Epoch  12 Batch   52/134   train_loss = 3.870
Epoch  12 Batch   72/134   train_loss = 3.923
Epoch  12 Batch   92/134   train_loss = 3.853
Epoch  12 Batch  112/134   train_loss = 4.074
Epoch  12 Batch  132/134   train_loss = 3.549
Epoch  13 Batch   18/134   train_loss = 3.932
Epoch  13 Batch   38/134   train_loss = 3.801
Epoch  13 Batch   58/134   train_loss = 3.778
Epoch  13 Batch   78/134   train_loss = 3.743
Epoch  13 Batch   98/134   train_loss = 3.732
Epoch  13 Batch  118/134   train_loss = 3.906
Epoch  14 Batch    4/134   train_loss = 3.714
Epoch  14 Batch   24/134   train_loss = 3.744
Epoch  14 Batch   44/134   train_loss = 3.724
Epoch  14 Batch   64/134   train_loss = 3.860
Epoch  14 Batch   84/134   train_loss = 3.771
Epoch  14 Batch  104/134   train_loss = 3.652
Epoch  14 Batch  124/134   train_loss = 3.785
Epoch  15 Batch   10/134   train_loss = 3.631
Epoch  15 Batch   30/134   train_loss = 3.581
Epoch  15 Batch   50/134   train_loss = 3.314
Epoch  15 Batch   70/134   train_loss = 3.726
Epoch  15 Batch   90/134   train_loss = 3.640
Epoch  15 Batch  110/134   train_loss = 3.714
Epoch  15 Batch  130/134   train_loss = 3.374
Epoch  16 Batch   16/134   train_loss = 3.418
Epoch  16 Batch   36/134   train_loss = 3.348
Epoch  16 Batch   56/134   train_loss = 3.499
Epoch  16 Batch   76/134   train_loss = 3.560
Epoch  16 Batch   96/134   train_loss = 3.501
Epoch  16 Batch  116/134   train_loss = 3.546
Epoch  17 Batch    2/134   train_loss = 3.359
Epoch  17 Batch   22/134   train_loss = 3.389
Epoch  17 Batch   42/134   train_loss = 3.323
Epoch  17 Batch   62/134   train_loss = 3.149
Epoch  17 Batch   82/134   train_loss = 3.439
Epoch  17 Batch  102/134   train_loss = 3.452
Epoch  17 Batch  122/134   train_loss = 3.216
Epoch  18 Batch    8/134   train_loss = 3.268
Epoch  18 Batch   28/134   train_loss = 3.421
Epoch  18 Batch   48/134   train_loss = 3.199
Epoch  18 Batch   68/134   train_loss = 3.616
Epoch  18 Batch   88/134   train_loss = 3.367
Epoch  18 Batch  108/134   train_loss = 3.148
Epoch  18 Batch  128/134   train_loss = 3.434
Epoch  19 Batch   14/134   train_loss = 3.144
Epoch  19 Batch   34/134   train_loss = 3.090
Epoch  19 Batch   54/134   train_loss = 3.153
Epoch  19 Batch   74/134   train_loss = 3.048
Epoch  19 Batch   94/134   train_loss = 3.151
Epoch  19 Batch  114/134   train_loss = 3.321
Epoch  20 Batch    0/134   train_loss = 3.117
Epoch  20 Batch   20/134   train_loss = 3.162
Epoch  20 Batch   40/134   train_loss = 3.123
Epoch  20 Batch   60/134   train_loss = 3.048
Epoch  20 Batch   80/134   train_loss = 3.174
Epoch  20 Batch  100/134   train_loss = 3.012
Epoch  20 Batch  120/134   train_loss = 3.108
Epoch  21 Batch    6/134   train_loss = 2.859
Epoch  21 Batch   26/134   train_loss = 3.055
Epoch  21 Batch   46/134   train_loss = 3.060
Epoch  21 Batch   66/134   train_loss = 3.156
Epoch  21 Batch   86/134   train_loss = 3.026
Epoch  21 Batch  106/134   train_loss = 3.039
Epoch  21 Batch  126/134   train_loss = 3.119
Epoch  22 Batch   12/134   train_loss = 2.832
Epoch  22 Batch   32/134   train_loss = 3.171
Epoch  22 Batch   52/134   train_loss = 2.853
Epoch  22 Batch   72/134   train_loss = 2.948
Epoch  22 Batch   92/134   train_loss = 2.962
Epoch  22 Batch  112/134   train_loss = 2.981
Epoch  22 Batch  132/134   train_loss = 2.666
Epoch  23 Batch   18/134   train_loss = 2.928
Epoch  23 Batch   38/134   train_loss = 2.756
Epoch  23 Batch   58/134   train_loss = 2.910
Epoch  23 Batch   78/134   train_loss = 2.727
Epoch  23 Batch   98/134   train_loss = 2.877
Epoch  23 Batch  118/134   train_loss = 2.840
Epoch  24 Batch    4/134   train_loss = 2.710
Epoch  24 Batch   24/134   train_loss = 2.731
Epoch  24 Batch   44/134   train_loss = 2.809
Epoch  24 Batch   64/134   train_loss = 2.834
Epoch  24 Batch   84/134   train_loss = 2.838
Epoch  24 Batch  104/134   train_loss = 2.665
Epoch  24 Batch  124/134   train_loss = 2.743
Epoch  25 Batch   10/134   train_loss = 2.748
Epoch  25 Batch   30/134   train_loss = 2.779
Epoch  25 Batch   50/134   train_loss = 2.553
Epoch  25 Batch   70/134   train_loss = 2.781
Epoch  25 Batch   90/134   train_loss = 2.764
Epoch  25 Batch  110/134   train_loss = 2.724
Epoch  25 Batch  130/134   train_loss = 2.527
Epoch  26 Batch   16/134   train_loss = 2.507
Epoch  26 Batch   36/134   train_loss = 2.549
Epoch  26 Batch   56/134   train_loss = 2.602
Epoch  26 Batch   76/134   train_loss = 2.697
Epoch  26 Batch   96/134   train_loss = 2.664
Epoch  26 Batch  116/134   train_loss = 2.704
Epoch  27 Batch    2/134   train_loss = 2.491
Epoch  27 Batch   22/134   train_loss = 2.505
Epoch  27 Batch   42/134   train_loss = 2.606
Epoch  27 Batch   62/134   train_loss = 2.585
Epoch  27 Batch   82/134   train_loss = 2.497
Epoch  27 Batch  102/134   train_loss = 2.536
Epoch  27 Batch  122/134   train_loss = 2.246
Epoch  28 Batch    8/134   train_loss = 2.473
Epoch  28 Batch   28/134   train_loss = 2.565
Epoch  28 Batch   48/134   train_loss = 2.551
Epoch  28 Batch   68/134   train_loss = 2.523
Epoch  28 Batch   88/134   train_loss = 2.436
Epoch  28 Batch  108/134   train_loss = 2.385
Epoch  28 Batch  128/134   train_loss = 2.650
Epoch  29 Batch   14/134   train_loss = 2.412
Epoch  29 Batch   34/134   train_loss = 2.378
Epoch  29 Batch   54/134   train_loss = 2.423
Epoch  29 Batch   74/134   train_loss = 2.368
Epoch  29 Batch   94/134   train_loss = 2.378
Epoch  29 Batch  114/134   train_loss = 2.463
Epoch  30 Batch    0/134   train_loss = 2.267
Epoch  30 Batch   20/134   train_loss = 2.362
Epoch  30 Batch   40/134   train_loss = 2.305
Epoch  30 Batch   60/134   train_loss = 2.341
Epoch  30 Batch   80/134   train_loss = 2.329
Epoch  30 Batch  100/134   train_loss = 2.204
Epoch  30 Batch  120/134   train_loss = 2.197
Epoch  31 Batch    6/134   train_loss = 2.116
Epoch  31 Batch   26/134   train_loss = 2.312
Epoch  31 Batch   46/134   train_loss = 2.331
Epoch  31 Batch   66/134   train_loss = 2.439
Epoch  31 Batch   86/134   train_loss = 2.305
Epoch  31 Batch  106/134   train_loss = 2.214
Epoch  31 Batch  126/134   train_loss = 2.347
Epoch  32 Batch   12/134   train_loss = 2.141
Epoch  32 Batch   32/134   train_loss = 2.376
Epoch  32 Batch   52/134   train_loss = 2.146
Epoch  32 Batch   72/134   train_loss = 2.136
Epoch  32 Batch   92/134   train_loss = 2.234
Epoch  32 Batch  112/134   train_loss = 2.240
Epoch  32 Batch  132/134   train_loss = 1.977
Epoch  33 Batch   18/134   train_loss = 2.154
Epoch  33 Batch   38/134   train_loss = 2.036
Epoch  33 Batch   58/134   train_loss = 2.250
Epoch  33 Batch   78/134   train_loss = 2.154
Epoch  33 Batch   98/134   train_loss = 2.186
Epoch  33 Batch  118/134   train_loss = 2.100
Epoch  34 Batch    4/134   train_loss = 2.125
Epoch  34 Batch   24/134   train_loss = 2.082
Epoch  34 Batch   44/134   train_loss = 2.085
Epoch  34 Batch   64/134   train_loss = 2.199
Epoch  34 Batch   84/134   train_loss = 2.160
Epoch  34 Batch  104/134   train_loss = 2.075
Epoch  34 Batch  124/134   train_loss = 2.110
Epoch  35 Batch   10/134   train_loss = 2.053
Epoch  35 Batch   30/134   train_loss = 2.224
Epoch  35 Batch   50/134   train_loss = 2.060
Epoch  35 Batch   70/134   train_loss = 2.102
Epoch  35 Batch   90/134   train_loss = 2.067
Epoch  35 Batch  110/134   train_loss = 1.974
Epoch  35 Batch  130/134   train_loss = 1.914
Epoch  36 Batch   16/134   train_loss = 2.054
Epoch  36 Batch   36/134   train_loss = 2.001
Epoch  36 Batch   56/134   train_loss = 1.929
Epoch  36 Batch   76/134   train_loss = 1.941
Epoch  36 Batch   96/134   train_loss = 2.024
Epoch  36 Batch  116/134   train_loss = 1.990
Epoch  37 Batch    2/134   train_loss = 1.984
Epoch  37 Batch   22/134   train_loss = 1.883
Epoch  37 Batch   42/134   train_loss = 1.956
Epoch  37 Batch   62/134   train_loss = 2.031
Epoch  37 Batch   82/134   train_loss = 1.865
Epoch  37 Batch  102/134   train_loss = 1.891
Epoch  37 Batch  122/134   train_loss = 1.588
Epoch  38 Batch    8/134   train_loss = 1.916
Epoch  38 Batch   28/134   train_loss = 2.000
Epoch  38 Batch   48/134   train_loss = 2.088
Epoch  38 Batch   68/134   train_loss = 1.942
Epoch  38 Batch   88/134   train_loss = 1.875
Epoch  38 Batch  108/134   train_loss = 1.815
Epoch  38 Batch  128/134   train_loss = 2.079
Epoch  39 Batch   14/134   train_loss = 1.752
Epoch  39 Batch   34/134   train_loss = 1.821
Epoch  39 Batch   54/134   train_loss = 1.854
Epoch  39 Batch   74/134   train_loss = 1.765
Epoch  39 Batch   94/134   train_loss = 1.856
Epoch  39 Batch  114/134   train_loss = 1.854
Epoch  40 Batch    0/134   train_loss = 1.676
Epoch  40 Batch   20/134   train_loss = 1.794
Epoch  40 Batch   40/134   train_loss = 1.746
Epoch  40 Batch   60/134   train_loss = 1.795
Epoch  40 Batch   80/134   train_loss = 1.849
Epoch  40 Batch  100/134   train_loss = 1.612
Epoch  40 Batch  120/134   train_loss = 1.649
Epoch  41 Batch    6/134   train_loss = 1.585
Epoch  41 Batch   26/134   train_loss = 1.748
Epoch  41 Batch   46/134   train_loss = 1.861
Epoch  41 Batch   66/134   train_loss = 1.908
Epoch  41 Batch   86/134   train_loss = 1.731
Epoch  41 Batch  106/134   train_loss = 1.693
Epoch  41 Batch  126/134   train_loss = 1.734
Epoch  42 Batch   12/134   train_loss = 1.675
Epoch  42 Batch   32/134   train_loss = 1.736
Epoch  42 Batch   52/134   train_loss = 1.597
Epoch  42 Batch   72/134   train_loss = 1.588
Epoch  42 Batch   92/134   train_loss = 1.682
Epoch  42 Batch  112/134   train_loss = 1.651
Epoch  42 Batch  132/134   train_loss = 1.587
Epoch  43 Batch   18/134   train_loss = 1.566
Epoch  43 Batch   38/134   train_loss = 1.528
Epoch  43 Batch   58/134   train_loss = 1.697
Epoch  43 Batch   78/134   train_loss = 1.586
Epoch  43 Batch   98/134   train_loss = 1.608
Epoch  43 Batch  118/134   train_loss = 1.634
Epoch  44 Batch    4/134   train_loss = 1.657
Epoch  44 Batch   24/134   train_loss = 1.587
Epoch  44 Batch   44/134   train_loss = 1.609
Epoch  44 Batch   64/134   train_loss = 1.596
Epoch  44 Batch   84/134   train_loss = 1.565
Epoch  44 Batch  104/134   train_loss = 1.492
Epoch  44 Batch  124/134   train_loss = 1.601
Epoch  45 Batch   10/134   train_loss = 1.654
Epoch  45 Batch   30/134   train_loss = 1.698
Epoch  45 Batch   50/134   train_loss = 1.646
Epoch  45 Batch   70/134   train_loss = 1.627
Epoch  45 Batch   90/134   train_loss = 1.592
Epoch  45 Batch  110/134   train_loss = 1.535
Epoch  45 Batch  130/134   train_loss = 1.467
Epoch  46 Batch   16/134   train_loss = 1.531
Epoch  46 Batch   36/134   train_loss = 1.593
Epoch  46 Batch   56/134   train_loss = 1.651
Epoch  46 Batch   76/134   train_loss = 1.488
Epoch  46 Batch   96/134   train_loss = 1.507
Epoch  46 Batch  116/134   train_loss = 1.511
Epoch  47 Batch    2/134   train_loss = 1.592
Epoch  47 Batch   22/134   train_loss = 1.465
Epoch  47 Batch   42/134   train_loss = 1.505
Epoch  47 Batch   62/134   train_loss = 1.643
Epoch  47 Batch   82/134   train_loss = 1.368
Epoch  47 Batch  102/134   train_loss = 1.467
Epoch  47 Batch  122/134   train_loss = 1.209
Epoch  48 Batch    8/134   train_loss = 1.443
Epoch  48 Batch   28/134   train_loss = 1.522
Epoch  48 Batch   48/134   train_loss = 1.633
Epoch  48 Batch   68/134   train_loss = 1.493
Epoch  48 Batch   88/134   train_loss = 1.526
Epoch  48 Batch  108/134   train_loss = 1.498
Epoch  48 Batch  128/134   train_loss = 1.561
Epoch  49 Batch   14/134   train_loss = 1.381
Epoch  49 Batch   34/134   train_loss = 1.435
Epoch  49 Batch   54/134   train_loss = 1.424
Epoch  49 Batch   74/134   train_loss = 1.413
Epoch  49 Batch   94/134   train_loss = 1.405
Epoch  49 Batch  114/134   train_loss = 1.450
Epoch  50 Batch    0/134   train_loss = 1.306
Epoch  50 Batch   20/134   train_loss = 1.367
Epoch  50 Batch   40/134   train_loss = 1.291
Epoch  50 Batch   60/134   train_loss = 1.375
Epoch  50 Batch   80/134   train_loss = 1.427
Epoch  50 Batch  100/134   train_loss = 1.269
Epoch  50 Batch  120/134   train_loss = 1.261
Epoch  51 Batch    6/134   train_loss = 1.228
Epoch  51 Batch   26/134   train_loss = 1.328
Epoch  51 Batch   46/134   train_loss = 1.416
Epoch  51 Batch   66/134   train_loss = 1.456
Epoch  51 Batch   86/134   train_loss = 1.258
Epoch  51 Batch  106/134   train_loss = 1.209
Epoch  51 Batch  126/134   train_loss = 1.385
Epoch  52 Batch   12/134   train_loss = 1.306
Epoch  52 Batch   32/134   train_loss = 1.281
Epoch  52 Batch   52/134   train_loss = 1.285
Epoch  52 Batch   72/134   train_loss = 1.283
Epoch  52 Batch   92/134   train_loss = 1.449
Epoch  52 Batch  112/134   train_loss = 1.203
Epoch  52 Batch  132/134   train_loss = 1.253
Epoch  53 Batch   18/134   train_loss = 1.236
Epoch  53 Batch   38/134   train_loss = 1.193
Epoch  53 Batch   58/134   train_loss = 1.373
Epoch  53 Batch   78/134   train_loss = 1.267
Epoch  53 Batch   98/134   train_loss = 1.291
Epoch  53 Batch  118/134   train_loss = 1.232
Epoch  54 Batch    4/134   train_loss = 1.236
Epoch  54 Batch   24/134   train_loss = 1.330
Epoch  54 Batch   44/134   train_loss = 1.183
Epoch  54 Batch   64/134   train_loss = 1.234
Epoch  54 Batch   84/134   train_loss = 1.210
Epoch  54 Batch  104/134   train_loss = 1.287
Epoch  54 Batch  124/134   train_loss = 1.229
Epoch  55 Batch   10/134   train_loss = 1.222
Epoch  55 Batch   30/134   train_loss = 1.355
Epoch  55 Batch   50/134   train_loss = 1.284
Epoch  55 Batch   70/134   train_loss = 1.356
Epoch  55 Batch   90/134   train_loss = 1.370
Epoch  55 Batch  110/134   train_loss = 1.175
Epoch  55 Batch  130/134   train_loss = 1.093
Epoch  56 Batch   16/134   train_loss = 1.252
Epoch  56 Batch   36/134   train_loss = 1.237
Epoch  56 Batch   56/134   train_loss = 1.276
Epoch  56 Batch   76/134   train_loss = 1.144
Epoch  56 Batch   96/134   train_loss = 1.275
Epoch  56 Batch  116/134   train_loss = 1.213
Epoch  57 Batch    2/134   train_loss = 1.308
Epoch  57 Batch   22/134   train_loss = 1.079
Epoch  57 Batch   42/134   train_loss = 1.205
Epoch  57 Batch   62/134   train_loss = 1.324
Epoch  57 Batch   82/134   train_loss = 1.161
Epoch  57 Batch  102/134   train_loss = 1.063
Epoch  57 Batch  122/134   train_loss = 0.987
Epoch  58 Batch    8/134   train_loss = 1.121
Epoch  58 Batch   28/134   train_loss = 1.147
Epoch  58 Batch   48/134   train_loss = 1.373
Epoch  58 Batch   68/134   train_loss = 1.153
Epoch  58 Batch   88/134   train_loss = 1.152
Epoch  58 Batch  108/134   train_loss = 1.186
Epoch  58 Batch  128/134   train_loss = 1.246
Epoch  59 Batch   14/134   train_loss = 1.128
Epoch  59 Batch   34/134   train_loss = 1.134
Epoch  59 Batch   54/134   train_loss = 1.191
Epoch  59 Batch   74/134   train_loss = 1.077
Epoch  59 Batch   94/134   train_loss = 1.131
Epoch  59 Batch  114/134   train_loss = 1.088
Epoch  60 Batch    0/134   train_loss = 1.043
Epoch  60 Batch   20/134   train_loss = 1.064
Epoch  60 Batch   40/134   train_loss = 0.981
Epoch  60 Batch   60/134   train_loss = 1.131
Epoch  60 Batch   80/134   train_loss = 1.088
Epoch  60 Batch  100/134   train_loss = 1.010
Epoch  60 Batch  120/134   train_loss = 1.023
Epoch  61 Batch    6/134   train_loss = 1.000
Epoch  61 Batch   26/134   train_loss = 1.017
Epoch  61 Batch   46/134   train_loss = 1.096
Epoch  61 Batch   66/134   train_loss = 1.247
Epoch  61 Batch   86/134   train_loss = 1.017
Epoch  61 Batch  106/134   train_loss = 1.044
Epoch  61 Batch  126/134   train_loss = 1.107
Epoch  62 Batch   12/134   train_loss = 0.970
Epoch  62 Batch   32/134   train_loss = 0.999
Epoch  62 Batch   52/134   train_loss = 0.994
Epoch  62 Batch   72/134   train_loss = 1.073
Epoch  62 Batch   92/134   train_loss = 1.173
Epoch  62 Batch  112/134   train_loss = 0.978
Epoch  62 Batch  132/134   train_loss = 0.967
Epoch  63 Batch   18/134   train_loss = 0.974
Epoch  63 Batch   38/134   train_loss = 0.997
Epoch  63 Batch   58/134   train_loss = 1.126
Epoch  63 Batch   78/134   train_loss = 0.968
Epoch  63 Batch   98/134   train_loss = 1.104
Epoch  63 Batch  118/134   train_loss = 1.037
Epoch  64 Batch    4/134   train_loss = 1.029
Epoch  64 Batch   24/134   train_loss = 1.080
Epoch  64 Batch   44/134   train_loss = 0.996
Epoch  64 Batch   64/134   train_loss = 1.023
Epoch  64 Batch   84/134   train_loss = 0.993
Epoch  64 Batch  104/134   train_loss = 0.973
Epoch  64 Batch  124/134   train_loss = 1.014
Epoch  65 Batch   10/134   train_loss = 0.971
Epoch  65 Batch   30/134   train_loss = 1.196
Epoch  65 Batch   50/134   train_loss = 1.067
Epoch  65 Batch   70/134   train_loss = 1.098
Epoch  65 Batch   90/134   train_loss = 1.130
Epoch  65 Batch  110/134   train_loss = 0.901
Epoch  65 Batch  130/134   train_loss = 0.930
Epoch  66 Batch   16/134   train_loss = 0.945
Epoch  66 Batch   36/134   train_loss = 1.003
Epoch  66 Batch   56/134   train_loss = 1.075
Epoch  66 Batch   76/134   train_loss = 0.975
Epoch  66 Batch   96/134   train_loss = 0.992
Epoch  66 Batch  116/134   train_loss = 0.984
Epoch  67 Batch    2/134   train_loss = 1.065
Epoch  67 Batch   22/134   train_loss = 0.922
Epoch  67 Batch   42/134   train_loss = 0.965
Epoch  67 Batch   62/134   train_loss = 1.049
Epoch  67 Batch   82/134   train_loss = 0.889
Epoch  67 Batch  102/134   train_loss = 0.894
Epoch  67 Batch  122/134   train_loss = 0.847
Epoch  68 Batch    8/134   train_loss = 0.937
Epoch  68 Batch   28/134   train_loss = 0.941
Epoch  68 Batch   48/134   train_loss = 1.134
Epoch  68 Batch   68/134   train_loss = 0.832
Epoch  68 Batch   88/134   train_loss = 0.935
Epoch  68 Batch  108/134   train_loss = 0.989
Epoch  68 Batch  128/134   train_loss = 1.074
Epoch  69 Batch   14/134   train_loss = 0.901
Epoch  69 Batch   34/134   train_loss = 1.012
Epoch  69 Batch   54/134   train_loss = 0.980
Epoch  69 Batch   74/134   train_loss = 0.936
Epoch  69 Batch   94/134   train_loss = 1.003
Epoch  69 Batch  114/134   train_loss = 0.941
Epoch  70 Batch    0/134   train_loss = 0.909
Epoch  70 Batch   20/134   train_loss = 0.999
Epoch  70 Batch   40/134   train_loss = 0.777
Epoch  70 Batch   60/134   train_loss = 0.891
Epoch  70 Batch   80/134   train_loss = 0.922
Epoch  70 Batch  100/134   train_loss = 0.893
Epoch  70 Batch  120/134   train_loss = 0.760
Epoch  71 Batch    6/134   train_loss = 0.778
Epoch  71 Batch   26/134   train_loss = 0.912
Epoch  71 Batch   46/134   train_loss = 0.935
Epoch  71 Batch   66/134   train_loss = 1.110
Epoch  71 Batch   86/134   train_loss = 0.843
Epoch  71 Batch  106/134   train_loss = 0.804
Epoch  71 Batch  126/134   train_loss = 0.902
Epoch  72 Batch   12/134   train_loss = 0.899
Epoch  72 Batch   32/134   train_loss = 0.879
Epoch  72 Batch   52/134   train_loss = 0.845
Epoch  72 Batch   72/134   train_loss = 0.877
Epoch  72 Batch   92/134   train_loss = 0.948
Epoch  72 Batch  112/134   train_loss = 0.801
Epoch  72 Batch  132/134   train_loss = 0.919
Epoch  73 Batch   18/134   train_loss = 0.891
Epoch  73 Batch   38/134   train_loss = 0.827
Epoch  73 Batch   58/134   train_loss = 0.883
Epoch  73 Batch   78/134   train_loss = 0.866
Epoch  73 Batch   98/134   train_loss = 0.948
Epoch  73 Batch  118/134   train_loss = 0.870
Epoch  74 Batch    4/134   train_loss = 0.821
Epoch  74 Batch   24/134   train_loss = 0.861
Epoch  74 Batch   44/134   train_loss = 0.830
Epoch  74 Batch   64/134   train_loss = 0.864
Epoch  74 Batch   84/134   train_loss = 0.763
Epoch  74 Batch  104/134   train_loss = 0.826
Epoch  74 Batch  124/134   train_loss = 0.860
Epoch  75 Batch   10/134   train_loss = 0.883
Epoch  75 Batch   30/134   train_loss = 0.895
Epoch  75 Batch   50/134   train_loss = 0.906
Epoch  75 Batch   70/134   train_loss = 0.859
Epoch  75 Batch   90/134   train_loss = 1.017
Epoch  75 Batch  110/134   train_loss = 0.870
Epoch  75 Batch  130/134   train_loss = 0.798
Epoch  76 Batch   16/134   train_loss = 0.886
Epoch  76 Batch   36/134   train_loss = 0.859
Epoch  76 Batch   56/134   train_loss = 0.824
Epoch  76 Batch   76/134   train_loss = 0.815
Epoch  76 Batch   96/134   train_loss = 0.773
Epoch  76 Batch  116/134   train_loss = 0.771
Epoch  77 Batch    2/134   train_loss = 0.907
Epoch  77 Batch   22/134   train_loss = 0.776
Epoch  77 Batch   42/134   train_loss = 0.769
Epoch  77 Batch   62/134   train_loss = 0.935
Epoch  77 Batch   82/134   train_loss = 0.821
Epoch  77 Batch  102/134   train_loss = 0.830
Epoch  77 Batch  122/134   train_loss = 0.714
Epoch  78 Batch    8/134   train_loss = 0.776
Epoch  78 Batch   28/134   train_loss = 0.753
Epoch  78 Batch   48/134   train_loss = 0.947
Epoch  78 Batch   68/134   train_loss = 0.770
Epoch  78 Batch   88/134   train_loss = 0.817
Epoch  78 Batch  108/134   train_loss = 0.831
Epoch  78 Batch  128/134   train_loss = 0.895
Epoch  79 Batch   14/134   train_loss = 0.792
Epoch  79 Batch   34/134   train_loss = 0.821
Epoch  79 Batch   54/134   train_loss = 0.867
Epoch  79 Batch   74/134   train_loss = 0.779
Epoch  79 Batch   94/134   train_loss = 0.864
Epoch  79 Batch  114/134   train_loss = 0.776
Epoch  80 Batch    0/134   train_loss = 0.802
Epoch  80 Batch   20/134   train_loss = 0.804
Epoch  80 Batch   40/134   train_loss = 0.777
Epoch  80 Batch   60/134   train_loss = 0.886
Epoch  80 Batch   80/134   train_loss = 0.760
Epoch  80 Batch  100/134   train_loss = 0.774
Epoch  80 Batch  120/134   train_loss = 0.697
Epoch  81 Batch    6/134   train_loss = 0.591
Epoch  81 Batch   26/134   train_loss = 0.770
Epoch  81 Batch   46/134   train_loss = 0.745
Epoch  81 Batch   66/134   train_loss = 0.913
Epoch  81 Batch   86/134   train_loss = 0.760
Epoch  81 Batch  106/134   train_loss = 0.764
Epoch  81 Batch  126/134   train_loss = 0.779
Epoch  82 Batch   12/134   train_loss = 0.801
Epoch  82 Batch   32/134   train_loss = 0.757
Epoch  82 Batch   52/134   train_loss = 0.678
Epoch  82 Batch   72/134   train_loss = 0.808
Epoch  82 Batch   92/134   train_loss = 0.855
Epoch  82 Batch  112/134   train_loss = 0.687
Epoch  82 Batch  132/134   train_loss = 0.788
Epoch  83 Batch   18/134   train_loss = 0.757
Epoch  83 Batch   38/134   train_loss = 0.729
Epoch  83 Batch   58/134   train_loss = 0.725
Epoch  83 Batch   78/134   train_loss = 0.734
Epoch  83 Batch   98/134   train_loss = 0.784
Epoch  83 Batch  118/134   train_loss = 0.760
Epoch  84 Batch    4/134   train_loss = 0.672
Epoch  84 Batch   24/134   train_loss = 0.726
Epoch  84 Batch   44/134   train_loss = 0.747
Epoch  84 Batch   64/134   train_loss = 0.697
Epoch  84 Batch   84/134   train_loss = 0.700
Epoch  84 Batch  104/134   train_loss = 0.705
Epoch  84 Batch  124/134   train_loss = 0.747
Epoch  85 Batch   10/134   train_loss = 0.725
Epoch  85 Batch   30/134   train_loss = 0.827
Epoch  85 Batch   50/134   train_loss = 0.808
Epoch  85 Batch   70/134   train_loss = 0.831
Epoch  85 Batch   90/134   train_loss = 0.871
Epoch  85 Batch  110/134   train_loss = 0.694
Epoch  85 Batch  130/134   train_loss = 0.676
Epoch  86 Batch   16/134   train_loss = 0.732
Epoch  86 Batch   36/134   train_loss = 0.770
Epoch  86 Batch   56/134   train_loss = 0.813
Epoch  86 Batch   76/134   train_loss = 0.657
Epoch  86 Batch   96/134   train_loss = 0.738
Epoch  86 Batch  116/134   train_loss = 0.700
Epoch  87 Batch    2/134   train_loss = 0.779
Epoch  87 Batch   22/134   train_loss = 0.701
Epoch  87 Batch   42/134   train_loss = 0.689
Epoch  87 Batch   62/134   train_loss = 0.846
Epoch  87 Batch   82/134   train_loss = 0.698
Epoch  87 Batch  102/134   train_loss = 0.719
Epoch  87 Batch  122/134   train_loss = 0.644
Epoch  88 Batch    8/134   train_loss = 0.710
Epoch  88 Batch   28/134   train_loss = 0.745
Epoch  88 Batch   48/134   train_loss = 0.820
Epoch  88 Batch   68/134   train_loss = 0.706
Epoch  88 Batch   88/134   train_loss = 0.776
Epoch  88 Batch  108/134   train_loss = 0.696
Epoch  88 Batch  128/134   train_loss = 0.816
Epoch  89 Batch   14/134   train_loss = 0.721
Epoch  89 Batch   34/134   train_loss = 0.772
Epoch  89 Batch   54/134   train_loss = 0.697
Epoch  89 Batch   74/134   train_loss = 0.627
Epoch  89 Batch   94/134   train_loss = 0.690
Epoch  89 Batch  114/134   train_loss = 0.717
Epoch  90 Batch    0/134   train_loss = 0.764
Epoch  90 Batch   20/134   train_loss = 0.670
Epoch  90 Batch   40/134   train_loss = 0.653
Epoch  90 Batch   60/134   train_loss = 0.627
Epoch  90 Batch   80/134   train_loss = 0.651
Epoch  90 Batch  100/134   train_loss = 0.661
Epoch  90 Batch  120/134   train_loss = 0.639
Epoch  91 Batch    6/134   train_loss = 0.563
Epoch  91 Batch   26/134   train_loss = 0.685
Epoch  91 Batch   46/134   train_loss = 0.666
Epoch  91 Batch   66/134   train_loss = 0.771
Epoch  91 Batch   86/134   train_loss = 0.654
Epoch  91 Batch  106/134   train_loss = 0.674
Epoch  91 Batch  126/134   train_loss = 0.733
Epoch  92 Batch   12/134   train_loss = 0.669
Epoch  92 Batch   32/134   train_loss = 0.639
Epoch  92 Batch   52/134   train_loss = 0.664
Epoch  92 Batch   72/134   train_loss = 0.649
Epoch  92 Batch   92/134   train_loss = 0.744
Epoch  92 Batch  112/134   train_loss = 0.615
Epoch  92 Batch  132/134   train_loss = 0.695
Epoch  93 Batch   18/134   train_loss = 0.702
Epoch  93 Batch   38/134   train_loss = 0.640
Epoch  93 Batch   58/134   train_loss = 0.737
Epoch  93 Batch   78/134   train_loss = 0.634
Epoch  93 Batch   98/134   train_loss = 0.748
Epoch  93 Batch  118/134   train_loss = 0.656
Epoch  94 Batch    4/134   train_loss = 0.648
Epoch  94 Batch   24/134   train_loss = 0.661
Epoch  94 Batch   44/134   train_loss = 0.603
Epoch  94 Batch   64/134   train_loss = 0.624
Epoch  94 Batch   84/134   train_loss = 0.640
Epoch  94 Batch  104/134   train_loss = 0.648
Epoch  94 Batch  124/134   train_loss = 0.651
Epoch  95 Batch   10/134   train_loss = 0.692
Epoch  95 Batch   30/134   train_loss = 0.717
Epoch  95 Batch   50/134   train_loss = 0.639
Epoch  95 Batch   70/134   train_loss = 0.769
Epoch  95 Batch   90/134   train_loss = 0.796
Epoch  95 Batch  110/134   train_loss = 0.624
Epoch  95 Batch  130/134   train_loss = 0.648
Epoch  96 Batch   16/134   train_loss = 0.690
Epoch  96 Batch   36/134   train_loss = 0.644
Epoch  96 Batch   56/134   train_loss = 0.644
Epoch  96 Batch   76/134   train_loss = 0.636
Epoch  96 Batch   96/134   train_loss = 0.632
Epoch  96 Batch  116/134   train_loss = 0.630
Epoch  97 Batch    2/134   train_loss = 0.698
Epoch  97 Batch   22/134   train_loss = 0.599
Epoch  97 Batch   42/134   train_loss = 0.599
Epoch  97 Batch   62/134   train_loss = 0.647
Epoch  97 Batch   82/134   train_loss = 0.619
Epoch  97 Batch  102/134   train_loss = 0.661
Epoch  97 Batch  122/134   train_loss = 0.588
Epoch  98 Batch    8/134   train_loss = 0.628
Epoch  98 Batch   28/134   train_loss = 0.586
Epoch  98 Batch   48/134   train_loss = 0.740
Epoch  98 Batch   68/134   train_loss = 0.598
Epoch  98 Batch   88/134   train_loss = 0.586
Epoch  98 Batch  108/134   train_loss = 0.622
Epoch  98 Batch  128/134   train_loss = 0.783
Epoch  99 Batch   14/134   train_loss = 0.660
Epoch  99 Batch   34/134   train_loss = 0.648
Epoch  99 Batch   54/134   train_loss = 0.736
Epoch  99 Batch   74/134   train_loss = 0.596
Epoch  99 Batch   94/134   train_loss = 0.608
Epoch  99 Batch  114/134   train_loss = 0.585
Model Trained and Saved

Save Parameters

Save seq_length and save_dir for generating a new TV script.


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

Checkpoint


In [13]:
"""
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 [14]:
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
    inputs= 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 inputs, 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 [15]:
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
    #idx = np.argmax(probabilities)
    n=10
    #get the top n indice of words 
    top_n = probabilities.argsort()[-n:]
    p_top_n = probabilities[top_n]
    #normalization
    p_top_n = p_top_n / np.sum(p_top_n)
    
    idx = np.random.choice(top_n,1,p=p_top_n)
    return int_to_vocab[idx[0]]


"""
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 [16]:
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: hey! hey! quit close!
homer_simpson:(reading)" we had to dig than homer-- and the worst girl is one a kind of duff and do you got this one, huh?
moe_szyslak: yeah. uh me was an abusive into" here, which not see as even uh, while lenny, but you're gonna carve my back to my fridge. and that will do it!
homer_simpson:(small idea) was fat tony...
kang: all people!
moe_szyslak: ah, moe can just all close, buy a snake friends?
moe_szyslak: what's the matter, carl?
moe_szyslak: righ bad is it. i used a bunion that?
moe_szyslak: i just was sitting here to get it) uh, now i think what i'm going to know it. it was too thing to really mr. good can.
moe that's you to be people with him-- take it at the special-- i saw a wife! i'm gonna be(flowers) why don't you're two out as your life

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.