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 = (8, 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 8 to 100:


Moe_Szyslak: Ah, isn't that nice. Now, there is a politician who cares.
Barney_Gumble: If I ever vote, it'll be for him. (BELCH)


Barney_Gumble: Hey Homer, how's your neighbor's store doing?
Homer_Simpson: Lousy. He just sits there all day. He'd have a great job if he didn't own the place. (CHUCKLES)
Moe_Szyslak: (STRUGGLING WITH CORKSCREW) Crummy right-handed corkscrews! What does he sell?
Homer_Simpson: Uh, well actually, Moe...
HOMER_(CONT'D: I dunno.


Moe_Szyslak: Looks like this is the end.
Barney_Gumble: That's all right. I couldn't have led a richer life.
Barney_Gumble: So the next time somebody tells you county folk are good, honest people, you can spit in their faces for me!
Lisa_Simpson: I will, Mr. Gumbel. But if you'll excuse me, I'm profiling my dad for the school paper. I thought it would be neat to follow him around for a day to see what makes him tick.
Barney_Gumble: Oh, that's sweet. I used to follow my dad to a lot of bars too. (BELCH)
Moe_Szyslak: Here you go. One beer, one chocolate milk.
Lisa_Simpson: Uh, excuse me, I have the chocolate milk.
Moe_Szyslak: Oh.
Moe_Szyslak: What's the matter, Homer? The depressin' effects of alcohol usually don't kick in 'til closing time.
Lisa_Simpson: He's just a little nervous. (PROUDLY) He has to give a speech tomorrow on "How To Keep Cool In A Crisis."
Homer_Simpson: (SOBS) What am I gonna do? What am I gonna do?
Barney_Gumble: Hey, I had to give a speech once. I was pretty nervous, so I used a little trick. I pictured everyone in their underwear. The judge, the jury, my lawyer, everybody.
Homer_Simpson: Did it work?
Barney_Gumble: I'm a free man, ain't I?
Barney_Gumble: Whoa!
Barney_Gumble: Huh? A pretzel? Wow, looks like I pulled a Homer!


Patrons: (MUMBLING, NOT IN UNISON) Happy thoughts... happy thoughts... we love that boy.
Moe_Szyslak: (INTO PHONE) Moe's Tavern. Hold on, I'll check. ... (LOUD) Hey everybody! I'm a stupid moron with an ugly face and a big butt, and my butt smells, and I like to kiss my own butt.
Barney_Gumble: That's a new one (LAUGHING).
Moe_Szyslak: Now wait a minute...


Homer_Simpson: Hurry, Moe, hurry! I've only got five minutes till the music store closes.
Moe_Szyslak: Why don't you go there first?
Homer_Simpson: Hey, do I tell you how to do your job?
Moe_Szyslak: Sorry, Homer.
Homer_Simpson: You know, if you tip the glass, there won't be so much foam on top.
Moe_Szyslak: Sorry, Homer.
Homer_Simpson: (LOOKING AT WATCH) Ah. Finished with fifteen seconds to spare.
Little_Man: (CONCERNED) What's the matter, buddy?
Homer_Simpson: The moron next door closed early!
Little_Man: (STIFFENING) I happen to be that moron.
Homer_Simpson: Oh, me and my trenchant mouth.
Homer_Simpson: Please, you've got to open that store.
Little_Man: Let me think about it... Eh... No.
Homer_Simpson: Okay, okay. But I want you to see a picture of the little girl you're disappointing. (GOES THROUGH HIS WALLET) Well I don't have one.
Moe_Szyslak: (TO LITTLE MAN) Come on, Jer. Open up. Be a pal. Remember when I pulled you and your wife out of that burning car?
Little_Man: (GRUDGINGLY) Okay. Okay. But now we're even. (TO HOMER) So what does your daughter need?
Homer_Simpson: (SMOOTHLY) I'll have you know, I wrote it down.
Homer_Simpson: Number Four and a half -- Stupid gum!
Homer_Simpson: Number Four and a Half reed! Whoo hoo!
Little_Man: Uh-huh. And what instrument does she play?
Homer_Simpson: (SUNK) I dunno.


Moe_Szyslak: (TO PATRONS) Figure of speech.
Moe_Szyslak: Hiya, Homer. (SIGHS)
Homer_Simpson: What's the matter, Moe?
Moe_Szyslak: Ah, business is slow. People today are healthier and drinking less. You know, if it wasn't for the Junior High school next door no one would even use the cigarette machine.
Homer_Simpson: (MOUTH FULL) Yeah, things are tough all over.
Moe_Szyslak: Increased job satisfaction and family togetherness are poison for a purveyor of mind-numbing intoxicants like myself.
Homer_Simpson: Could I get a beer?
Moe_Szyslak: Uh, yeah, sure.
Moe_Szyslak: Oh sorry, I forgot we're out of beer.
Moe_Szyslak: Yeah, I know, I got behind on my beer payments. The distributor cut me off and I spent my last ten grand on the "Love Tester".
Moe_Szyslak: You're too late, Homer. Barney sucked it dry. Cut his gums up pretty bad.
Moe_Szyslak: Take it easy, Homer. I learned how to make other drinks at Bartender's School.
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)
    """
    # TODO: Implement Function
    words = set()
    index_to_word = {}
    word_to_index = {}
    
    for word in text:
        words.add(word)
    
    for index, word in enumerate(words):
        #print (word,index)
        index_to_word[index] = word
        word_to_index[word] = index
        
    return word_to_index, index_to_word


"""
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
    ret = {}
    ret['.'] = "||Period||" #( . )
    ret[','] = "||Comma||" #( , )
    ret['"'] = "||Quotation_Mark||" # ( " )
    ret[';'] = "||Semicolon||" #( ; )
    ret['!'] = "||Exclamation_mark||" #( ! )
    ret['?'] = "||Question_mark||" #( ? )
    ret['('] = "||Left_Parentheses||" #( ( )
    ret[')'] = "||Right_Parentheses||" #( ) )
    ret['--'] = "||Dash||" # ( -- )
    ret['\n'] = "||Return||" # ( \n )
    
    return ret

"""
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 [6]:
"""
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 [7]:
"""
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 [8]:
def get_inputs():
    """
    Create TF Placeholders for input, targets, and learning rate.
    :return: Tuple (input, targets, learning rate)
    """
    # TODO: Implement Function
    inputs        = tf.placeholder(tf.int32, [None, None ], name="input")
    targets       = tf.placeholder(tf.int32, [None, None ], name="targets")
    learning_rate = tf.placeholder(tf.float32, None, name="LearningRate")
    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 [9]:
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
    layer_count = 2
    keep_prob = tf.constant(0.7,tf.float32, name="keep_prob")
    lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size, state_is_tuple=True)
    lstm2 = tf.contrib.rnn.BasicLSTMCell(rnn_size, state_is_tuple=True)
    
    dropout = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
    
    cell = tf.contrib.rnn.MultiRNNCell([lstm, lstm2], state_is_tuple=True)
    initial_state = cell.zero_state( batch_size, tf.float32)
    initial_state = tf.identity(initial_state, name="initial_state" )
    #_outputs, final_state = tf.nn.rnn(cell, rnn_inputs, initial_state=init_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 [10]:
import random
import math
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    
    ret = tf.Variable(tf.random_uniform((vocab_size, embed_dim), -1, 1))
    ret = tf.nn.embedding_lookup(ret, input_data)
    print("shape {}".format(ret.get_shape().as_list()))
    return ret


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


shape [50, 5, 256]
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 [11]:
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
    output, final_state = tf.nn.dynamic_rnn(cell, inputs, dtype = tf.float32)
    final_state = tf.identity (final_state, "final_state")
    return output, 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 [12]:
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
    embedded = get_embed(input_data, vocab_size, rnn_size)
    out, fin = build_rnn(cell, embedded) 
    out = tf.contrib.layers.fully_connected(out,vocab_size, activation_fn=None)
    
    out_shape = out.get_shape().as_list() 
    print("build_nn embedded{}, out:{}, fin:{}".format(embedded.get_shape().as_list(),out_shape, fin.get_shape().as_list()))
    print()
    return out, fin


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


shape [128, 5, 256]
build_nn embedded[128, 5, 256], out:[128, 5, 27], fin:[2, 2, None, 256]

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


In [13]:
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
    text = int_text
    ret = np.array([])
    inputs = []
    targets = []
    text_len = len(text) - len(text) % (seq_length*batch_size)
    print ("get_batches text:{}, batch:{}, seq:{}".format(text_len, batch_size, seq_length))
    ret=[]  
    
    for i in range(0, text_len-1, seq_length):
        seq = list(int_text[i:i+seq_length])
        inputs.append(list(int_text[i:i+seq_length]))
        targets.append(list(int_text[i+1:i+seq_length+1]))
        
        
    for i in range(0,len(inputs),batch_size):
        pos=batch_size
        #batch_pair = n
        ret.append([inputs[i:i+batch_size], targets[i:i+batch_size]])
    ret = np.asanyarray(ret)
    print("batch test ", ret.shape, ret[3,:,2])
    return ret


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


get_batches text:4480, batch:128, seq:5
batch test  (7, 2, 128, 5) [[1930 1931 1932 1933 1934]
 [1931 1932 1933 1934 1935]]
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 [14]:
# Number of Epochs
num_epochs = 300  # previously 150, but want to get lower loss.
# Batch Size
batch_size = 128
# RNN Size
rnn_size = 1024
# Embedding Dimension Size
embed_dim = None
# Sequence Length
seq_length = 12  # already discouraged from using 6 and 16,  avg sentence length being 10-12
# I'm favoring this formula frm the curse of lerning rate being a function of parameter count. 
#This is guess work (empirical), but gives good results.
learning_rate = 1/np.sqrt(rnn_size*seq_length*6700)
print( "learning rate {}, vocab_size {}".format(learning_rate,6700))
"""
                100      inf
    0.0012 --  1.666       860-1210: 1.259
    0.00012 -- 5.878      1920-2190: 1.070
    0.000012   7.4             3000: 2.107
    0.00012 -- 6.047           3000: 0.964-- embedding w truncated normal.
    
    1024
    0.00812  -- 1.182 stuck
    0.00612  -- 0.961 stuck
"""

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

tf.set_random_seed(42)

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


learning rate 0.00011021025247790598, vocab_size 6700

Build the Graph

Build the graph using the neural network you implemented.


In [15]:
"""
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)


