Language Translation

In this project, you’re going to take a peek into the realm of neural network machine translation. You’ll be training a sequence to sequence model on a dataset of English and French sentences that can translate new sentences from English to French.

Get the Data

Since translating the whole language of English to French will take lots of time to train, we have provided you with a small portion of the English corpus.


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

source_path = 'data/small_vocab_en'
target_path = 'data/small_vocab_fr'
source_text = helper.load_data(source_path)
target_text = helper.load_data(target_path)

Explore the Data

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


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

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

print('Dataset Stats')
print('Roughly the number of unique words: {}'.format(len({word: None for word in source_text.split()})))

sentences = source_text.split('\n')
word_counts = [len(sentence.split()) for sentence in sentences]
print('Number of sentences: {}'.format(len(sentences)))
print('Average number of words in a sentence: {}'.format(np.average(word_counts)))

print()
print('English sentences {} to {}:'.format(*view_sentence_range))
print('\n'.join(source_text.split('\n')[view_sentence_range[0]:view_sentence_range[1]]))
print()
print('French sentences {} to {}:'.format(*view_sentence_range))
print('\n'.join(target_text.split('\n')[view_sentence_range[0]:view_sentence_range[1]]))


Dataset Stats
Roughly the number of unique words: 227
Number of sentences: 137861
Average number of words in a sentence: 13.225277634719028

English sentences 0 to 10:
new jersey is sometimes quiet during autumn , and it is snowy in april .
the united states is usually chilly during july , and it is usually freezing in november .
california is usually quiet during march , and it is usually hot in june .
the united states is sometimes mild during june , and it is cold in september .
your least liked fruit is the grape , but my least liked is the apple .
his favorite fruit is the orange , but my favorite is the grape .
paris is relaxing during december , but it is usually chilly in july .
new jersey is busy during spring , and it is never hot in march .
our least liked fruit is the lemon , but my least liked is the grape .
the united states is sometimes busy during january , and it is sometimes warm in november .

French sentences 0 to 10:
new jersey est parfois calme pendant l' automne , et il est neigeux en avril .
les états-unis est généralement froid en juillet , et il gèle habituellement en novembre .
california est généralement calme en mars , et il est généralement chaud en juin .
les états-unis est parfois légère en juin , et il fait froid en septembre .
votre moins aimé fruit est le raisin , mais mon moins aimé est la pomme .
son fruit préféré est l'orange , mais mon préféré est le raisin .
paris est relaxant en décembre , mais il est généralement froid en juillet .
new jersey est occupé au printemps , et il est jamais chaude en mars .
notre fruit est moins aimé le citron , mais mon moins aimé est le raisin .
les états-unis est parfois occupé en janvier , et il est parfois chaud en novembre .

Implement Preprocessing Function

Text to Word Ids

As you did with other RNNs, you must turn the text into a number so the computer can understand it. In the function text_to_ids(), you'll turn source_text and target_text from words to ids. However, you need to add the <EOS> word id at the end of target_text. This will help the neural network predict when the sentence should end.

You can get the <EOS> word id by doing:

target_vocab_to_int['<EOS>']

You can get other word ids using source_vocab_to_int and target_vocab_to_int.


In [3]:
def text_to_ids(source_text, target_text, source_vocab_to_int, target_vocab_to_int):
    """
    Convert source and target text to proper word ids
    :param source_text: String that contains all the source text.
    :param target_text: String that contains all the target text.
    :param source_vocab_to_int: Dictionary to go from the source words to an id
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :return: A tuple of lists (source_id_text, target_id_text)
    """
    # TODO: Implement Function
    ###source_sent = [ sent for sent in source_text.split("\n") ]
    ###target_sent = [ sent + ' <EOS>' for sent in target_text.split("\n") ]
    
    ###source_ids = [ [ source_vocab_to_int[word] for word in sent.split() ] for sent in source_sent ]
    ###target_ids = [ [ target_vocab_to_int[word] for word in sent.split() ] for sent in target_sent ]
    
    # Advice from Udacity Reviewer
    target_ids = [[target_vocab_to_int[w] for w in s.split()] + [target_vocab_to_int['<EOS>']] for s in target_text.split('\n')]
    source_ids = [[source_vocab_to_int[w] for w in s.split()] for s in source_text.split('\n')]
    
    return source_ids, target_ids

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


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 [4]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
helper.preprocess_and_save_data(source_path, target_path, text_to_ids)

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 [5]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np
import helper

(source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = helper.load_preprocess()

Check the Version of TensorFlow and Access to GPU

This will check to make sure you have the correct version of TensorFlow and access to a GPU


In [6]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
from distutils.version import LooseVersion
import warnings
import tensorflow as tf
from tensorflow.python.layers.core import Dense

# Check TensorFlow Version
assert LooseVersion(tf.__version__) >= LooseVersion('1.1'), 'Please use TensorFlow version 1.1 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.1.0
Default GPU Device: /gpu:0

Build the Neural Network

You'll build the components necessary to build a Sequence-to-Sequence model by implementing the following functions below:

  • model_inputs
  • process_decoder_input
  • encoding_layer
  • decoding_layer_train
  • decoding_layer_infer
  • decoding_layer
  • seq2seq_model

Input

Implement the model_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 with rank 2.
  • Targets placeholder with rank 2.
  • Learning rate placeholder with rank 0.
  • Keep probability placeholder named "keep_prob" using the TF Placeholder name parameter with rank 0.
  • Target sequence length placeholder named "target_sequence_length" with rank 1
  • Max target sequence length tensor named "max_target_len" getting its value from applying tf.reduce_max on the target_sequence_length placeholder. Rank 0.
  • Source sequence length placeholder named "source_sequence_length" with rank 1

Return the placeholders in the following the tuple (input, targets, learning rate, keep probability, target sequence length, max target sequence length, source sequence length)


In [7]:
def model_inputs():
    """
    Create TF Placeholders for input, targets, learning rate, and lengths of source and target sequences.
    :return: Tuple (input, targets, learning rate, keep probability, target sequence length,
    max target sequence length, source sequence length)
    """
    # TODO: Implement Function
    
    input_ = tf.placeholder( tf.int32, [None, None], name = "input" )
    target_ = tf.placeholder( tf.int32, [None, None], name = "target" )
    learn_rate_ = tf.placeholder( tf.float32, None, name = "learn_rate" )
    keep_prob_ = tf.placeholder( tf.float32, None, name = "keep_prob" )
    target_sequence_length = tf.placeholder( tf.int32, [None], name="target_sequence_length" )
    max_target_sequence_length = tf.reduce_max( target_sequence_length )
    source_sequence_length = tf.placeholder( tf.int32, [None], name="source_sequence_length" )
    
    return input_, target_, learn_rate_, keep_prob_, target_sequence_length, max_target_sequence_length, source_sequence_length


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


Tests Passed

Process Decoder Input

Implement process_decoder_input by removing the last word id from each batch in target_data and concat the GO ID to the begining of each batch.


In [8]:
def process_decoder_input(target_data, target_vocab_to_int, batch_size):
    """
    Preprocess target data for encoding
    :param target_data: Target Placehoder
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :param batch_size: Batch Size
    :return: Preprocessed target data
    """
    # TODO: Implement Function
    
    go_id = source_vocab_to_int[ '<GO>' ]
    ending_text = tf.strided_slice( target_data, [0, 0], [batch_size, -1], [1, 1] )
    decoded_text = tf.concat( [ tf.fill([batch_size, 1], go_id), ending_text ], 1) 
    
    return decoded_text

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


Tests Passed

Encoding

Implement encoding_layer() to create a Encoder RNN layer:


In [9]:
from imp import reload
reload(tests)

def encoding_layer(rnn_inputs, rnn_size, num_layers, keep_prob, 
                   source_sequence_length, source_vocab_size, 
                   encoding_embedding_size):
    """
    Create encoding layer
    :param rnn_inputs: Inputs for the RNN
    :param rnn_size: RNN Size
    :param num_layers: Number of layers
    :param keep_prob: Dropout keep probability
    :param source_sequence_length: a list of the lengths of each sequence in the batch
    :param source_vocab_size: vocabulary size of source data
    :param encoding_embedding_size: embedding size of source data
    :return: tuple (RNN output, RNN state)
    """
    # TODO: Implement Function
    encod_inputs = tf.contrib.layers.embed_sequence( rnn_inputs, source_vocab_size, encoding_embedding_size )
    
    rnn_cell = tf.contrib.rnn.MultiRNNCell( [ tf.contrib.rnn.LSTMCell( rnn_size ) for _ in range(num_layers) ] )
    # Adding dropout layer
    rnn_cell = tf.contrib.rnn.DropoutWrapper( rnn_cell, output_keep_prob = keep_prob )
    rnn_output, rnn_state = tf.nn.dynamic_rnn( rnn_cell, encod_inputs, source_sequence_length, dtype = tf.float32 )
    
    return rnn_output, rnn_state
    

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


Tests Passed

Decoding - Training

Create a training decoding layer:


In [10]:
def decoding_layer_train(encoder_state, dec_cell, dec_embed_input, 
                         target_sequence_length, max_summary_length, 
                         output_layer, keep_prob):
    """
    Create a decoding layer for training
    :param encoder_state: Encoder State
    :param dec_cell: Decoder RNN Cell
    :param dec_embed_input: Decoder embedded input
    :param target_sequence_length: The lengths of each sequence in the target batch
    :param max_summary_length: The length of the longest sequence in the batch
    :param output_layer: Function to apply the output layer
    :param keep_prob: Dropout keep probability
    :return: BasicDecoderOutput containing training logits and sample_id
    """
    # TODO: Implement Function
    
    decode_helper = tf.contrib.seq2seq.TrainingHelper( dec_embed_input, target_sequence_length )
    decoder = tf.contrib.seq2seq.BasicDecoder( dec_cell, decode_helper, encoder_state, output_layer )
    decoder_outputs, decoder_state = tf.contrib.seq2seq.dynamic_decode( decoder, impute_finished=True, 
                                                                       maximum_iterations= max_summary_length )
    return decoder_outputs



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


Tests Passed

Decoding - Inference

Create inference decoder:


In [11]:
def decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id,
                         end_of_sequence_id, max_target_sequence_length,
                         vocab_size, output_layer, batch_size, keep_prob):
    """
    Create a decoding layer for inference
    :param encoder_state: Encoder state
    :param dec_cell: Decoder RNN Cell
    :param dec_embeddings: Decoder embeddings
    :param start_of_sequence_id: GO ID
    :param end_of_sequence_id: EOS Id
    :param max_target_sequence_length: Maximum length of target sequences
    :param vocab_size: Size of decoder/target vocabulary
    :param decoding_scope: TenorFlow Variable Scope for decoding
    :param output_layer: Function to apply the output layer
    :param batch_size: Batch size
    :param keep_prob: Dropout keep probability
    :return: BasicDecoderOutput containing inference logits and sample_id
    """
    # TODO: Implement Function    
    start_tokens = tf.tile( tf.constant( [start_of_sequence_id], dtype=tf.int32), 
                           [ batch_size ], name = "start_tokens" )
    decode_helper = tf.contrib.seq2seq.GreedyEmbeddingHelper( dec_embeddings, start_tokens, end_of_sequence_id )
    decoder = tf.contrib.seq2seq.BasicDecoder( dec_cell, decode_helper, encoder_state, output_layer = output_layer )
    decoder_outputs, decoder_state = tf.contrib.seq2seq.dynamic_decode( decoder, impute_finished=True,
                                        maximum_iterations = max_target_sequence_length )
    
    return decoder_outputs



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


Tests Passed

Build the Decoding Layer

Implement decoding_layer() to create a Decoder RNN layer.

  • Embed the target sequences
  • Construct the decoder LSTM cell (just like you constructed the encoder cell above)
  • Create an output layer to map the outputs of the decoder to the elements of our vocabulary
  • Use the your decoding_layer_train(encoder_state, dec_cell, dec_embed_input, target_sequence_length, max_target_sequence_length, output_layer, keep_prob) function to get the training logits.
  • Use your decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id, end_of_sequence_id, max_target_sequence_length, vocab_size, output_layer, batch_size, keep_prob) function to get the inference logits.

Note: You'll need to use tf.variable_scope to share variables between training and inference.


In [12]:
from tensorflow.python.layers import core as layers_core

def decoding_layer(dec_input, encoder_state,
                   target_sequence_length, max_target_sequence_length,
                   rnn_size,
                   num_layers, target_vocab_to_int, target_vocab_size,
                   batch_size, keep_prob, decoding_embedding_size):
    """
    Create decoding layer
    :param dec_input: Decoder input
    :param encoder_state: Encoder state
    :param target_sequence_length: The lengths of each sequence in the target batch
    :param max_target_sequence_length: Maximum length of target sequences
    :param rnn_size: RNN Size
    :param num_layers: Number of layers
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :param target_vocab_size: Size of target vocabulary
    :param batch_size: The size of the batch
    :param keep_prob: Dropout keep probability
    :return: Tuple of (Training BasicDecoderOutput, Inference BasicDecoderOutput)
    """
    # TODO: Implement Function
    
    decode_embed = tf.Variable( tf.random_uniform( [ target_vocab_size, decoding_embedding_size ] ) )
    decode_embed_input = tf.nn.embedding_lookup( decode_embed, dec_input )
    
    decode_cell = tf.contrib.rnn.MultiRNNCell( [ tf.contrib.rnn.LSTMCell(rnn_size) for _ in range(num_layers) ] )
    # Adding dropout layer
    decode_cell = tf.contrib.rnn.DropoutWrapper( decode_cell, output_keep_prob = keep_prob )
    
    output_layer = layers_core.Dense( target_vocab_size, 
                                     kernel_initializer = tf.truncated_normal_initializer( mean = 0.0, stddev=0.1 ) )
    
    with tf.variable_scope( "decoding" ) as decoding_scope:
        decode_outputs_train = decoding_layer_train( encoder_state, decode_cell, decode_embed_input, 
                             target_sequence_length, max_target_sequence_length, output_layer, keep_prob )

    SOS_id = target_vocab_to_int[ "<GO>" ]
    EOS_id = target_vocab_to_int[ "<EOS>" ]
    
    with tf.variable_scope( "decoding", reuse=True) as decoding_scope:
        decode_outputs_infer = decoding_layer_infer( encoder_state, decode_cell, decode_embed, SOS_id,EOS_id, 
                               max_target_sequence_length,target_vocab_size, output_layer, batch_size, keep_prob )
        
    return decode_outputs_train, decode_outputs_infer
    

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


Tests Passed

Build the Neural Network

Apply the functions you implemented above to:

  • Apply embedding to the input data for the encoder.
  • Encode the input using your encoding_layer(rnn_inputs, rnn_size, num_layers, keep_prob, source_sequence_length, source_vocab_size, encoding_embedding_size).
  • Process target data using your process_decoder_input(target_data, target_vocab_to_int, batch_size) function.
  • Apply embedding to the target data for the decoder.
  • Decode the encoded input using your decoding_layer(dec_input, enc_state, target_sequence_length, max_target_sentence_length, rnn_size, num_layers, target_vocab_to_int, target_vocab_size, batch_size, keep_prob, dec_embedding_size) function.

In [13]:
def seq2seq_model(input_data, target_data, keep_prob, batch_size,
                  source_sequence_length, target_sequence_length,
                  max_target_sentence_length,
                  source_vocab_size, target_vocab_size,
                  enc_embedding_size, dec_embedding_size,
                  rnn_size, num_layers, target_vocab_to_int):
    """
    Build the Sequence-to-Sequence part of the neural network
    :param input_data: Input placeholder
    :param target_data: Target placeholder
    :param keep_prob: Dropout keep probability placeholder
    :param batch_size: Batch Size
    :param source_sequence_length: Sequence Lengths of source sequences in the batch
    :param target_sequence_length: Sequence Lengths of target sequences in the batch
    :param source_vocab_size: Source vocabulary size
    :param target_vocab_size: Target vocabulary size
    :param enc_embedding_size: Decoder embedding size
    :param dec_embedding_size: Encoder embedding size
    :param rnn_size: RNN Size
    :param num_layers: Number of layers
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :return: Tuple of (Training BasicDecoderOutput, Inference BasicDecoderOutput)
    """
    # TODO: Implement Function
    
    encode_output, encode_state = encoding_layer( input_data, rnn_size, num_layers, keep_prob, 
                                source_sequence_length, source_vocab_size, enc_embedding_size )
    
    decode_input = process_decoder_input( target_data, target_vocab_to_int, batch_size ) 
    
    decode_outputs_train, decode_outputs_infer = decoding_layer( decode_input, encode_state,
                   target_sequence_length, tf.reduce_max( target_sequence_length ), rnn_size, num_layers, 
                   target_vocab_to_int, target_vocab_size, batch_size, keep_prob, dec_embedding_size )
    
    return decode_outputs_train, decode_outputs_infer


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


Tests Passed

Neural Network Training

Hyperparameters

Tune the following parameters:

  • Set epochs to the number of epochs.
  • Set batch_size to the batch size.
  • Set rnn_size to the size of the RNNs.
  • Set num_layers to the number of layers.
  • Set encoding_embedding_size to the size of the embedding for the encoder.
  • Set decoding_embedding_size to the size of the embedding for the decoder.
  • Set learning_rate to the learning rate.
  • Set keep_probability to the Dropout keep probability
  • Set display_step to state how many steps between each debug output statement

In [14]:
# Number of Epochs
epochs = 10
# Batch Size
batch_size = 256
# RNN Size
rnn_size = 256
# Number of Layers
num_layers = 2
# Embedding Size
encoding_embedding_size = 128
decoding_embedding_size = 128
# Learning Rate
learning_rate = 0.01
# Dropout Keep Probability
keep_probability = 0.8
display_step = 10

Build the Graph

Build the graph using the neural network you implemented.


In [15]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
save_path = 'checkpoints/dev'
(source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = helper.load_preprocess()
max_target_sentence_length = max([len(sentence) for sentence in source_int_text])

train_graph = tf.Graph()
with train_graph.as_default():
    input_data, targets, lr, keep_prob, target_sequence_length, max_target_sequence_length, source_sequence_length = model_inputs()

    #sequence_length = tf.placeholder_with_default(max_target_sentence_length, None, name='sequence_length')
    input_shape = tf.shape(input_data)

    train_logits, inference_logits = seq2seq_model(tf.reverse(input_data, [-1]),
                                                   targets,
                                                   keep_prob,
                                                   batch_size,
                                                   source_sequence_length,
                                                   target_sequence_length,
                                                   max_target_sequence_length,
                                                   len(source_vocab_to_int),
                                                   len(target_vocab_to_int),
                                                   encoding_embedding_size,
                                                   decoding_embedding_size,
                                                   rnn_size,
                                                   num_layers,
                                                   target_vocab_to_int)


    training_logits = tf.identity(train_logits.rnn_output, name='logits')
    inference_logits = tf.identity(inference_logits.sample_id, name='predictions')

    masks = tf.sequence_mask(target_sequence_length, max_target_sequence_length, dtype=tf.float32, name='masks')

    with tf.name_scope("optimization"):
        # Loss function
        cost = tf.contrib.seq2seq.sequence_loss(
            training_logits,
            targets,
            masks)

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

Batch and pad the source and target sequences


In [16]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
def pad_sentence_batch(sentence_batch, pad_int):
    """Pad sentences with <PAD> so that each sentence of a batch has the same length"""
    max_sentence = max([len(sentence) for sentence in sentence_batch])
    return [sentence + [pad_int] * (max_sentence - len(sentence)) for sentence in sentence_batch]


def get_batches(sources, targets, batch_size, source_pad_int, target_pad_int):
    """Batch targets, sources, and the lengths of their sentences together"""
    for batch_i in range(0, len(sources)//batch_size):
        start_i = batch_i * batch_size

        # Slice the right amount for the batch
        sources_batch = sources[start_i:start_i + batch_size]
        targets_batch = targets[start_i:start_i + batch_size]

        # Pad
        pad_sources_batch = np.array(pad_sentence_batch(sources_batch, source_pad_int))
        pad_targets_batch = np.array(pad_sentence_batch(targets_batch, target_pad_int))

        # Need the lengths for the _lengths parameters
        pad_targets_lengths = []
        for target in pad_targets_batch:
            pad_targets_lengths.append(len(target))

        pad_source_lengths = []
        for source in pad_sources_batch:
            pad_source_lengths.append(len(source))

        yield pad_sources_batch, pad_targets_batch, pad_source_lengths, pad_targets_lengths

Train

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


In [17]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
def get_accuracy(target, logits):
    """
    Calculate accuracy
    """
    max_seq = max(target.shape[1], logits.shape[1])
    if max_seq - target.shape[1]:
        target = np.pad(
            target,
            [(0,0),(0,max_seq - target.shape[1])],
            'constant')
    if max_seq - logits.shape[1]:
        logits = np.pad(
            logits,
            [(0,0),(0,max_seq - logits.shape[1])],
            'constant')

    return np.mean(np.equal(target, logits))

# Split data to training and validation sets
train_source = source_int_text[batch_size:]
train_target = target_int_text[batch_size:]
valid_source = source_int_text[:batch_size]
valid_target = target_int_text[:batch_size]
(valid_sources_batch, valid_targets_batch, valid_sources_lengths, valid_targets_lengths ) = next(get_batches(valid_source,
                                                                                                             valid_target,
                                                                                                             batch_size,
                                                                                                             source_vocab_to_int['<PAD>'],
                                                                                                             target_vocab_to_int['<PAD>']))                                                                                                  
with tf.Session(graph=train_graph) as sess:
    sess.run(tf.global_variables_initializer())

    for epoch_i in range(epochs):
        for batch_i, (source_batch, target_batch, sources_lengths, targets_lengths) in enumerate(
                get_batches(train_source, train_target, batch_size,
                            source_vocab_to_int['<PAD>'],
                            target_vocab_to_int['<PAD>'])):

            _, loss = sess.run(
                [train_op, cost],
                {input_data: source_batch,
                 targets: target_batch,
                 lr: learning_rate,
                 target_sequence_length: targets_lengths,
                 source_sequence_length: sources_lengths,
                 keep_prob: keep_probability})


            if batch_i % display_step == 0 and batch_i > 0:


                batch_train_logits = sess.run(
                    inference_logits,
                    {input_data: source_batch,
                     source_sequence_length: sources_lengths,
                     target_sequence_length: targets_lengths,
                     keep_prob: 1.0})


                batch_valid_logits = sess.run(
                    inference_logits,
                    {input_data: valid_sources_batch,
                     source_sequence_length: valid_sources_lengths,
                     target_sequence_length: valid_targets_lengths,
                     keep_prob: 1.0})

                train_acc = get_accuracy(target_batch, batch_train_logits)

                valid_acc = get_accuracy(valid_targets_batch, batch_valid_logits)

                print('Epoch {:>3} Batch {:>4}/{} - Train Accuracy: {:>6.4f}, Validation Accuracy: {:>6.4f}, Loss: {:>6.4f}'
                      .format(epoch_i, batch_i, len(source_int_text) // batch_size, train_acc, valid_acc, loss))

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


Epoch   0 Batch   10/538 - Train Accuracy: 0.2750, Validation Accuracy: 0.3580, Loss: 3.5907
Epoch   0 Batch   20/538 - Train Accuracy: 0.3421, Validation Accuracy: 0.3867, Loss: 3.1406
Epoch   0 Batch   30/538 - Train Accuracy: 0.3754, Validation Accuracy: 0.4334, Loss: 2.7792
Epoch   0 Batch   40/538 - Train Accuracy: 0.4814, Validation Accuracy: 0.4862, Loss: 2.2332
Epoch   0 Batch   50/538 - Train Accuracy: 0.4697, Validation Accuracy: 0.4993, Loss: 2.1344
Epoch   0 Batch   60/538 - Train Accuracy: 0.4504, Validation Accuracy: 0.4885, Loss: 1.9996
Epoch   0 Batch   70/538 - Train Accuracy: 0.5130, Validation Accuracy: 0.5126, Loss: 1.6135
Epoch   0 Batch   80/538 - Train Accuracy: 0.4852, Validation Accuracy: 0.5353, Loss: 1.4580
Epoch   0 Batch   90/538 - Train Accuracy: 0.5355, Validation Accuracy: 0.5605, Loss: 1.1581
Epoch   0 Batch  100/538 - Train Accuracy: 0.5424, Validation Accuracy: 0.5543, Loss: 0.9910
Epoch   0 Batch  110/538 - Train Accuracy: 0.5148, Validation Accuracy: 0.5625, Loss: 0.9339
Epoch   0 Batch  120/538 - Train Accuracy: 0.5584, Validation Accuracy: 0.5742, Loss: 0.7966
Epoch   0 Batch  130/538 - Train Accuracy: 0.5582, Validation Accuracy: 0.5444, Loss: 0.7359
Epoch   0 Batch  140/538 - Train Accuracy: 0.5469, Validation Accuracy: 0.5595, Loss: 0.7922
Epoch   0 Batch  150/538 - Train Accuracy: 0.5795, Validation Accuracy: 0.5767, Loss: 0.6854
Epoch   0 Batch  160/538 - Train Accuracy: 0.6034, Validation Accuracy: 0.5964, Loss: 0.6193
Epoch   0 Batch  170/538 - Train Accuracy: 0.6055, Validation Accuracy: 0.5906, Loss: 0.6137
Epoch   0 Batch  180/538 - Train Accuracy: 0.6546, Validation Accuracy: 0.6289, Loss: 0.5773
Epoch   0 Batch  190/538 - Train Accuracy: 0.6404, Validation Accuracy: 0.6341, Loss: 0.5809
Epoch   0 Batch  200/538 - Train Accuracy: 0.6432, Validation Accuracy: 0.6296, Loss: 0.5532
Epoch   0 Batch  210/538 - Train Accuracy: 0.6235, Validation Accuracy: 0.6472, Loss: 0.5421
Epoch   0 Batch  220/538 - Train Accuracy: 0.6328, Validation Accuracy: 0.6506, Loss: 0.5062
Epoch   0 Batch  230/538 - Train Accuracy: 0.6570, Validation Accuracy: 0.6483, Loss: 0.5024
Epoch   0 Batch  240/538 - Train Accuracy: 0.6400, Validation Accuracy: 0.6619, Loss: 0.4988
Epoch   0 Batch  250/538 - Train Accuracy: 0.6844, Validation Accuracy: 0.6564, Loss: 0.4717
Epoch   0 Batch  260/538 - Train Accuracy: 0.6663, Validation Accuracy: 0.6758, Loss: 0.4429
Epoch   0 Batch  270/538 - Train Accuracy: 0.6801, Validation Accuracy: 0.6866, Loss: 0.4447
Epoch   0 Batch  280/538 - Train Accuracy: 0.7115, Validation Accuracy: 0.6799, Loss: 0.4020
Epoch   0 Batch  290/538 - Train Accuracy: 0.7143, Validation Accuracy: 0.6900, Loss: 0.3985
Epoch   0 Batch  300/538 - Train Accuracy: 0.7178, Validation Accuracy: 0.7040, Loss: 0.3906
Epoch   0 Batch  310/538 - Train Accuracy: 0.7291, Validation Accuracy: 0.6976, Loss: 0.3852
Epoch   0 Batch  320/538 - Train Accuracy: 0.6804, Validation Accuracy: 0.7053, Loss: 0.3711
Epoch   0 Batch  330/538 - Train Accuracy: 0.7347, Validation Accuracy: 0.7072, Loss: 0.3398
Epoch   0 Batch  340/538 - Train Accuracy: 0.7008, Validation Accuracy: 0.7310, Loss: 0.3530
Epoch   0 Batch  350/538 - Train Accuracy: 0.7502, Validation Accuracy: 0.7347, Loss: 0.3443
Epoch   0 Batch  360/538 - Train Accuracy: 0.7365, Validation Accuracy: 0.7227, Loss: 0.3216
Epoch   0 Batch  370/538 - Train Accuracy: 0.7541, Validation Accuracy: 0.7445, Loss: 0.3156
Epoch   0 Batch  380/538 - Train Accuracy: 0.7707, Validation Accuracy: 0.7202, Loss: 0.2817
Epoch   0 Batch  390/538 - Train Accuracy: 0.7814, Validation Accuracy: 0.7443, Loss: 0.2698
Epoch   0 Batch  400/538 - Train Accuracy: 0.7684, Validation Accuracy: 0.7591, Loss: 0.2666
Epoch   0 Batch  410/538 - Train Accuracy: 0.8049, Validation Accuracy: 0.7523, Loss: 0.2599
Epoch   0 Batch  420/538 - Train Accuracy: 0.7945, Validation Accuracy: 0.7624, Loss: 0.2545
Epoch   0 Batch  430/538 - Train Accuracy: 0.7766, Validation Accuracy: 0.7694, Loss: 0.2349
Epoch   0 Batch  440/538 - Train Accuracy: 0.7918, Validation Accuracy: 0.7766, Loss: 0.2389
Epoch   0 Batch  450/538 - Train Accuracy: 0.8155, Validation Accuracy: 0.7919, Loss: 0.2342
Epoch   0 Batch  460/538 - Train Accuracy: 0.7889, Validation Accuracy: 0.8061, Loss: 0.2134
Epoch   0 Batch  470/538 - Train Accuracy: 0.8451, Validation Accuracy: 0.7985, Loss: 0.1917
Epoch   0 Batch  480/538 - Train Accuracy: 0.8562, Validation Accuracy: 0.8232, Loss: 0.1809
Epoch   0 Batch  490/538 - Train Accuracy: 0.8415, Validation Accuracy: 0.8192, Loss: 0.1679
Epoch   0 Batch  500/538 - Train Accuracy: 0.8686, Validation Accuracy: 0.8358, Loss: 0.1494
Epoch   0 Batch  510/538 - Train Accuracy: 0.8722, Validation Accuracy: 0.8303, Loss: 0.1552
Epoch   0 Batch  520/538 - Train Accuracy: 0.8535, Validation Accuracy: 0.8473, Loss: 0.1692
Epoch   0 Batch  530/538 - Train Accuracy: 0.8426, Validation Accuracy: 0.8466, Loss: 0.1530
Epoch   1 Batch   10/538 - Train Accuracy: 0.8812, Validation Accuracy: 0.8565, Loss: 0.1379
Epoch   1 Batch   20/538 - Train Accuracy: 0.8906, Validation Accuracy: 0.8491, Loss: 0.1281
Epoch   1 Batch   30/538 - Train Accuracy: 0.8645, Validation Accuracy: 0.8601, Loss: 0.1342
Epoch   1 Batch   40/538 - Train Accuracy: 0.8958, Validation Accuracy: 0.8688, Loss: 0.1084
Epoch   1 Batch   50/538 - Train Accuracy: 0.8926, Validation Accuracy: 0.8649, Loss: 0.1135
Epoch   1 Batch   60/538 - Train Accuracy: 0.9018, Validation Accuracy: 0.8841, Loss: 0.1106
Epoch   1 Batch   70/538 - Train Accuracy: 0.8904, Validation Accuracy: 0.8645, Loss: 0.1062
Epoch   1 Batch   80/538 - Train Accuracy: 0.9000, Validation Accuracy: 0.8915, Loss: 0.1092
Epoch   1 Batch   90/538 - Train Accuracy: 0.8938, Validation Accuracy: 0.8752, Loss: 0.1103
Epoch   1 Batch  100/538 - Train Accuracy: 0.9051, Validation Accuracy: 0.8961, Loss: 0.0889
Epoch   1 Batch  110/538 - Train Accuracy: 0.9010, Validation Accuracy: 0.9023, Loss: 0.0901
Epoch   1 Batch  120/538 - Train Accuracy: 0.9471, Validation Accuracy: 0.9022, Loss: 0.0723
Epoch   1 Batch  130/538 - Train Accuracy: 0.9107, Validation Accuracy: 0.9070, Loss: 0.0847
Epoch   1 Batch  140/538 - Train Accuracy: 0.8945, Validation Accuracy: 0.8841, Loss: 0.1078
Epoch   1 Batch  150/538 - Train Accuracy: 0.9164, Validation Accuracy: 0.8988, Loss: 0.0760
Epoch   1 Batch  160/538 - Train Accuracy: 0.9252, Validation Accuracy: 0.8972, Loss: 0.0708
Epoch   1 Batch  170/538 - Train Accuracy: 0.9118, Validation Accuracy: 0.8794, Loss: 0.0827
Epoch   1 Batch  180/538 - Train Accuracy: 0.9169, Validation Accuracy: 0.8835, Loss: 0.0807
Epoch   1 Batch  190/538 - Train Accuracy: 0.9152, Validation Accuracy: 0.8931, Loss: 0.0961
Epoch   1 Batch  200/538 - Train Accuracy: 0.9229, Validation Accuracy: 0.9134, Loss: 0.0689
Epoch   1 Batch  210/538 - Train Accuracy: 0.8899, Validation Accuracy: 0.8977, Loss: 0.0671
Epoch   1 Batch  220/538 - Train Accuracy: 0.9219, Validation Accuracy: 0.9054, Loss: 0.0774
Epoch   1 Batch  230/538 - Train Accuracy: 0.9039, Validation Accuracy: 0.9190, Loss: 0.0704
Epoch   1 Batch  240/538 - Train Accuracy: 0.9270, Validation Accuracy: 0.9148, Loss: 0.0717
Epoch   1 Batch  250/538 - Train Accuracy: 0.9326, Validation Accuracy: 0.9158, Loss: 0.0714
Epoch   1 Batch  260/538 - Train Accuracy: 0.9001, Validation Accuracy: 0.9153, Loss: 0.0723
Epoch   1 Batch  270/538 - Train Accuracy: 0.9205, Validation Accuracy: 0.9082, Loss: 0.0630
Epoch   1 Batch  280/538 - Train Accuracy: 0.9271, Validation Accuracy: 0.9153, Loss: 0.0583
Epoch   1 Batch  290/538 - Train Accuracy: 0.9494, Validation Accuracy: 0.9112, Loss: 0.0597
Epoch   1 Batch  300/538 - Train Accuracy: 0.9345, Validation Accuracy: 0.9263, Loss: 0.0668
Epoch   1 Batch  310/538 - Train Accuracy: 0.9580, Validation Accuracy: 0.9297, Loss: 0.0659
Epoch   1 Batch  320/538 - Train Accuracy: 0.9204, Validation Accuracy: 0.9213, Loss: 0.0587
Epoch   1 Batch  330/538 - Train Accuracy: 0.9619, Validation Accuracy: 0.9171, Loss: 0.0582
Epoch   1 Batch  340/538 - Train Accuracy: 0.9309, Validation Accuracy: 0.9254, Loss: 0.0586
Epoch   1 Batch  350/538 - Train Accuracy: 0.9384, Validation Accuracy: 0.9110, Loss: 0.0708
Epoch   1 Batch  360/538 - Train Accuracy: 0.9318, Validation Accuracy: 0.9387, Loss: 0.0581
Epoch   1 Batch  370/538 - Train Accuracy: 0.9369, Validation Accuracy: 0.9393, Loss: 0.0588
Epoch   1 Batch  380/538 - Train Accuracy: 0.9371, Validation Accuracy: 0.9292, Loss: 0.0519
Epoch   1 Batch  390/538 - Train Accuracy: 0.9308, Validation Accuracy: 0.9199, Loss: 0.0481
Epoch   1 Batch  400/538 - Train Accuracy: 0.9323, Validation Accuracy: 0.9334, Loss: 0.0584
Epoch   1 Batch  410/538 - Train Accuracy: 0.9346, Validation Accuracy: 0.9261, Loss: 0.0582
Epoch   1 Batch  420/538 - Train Accuracy: 0.9400, Validation Accuracy: 0.9361, Loss: 0.0496
Epoch   1 Batch  430/538 - Train Accuracy: 0.9334, Validation Accuracy: 0.9228, Loss: 0.0493
Epoch   1 Batch  440/538 - Train Accuracy: 0.9225, Validation Accuracy: 0.9403, Loss: 0.0547
Epoch   1 Batch  450/538 - Train Accuracy: 0.9180, Validation Accuracy: 0.9256, Loss: 0.0703
Epoch   1 Batch  460/538 - Train Accuracy: 0.9128, Validation Accuracy: 0.9190, Loss: 0.0592
Epoch   1 Batch  470/538 - Train Accuracy: 0.9410, Validation Accuracy: 0.9531, Loss: 0.0489
Epoch   1 Batch  480/538 - Train Accuracy: 0.9427, Validation Accuracy: 0.9263, Loss: 0.0418
Epoch   1 Batch  490/538 - Train Accuracy: 0.9394, Validation Accuracy: 0.9203, Loss: 0.0484
Epoch   1 Batch  500/538 - Train Accuracy: 0.9508, Validation Accuracy: 0.9279, Loss: 0.0376
Epoch   1 Batch  510/538 - Train Accuracy: 0.9585, Validation Accuracy: 0.9286, Loss: 0.0463
Epoch   1 Batch  520/538 - Train Accuracy: 0.9586, Validation Accuracy: 0.9339, Loss: 0.0464
Epoch   1 Batch  530/538 - Train Accuracy: 0.9381, Validation Accuracy: 0.9512, Loss: 0.0568
Epoch   2 Batch   10/538 - Train Accuracy: 0.9371, Validation Accuracy: 0.9395, Loss: 0.0470
Epoch   2 Batch   20/538 - Train Accuracy: 0.9468, Validation Accuracy: 0.9547, Loss: 0.0440
Epoch   2 Batch   30/538 - Train Accuracy: 0.9402, Validation Accuracy: 0.9313, Loss: 0.0509
Epoch   2 Batch   40/538 - Train Accuracy: 0.9434, Validation Accuracy: 0.9267, Loss: 0.0364
Epoch   2 Batch   50/538 - Train Accuracy: 0.9389, Validation Accuracy: 0.9283, Loss: 0.0415
Epoch   2 Batch   60/538 - Train Accuracy: 0.9332, Validation Accuracy: 0.9510, Loss: 0.0509
Epoch   2 Batch   70/538 - Train Accuracy: 0.9275, Validation Accuracy: 0.9267, Loss: 0.0375
Epoch   2 Batch   80/538 - Train Accuracy: 0.9437, Validation Accuracy: 0.9258, Loss: 0.0425
Epoch   2 Batch   90/538 - Train Accuracy: 0.9554, Validation Accuracy: 0.9265, Loss: 0.0469
Epoch   2 Batch  100/538 - Train Accuracy: 0.9504, Validation Accuracy: 0.9409, Loss: 0.0362
Epoch   2 Batch  110/538 - Train Accuracy: 0.9437, Validation Accuracy: 0.9380, Loss: 0.0440
Epoch   2 Batch  120/538 - Train Accuracy: 0.9596, Validation Accuracy: 0.9201, Loss: 0.0358
Epoch   2 Batch  130/538 - Train Accuracy: 0.9496, Validation Accuracy: 0.9318, Loss: 0.0415
Epoch   2 Batch  140/538 - Train Accuracy: 0.9230, Validation Accuracy: 0.9409, Loss: 0.0573
Epoch   2 Batch  150/538 - Train Accuracy: 0.9609, Validation Accuracy: 0.9425, Loss: 0.0345
Epoch   2 Batch  160/538 - Train Accuracy: 0.9487, Validation Accuracy: 0.9364, Loss: 0.0361
Epoch   2 Batch  170/538 - Train Accuracy: 0.9477, Validation Accuracy: 0.9240, Loss: 0.0398
Epoch   2 Batch  180/538 - Train Accuracy: 0.9496, Validation Accuracy: 0.9435, Loss: 0.0437
Epoch   2 Batch  190/538 - Train Accuracy: 0.9412, Validation Accuracy: 0.9386, Loss: 0.0570
Epoch   2 Batch  200/538 - Train Accuracy: 0.9607, Validation Accuracy: 0.9425, Loss: 0.0352
Epoch   2 Batch  210/538 - Train Accuracy: 0.9362, Validation Accuracy: 0.9361, Loss: 0.0423
Epoch   2 Batch  220/538 - Train Accuracy: 0.9414, Validation Accuracy: 0.9556, Loss: 0.0428
Epoch   2 Batch  230/538 - Train Accuracy: 0.9381, Validation Accuracy: 0.9489, Loss: 0.0398
Epoch   2 Batch  240/538 - Train Accuracy: 0.9525, Validation Accuracy: 0.9476, Loss: 0.0398
Epoch   2 Batch  250/538 - Train Accuracy: 0.9537, Validation Accuracy: 0.9464, Loss: 0.0382
Epoch   2 Batch  260/538 - Train Accuracy: 0.9448, Validation Accuracy: 0.9375, Loss: 0.0388
Epoch   2 Batch  270/538 - Train Accuracy: 0.9543, Validation Accuracy: 0.9363, Loss: 0.0378
Epoch   2 Batch  280/538 - Train Accuracy: 0.9600, Validation Accuracy: 0.9616, Loss: 0.0289
Epoch   2 Batch  290/538 - Train Accuracy: 0.9738, Validation Accuracy: 0.9396, Loss: 0.0341
Epoch   2 Batch  300/538 - Train Accuracy: 0.9481, Validation Accuracy: 0.9551, Loss: 0.0332
Epoch   2 Batch  310/538 - Train Accuracy: 0.9590, Validation Accuracy: 0.9556, Loss: 0.0457
Epoch   2 Batch  320/538 - Train Accuracy: 0.9578, Validation Accuracy: 0.9616, Loss: 0.0322
Epoch   2 Batch  330/538 - Train Accuracy: 0.9719, Validation Accuracy: 0.9535, Loss: 0.0298
Epoch   2 Batch  340/538 - Train Accuracy: 0.9488, Validation Accuracy: 0.9684, Loss: 0.0353
Epoch   2 Batch  350/538 - Train Accuracy: 0.9576, Validation Accuracy: 0.9450, Loss: 0.0504
Epoch   2 Batch  360/538 - Train Accuracy: 0.9520, Validation Accuracy: 0.9615, Loss: 0.0320
Epoch   2 Batch  370/538 - Train Accuracy: 0.9598, Validation Accuracy: 0.9469, Loss: 0.0347
Epoch   2 Batch  380/538 - Train Accuracy: 0.9637, Validation Accuracy: 0.9554, Loss: 0.0280
Epoch   2 Batch  390/538 - Train Accuracy: 0.9449, Validation Accuracy: 0.9512, Loss: 0.0326
Epoch   2 Batch  400/538 - Train Accuracy: 0.9580, Validation Accuracy: 0.9549, Loss: 0.0364
Epoch   2 Batch  410/538 - Train Accuracy: 0.9590, Validation Accuracy: 0.9563, Loss: 0.0337
Epoch   2 Batch  420/538 - Train Accuracy: 0.9543, Validation Accuracy: 0.9474, Loss: 0.0339
Epoch   2 Batch  430/538 - Train Accuracy: 0.9369, Validation Accuracy: 0.9373, Loss: 0.0306
Epoch   2 Batch  440/538 - Train Accuracy: 0.9625, Validation Accuracy: 0.9515, Loss: 0.0340
Epoch   2 Batch  450/538 - Train Accuracy: 0.9343, Validation Accuracy: 0.9581, Loss: 0.0466
Epoch   2 Batch  460/538 - Train Accuracy: 0.9332, Validation Accuracy: 0.9444, Loss: 0.0387
Epoch   2 Batch  470/538 - Train Accuracy: 0.9513, Validation Accuracy: 0.9379, Loss: 0.0303
Epoch   2 Batch  480/538 - Train Accuracy: 0.9674, Validation Accuracy: 0.9478, Loss: 0.0264
Epoch   2 Batch  490/538 - Train Accuracy: 0.9507, Validation Accuracy: 0.9593, Loss: 0.0338
Epoch   2 Batch  500/538 - Train Accuracy: 0.9721, Validation Accuracy: 0.9490, Loss: 0.0243
Epoch   2 Batch  510/538 - Train Accuracy: 0.9619, Validation Accuracy: 0.9498, Loss: 0.0323
Epoch   2 Batch  520/538 - Train Accuracy: 0.9605, Validation Accuracy: 0.9535, Loss: 0.0308
Epoch   2 Batch  530/538 - Train Accuracy: 0.9406, Validation Accuracy: 0.9647, Loss: 0.0392
Epoch   3 Batch   10/538 - Train Accuracy: 0.9551, Validation Accuracy: 0.9476, Loss: 0.0360
Epoch   3 Batch   20/538 - Train Accuracy: 0.9578, Validation Accuracy: 0.9700, Loss: 0.0317
Epoch   3 Batch   30/538 - Train Accuracy: 0.9578, Validation Accuracy: 0.9664, Loss: 0.0317
Epoch   3 Batch   40/538 - Train Accuracy: 0.9613, Validation Accuracy: 0.9604, Loss: 0.0263
Epoch   3 Batch   50/538 - Train Accuracy: 0.9600, Validation Accuracy: 0.9505, Loss: 0.0313
Epoch   3 Batch   60/538 - Train Accuracy: 0.9494, Validation Accuracy: 0.9522, Loss: 0.0364
Epoch   3 Batch   70/538 - Train Accuracy: 0.9544, Validation Accuracy: 0.9599, Loss: 0.0261
Epoch   3 Batch   80/538 - Train Accuracy: 0.9615, Validation Accuracy: 0.9689, Loss: 0.0299
Epoch   3 Batch   90/538 - Train Accuracy: 0.9580, Validation Accuracy: 0.9537, Loss: 0.0373
Epoch   3 Batch  100/538 - Train Accuracy: 0.9703, Validation Accuracy: 0.9531, Loss: 0.0255
Epoch   3 Batch  110/538 - Train Accuracy: 0.9627, Validation Accuracy: 0.9579, Loss: 0.0308
Epoch   3 Batch  120/538 - Train Accuracy: 0.9832, Validation Accuracy: 0.9615, Loss: 0.0208
Epoch   3 Batch  130/538 - Train Accuracy: 0.9628, Validation Accuracy: 0.9453, Loss: 0.0342
Epoch   3 Batch  140/538 - Train Accuracy: 0.9504, Validation Accuracy: 0.9526, Loss: 0.0411
Epoch   3 Batch  150/538 - Train Accuracy: 0.9723, Validation Accuracy: 0.9513, Loss: 0.0234
Epoch   3 Batch  160/538 - Train Accuracy: 0.9535, Validation Accuracy: 0.9588, Loss: 0.0283
Epoch   3 Batch  170/538 - Train Accuracy: 0.9487, Validation Accuracy: 0.9423, Loss: 0.0293
Epoch   3 Batch  180/538 - Train Accuracy: 0.9615, Validation Accuracy: 0.9471, Loss: 0.0304
Epoch   3 Batch  190/538 - Train Accuracy: 0.9500, Validation Accuracy: 0.9492, Loss: 0.0442
Epoch   3 Batch  200/538 - Train Accuracy: 0.9684, Validation Accuracy: 0.9632, Loss: 0.0254
Epoch   3 Batch  210/538 - Train Accuracy: 0.9578, Validation Accuracy: 0.9672, Loss: 0.0336
Epoch   3 Batch  220/538 - Train Accuracy: 0.9429, Validation Accuracy: 0.9492, Loss: 0.0311
Epoch   3 Batch  230/538 - Train Accuracy: 0.9584, Validation Accuracy: 0.9595, Loss: 0.0318
Epoch   3 Batch  240/538 - Train Accuracy: 0.9543, Validation Accuracy: 0.9542, Loss: 0.0284
Epoch   3 Batch  250/538 - Train Accuracy: 0.9736, Validation Accuracy: 0.9576, Loss: 0.0285
Epoch   3 Batch  260/538 - Train Accuracy: 0.9524, Validation Accuracy: 0.9403, Loss: 0.0313
Epoch   3 Batch  270/538 - Train Accuracy: 0.9580, Validation Accuracy: 0.9574, Loss: 0.0280
Epoch   3 Batch  280/538 - Train Accuracy: 0.9734, Validation Accuracy: 0.9513, Loss: 0.0229
Epoch   3 Batch  290/538 - Train Accuracy: 0.9773, Validation Accuracy: 0.9503, Loss: 0.0257
Epoch   3 Batch  300/538 - Train Accuracy: 0.9554, Validation Accuracy: 0.9680, Loss: 0.0274
Epoch   3 Batch  310/538 - Train Accuracy: 0.9598, Validation Accuracy: 0.9625, Loss: 0.0304
Epoch   3 Batch  320/538 - Train Accuracy: 0.9492, Validation Accuracy: 0.9595, Loss: 0.0302
Epoch   3 Batch  330/538 - Train Accuracy: 0.9673, Validation Accuracy: 0.9554, Loss: 0.0291
Epoch   3 Batch  340/538 - Train Accuracy: 0.9570, Validation Accuracy: 0.9608, Loss: 0.0265
Epoch   3 Batch  350/538 - Train Accuracy: 0.9615, Validation Accuracy: 0.9483, Loss: 0.0402
Epoch   3 Batch  360/538 - Train Accuracy: 0.9666, Validation Accuracy: 0.9631, Loss: 0.0243
Epoch   3 Batch  370/538 - Train Accuracy: 0.9713, Validation Accuracy: 0.9496, Loss: 0.0221
Epoch   3 Batch  380/538 - Train Accuracy: 0.9637, Validation Accuracy: 0.9670, Loss: 0.0237
Epoch   3 Batch  390/538 - Train Accuracy: 0.9637, Validation Accuracy: 0.9600, Loss: 0.0230
Epoch   3 Batch  400/538 - Train Accuracy: 0.9680, Validation Accuracy: 0.9627, Loss: 0.0259
Epoch   3 Batch  410/538 - Train Accuracy: 0.9727, Validation Accuracy: 0.9721, Loss: 0.0284
Epoch   3 Batch  420/538 - Train Accuracy: 0.9643, Validation Accuracy: 0.9604, Loss: 0.0331
Epoch   3 Batch  430/538 - Train Accuracy: 0.9494, Validation Accuracy: 0.9457, Loss: 0.0308
Epoch   3 Batch  440/538 - Train Accuracy: 0.9643, Validation Accuracy: 0.9572, Loss: 0.0321
Epoch   3 Batch  450/538 - Train Accuracy: 0.9442, Validation Accuracy: 0.9615, Loss: 0.0382
Epoch   3 Batch  460/538 - Train Accuracy: 0.9608, Validation Accuracy: 0.9656, Loss: 0.0246
Epoch   3 Batch  470/538 - Train Accuracy: 0.9682, Validation Accuracy: 0.9730, Loss: 0.0271
Epoch   3 Batch  480/538 - Train Accuracy: 0.9626, Validation Accuracy: 0.9473, Loss: 0.0232
Epoch   3 Batch  490/538 - Train Accuracy: 0.9604, Validation Accuracy: 0.9672, Loss: 0.0276
Epoch   3 Batch  500/538 - Train Accuracy: 0.9743, Validation Accuracy: 0.9595, Loss: 0.0203
Epoch   3 Batch  510/538 - Train Accuracy: 0.9650, Validation Accuracy: 0.9528, Loss: 0.0246
Epoch   3 Batch  520/538 - Train Accuracy: 0.9689, Validation Accuracy: 0.9652, Loss: 0.0271
Epoch   3 Batch  530/538 - Train Accuracy: 0.9506, Validation Accuracy: 0.9558, Loss: 0.0327
Epoch   4 Batch   10/538 - Train Accuracy: 0.9434, Validation Accuracy: 0.9478, Loss: 0.0313
Epoch   4 Batch   20/538 - Train Accuracy: 0.9686, Validation Accuracy: 0.9588, Loss: 0.0294
Epoch   4 Batch   30/538 - Train Accuracy: 0.9594, Validation Accuracy: 0.9604, Loss: 0.0251
Epoch   4 Batch   40/538 - Train Accuracy: 0.9544, Validation Accuracy: 0.9618, Loss: 0.0245
Epoch   4 Batch   50/538 - Train Accuracy: 0.9691, Validation Accuracy: 0.9570, Loss: 0.0261
Epoch   4 Batch   60/538 - Train Accuracy: 0.9600, Validation Accuracy: 0.9602, Loss: 0.0293
Epoch   4 Batch   70/538 - Train Accuracy: 0.9712, Validation Accuracy: 0.9567, Loss: 0.0169
Epoch   4 Batch   80/538 - Train Accuracy: 0.9744, Validation Accuracy: 0.9560, Loss: 0.0230
Epoch   4 Batch   90/538 - Train Accuracy: 0.9702, Validation Accuracy: 0.9634, Loss: 0.0302
Epoch   4 Batch  100/538 - Train Accuracy: 0.9807, Validation Accuracy: 0.9648, Loss: 0.0192
Epoch   4 Batch  110/538 - Train Accuracy: 0.9678, Validation Accuracy: 0.9608, Loss: 0.0293
Epoch   4 Batch  120/538 - Train Accuracy: 0.9738, Validation Accuracy: 0.9688, Loss: 0.0179
Epoch   4 Batch  130/538 - Train Accuracy: 0.9693, Validation Accuracy: 0.9581, Loss: 0.0246
Epoch   4 Batch  140/538 - Train Accuracy: 0.9607, Validation Accuracy: 0.9560, Loss: 0.0306
Epoch   4 Batch  150/538 - Train Accuracy: 0.9795, Validation Accuracy: 0.9583, Loss: 0.0200
Epoch   4 Batch  160/538 - Train Accuracy: 0.9529, Validation Accuracy: 0.9517, Loss: 0.0228
Epoch   4 Batch  170/538 - Train Accuracy: 0.9546, Validation Accuracy: 0.9529, Loss: 0.0244
Epoch   4 Batch  180/538 - Train Accuracy: 0.9617, Validation Accuracy: 0.9638, Loss: 0.0262
Epoch   4 Batch  190/538 - Train Accuracy: 0.9574, Validation Accuracy: 0.9476, Loss: 0.0335
Epoch   4 Batch  200/538 - Train Accuracy: 0.9787, Validation Accuracy: 0.9586, Loss: 0.0200
Epoch   4 Batch  210/538 - Train Accuracy: 0.9624, Validation Accuracy: 0.9606, Loss: 0.0308
Epoch   4 Batch  220/538 - Train Accuracy: 0.9572, Validation Accuracy: 0.9508, Loss: 0.0308
Epoch   4 Batch  230/538 - Train Accuracy: 0.9621, Validation Accuracy: 0.9657, Loss: 0.0278
Epoch   4 Batch  240/538 - Train Accuracy: 0.9684, Validation Accuracy: 0.9563, Loss: 0.0266
Epoch   4 Batch  250/538 - Train Accuracy: 0.9697, Validation Accuracy: 0.9483, Loss: 0.0267
Epoch   4 Batch  260/538 - Train Accuracy: 0.9611, Validation Accuracy: 0.9510, Loss: 0.0265
Epoch   4 Batch  270/538 - Train Accuracy: 0.9658, Validation Accuracy: 0.9600, Loss: 0.0238
Epoch   4 Batch  280/538 - Train Accuracy: 0.9747, Validation Accuracy: 0.9561, Loss: 0.0204
Epoch   4 Batch  290/538 - Train Accuracy: 0.9762, Validation Accuracy: 0.9643, Loss: 0.0201
Epoch   4 Batch  300/538 - Train Accuracy: 0.9671, Validation Accuracy: 0.9608, Loss: 0.0200
Epoch   4 Batch  310/538 - Train Accuracy: 0.9697, Validation Accuracy: 0.9604, Loss: 0.0286
Epoch   4 Batch  320/538 - Train Accuracy: 0.9663, Validation Accuracy: 0.9728, Loss: 0.0191
Epoch   4 Batch  330/538 - Train Accuracy: 0.9721, Validation Accuracy: 0.9599, Loss: 0.0192
Epoch   4 Batch  340/538 - Train Accuracy: 0.9703, Validation Accuracy: 0.9604, Loss: 0.0268
Epoch   4 Batch  350/538 - Train Accuracy: 0.9727, Validation Accuracy: 0.9684, Loss: 0.0303
Epoch   4 Batch  360/538 - Train Accuracy: 0.9650, Validation Accuracy: 0.9629, Loss: 0.0186
Epoch   4 Batch  370/538 - Train Accuracy: 0.9828, Validation Accuracy: 0.9560, Loss: 0.0184
Epoch   4 Batch  380/538 - Train Accuracy: 0.9859, Validation Accuracy: 0.9668, Loss: 0.0179
Epoch   4 Batch  390/538 - Train Accuracy: 0.9624, Validation Accuracy: 0.9620, Loss: 0.0198
Epoch   4 Batch  400/538 - Train Accuracy: 0.9663, Validation Accuracy: 0.9719, Loss: 0.0275
Epoch   4 Batch  410/538 - Train Accuracy: 0.9760, Validation Accuracy: 0.9613, Loss: 0.0223
Epoch   4 Batch  420/538 - Train Accuracy: 0.9691, Validation Accuracy: 0.9602, Loss: 0.0246
Epoch   4 Batch  430/538 - Train Accuracy: 0.9451, Validation Accuracy: 0.9586, Loss: 0.0241
Epoch   4 Batch  440/538 - Train Accuracy: 0.9660, Validation Accuracy: 0.9608, Loss: 0.0277
Epoch   4 Batch  450/538 - Train Accuracy: 0.9591, Validation Accuracy: 0.9631, Loss: 0.0291
Epoch   4 Batch  460/538 - Train Accuracy: 0.9593, Validation Accuracy: 0.9624, Loss: 0.0261
Epoch   4 Batch  470/538 - Train Accuracy: 0.9714, Validation Accuracy: 0.9709, Loss: 0.0213
Epoch   4 Batch  480/538 - Train Accuracy: 0.9704, Validation Accuracy: 0.9618, Loss: 0.0212
Epoch   4 Batch  490/538 - Train Accuracy: 0.9555, Validation Accuracy: 0.9600, Loss: 0.0233
Epoch   4 Batch  500/538 - Train Accuracy: 0.9735, Validation Accuracy: 0.9549, Loss: 0.0180
Epoch   4 Batch  510/538 - Train Accuracy: 0.9669, Validation Accuracy: 0.9627, Loss: 0.0211
Epoch   4 Batch  520/538 - Train Accuracy: 0.9693, Validation Accuracy: 0.9725, Loss: 0.0234
Epoch   4 Batch  530/538 - Train Accuracy: 0.9650, Validation Accuracy: 0.9718, Loss: 0.0242
Epoch   5 Batch   10/538 - Train Accuracy: 0.9668, Validation Accuracy: 0.9631, Loss: 0.0228
Epoch   5 Batch   20/538 - Train Accuracy: 0.9701, Validation Accuracy: 0.9716, Loss: 0.0233
Epoch   5 Batch   30/538 - Train Accuracy: 0.9617, Validation Accuracy: 0.9498, Loss: 0.0225
Epoch   5 Batch   40/538 - Train Accuracy: 0.9757, Validation Accuracy: 0.9638, Loss: 0.0210
Epoch   5 Batch   50/538 - Train Accuracy: 0.9703, Validation Accuracy: 0.9643, Loss: 0.0205
Epoch   5 Batch   60/538 - Train Accuracy: 0.9717, Validation Accuracy: 0.9604, Loss: 0.0277
Epoch   5 Batch   70/538 - Train Accuracy: 0.9758, Validation Accuracy: 0.9616, Loss: 0.0192
Epoch   5 Batch   80/538 - Train Accuracy: 0.9785, Validation Accuracy: 0.9528, Loss: 0.0214
Epoch   5 Batch   90/538 - Train Accuracy: 0.9632, Validation Accuracy: 0.9577, Loss: 0.0299
Epoch   5 Batch  100/538 - Train Accuracy: 0.9725, Validation Accuracy: 0.9624, Loss: 0.0188
Epoch   5 Batch  110/538 - Train Accuracy: 0.9707, Validation Accuracy: 0.9611, Loss: 0.0266
Epoch   5 Batch  120/538 - Train Accuracy: 0.9734, Validation Accuracy: 0.9719, Loss: 0.0179
Epoch   5 Batch  130/538 - Train Accuracy: 0.9721, Validation Accuracy: 0.9672, Loss: 0.0283
Epoch   5 Batch  140/538 - Train Accuracy: 0.9678, Validation Accuracy: 0.9611, Loss: 0.0263
Epoch   5 Batch  150/538 - Train Accuracy: 0.9693, Validation Accuracy: 0.9577, Loss: 0.0206
Epoch   5 Batch  160/538 - Train Accuracy: 0.9602, Validation Accuracy: 0.9563, Loss: 0.0223
Epoch   5 Batch  170/538 - Train Accuracy: 0.9702, Validation Accuracy: 0.9604, Loss: 0.0276
Epoch   5 Batch  180/538 - Train Accuracy: 0.9548, Validation Accuracy: 0.9611, Loss: 0.0305
Epoch   5 Batch  190/538 - Train Accuracy: 0.9587, Validation Accuracy: 0.9483, Loss: 0.0296
Epoch   5 Batch  200/538 - Train Accuracy: 0.9771, Validation Accuracy: 0.9725, Loss: 0.0248
Epoch   5 Batch  210/538 - Train Accuracy: 0.9563, Validation Accuracy: 0.9599, Loss: 0.0299
Epoch   5 Batch  220/538 - Train Accuracy: 0.9554, Validation Accuracy: 0.9467, Loss: 0.0287
Epoch   5 Batch  230/538 - Train Accuracy: 0.9688, Validation Accuracy: 0.9673, Loss: 0.0276
Epoch   5 Batch  240/538 - Train Accuracy: 0.9643, Validation Accuracy: 0.9613, Loss: 0.0217
Epoch   5 Batch  250/538 - Train Accuracy: 0.9635, Validation Accuracy: 0.9478, Loss: 0.0297
Epoch   5 Batch  260/538 - Train Accuracy: 0.9516, Validation Accuracy: 0.9380, Loss: 0.0312
Epoch   5 Batch  270/538 - Train Accuracy: 0.9645, Validation Accuracy: 0.9517, Loss: 0.0289
Epoch   5 Batch  280/538 - Train Accuracy: 0.9756, Validation Accuracy: 0.9485, Loss: 0.0238
Epoch   5 Batch  290/538 - Train Accuracy: 0.9684, Validation Accuracy: 0.9513, Loss: 0.0241
Epoch   5 Batch  300/538 - Train Accuracy: 0.9743, Validation Accuracy: 0.9600, Loss: 0.0242
Epoch   5 Batch  310/538 - Train Accuracy: 0.9811, Validation Accuracy: 0.9585, Loss: 0.0284
Epoch   5 Batch  320/538 - Train Accuracy: 0.9704, Validation Accuracy: 0.9570, Loss: 0.0232
Epoch   5 Batch  330/538 - Train Accuracy: 0.9829, Validation Accuracy: 0.9579, Loss: 0.0250
Epoch   5 Batch  340/538 - Train Accuracy: 0.9611, Validation Accuracy: 0.9643, Loss: 0.0265
Epoch   5 Batch  350/538 - Train Accuracy: 0.9554, Validation Accuracy: 0.9542, Loss: 0.0324
Epoch   5 Batch  360/538 - Train Accuracy: 0.9680, Validation Accuracy: 0.9576, Loss: 0.0220
Epoch   5 Batch  370/538 - Train Accuracy: 0.9707, Validation Accuracy: 0.9656, Loss: 0.0190
Epoch   5 Batch  380/538 - Train Accuracy: 0.9713, Validation Accuracy: 0.9576, Loss: 0.0203
Epoch   5 Batch  390/538 - Train Accuracy: 0.9591, Validation Accuracy: 0.9636, Loss: 0.0210
Epoch   5 Batch  400/538 - Train Accuracy: 0.9756, Validation Accuracy: 0.9753, Loss: 0.0245
Epoch   5 Batch  410/538 - Train Accuracy: 0.9725, Validation Accuracy: 0.9565, Loss: 0.0264
Epoch   5 Batch  420/538 - Train Accuracy: 0.9740, Validation Accuracy: 0.9679, Loss: 0.0225
Epoch   5 Batch  430/538 - Train Accuracy: 0.9510, Validation Accuracy: 0.9608, Loss: 0.0257
Epoch   5 Batch  440/538 - Train Accuracy: 0.9664, Validation Accuracy: 0.9483, Loss: 0.0279
Epoch   5 Batch  450/538 - Train Accuracy: 0.9492, Validation Accuracy: 0.9553, Loss: 0.0328
Epoch   5 Batch  460/538 - Train Accuracy: 0.9466, Validation Accuracy: 0.9606, Loss: 0.0265
Epoch   5 Batch  470/538 - Train Accuracy: 0.9645, Validation Accuracy: 0.9620, Loss: 0.0230
Epoch   5 Batch  480/538 - Train Accuracy: 0.9712, Validation Accuracy: 0.9586, Loss: 0.0271
Epoch   5 Batch  490/538 - Train Accuracy: 0.9637, Validation Accuracy: 0.9632, Loss: 0.0254
Epoch   5 Batch  500/538 - Train Accuracy: 0.9828, Validation Accuracy: 0.9593, Loss: 0.0135
Epoch   5 Batch  510/538 - Train Accuracy: 0.9784, Validation Accuracy: 0.9631, Loss: 0.0188
Epoch   5 Batch  520/538 - Train Accuracy: 0.9695, Validation Accuracy: 0.9638, Loss: 0.0210
Epoch   5 Batch  530/538 - Train Accuracy: 0.9693, Validation Accuracy: 0.9615, Loss: 0.0231
Epoch   6 Batch   10/538 - Train Accuracy: 0.9697, Validation Accuracy: 0.9679, Loss: 0.0230
Epoch   6 Batch   20/538 - Train Accuracy: 0.9613, Validation Accuracy: 0.9696, Loss: 0.0256
Epoch   6 Batch   30/538 - Train Accuracy: 0.9584, Validation Accuracy: 0.9480, Loss: 0.0239
Epoch   6 Batch   40/538 - Train Accuracy: 0.9627, Validation Accuracy: 0.9629, Loss: 0.0211
Epoch   6 Batch   50/538 - Train Accuracy: 0.9734, Validation Accuracy: 0.9698, Loss: 0.0206
Epoch   6 Batch   60/538 - Train Accuracy: 0.9695, Validation Accuracy: 0.9620, Loss: 0.0231
Epoch   6 Batch   70/538 - Train Accuracy: 0.9792, Validation Accuracy: 0.9593, Loss: 0.0169
Epoch   6 Batch   80/538 - Train Accuracy: 0.9736, Validation Accuracy: 0.9572, Loss: 0.0210
Epoch   6 Batch   90/538 - Train Accuracy: 0.9621, Validation Accuracy: 0.9636, Loss: 0.0259
Epoch   6 Batch  100/538 - Train Accuracy: 0.9775, Validation Accuracy: 0.9689, Loss: 0.0181
Epoch   6 Batch  110/538 - Train Accuracy: 0.9697, Validation Accuracy: 0.9616, Loss: 0.0240
Epoch   6 Batch  120/538 - Train Accuracy: 0.9803, Validation Accuracy: 0.9682, Loss: 0.0136
Epoch   6 Batch  130/538 - Train Accuracy: 0.9781, Validation Accuracy: 0.9688, Loss: 0.0206
Epoch   6 Batch  140/538 - Train Accuracy: 0.9602, Validation Accuracy: 0.9577, Loss: 0.0300
Epoch   6 Batch  150/538 - Train Accuracy: 0.9695, Validation Accuracy: 0.9577, Loss: 0.0186
Epoch   6 Batch  160/538 - Train Accuracy: 0.9697, Validation Accuracy: 0.9581, Loss: 0.0210
Epoch   6 Batch  170/538 - Train Accuracy: 0.9741, Validation Accuracy: 0.9679, Loss: 0.0196
Epoch   6 Batch  180/538 - Train Accuracy: 0.9617, Validation Accuracy: 0.9599, Loss: 0.0222
Epoch   6 Batch  190/538 - Train Accuracy: 0.9667, Validation Accuracy: 0.9577, Loss: 0.0272
Epoch   6 Batch  200/538 - Train Accuracy: 0.9797, Validation Accuracy: 0.9585, Loss: 0.0185
Epoch   6 Batch  210/538 - Train Accuracy: 0.9641, Validation Accuracy: 0.9711, Loss: 0.0237
Epoch   6 Batch  220/538 - Train Accuracy: 0.9559, Validation Accuracy: 0.9569, Loss: 0.0259
Epoch   6 Batch  230/538 - Train Accuracy: 0.9705, Validation Accuracy: 0.9496, Loss: 0.0233
Epoch   6 Batch  240/538 - Train Accuracy: 0.9754, Validation Accuracy: 0.9615, Loss: 0.0199
Epoch   6 Batch  250/538 - Train Accuracy: 0.9730, Validation Accuracy: 0.9624, Loss: 0.0209
Epoch   6 Batch  260/538 - Train Accuracy: 0.9500, Validation Accuracy: 0.9638, Loss: 0.0235
Epoch   6 Batch  270/538 - Train Accuracy: 0.9797, Validation Accuracy: 0.9640, Loss: 0.0169
Epoch   6 Batch  280/538 - Train Accuracy: 0.9784, Validation Accuracy: 0.9544, Loss: 0.0175
Epoch   6 Batch  290/538 - Train Accuracy: 0.9766, Validation Accuracy: 0.9549, Loss: 0.0199
Epoch   6 Batch  300/538 - Train Accuracy: 0.9667, Validation Accuracy: 0.9659, Loss: 0.0231
Epoch   6 Batch  310/538 - Train Accuracy: 0.9785, Validation Accuracy: 0.9632, Loss: 0.0287
Epoch   6 Batch  320/538 - Train Accuracy: 0.9738, Validation Accuracy: 0.9664, Loss: 0.0198
Epoch   6 Batch  330/538 - Train Accuracy: 0.9883, Validation Accuracy: 0.9577, Loss: 0.0191
Epoch   6 Batch  340/538 - Train Accuracy: 0.9656, Validation Accuracy: 0.9716, Loss: 0.0219
Epoch   6 Batch  350/538 - Train Accuracy: 0.9669, Validation Accuracy: 0.9641, Loss: 0.0283
Epoch   6 Batch  360/538 - Train Accuracy: 0.9783, Validation Accuracy: 0.9611, Loss: 0.0182
Epoch   6 Batch  370/538 - Train Accuracy: 0.9717, Validation Accuracy: 0.9611, Loss: 0.0216
Epoch   6 Batch  380/538 - Train Accuracy: 0.9846, Validation Accuracy: 0.9679, Loss: 0.0190
Epoch   6 Batch  390/538 - Train Accuracy: 0.9701, Validation Accuracy: 0.9723, Loss: 0.0284
Epoch   6 Batch  400/538 - Train Accuracy: 0.9758, Validation Accuracy: 0.9716, Loss: 0.0254
Epoch   6 Batch  410/538 - Train Accuracy: 0.9773, Validation Accuracy: 0.9689, Loss: 0.0208
Epoch   6 Batch  420/538 - Train Accuracy: 0.9678, Validation Accuracy: 0.9592, Loss: 0.0290
Epoch   6 Batch  430/538 - Train Accuracy: 0.9639, Validation Accuracy: 0.9556, Loss: 0.0262
Epoch   6 Batch  440/538 - Train Accuracy: 0.9754, Validation Accuracy: 0.9585, Loss: 0.0298
Epoch   6 Batch  450/538 - Train Accuracy: 0.9544, Validation Accuracy: 0.9631, Loss: 0.0361
Epoch   6 Batch  460/538 - Train Accuracy: 0.9626, Validation Accuracy: 0.9613, Loss: 0.0234
Epoch   6 Batch  470/538 - Train Accuracy: 0.9721, Validation Accuracy: 0.9620, Loss: 0.0223
Epoch   6 Batch  480/538 - Train Accuracy: 0.9812, Validation Accuracy: 0.9554, Loss: 0.0207
Epoch   6 Batch  490/538 - Train Accuracy: 0.9723, Validation Accuracy: 0.9668, Loss: 0.0226
Epoch   6 Batch  500/538 - Train Accuracy: 0.9858, Validation Accuracy: 0.9590, Loss: 0.0155
Epoch   6 Batch  510/538 - Train Accuracy: 0.9740, Validation Accuracy: 0.9647, Loss: 0.0215
Epoch   6 Batch  520/538 - Train Accuracy: 0.9691, Validation Accuracy: 0.9560, Loss: 0.0244
Epoch   6 Batch  530/538 - Train Accuracy: 0.9764, Validation Accuracy: 0.9600, Loss: 0.0260
Epoch   7 Batch   10/538 - Train Accuracy: 0.9717, Validation Accuracy: 0.9513, Loss: 0.0210
Epoch   7 Batch   20/538 - Train Accuracy: 0.9790, Validation Accuracy: 0.9569, Loss: 0.0222
Epoch   7 Batch   30/538 - Train Accuracy: 0.9684, Validation Accuracy: 0.9606, Loss: 0.0220
Epoch   7 Batch   40/538 - Train Accuracy: 0.9606, Validation Accuracy: 0.9624, Loss: 0.0255
Epoch   7 Batch   50/538 - Train Accuracy: 0.9682, Validation Accuracy: 0.9503, Loss: 0.0248
Epoch   7 Batch   60/538 - Train Accuracy: 0.9697, Validation Accuracy: 0.9638, Loss: 0.0240
Epoch   7 Batch   70/538 - Train Accuracy: 0.9786, Validation Accuracy: 0.9498, Loss: 0.0185
Epoch   7 Batch   80/538 - Train Accuracy: 0.9678, Validation Accuracy: 0.9553, Loss: 0.0222
Epoch   7 Batch   90/538 - Train Accuracy: 0.9788, Validation Accuracy: 0.9590, Loss: 0.0243
Epoch   7 Batch  100/538 - Train Accuracy: 0.9748, Validation Accuracy: 0.9705, Loss: 0.0162
Epoch   7 Batch  110/538 - Train Accuracy: 0.9768, Validation Accuracy: 0.9586, Loss: 0.0216
Epoch   7 Batch  120/538 - Train Accuracy: 0.9715, Validation Accuracy: 0.9625, Loss: 0.0167
Epoch   7 Batch  130/538 - Train Accuracy: 0.9678, Validation Accuracy: 0.9648, Loss: 0.0198
Epoch   7 Batch  140/538 - Train Accuracy: 0.9680, Validation Accuracy: 0.9517, Loss: 0.0305
Epoch   7 Batch  150/538 - Train Accuracy: 0.9811, Validation Accuracy: 0.9524, Loss: 0.0174
Epoch   7 Batch  160/538 - Train Accuracy: 0.9650, Validation Accuracy: 0.9494, Loss: 0.0217
Epoch   7 Batch  170/538 - Train Accuracy: 0.9650, Validation Accuracy: 0.9645, Loss: 0.0213
Epoch   7 Batch  180/538 - Train Accuracy: 0.9667, Validation Accuracy: 0.9547, Loss: 0.0258
Epoch   7 Batch  190/538 - Train Accuracy: 0.9648, Validation Accuracy: 0.9556, Loss: 0.0305
Epoch   7 Batch  200/538 - Train Accuracy: 0.9783, Validation Accuracy: 0.9572, Loss: 0.0183
Epoch   7 Batch  210/538 - Train Accuracy: 0.9821, Validation Accuracy: 0.9471, Loss: 0.0246
Epoch   7 Batch  220/538 - Train Accuracy: 0.9611, Validation Accuracy: 0.9620, Loss: 0.0240
Epoch   7 Batch  230/538 - Train Accuracy: 0.9691, Validation Accuracy: 0.9693, Loss: 0.0279
Epoch   7 Batch  240/538 - Train Accuracy: 0.9721, Validation Accuracy: 0.9638, Loss: 0.0254
Epoch   7 Batch  250/538 - Train Accuracy: 0.9725, Validation Accuracy: 0.9446, Loss: 0.0202
Epoch   7 Batch  260/538 - Train Accuracy: 0.9487, Validation Accuracy: 0.9648, Loss: 0.0281
Epoch   7 Batch  270/538 - Train Accuracy: 0.9717, Validation Accuracy: 0.9688, Loss: 0.0213
Epoch   7 Batch  280/538 - Train Accuracy: 0.9706, Validation Accuracy: 0.9524, Loss: 0.0216
Epoch   7 Batch  290/538 - Train Accuracy: 0.9857, Validation Accuracy: 0.9712, Loss: 0.0166
Epoch   7 Batch  300/538 - Train Accuracy: 0.9738, Validation Accuracy: 0.9659, Loss: 0.0218
Epoch   7 Batch  310/538 - Train Accuracy: 0.9820, Validation Accuracy: 0.9487, Loss: 0.0232
Epoch   7 Batch  320/538 - Train Accuracy: 0.9667, Validation Accuracy: 0.9654, Loss: 0.0194
Epoch   7 Batch  330/538 - Train Accuracy: 0.9840, Validation Accuracy: 0.9604, Loss: 0.0214
Epoch   7 Batch  340/538 - Train Accuracy: 0.9678, Validation Accuracy: 0.9641, Loss: 0.0251
Epoch   7 Batch  350/538 - Train Accuracy: 0.9741, Validation Accuracy: 0.9503, Loss: 0.0274
Epoch   7 Batch  360/538 - Train Accuracy: 0.9744, Validation Accuracy: 0.9608, Loss: 0.0201
Epoch   7 Batch  370/538 - Train Accuracy: 0.9766, Validation Accuracy: 0.9597, Loss: 0.0211
Epoch   7 Batch  380/538 - Train Accuracy: 0.9811, Validation Accuracy: 0.9624, Loss: 0.0178
Epoch   7 Batch  390/538 - Train Accuracy: 0.9771, Validation Accuracy: 0.9659, Loss: 0.0173
Epoch   7 Batch  400/538 - Train Accuracy: 0.9714, Validation Accuracy: 0.9806, Loss: 0.0239
Epoch   7 Batch  410/538 - Train Accuracy: 0.9754, Validation Accuracy: 0.9609, Loss: 0.0204
Epoch   7 Batch  420/538 - Train Accuracy: 0.9594, Validation Accuracy: 0.9647, Loss: 0.0279
Epoch   7 Batch  430/538 - Train Accuracy: 0.9645, Validation Accuracy: 0.9723, Loss: 0.0227
Epoch   7 Batch  440/538 - Train Accuracy: 0.9686, Validation Accuracy: 0.9585, Loss: 0.0210
Epoch   7 Batch  450/538 - Train Accuracy: 0.9600, Validation Accuracy: 0.9510, Loss: 0.0334
Epoch   7 Batch  460/538 - Train Accuracy: 0.9689, Validation Accuracy: 0.9647, Loss: 0.0234
Epoch   7 Batch  470/538 - Train Accuracy: 0.9697, Validation Accuracy: 0.9675, Loss: 0.0239
Epoch   7 Batch  480/538 - Train Accuracy: 0.9691, Validation Accuracy: 0.9600, Loss: 0.0244
Epoch   7 Batch  490/538 - Train Accuracy: 0.9645, Validation Accuracy: 0.9679, Loss: 0.0243
Epoch   7 Batch  500/538 - Train Accuracy: 0.9876, Validation Accuracy: 0.9553, Loss: 0.0181
Epoch   7 Batch  510/538 - Train Accuracy: 0.9844, Validation Accuracy: 0.9640, Loss: 0.0201
Epoch   7 Batch  520/538 - Train Accuracy: 0.9637, Validation Accuracy: 0.9560, Loss: 0.0198
Epoch   7 Batch  530/538 - Train Accuracy: 0.9637, Validation Accuracy: 0.9609, Loss: 0.0250
Epoch   8 Batch   10/538 - Train Accuracy: 0.9723, Validation Accuracy: 0.9531, Loss: 0.0282
Epoch   8 Batch   20/538 - Train Accuracy: 0.9680, Validation Accuracy: 0.9641, Loss: 0.0264
Epoch   8 Batch   30/538 - Train Accuracy: 0.9643, Validation Accuracy: 0.9538, Loss: 0.0242
Epoch   8 Batch   40/538 - Train Accuracy: 0.9721, Validation Accuracy: 0.9531, Loss: 0.0210
Epoch   8 Batch   50/538 - Train Accuracy: 0.9807, Validation Accuracy: 0.9624, Loss: 0.0203
Epoch   8 Batch   60/538 - Train Accuracy: 0.9658, Validation Accuracy: 0.9629, Loss: 0.0269
Epoch   8 Batch   70/538 - Train Accuracy: 0.9751, Validation Accuracy: 0.9606, Loss: 0.0184
Epoch   8 Batch   80/538 - Train Accuracy: 0.9699, Validation Accuracy: 0.9595, Loss: 0.0165
Epoch   8 Batch   90/538 - Train Accuracy: 0.9754, Validation Accuracy: 0.9650, Loss: 0.0271
Epoch   8 Batch  100/538 - Train Accuracy: 0.9766, Validation Accuracy: 0.9592, Loss: 0.0182
Epoch   8 Batch  110/538 - Train Accuracy: 0.9789, Validation Accuracy: 0.9696, Loss: 0.0192
Epoch   8 Batch  120/538 - Train Accuracy: 0.9758, Validation Accuracy: 0.9691, Loss: 0.0167
Epoch   8 Batch  130/538 - Train Accuracy: 0.9699, Validation Accuracy: 0.9585, Loss: 0.0271
Epoch   8 Batch  140/538 - Train Accuracy: 0.9678, Validation Accuracy: 0.9616, Loss: 0.0317
Epoch   8 Batch  150/538 - Train Accuracy: 0.9748, Validation Accuracy: 0.9631, Loss: 0.0211
Epoch   8 Batch  160/538 - Train Accuracy: 0.9652, Validation Accuracy: 0.9540, Loss: 0.0222
Epoch   8 Batch  170/538 - Train Accuracy: 0.9647, Validation Accuracy: 0.9457, Loss: 0.0203
Epoch   8 Batch  180/538 - Train Accuracy: 0.9691, Validation Accuracy: 0.9544, Loss: 0.0222
Epoch   8 Batch  190/538 - Train Accuracy: 0.9611, Validation Accuracy: 0.9522, Loss: 0.0284
Epoch   8 Batch  200/538 - Train Accuracy: 0.9811, Validation Accuracy: 0.9538, Loss: 0.0177
Epoch   8 Batch  210/538 - Train Accuracy: 0.9647, Validation Accuracy: 0.9545, Loss: 0.0211
Epoch   8 Batch  220/538 - Train Accuracy: 0.9591, Validation Accuracy: 0.9501, Loss: 0.0260
Epoch   8 Batch  230/538 - Train Accuracy: 0.9715, Validation Accuracy: 0.9664, Loss: 0.0214
Epoch   8 Batch  240/538 - Train Accuracy: 0.9670, Validation Accuracy: 0.9682, Loss: 0.0239
Epoch   8 Batch  250/538 - Train Accuracy: 0.9758, Validation Accuracy: 0.9581, Loss: 0.0189
Epoch   8 Batch  260/538 - Train Accuracy: 0.9624, Validation Accuracy: 0.9688, Loss: 0.0270
Epoch   8 Batch  270/538 - Train Accuracy: 0.9689, Validation Accuracy: 0.9570, Loss: 0.0195
Epoch   8 Batch  280/538 - Train Accuracy: 0.9756, Validation Accuracy: 0.9599, Loss: 0.0140
Epoch   8 Batch  290/538 - Train Accuracy: 0.9836, Validation Accuracy: 0.9592, Loss: 0.0163
Epoch   8 Batch  300/538 - Train Accuracy: 0.9719, Validation Accuracy: 0.9641, Loss: 0.0227
Epoch   8 Batch  310/538 - Train Accuracy: 0.9729, Validation Accuracy: 0.9654, Loss: 0.0243
Epoch   8 Batch  320/538 - Train Accuracy: 0.9693, Validation Accuracy: 0.9675, Loss: 0.0202
Epoch   8 Batch  330/538 - Train Accuracy: 0.9808, Validation Accuracy: 0.9595, Loss: 0.0188
Epoch   8 Batch  340/538 - Train Accuracy: 0.9703, Validation Accuracy: 0.9618, Loss: 0.0247
Epoch   8 Batch  350/538 - Train Accuracy: 0.9673, Validation Accuracy: 0.9577, Loss: 0.0249
Epoch   8 Batch  360/538 - Train Accuracy: 0.9830, Validation Accuracy: 0.9585, Loss: 0.0142
Epoch   8 Batch  370/538 - Train Accuracy: 0.9684, Validation Accuracy: 0.9611, Loss: 0.0219
Epoch   8 Batch  380/538 - Train Accuracy: 0.9807, Validation Accuracy: 0.9537, Loss: 0.0190
Epoch   8 Batch  390/538 - Train Accuracy: 0.9689, Validation Accuracy: 0.9675, Loss: 0.0164
Epoch   8 Batch  400/538 - Train Accuracy: 0.9719, Validation Accuracy: 0.9654, Loss: 0.0210
Epoch   8 Batch  410/538 - Train Accuracy: 0.9875, Validation Accuracy: 0.9592, Loss: 0.0204
Epoch   8 Batch  420/538 - Train Accuracy: 0.9654, Validation Accuracy: 0.9570, Loss: 0.0222
Epoch   8 Batch  430/538 - Train Accuracy: 0.9662, Validation Accuracy: 0.9638, Loss: 0.0182
Epoch   8 Batch  440/538 - Train Accuracy: 0.9732, Validation Accuracy: 0.9572, Loss: 0.0210
Epoch   8 Batch  450/538 - Train Accuracy: 0.9531, Validation Accuracy: 0.9608, Loss: 0.0290
Epoch   8 Batch  460/538 - Train Accuracy: 0.9717, Validation Accuracy: 0.9572, Loss: 0.0227
Epoch   8 Batch  470/538 - Train Accuracy: 0.9769, Validation Accuracy: 0.9624, Loss: 0.0219
Epoch   8 Batch  480/538 - Train Accuracy: 0.9814, Validation Accuracy: 0.9583, Loss: 0.0182
Epoch   8 Batch  490/538 - Train Accuracy: 0.9626, Validation Accuracy: 0.9624, Loss: 0.0233
Epoch   8 Batch  500/538 - Train Accuracy: 0.9842, Validation Accuracy: 0.9563, Loss: 0.0130
Epoch   8 Batch  510/538 - Train Accuracy: 0.9812, Validation Accuracy: 0.9609, Loss: 0.0197
Epoch   8 Batch  520/538 - Train Accuracy: 0.9697, Validation Accuracy: 0.9647, Loss: 0.0231
Epoch   8 Batch  530/538 - Train Accuracy: 0.9736, Validation Accuracy: 0.9513, Loss: 0.0267
Epoch   9 Batch   10/538 - Train Accuracy: 0.9693, Validation Accuracy: 0.9657, Loss: 0.0227
Epoch   9 Batch   20/538 - Train Accuracy: 0.9704, Validation Accuracy: 0.9670, Loss: 0.0198
Epoch   9 Batch   30/538 - Train Accuracy: 0.9760, Validation Accuracy: 0.9654, Loss: 0.0210
Epoch   9 Batch   40/538 - Train Accuracy: 0.9645, Validation Accuracy: 0.9561, Loss: 0.0224
Epoch   9 Batch   50/538 - Train Accuracy: 0.9711, Validation Accuracy: 0.9599, Loss: 0.0195
Epoch   9 Batch   60/538 - Train Accuracy: 0.9762, Validation Accuracy: 0.9569, Loss: 0.0238
Epoch   9 Batch   70/538 - Train Accuracy: 0.9740, Validation Accuracy: 0.9622, Loss: 0.0201
Epoch   9 Batch   80/538 - Train Accuracy: 0.9777, Validation Accuracy: 0.9620, Loss: 0.0184
Epoch   9 Batch   90/538 - Train Accuracy: 0.9808, Validation Accuracy: 0.9586, Loss: 0.0256
Epoch   9 Batch  100/538 - Train Accuracy: 0.9789, Validation Accuracy: 0.9608, Loss: 0.0215
Epoch   9 Batch  110/538 - Train Accuracy: 0.9799, Validation Accuracy: 0.9533, Loss: 0.0222
Epoch   9 Batch  120/538 - Train Accuracy: 0.9721, Validation Accuracy: 0.9698, Loss: 0.0189
Epoch   9 Batch  130/538 - Train Accuracy: 0.9779, Validation Accuracy: 0.9508, Loss: 0.0190
Epoch   9 Batch  140/538 - Train Accuracy: 0.9684, Validation Accuracy: 0.9528, Loss: 0.0389
Epoch   9 Batch  150/538 - Train Accuracy: 0.9865, Validation Accuracy: 0.9672, Loss: 0.0165
Epoch   9 Batch  160/538 - Train Accuracy: 0.9743, Validation Accuracy: 0.9657, Loss: 0.0183
Epoch   9 Batch  170/538 - Train Accuracy: 0.9706, Validation Accuracy: 0.9627, Loss: 0.0182
Epoch   9 Batch  180/538 - Train Accuracy: 0.9634, Validation Accuracy: 0.9563, Loss: 0.0215
Epoch   9 Batch  190/538 - Train Accuracy: 0.9719, Validation Accuracy: 0.9599, Loss: 0.0289
Epoch   9 Batch  200/538 - Train Accuracy: 0.9812, Validation Accuracy: 0.9682, Loss: 0.0178
Epoch   9 Batch  210/538 - Train Accuracy: 0.9903, Validation Accuracy: 0.9650, Loss: 0.0204
Epoch   9 Batch  220/538 - Train Accuracy: 0.9661, Validation Accuracy: 0.9700, Loss: 0.0239
Epoch   9 Batch  230/538 - Train Accuracy: 0.9775, Validation Accuracy: 0.9597, Loss: 0.0205
Epoch   9 Batch  240/538 - Train Accuracy: 0.9676, Validation Accuracy: 0.9654, Loss: 0.0256
Epoch   9 Batch  250/538 - Train Accuracy: 0.9777, Validation Accuracy: 0.9609, Loss: 0.0227
Epoch   9 Batch  260/538 - Train Accuracy: 0.9591, Validation Accuracy: 0.9595, Loss: 0.0271
Epoch   9 Batch  270/538 - Train Accuracy: 0.9756, Validation Accuracy: 0.9494, Loss: 0.0208
Epoch   9 Batch  280/538 - Train Accuracy: 0.9794, Validation Accuracy: 0.9505, Loss: 0.0210
Epoch   9 Batch  290/538 - Train Accuracy: 0.9838, Validation Accuracy: 0.9599, Loss: 0.0165
Epoch   9 Batch  300/538 - Train Accuracy: 0.9702, Validation Accuracy: 0.9643, Loss: 0.0204
Epoch   9 Batch  310/538 - Train Accuracy: 0.9787, Validation Accuracy: 0.9503, Loss: 0.0231
Epoch   9 Batch  320/538 - Train Accuracy: 0.9812, Validation Accuracy: 0.9645, Loss: 0.0155
Epoch   9 Batch  330/538 - Train Accuracy: 0.9875, Validation Accuracy: 0.9588, Loss: 0.0197
Epoch   9 Batch  340/538 - Train Accuracy: 0.9770, Validation Accuracy: 0.9664, Loss: 0.0210
Epoch   9 Batch  350/538 - Train Accuracy: 0.9762, Validation Accuracy: 0.9677, Loss: 0.0199
Epoch   9 Batch  360/538 - Train Accuracy: 0.9791, Validation Accuracy: 0.9666, Loss: 0.0185
Epoch   9 Batch  370/538 - Train Accuracy: 0.9795, Validation Accuracy: 0.9622, Loss: 0.0228
Epoch   9 Batch  380/538 - Train Accuracy: 0.9775, Validation Accuracy: 0.9696, Loss: 0.0179
Epoch   9 Batch  390/538 - Train Accuracy: 0.9730, Validation Accuracy: 0.9577, Loss: 0.0147
Epoch   9 Batch  400/538 - Train Accuracy: 0.9741, Validation Accuracy: 0.9643, Loss: 0.0203
Epoch   9 Batch  410/538 - Train Accuracy: 0.9738, Validation Accuracy: 0.9659, Loss: 0.0215
Epoch   9 Batch  420/538 - Train Accuracy: 0.9619, Validation Accuracy: 0.9650, Loss: 0.0277
Epoch   9 Batch  430/538 - Train Accuracy: 0.9705, Validation Accuracy: 0.9597, Loss: 0.0277
Epoch   9 Batch  440/538 - Train Accuracy: 0.9691, Validation Accuracy: 0.9672, Loss: 0.0260
Epoch   9 Batch  450/538 - Train Accuracy: 0.9580, Validation Accuracy: 0.9652, Loss: 0.0360
Epoch   9 Batch  460/538 - Train Accuracy: 0.9658, Validation Accuracy: 0.9725, Loss: 0.0262
Epoch   9 Batch  470/538 - Train Accuracy: 0.9732, Validation Accuracy: 0.9528, Loss: 0.0219
Epoch   9 Batch  480/538 - Train Accuracy: 0.9749, Validation Accuracy: 0.9599, Loss: 0.0224
Epoch   9 Batch  490/538 - Train Accuracy: 0.9663, Validation Accuracy: 0.9604, Loss: 0.0254
Epoch   9 Batch  500/538 - Train Accuracy: 0.9822, Validation Accuracy: 0.9604, Loss: 0.0152
Epoch   9 Batch  510/538 - Train Accuracy: 0.9715, Validation Accuracy: 0.9535, Loss: 0.0208
Epoch   9 Batch  520/538 - Train Accuracy: 0.9654, Validation Accuracy: 0.9648, Loss: 0.0266
Epoch   9 Batch  530/538 - Train Accuracy: 0.9736, Validation Accuracy: 0.9640, Loss: 0.0306
Model Trained and Saved

Save Parameters

Save the batch_size and save_path parameters for inference.


In [18]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
# Save parameters for checkpoint
helper.save_params(save_path)

Checkpoint


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

_, (source_vocab_to_int, target_vocab_to_int), (source_int_to_vocab, target_int_to_vocab) = helper.load_preprocess()
load_path = helper.load_params()

Sentence to Sequence

To feed a sentence into the model for translation, you first need to preprocess it. Implement the function sentence_to_seq() to preprocess new sentences.

  • Convert the sentence to lowercase
  • Convert words into ids using vocab_to_int
    • Convert words not in the vocabulary, to the <UNK> word id.

In [20]:
def sentence_to_seq(sentence, vocab_to_int):
    """
    Convert a sentence to a sequence of ids
    :param sentence: String
    :param vocab_to_int: Dictionary to go from the words to an id
    :return: List of word ids
    """
    # TODO: Implement Function
    
    sequence = [ vocab_to_int.get( word, vocab_to_int[ "<UNK>"] ) for word in sentence.lower().split() ]
    
    return sequence


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


Tests Passed

Translate

This will translate translate_sentence from English to French.


In [21]:
translate_sentence = 'he saw a old yellow truck .'


"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
translate_sentence = sentence_to_seq(translate_sentence, source_vocab_to_int)

loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
    # Load saved model
    loader = tf.train.import_meta_graph(load_path + '.meta')
    loader.restore(sess, load_path)

    input_data = loaded_graph.get_tensor_by_name('input:0')
    logits = loaded_graph.get_tensor_by_name('predictions:0')
    target_sequence_length = loaded_graph.get_tensor_by_name('target_sequence_length:0')
    source_sequence_length = loaded_graph.get_tensor_by_name('source_sequence_length:0')
    keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0')

    translate_logits = sess.run(logits, {input_data: [translate_sentence]*batch_size,
                                         target_sequence_length: [len(translate_sentence)*2]*batch_size,
                                         source_sequence_length: [len(translate_sentence)]*batch_size,
                                         keep_prob: 1.0})[0]

print('Input')
print('  Word Ids:      {}'.format([i for i in translate_sentence]))
print('  English Words: {}'.format([source_int_to_vocab[i] for i in translate_sentence]))

print('\nPrediction')
print('  Word Ids:      {}'.format([i for i in translate_logits]))
print('  French Words: {}'.format(" ".join([target_int_to_vocab[i] for i in translate_logits])))


INFO:tensorflow:Restoring parameters from checkpoints/dev
Input
  Word Ids:      [7, 158, 180, 33, 179, 172, 89]
  English Words: ['he', 'saw', 'a', 'old', 'yellow', 'truck', '.']

Prediction
  Word Ids:      [330, 167, 283, 316, 69, 21, 208, 122, 1]
  French Words: il a vu la petite voiture jaune . <EOS>

Imperfect Translation

You might notice that some sentences translate better than others. Since the dataset you're using only has a vocabulary of 227 English words of the thousands that you use, you're only going to see good results using these words. For this project, you don't need a perfect translation. However, if you want to create a better translation model, you'll need better data.

You can train on the WMT10 French-English corpus. This dataset has more vocabulary and richer in topics discussed. However, this will take you days to train, so make sure you've a GPU and the neural network is performing well on dataset we provided. Just make sure you play with the WMT10 corpus after you've submitted this project.

Submitting This Project

When submitting this project, make sure to run all the cells before saving the notebook. Save the notebook file as "dlnd_language_translation.ipynb" and save it as a HTML file under "File" -> "Download as". Include the "helper.py" and "problem_unittests.py" files in your submission.