shape [None, None, 1024]
build_nn embedded[None, None, 1024], out:[None, None, 6779], fin:[2, 2, None, 1024]

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 [16]:
"""
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')


get_batches text:67584, batch:128, seq:12
batch test  (44, 2, 128, 12) [[1972 4955 4729 4631 5087 3522 2369 3937 4265 2961 5220 1904]
 [4955 4729 4631 5087 3522 2369 3937 4265 2961 5220 1904 5150]]
Epoch   0 Batch    0/44   train_loss = 8.822
Epoch   0 Batch   20/44   train_loss = 7.185
Epoch   0 Batch   40/44   train_loss = 6.576
Epoch   1 Batch   16/44   train_loss = 6.069
Epoch   1 Batch   36/44   train_loss = 6.238
Epoch   2 Batch   12/44   train_loss = 6.052
Epoch   2 Batch   32/44   train_loss = 6.100
Epoch   3 Batch    8/44   train_loss = 5.914
Epoch   3 Batch   28/44   train_loss = 5.938
Epoch   4 Batch    4/44   train_loss = 5.665
Epoch   4 Batch   24/44   train_loss = 5.779
Epoch   5 Batch    0/44   train_loss = 5.496
Epoch   5 Batch   20/44   train_loss = 5.449
Epoch   5 Batch   40/44   train_loss = 5.773
Epoch   6 Batch   16/44   train_loss = 5.305
Epoch   6 Batch   36/44   train_loss = 5.540
Epoch   7 Batch   12/44   train_loss = 5.336
Epoch   7 Batch   32/44   train_loss = 5.496
Epoch   8 Batch    8/44   train_loss = 5.311
Epoch   8 Batch   28/44   train_loss = 5.423
Epoch   9 Batch    4/44   train_loss = 5.157
Epoch   9 Batch   24/44   train_loss = 5.334
Epoch  10 Batch    0/44   train_loss = 5.050
Epoch  10 Batch   20/44   train_loss = 5.004
Epoch  10 Batch   40/44   train_loss = 5.396
Epoch  11 Batch   16/44   train_loss = 4.912
Epoch  11 Batch   36/44   train_loss = 5.183
Epoch  12 Batch   12/44   train_loss = 4.993
Epoch  12 Batch   32/44   train_loss = 5.174
Epoch  13 Batch    8/44   train_loss = 4.987
Epoch  13 Batch   28/44   train_loss = 5.136
Epoch  14 Batch    4/44   train_loss = 4.859
Epoch  14 Batch   24/44   train_loss = 5.050
Epoch  15 Batch    0/44   train_loss = 4.787
Epoch  15 Batch   20/44   train_loss = 4.730
Epoch  15 Batch   40/44   train_loss = 5.131
Epoch  16 Batch   16/44   train_loss = 4.661
Epoch  16 Batch   36/44   train_loss = 4.930
Epoch  17 Batch   12/44   train_loss = 4.767
Epoch  17 Batch   32/44   train_loss = 4.946
Epoch  18 Batch    8/44   train_loss = 4.754
Epoch  18 Batch   28/44   train_loss = 4.920
Epoch  19 Batch    4/44   train_loss = 4.635
Epoch  19 Batch   24/44   train_loss = 4.825
Epoch  20 Batch    0/44   train_loss = 4.583
Epoch  20 Batch   20/44   train_loss = 4.517
Epoch  20 Batch   40/44   train_loss = 4.903
Epoch  21 Batch   16/44   train_loss = 4.464
Epoch  21 Batch   36/44   train_loss = 4.725
Epoch  22 Batch   12/44   train_loss = 4.584
Epoch  22 Batch   32/44   train_loss = 4.764
Epoch  23 Batch    8/44   train_loss = 4.567
Epoch  23 Batch   28/44   train_loss = 4.738
Epoch  24 Batch    4/44   train_loss = 4.458
Epoch  24 Batch   24/44   train_loss = 4.633
Epoch  25 Batch    0/44   train_loss = 4.408
Epoch  25 Batch   20/44   train_loss = 4.346
Epoch  25 Batch   40/44   train_loss = 4.711
Epoch  26 Batch   16/44   train_loss = 4.296
Epoch  26 Batch   36/44   train_loss = 4.556
Epoch  27 Batch   12/44   train_loss = 4.419
Epoch  27 Batch   32/44   train_loss = 4.598
Epoch  28 Batch    8/44   train_loss = 4.404
Epoch  28 Batch   28/44   train_loss = 4.572
Epoch  29 Batch    4/44   train_loss = 4.300
Epoch  29 Batch   24/44   train_loss = 4.452
Epoch  30 Batch    0/44   train_loss = 4.251
Epoch  30 Batch   20/44   train_loss = 4.196
Epoch  30 Batch   40/44   train_loss = 4.530
Epoch  31 Batch   16/44   train_loss = 4.143
Epoch  31 Batch   36/44   train_loss = 4.389
Epoch  32 Batch   12/44   train_loss = 4.256
Epoch  32 Batch   32/44   train_loss = 4.442
Epoch  33 Batch    8/44   train_loss = 4.255
Epoch  33 Batch   28/44   train_loss = 4.428
Epoch  34 Batch    4/44   train_loss = 4.175
Epoch  34 Batch   24/44   train_loss = 4.299
Epoch  35 Batch    0/44   train_loss = 4.133
Epoch  35 Batch   20/44   train_loss = 4.085
Epoch  35 Batch   40/44   train_loss = 4.360
Epoch  36 Batch   16/44   train_loss = 4.006
Epoch  36 Batch   36/44   train_loss = 4.239
Epoch  37 Batch   12/44   train_loss = 4.128
Epoch  37 Batch   32/44   train_loss = 4.292
Epoch  38 Batch    8/44   train_loss = 4.126
Epoch  38 Batch   28/44   train_loss = 4.271
Epoch  39 Batch    4/44   train_loss = 4.035
Epoch  39 Batch   24/44   train_loss = 4.157
Epoch  40 Batch    0/44   train_loss = 4.002
Epoch  40 Batch   20/44   train_loss = 3.945
Epoch  40 Batch   40/44   train_loss = 4.219
Epoch  41 Batch   16/44   train_loss = 3.888
Epoch  41 Batch   36/44   train_loss = 4.123
Epoch  42 Batch   12/44   train_loss = 3.977
Epoch  42 Batch   32/44   train_loss = 4.150
Epoch  43 Batch    8/44   train_loss = 3.991
Epoch  43 Batch   28/44   train_loss = 4.121
Epoch  44 Batch    4/44   train_loss = 3.888
Epoch  44 Batch   24/44   train_loss = 3.981
Epoch  45 Batch    0/44   train_loss = 3.858
Epoch  45 Batch   20/44   train_loss = 3.831
Epoch  45 Batch   40/44   train_loss = 4.063
Epoch  46 Batch   16/44   train_loss = 3.741
Epoch  46 Batch   36/44   train_loss = 3.970
Epoch  47 Batch   12/44   train_loss = 3.842
Epoch  47 Batch   32/44   train_loss = 3.990
Epoch  48 Batch    8/44   train_loss = 3.840
Epoch  48 Batch   28/44   train_loss = 3.962
Epoch  49 Batch    4/44   train_loss = 3.723
Epoch  49 Batch   24/44   train_loss = 3.824
Epoch  50 Batch    0/44   train_loss = 3.719
Epoch  50 Batch   20/44   train_loss = 3.685
Epoch  50 Batch   40/44   train_loss = 3.894
Epoch  51 Batch   16/44   train_loss = 3.601
Epoch  51 Batch   36/44   train_loss = 3.804
Epoch  52 Batch   12/44   train_loss = 3.698
Epoch  52 Batch   32/44   train_loss = 3.833
Epoch  53 Batch    8/44   train_loss = 3.701
Epoch  53 Batch   28/44   train_loss = 3.790
Epoch  54 Batch    4/44   train_loss = 3.585
Epoch  54 Batch   24/44   train_loss = 3.682
Epoch  55 Batch    0/44   train_loss = 3.590
Epoch  55 Batch   20/44   train_loss = 3.598
Epoch  55 Batch   40/44   train_loss = 3.747
Epoch  56 Batch   16/44   train_loss = 3.537
Epoch  56 Batch   36/44   train_loss = 3.737
Epoch  57 Batch   12/44   train_loss = 3.581
Epoch  57 Batch   32/44   train_loss = 3.724
Epoch  58 Batch    8/44   train_loss = 3.580
Epoch  58 Batch   28/44   train_loss = 3.703
Epoch  59 Batch    4/44   train_loss = 3.471
Epoch  59 Batch   24/44   train_loss = 3.576
Epoch  60 Batch    0/44   train_loss = 3.456
Epoch  60 Batch   20/44   train_loss = 3.462
Epoch  60 Batch   40/44   train_loss = 3.621
Epoch  61 Batch   16/44   train_loss = 3.364
Epoch  61 Batch   36/44   train_loss = 3.567
Epoch  62 Batch   12/44   train_loss = 3.443
Epoch  62 Batch   32/44   train_loss = 3.575
Epoch  63 Batch    8/44   train_loss = 3.502
Epoch  63 Batch   28/44   train_loss = 3.512
Epoch  64 Batch    4/44   train_loss = 3.319
Epoch  64 Batch   24/44   train_loss = 3.391
Epoch  65 Batch    0/44   train_loss = 3.318
Epoch  65 Batch   20/44   train_loss = 3.336
Epoch  65 Batch   40/44   train_loss = 3.428
Epoch  66 Batch   16/44   train_loss = 3.228
Epoch  66 Batch   36/44   train_loss = 3.353
Epoch  67 Batch   12/44   train_loss = 3.272
Epoch  67 Batch   32/44   train_loss = 3.387
Epoch  68 Batch    8/44   train_loss = 3.310
Epoch  68 Batch   28/44   train_loss = 3.359
Epoch  69 Batch    4/44   train_loss = 3.167
Epoch  69 Batch   24/44   train_loss = 3.234
Epoch  70 Batch    0/44   train_loss = 3.209
Epoch  70 Batch   20/44   train_loss = 3.212
Epoch  70 Batch   40/44   train_loss = 3.258
Epoch  71 Batch   16/44   train_loss = 3.125
Epoch  71 Batch   36/44   train_loss = 3.222
Epoch  72 Batch   12/44   train_loss = 3.188
Epoch  72 Batch   32/44   train_loss = 3.290
Epoch  73 Batch    8/44   train_loss = 3.263
Epoch  73 Batch   28/44   train_loss = 3.300
Epoch  74 Batch    4/44   train_loss = 3.075
Epoch  74 Batch   24/44   train_loss = 3.270
Epoch  75 Batch    0/44   train_loss = 3.218
Epoch  75 Batch   20/44   train_loss = 3.169
Epoch  75 Batch   40/44   train_loss = 3.237
Epoch  76 Batch   16/44   train_loss = 3.101
Epoch  76 Batch   36/44   train_loss = 3.173
Epoch  77 Batch   12/44   train_loss = 3.073
Epoch  77 Batch   32/44   train_loss = 3.178
Epoch  78 Batch    8/44   train_loss = 3.131
Epoch  78 Batch   28/44   train_loss = 3.120
Epoch  79 Batch    4/44   train_loss = 2.971
Epoch  79 Batch   24/44   train_loss = 3.046
Epoch  80 Batch    0/44   train_loss = 3.016
Epoch  80 Batch   20/44   train_loss = 3.004
Epoch  80 Batch   40/44   train_loss = 3.003
Epoch  81 Batch   16/44   train_loss = 2.942
Epoch  81 Batch   36/44   train_loss = 3.043
Epoch  82 Batch   12/44   train_loss = 2.926
Epoch  82 Batch   32/44   train_loss = 2.994
Epoch  83 Batch    8/44   train_loss = 2.989
Epoch  83 Batch   28/44   train_loss = 3.012
Epoch  84 Batch    4/44   train_loss = 2.824
Epoch  84 Batch   24/44   train_loss = 2.869
Epoch  85 Batch    0/44   train_loss = 2.832
Epoch  85 Batch   20/44   train_loss = 2.844
Epoch  85 Batch   40/44   train_loss = 2.879
Epoch  86 Batch   16/44   train_loss = 2.760
Epoch  86 Batch   36/44   train_loss = 2.812
Epoch  87 Batch   12/44   train_loss = 2.752
Epoch  87 Batch   32/44   train_loss = 2.814
Epoch  88 Batch    8/44   train_loss = 2.832
Epoch  88 Batch   28/44   train_loss = 2.789
Epoch  89 Batch    4/44   train_loss = 2.668
Epoch  89 Batch   24/44   train_loss = 2.675
Epoch  90 Batch    0/44   train_loss = 2.686
Epoch  90 Batch   20/44   train_loss = 2.700
Epoch  90 Batch   40/44   train_loss = 2.656
Epoch  91 Batch   16/44   train_loss = 2.616
Epoch  91 Batch   36/44   train_loss = 2.626
Epoch  92 Batch   12/44   train_loss = 2.608
Epoch  92 Batch   32/44   train_loss = 2.664
Epoch  93 Batch    8/44   train_loss = 2.662
Epoch  93 Batch   28/44   train_loss = 2.620
Epoch  94 Batch    4/44   train_loss = 2.515
Epoch  94 Batch   24/44   train_loss = 2.558
Epoch  95 Batch    0/44   train_loss = 2.559
Epoch  95 Batch   20/44   train_loss = 2.573
Epoch  95 Batch   40/44   train_loss = 2.494
Epoch  96 Batch   16/44   train_loss = 2.480
Epoch  96 Batch   36/44   train_loss = 2.513
Epoch  97 Batch   12/44   train_loss = 2.482
Epoch  97 Batch   32/44   train_loss = 2.489
Epoch  98 Batch    8/44   train_loss = 2.512
Epoch  98 Batch   28/44   train_loss = 2.482
Epoch  99 Batch    4/44   train_loss = 2.415
Epoch  99 Batch   24/44   train_loss = 2.369
Epoch 100 Batch    0/44   train_loss = 2.419
Epoch 100 Batch   20/44   train_loss = 2.457
Epoch 100 Batch   40/44   train_loss = 2.416
Epoch 101 Batch   16/44   train_loss = 2.468
Epoch 101 Batch   36/44   train_loss = 2.415
Epoch 102 Batch   12/44   train_loss = 2.362
Epoch 102 Batch   32/44   train_loss = 2.498
Epoch 103 Batch    8/44   train_loss = 2.471
Epoch 103 Batch   28/44   train_loss = 2.486
Epoch 104 Batch    4/44   train_loss = 2.337
Epoch 104 Batch   24/44   train_loss = 2.310
Epoch 105 Batch    0/44   train_loss = 2.325
Epoch 105 Batch   20/44   train_loss = 2.330
Epoch 105 Batch   40/44   train_loss = 2.249
Epoch 106 Batch   16/44   train_loss = 2.248
Epoch 106 Batch   36/44   train_loss = 2.208
Epoch 107 Batch   12/44   train_loss = 2.230
Epoch 107 Batch   32/44   train_loss = 2.257
Epoch 108 Batch    8/44   train_loss = 2.264
Epoch 108 Batch   28/44   train_loss = 2.226
Epoch 109 Batch    4/44   train_loss = 2.213
Epoch 109 Batch   24/44   train_loss = 2.106
Epoch 110 Batch    0/44   train_loss = 2.219
Epoch 110 Batch   20/44   train_loss = 2.201
Epoch 110 Batch   40/44   train_loss = 2.127
Epoch 111 Batch   16/44   train_loss = 2.205
Epoch 111 Batch   36/44   train_loss = 2.090
Epoch 112 Batch   12/44   train_loss = 2.181
Epoch 112 Batch   32/44   train_loss = 2.161
Epoch 113 Batch    8/44   train_loss = 2.243
Epoch 113 Batch   28/44   train_loss = 2.106
Epoch 114 Batch    4/44   train_loss = 2.082
Epoch 114 Batch   24/44   train_loss = 2.029
Epoch 115 Batch    0/44   train_loss = 2.077
Epoch 115 Batch   20/44   train_loss = 2.101
Epoch 115 Batch   40/44   train_loss = 1.927
Epoch 116 Batch   16/44   train_loss = 2.038
Epoch 116 Batch   36/44   train_loss = 1.937
Epoch 117 Batch   12/44   train_loss = 1.949
Epoch 117 Batch   32/44   train_loss = 1.988
Epoch 118 Batch    8/44   train_loss = 2.003
Epoch 118 Batch   28/44   train_loss = 1.926
Epoch 119 Batch    4/44   train_loss = 1.907
Epoch 119 Batch   24/44   train_loss = 1.833
Epoch 120 Batch    0/44   train_loss = 1.920
Epoch 120 Batch   20/44   train_loss = 1.918
Epoch 120 Batch   40/44   train_loss = 1.788
Epoch 121 Batch   16/44   train_loss = 1.908
Epoch 121 Batch   36/44   train_loss = 1.813
Epoch 122 Batch   12/44   train_loss = 1.786
Epoch 122 Batch   32/44   train_loss = 1.831
Epoch 123 Batch    8/44   train_loss = 1.885
Epoch 123 Batch   28/44   train_loss = 1.783
Epoch 124 Batch    4/44   train_loss = 1.770
Epoch 124 Batch   24/44   train_loss = 1.662
Epoch 125 Batch    0/44   train_loss = 1.777
Epoch 125 Batch   20/44   train_loss = 1.808
Epoch 125 Batch   40/44   train_loss = 1.662
Epoch 126 Batch   16/44   train_loss = 1.736
Epoch 126 Batch   36/44   train_loss = 1.640
Epoch 127 Batch   12/44   train_loss = 1.662
Epoch 127 Batch   32/44   train_loss = 1.704
Epoch 128 Batch    8/44   train_loss = 1.712
Epoch 128 Batch   28/44   train_loss = 1.621
Epoch 129 Batch    4/44   train_loss = 1.678
Epoch 129 Batch   24/44   train_loss = 1.508
Epoch 130 Batch    0/44   train_loss = 1.674
Epoch 130 Batch   20/44   train_loss = 1.647
Epoch 130 Batch   40/44   train_loss = 1.493
Epoch 131 Batch   16/44   train_loss = 1.630
Epoch 131 Batch   36/44   train_loss = 1.492
Epoch 132 Batch   12/44   train_loss = 1.568
Epoch 132 Batch   32/44   train_loss = 1.529
Epoch 133 Batch    8/44   train_loss = 1.555
Epoch 133 Batch   28/44   train_loss = 1.529
Epoch 134 Batch    4/44   train_loss = 1.528
Epoch 134 Batch   24/44   train_loss = 1.391
Epoch 135 Batch    0/44   train_loss = 1.517
Epoch 135 Batch   20/44   train_loss = 1.528
Epoch 135 Batch   40/44   train_loss = 1.371
Epoch 136 Batch   16/44   train_loss = 1.473
Epoch 136 Batch   36/44   train_loss = 1.378
Epoch 137 Batch   12/44   train_loss = 1.431
Epoch 137 Batch   32/44   train_loss = 1.425
Epoch 138 Batch    8/44   train_loss = 1.441
Epoch 138 Batch   28/44   train_loss = 1.377
Epoch 139 Batch    4/44   train_loss = 1.452
Epoch 139 Batch   24/44   train_loss = 1.290
Epoch 140 Batch    0/44   train_loss = 1.402
Epoch 140 Batch   20/44   train_loss = 1.466
Epoch 140 Batch   40/44   train_loss = 1.252
Epoch 141 Batch   16/44   train_loss = 1.387
Epoch 141 Batch   36/44   train_loss = 1.252
Epoch 142 Batch   12/44   train_loss = 1.307
Epoch 142 Batch   32/44   train_loss = 1.342
Epoch 143 Batch    8/44   train_loss = 1.356
Epoch 143 Batch   28/44   train_loss = 1.265
Epoch 144 Batch    4/44   train_loss = 1.326
Epoch 144 Batch   24/44   train_loss = 1.172
Epoch 145 Batch    0/44   train_loss = 1.352
Epoch 145 Batch   20/44   train_loss = 1.373
Epoch 145 Batch   40/44   train_loss = 1.268
Epoch 146 Batch   16/44   train_loss = 1.285
Epoch 146 Batch   36/44   train_loss = 1.301
Epoch 147 Batch   12/44   train_loss = 1.282
Epoch 147 Batch   32/44   train_loss = 1.301
Epoch 148 Batch    8/44   train_loss = 1.277
Epoch 148 Batch   28/44   train_loss = 1.233
Epoch 149 Batch    4/44   train_loss = 1.302
Epoch 149 Batch   24/44   train_loss = 1.085
Epoch 150 Batch    0/44   train_loss = 1.260
Epoch 150 Batch   20/44   train_loss = 1.242
Epoch 150 Batch   40/44   train_loss = 1.089
Epoch 151 Batch   16/44   train_loss = 1.187
Epoch 151 Batch   36/44   train_loss = 1.070
Epoch 152 Batch   12/44   train_loss = 1.139
Epoch 152 Batch   32/44   train_loss = 1.103
Epoch 153 Batch    8/44   train_loss = 1.120
Epoch 153 Batch   28/44   train_loss = 1.062
Epoch 154 Batch    4/44   train_loss = 1.125
Epoch 154 Batch   24/44   train_loss = 0.938
Epoch 155 Batch    0/44   train_loss = 1.066
Epoch 155 Batch   20/44   train_loss = 1.060
Epoch 155 Batch   40/44   train_loss = 0.924
Epoch 156 Batch   16/44   train_loss = 1.033
Epoch 156 Batch   36/44   train_loss = 0.910
Epoch 157 Batch   12/44   train_loss = 0.975
Epoch 157 Batch   32/44   train_loss = 0.947
Epoch 158 Batch    8/44   train_loss = 0.967
Epoch 158 Batch   28/44   train_loss = 0.911
Epoch 159 Batch    4/44   train_loss = 0.992
Epoch 159 Batch   24/44   train_loss = 0.821
Epoch 160 Batch    0/44   train_loss = 0.914
Epoch 160 Batch   20/44   train_loss = 0.938
Epoch 160 Batch   40/44   train_loss = 0.821
Epoch 161 Batch   16/44   train_loss = 0.923
Epoch 161 Batch   36/44   train_loss = 0.804
Epoch 162 Batch   12/44   train_loss = 0.856
Epoch 162 Batch   32/44   train_loss = 0.866
Epoch 163 Batch    8/44   train_loss = 0.889
Epoch 163 Batch   28/44   train_loss = 0.813
Epoch 164 Batch    4/44   train_loss = 0.872
Epoch 164 Batch   24/44   train_loss = 0.753
Epoch 165 Batch    0/44   train_loss = 0.836
Epoch 165 Batch   20/44   train_loss = 0.860
Epoch 165 Batch   40/44   train_loss = 0.741
Epoch 166 Batch   16/44   train_loss = 0.812
Epoch 166 Batch   36/44   train_loss = 0.763
Epoch 167 Batch   12/44   train_loss = 0.788
Epoch 167 Batch   32/44   train_loss = 0.782
Epoch 168 Batch    8/44   train_loss = 0.805
Epoch 168 Batch   28/44   train_loss = 0.743
Epoch 169 Batch    4/44   train_loss = 0.818
Epoch 169 Batch   24/44   train_loss = 0.674
Epoch 170 Batch    0/44   train_loss = 0.749
Epoch 170 Batch   20/44   train_loss = 0.750
Epoch 170 Batch   40/44   train_loss = 0.669
Epoch 171 Batch   16/44   train_loss = 0.722
Epoch 171 Batch   36/44   train_loss = 0.651
Epoch 172 Batch   12/44   train_loss = 0.711
Epoch 172 Batch   32/44   train_loss = 0.701
Epoch 173 Batch    8/44   train_loss = 0.696
Epoch 173 Batch   28/44   train_loss = 0.653
Epoch 174 Batch    4/44   train_loss = 0.708
Epoch 174 Batch   24/44   train_loss = 0.606
Epoch 175 Batch    0/44   train_loss = 0.668
Epoch 175 Batch   20/44   train_loss = 0.668
Epoch 175 Batch   40/44   train_loss = 0.585
Epoch 176 Batch   16/44   train_loss = 0.650
Epoch 176 Batch   36/44   train_loss = 0.591
Epoch 177 Batch   12/44   train_loss = 0.644
Epoch 177 Batch   32/44   train_loss = 0.641
Epoch 178 Batch    8/44   train_loss = 0.627
Epoch 178 Batch   28/44   train_loss = 0.597
Epoch 179 Batch    4/44   train_loss = 0.661
Epoch 179 Batch   24/44   train_loss = 0.561
Epoch 180 Batch    0/44   train_loss = 0.606
Epoch 180 Batch   20/44   train_loss = 0.602
Epoch 180 Batch   40/44   train_loss = 0.548
Epoch 181 Batch   16/44   train_loss = 0.614
Epoch 181 Batch   36/44   train_loss = 0.559
Epoch 182 Batch   12/44   train_loss = 0.614
Epoch 182 Batch   32/44   train_loss = 0.612
Epoch 183 Batch    8/44   train_loss = 0.624
Epoch 183 Batch   28/44   train_loss = 0.569
Epoch 184 Batch    4/44   train_loss = 0.618
Epoch 184 Batch   24/44   train_loss = 0.544
Epoch 185 Batch    0/44   train_loss = 0.578
Epoch 185 Batch   20/44   train_loss = 0.613
Epoch 185 Batch   40/44   train_loss = 0.532
Epoch 186 Batch   16/44   train_loss = 0.576
Epoch 186 Batch   36/44   train_loss = 0.563
Epoch 187 Batch   12/44   train_loss = 0.568
Epoch 187 Batch   32/44   train_loss = 0.573
Epoch 188 Batch    8/44   train_loss = 0.556
Epoch 188 Batch   28/44   train_loss = 0.529
Epoch 189 Batch    4/44   train_loss = 0.563
Epoch 189 Batch   24/44   train_loss = 0.474
Epoch 190 Batch    0/44   train_loss = 0.525
Epoch 190 Batch   20/44   train_loss = 0.532
Epoch 190 Batch   40/44   train_loss = 0.466
Epoch 191 Batch   16/44   train_loss = 0.488
Epoch 191 Batch   36/44   train_loss = 0.472
Epoch 192 Batch   12/44   train_loss = 0.530
Epoch 192 Batch   32/44   train_loss = 0.520
Epoch 193 Batch    8/44   train_loss = 0.484
Epoch 193 Batch   28/44   train_loss = 0.470
Epoch 194 Batch    4/44   train_loss = 0.520
Epoch 194 Batch   24/44   train_loss = 0.440
Epoch 195 Batch    0/44   train_loss = 0.479
Epoch 195 Batch   20/44   train_loss = 0.468
Epoch 195 Batch   40/44   train_loss = 0.426
Epoch 196 Batch   16/44   train_loss = 0.455
Epoch 196 Batch   36/44   train_loss = 0.440
Epoch 197 Batch   12/44   train_loss = 0.477
Epoch 197 Batch   32/44   train_loss = 0.469
Epoch 198 Batch    8/44   train_loss = 0.449
Epoch 198 Batch   28/44   train_loss = 0.430
Epoch 199 Batch    4/44   train_loss = 0.481
Epoch 199 Batch   24/44   train_loss = 0.404
Epoch 200 Batch    0/44   train_loss = 0.441
Epoch 200 Batch   20/44   train_loss = 0.435
Epoch 200 Batch   40/44   train_loss = 0.395
Epoch 201 Batch   16/44   train_loss = 0.418
Epoch 201 Batch   36/44   train_loss = 0.406
Epoch 202 Batch   12/44   train_loss = 0.445
Epoch 202 Batch   32/44   train_loss = 0.443
Epoch 203 Batch    8/44   train_loss = 0.422
Epoch 203 Batch   28/44   train_loss = 0.400
Epoch 204 Batch    4/44   train_loss = 0.449
Epoch 204 Batch   24/44   train_loss = 0.380
Epoch 205 Batch    0/44   train_loss = 0.414
Epoch 205 Batch   20/44   train_loss = 0.413
Epoch 205 Batch   40/44   train_loss = 0.375
Epoch 206 Batch   16/44   train_loss = 0.388
Epoch 206 Batch   36/44   train_loss = 0.384
Epoch 207 Batch   12/44   train_loss = 0.438
Epoch 207 Batch   32/44   train_loss = 0.433
Epoch 208 Batch    8/44   train_loss = 0.403
Epoch 208 Batch   28/44   train_loss = 0.378
Epoch 209 Batch    4/44   train_loss = 0.428
Epoch 209 Batch   24/44   train_loss = 0.369
Epoch 210 Batch    0/44   train_loss = 0.392
Epoch 210 Batch   20/44   train_loss = 0.383
Epoch 210 Batch   40/44   train_loss = 0.361
Epoch 211 Batch   16/44   train_loss = 0.374
Epoch 211 Batch   36/44   train_loss = 0.371
Epoch 212 Batch   12/44   train_loss = 0.417
Epoch 212 Batch   32/44   train_loss = 0.404
Epoch 213 Batch    8/44   train_loss = 0.381
Epoch 213 Batch   28/44   train_loss = 0.362
Epoch 214 Batch    4/44   train_loss = 0.401
Epoch 214 Batch   24/44   train_loss = 0.344
Epoch 215 Batch    0/44   train_loss = 0.366
Epoch 215 Batch   20/44   train_loss = 0.358
Epoch 215 Batch   40/44   train_loss = 0.335
Epoch 216 Batch   16/44   train_loss = 0.346
Epoch 216 Batch   36/44   train_loss = 0.350
Epoch 217 Batch   12/44   train_loss = 0.396
Epoch 217 Batch   32/44   train_loss = 0.383
Epoch 218 Batch    8/44   train_loss = 0.354
Epoch 218 Batch   28/44   train_loss = 0.340
Epoch 219 Batch    4/44   train_loss = 0.376
Epoch 219 Batch   24/44   train_loss = 0.329
Epoch 220 Batch    0/44   train_loss = 0.350
Epoch 220 Batch   20/44   train_loss = 0.344
Epoch 220 Batch   40/44   train_loss = 0.319
Epoch 221 Batch   16/44   train_loss = 0.330
Epoch 221 Batch   36/44   train_loss = 0.338
Epoch 222 Batch   12/44   train_loss = 0.382
Epoch 222 Batch   32/44   train_loss = 0.371
Epoch 223 Batch    8/44   train_loss = 0.341
Epoch 223 Batch   28/44   train_loss = 0.326
Epoch 224 Batch    4/44   train_loss = 0.364
Epoch 224 Batch   24/44   train_loss = 0.317
Epoch 225 Batch    0/44   train_loss = 0.340
Epoch 225 Batch   20/44   train_loss = 0.335
Epoch 225 Batch   40/44   train_loss = 0.310
Epoch 226 Batch   16/44   train_loss = 0.319
Epoch 226 Batch   36/44   train_loss = 0.330
Epoch 227 Batch   12/44   train_loss = 0.374
Epoch 227 Batch   32/44   train_loss = 0.359
Epoch 228 Batch    8/44   train_loss = 0.333
Epoch 228 Batch   28/44   train_loss = 0.316
Epoch 229 Batch    4/44   train_loss = 0.356
Epoch 229 Batch   24/44   train_loss = 0.310
Epoch 230 Batch    0/44   train_loss = 0.332
Epoch 230 Batch   20/44   train_loss = 0.328
Epoch 230 Batch   40/44   train_loss = 0.304
Epoch 231 Batch   16/44   train_loss = 0.311
Epoch 231 Batch   36/44   train_loss = 0.322
Epoch 232 Batch   12/44   train_loss = 0.366
Epoch 232 Batch   32/44   train_loss = 0.349
Epoch 233 Batch    8/44   train_loss = 0.328
Epoch 233 Batch   28/44   train_loss = 0.309
Epoch 234 Batch    4/44   train_loss = 0.348
Epoch 234 Batch   24/44   train_loss = 0.306
Epoch 235 Batch    0/44   train_loss = 0.326
Epoch 235 Batch   20/44   train_loss = 0.322
Epoch 235 Batch   40/44   train_loss = 0.298
Epoch 236 Batch   16/44   train_loss = 0.304
Epoch 236 Batch   36/44   train_loss = 0.315
Epoch 237 Batch   12/44   train_loss = 0.360
Epoch 237 Batch   32/44   train_loss = 0.342
Epoch 238 Batch    8/44   train_loss = 0.323
Epoch 238 Batch   28/44   train_loss = 0.304
Epoch 239 Batch    4/44   train_loss = 0.342
Epoch 239 Batch   24/44   train_loss = 0.302
Epoch 240 Batch    0/44   train_loss = 0.321
Epoch 240 Batch   20/44   train_loss = 0.315
Epoch 240 Batch   40/44   train_loss = 0.292
Epoch 241 Batch   16/44   train_loss = 0.298
Epoch 241 Batch   36/44   train_loss = 0.310
Epoch 242 Batch   12/44   train_loss = 0.356
Epoch 242 Batch   32/44   train_loss = 0.338
Epoch 243 Batch    8/44   train_loss = 0.318
Epoch 243 Batch   28/44   train_loss = 0.299
Epoch 244 Batch    4/44   train_loss = 0.337
Epoch 244 Batch   24/44   train_loss = 0.298
Epoch 245 Batch    0/44   train_loss = 0.316
Epoch 245 Batch   20/44   train_loss = 0.310
Epoch 245 Batch   40/44   train_loss = 0.288
Epoch 246 Batch   16/44   train_loss = 0.294
Epoch 246 Batch   36/44   train_loss = 0.308
Epoch 247 Batch   12/44   train_loss = 0.352
Epoch 247 Batch   32/44   train_loss = 0.334
Epoch 248 Batch    8/44   train_loss = 0.313
Epoch 248 Batch   28/44   train_loss = 0.294
Epoch 249 Batch    4/44   train_loss = 0.333
Epoch 249 Batch   24/44   train_loss = 0.295
Epoch 250 Batch    0/44   train_loss = 0.311
Epoch 250 Batch   20/44   train_loss = 0.304
Epoch 250 Batch   40/44   train_loss = 0.285
Epoch 251 Batch   16/44   train_loss = 0.294
Epoch 251 Batch   36/44   train_loss = 0.306
Epoch 252 Batch   12/44   train_loss = 0.348
Epoch 252 Batch   32/44   train_loss = 0.330
Epoch 253 Batch    8/44   train_loss = 0.310
Epoch 253 Batch   28/44   train_loss = 0.292
Epoch 254 Batch    4/44   train_loss = 0.330
Epoch 254 Batch   24/44   train_loss = 0.292
Epoch 255 Batch    0/44   train_loss = 0.307
Epoch 255 Batch   20/44   train_loss = 0.300
Epoch 255 Batch   40/44   train_loss = 0.286
Epoch 256 Batch   16/44   train_loss = 0.294
Epoch 256 Batch   36/44   train_loss = 0.301
Epoch 257 Batch   12/44   train_loss = 0.342
Epoch 257 Batch   32/44   train_loss = 0.328
Epoch 258 Batch    8/44   train_loss = 0.311
Epoch 258 Batch   28/44   train_loss = 0.288
Epoch 259 Batch    4/44   train_loss = 0.324
Epoch 259 Batch   24/44   train_loss = 0.289
Epoch 260 Batch    0/44   train_loss = 0.309
Epoch 260 Batch   20/44   train_loss = 0.301
Epoch 260 Batch   40/44   train_loss = 0.279
Epoch 261 Batch   16/44   train_loss = 0.285
Epoch 261 Batch   36/44   train_loss = 0.302
Epoch 262 Batch   12/44   train_loss = 0.345
Epoch 262 Batch   32/44   train_loss = 0.326
Epoch 263 Batch    8/44   train_loss = 0.301
Epoch 263 Batch   28/44   train_loss = 0.286
Epoch 264 Batch    4/44   train_loss = 0.329
Epoch 264 Batch   24/44   train_loss = 0.287
Epoch 265 Batch    0/44   train_loss = 0.304
Epoch 265 Batch   20/44   train_loss = 0.296
Epoch 265 Batch   40/44   train_loss = 0.279
Epoch 266 Batch   16/44   train_loss = 0.286
Epoch 266 Batch   36/44   train_loss = 0.298
Epoch 267 Batch   12/44   train_loss = 0.336
Epoch 267 Batch   32/44   train_loss = 0.324
Epoch 268 Batch    8/44   train_loss = 0.302
Epoch 268 Batch   28/44   train_loss = 0.284
Epoch 269 Batch    4/44   train_loss = 0.321
Epoch 269 Batch   24/44   train_loss = 0.284
Epoch 270 Batch    0/44   train_loss = 0.302
Epoch 270 Batch   20/44   train_loss = 0.293
Epoch 270 Batch   40/44   train_loss = 0.273
Epoch 271 Batch   16/44   train_loss = 0.279
Epoch 271 Batch   36/44   train_loss = 0.295
Epoch 272 Batch   12/44   train_loss = 0.334
Epoch 272 Batch   32/44   train_loss = 0.321
Epoch 273 Batch    8/44   train_loss = 0.300
Epoch 273 Batch   28/44   train_loss = 0.281
Epoch 274 Batch    4/44   train_loss = 0.318
Epoch 274 Batch   24/44   train_loss = 0.283
Epoch 275 Batch    0/44   train_loss = 0.301
Epoch 275 Batch   20/44   train_loss = 0.291
Epoch 275 Batch   40/44   train_loss = 0.271
Epoch 276 Batch   16/44   train_loss = 0.279
Epoch 276 Batch   36/44   train_loss = 0.293
Epoch 277 Batch   12/44   train_loss = 0.331
Epoch 277 Batch   32/44   train_loss = 0.320
Epoch 278 Batch    8/44   train_loss = 0.299
Epoch 278 Batch   28/44   train_loss = 0.279
Epoch 279 Batch    4/44   train_loss = 0.315
Epoch 279 Batch   24/44   train_loss = 0.280
Epoch 280 Batch    0/44   train_loss = 0.299
Epoch 280 Batch   20/44   train_loss = 0.288
Epoch 280 Batch   40/44   train_loss = 0.269
Epoch 281 Batch   16/44   train_loss = 0.277
Epoch 281 Batch   36/44   train_loss = 0.293
Epoch 282 Batch   12/44   train_loss = 0.331
Epoch 282 Batch   32/44   train_loss = 0.318
Epoch 283 Batch    8/44   train_loss = 0.299
Epoch 283 Batch   28/44   train_loss = 0.279
Epoch 284 Batch    4/44   train_loss = 0.313
Epoch 284 Batch   24/44   train_loss = 0.278
Epoch 285 Batch    0/44   train_loss = 0.296
Epoch 285 Batch   20/44   train_loss = 0.286
Epoch 285 Batch   40/44   train_loss = 0.269
Epoch 286 Batch   16/44   train_loss = 0.277
Epoch 286 Batch   36/44   train_loss = 0.291
Epoch 287 Batch   12/44   train_loss = 0.327
Epoch 287 Batch   32/44   train_loss = 0.317
Epoch 288 Batch    8/44   train_loss = 0.297
Epoch 288 Batch   28/44   train_loss = 0.276
Epoch 289 Batch    4/44   train_loss = 0.310
Epoch 289 Batch   24/44   train_loss = 0.275
Epoch 290 Batch    0/44   train_loss = 0.295
Epoch 290 Batch   20/44   train_loss = 0.286
Epoch 290 Batch   40/44   train_loss = 0.268
Epoch 291 Batch   16/44   train_loss = 0.274
Epoch 291 Batch   36/44   train_loss = 0.292
Epoch 292 Batch   12/44   train_loss = 0.327
Epoch 292 Batch   32/44   train_loss = 0.315
Epoch 293 Batch    8/44   train_loss = 0.292
Epoch 293 Batch   28/44   train_loss = 0.275
Epoch 294 Batch    4/44   train_loss = 0.311
Epoch 294 Batch   24/44   train_loss = 0.275
Epoch 295 Batch    0/44   train_loss = 0.293
Epoch 295 Batch   20/44   train_loss = 0.282
Epoch 295 Batch   40/44   train_loss = 0.268
Epoch 296 Batch   16/44   train_loss = 0.274
Epoch 296 Batch   36/44   train_loss = 0.290
Epoch 297 Batch   12/44   train_loss = 0.324
Epoch 297 Batch   32/44   train_loss = 0.311
Epoch 298 Batch    8/44   train_loss = 0.292
Epoch 298 Batch   28/44   train_loss = 0.276
Epoch 299 Batch    4/44   train_loss = 0.308
Epoch 299 Batch   24/44   train_loss = 0.271
Model Trained and Saved

Save Parameters

Save seq_length and save_dir for generating a new TV script.


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

Checkpoint


In [18]:
"""
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 [19]:
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")
    initials = loaded_graph.get_tensor_by_name("initial_state:0")
    finals = loaded_graph.get_tensor_by_name("final_state:0")
    probs = loaded_graph.get_tensor_by_name("probs:0")   
    
    return inputs, initials, finals, 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 [20]:
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
    # As suggested by the last reviewer - tuning randomness
    #print("probabs:{}, - {}".format(probabilities.shape, int_to_vocab[np.argmax(probabilities)]))
    mostprobable = np.argsort(probabilities)
    ret = np.random.choice(mostprobable[-3:],1, p=[0.1, 0.2, 0.7])
    return int_to_vocab[ret[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 [21]:
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:(into phone) moe's dead. oh where the hell! i'm the best book are on an ugly table, we can eat big back an edge.
homer_simpson:(tipsy noise, then:) okay! let's try his fictional tv!(slight)
homer_simpson: oh the super people-- i'm an idiot. have to the secret, people because on a day man from pick of my people.
bart_simpson:(quickly) that's sweet fat, but i'm boring happy.
barney_gumble: he's a laugh) i'm hundred here to(air)...
homer_simpson: moe, i want you.
homer_simpson:(drunk man, homer) the thing are she, carl, or i?
homer_simpson:(looking) let 'em make up...(sighs) oh, uh, boy, right baby-- she dead from the wife, lenny.
carl_carlson:(on phone,) uh, fat?
homer_simpson:(reading)".
jacques:" i need to get yourself of moe's under his face.
marge_simpson: what

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.


In [ ]:


In [ ]:


In [ ]: