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
/Users/rodolfosantana/anaconda/lib/python3.6/site-packages/ipykernel/__main__.py:15: UserWarning: No GPU found. Please use a GPU to train your neural network.

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 [13]:
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 [14]:
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 [15]:
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 [16]:
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 [52]:
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 [1]:
# 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 [54]:
"""
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 [55]:
"""
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 [56]:
"""
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/1077 - Train Accuracy: 0.2488, Validation Accuracy: 0.3469, Loss: 3.5172
Epoch   0 Batch   20/1077 - Train Accuracy: 0.3344, Validation Accuracy: 0.4020, Loss: 2.8984
Epoch   0 Batch   30/1077 - Train Accuracy: 0.3801, Validation Accuracy: 0.4418, Loss: 2.5596
Epoch   0 Batch   40/1077 - Train Accuracy: 0.4570, Validation Accuracy: 0.5146, Loss: 2.2850
Epoch   0 Batch   50/1077 - Train Accuracy: 0.4723, Validation Accuracy: 0.5210, Loss: 2.0938
Epoch   0 Batch   60/1077 - Train Accuracy: 0.5212, Validation Accuracy: 0.5518, Loss: 1.8425
Epoch   0 Batch   70/1077 - Train Accuracy: 0.5119, Validation Accuracy: 0.5646, Loss: 1.8315
Epoch   0 Batch   80/1077 - Train Accuracy: 0.5523, Validation Accuracy: 0.5735, Loss: 1.6694
Epoch   0 Batch   90/1077 - Train Accuracy: 0.5367, Validation Accuracy: 0.5611, Loss: 1.6306
Epoch   0 Batch  100/1077 - Train Accuracy: 0.5789, Validation Accuracy: 0.5813, Loss: 1.5260
Epoch   0 Batch  110/1077 - Train Accuracy: 0.6059, Validation Accuracy: 0.5895, Loss: 1.3786
Epoch   0 Batch  120/1077 - Train Accuracy: 0.5926, Validation Accuracy: 0.5930, Loss: 1.3565
Epoch   0 Batch  130/1077 - Train Accuracy: 0.5740, Validation Accuracy: 0.5756, Loss: 1.1752
Epoch   0 Batch  140/1077 - Train Accuracy: 0.5637, Validation Accuracy: 0.6076, Loss: 1.1844
Epoch   0 Batch  150/1077 - Train Accuracy: 0.5911, Validation Accuracy: 0.5756, Loss: 1.0146
Epoch   0 Batch  160/1077 - Train Accuracy: 0.5668, Validation Accuracy: 0.5916, Loss: 0.9620
Epoch   0 Batch  170/1077 - Train Accuracy: 0.5492, Validation Accuracy: 0.5774, Loss: 0.9377
Epoch   0 Batch  180/1077 - Train Accuracy: 0.6133, Validation Accuracy: 0.6076, Loss: 0.8439
Epoch   0 Batch  190/1077 - Train Accuracy: 0.6316, Validation Accuracy: 0.6282, Loss: 0.7793
Epoch   0 Batch  200/1077 - Train Accuracy: 0.6277, Validation Accuracy: 0.6183, Loss: 0.7296
Epoch   0 Batch  210/1077 - Train Accuracy: 0.6395, Validation Accuracy: 0.6399, Loss: 0.7042
Epoch   0 Batch  220/1077 - Train Accuracy: 0.6632, Validation Accuracy: 0.6364, Loss: 0.6610
Epoch   0 Batch  230/1077 - Train Accuracy: 0.6741, Validation Accuracy: 0.6442, Loss: 0.6103
Epoch   0 Batch  240/1077 - Train Accuracy: 0.7199, Validation Accuracy: 0.6385, Loss: 0.5732
Epoch   0 Batch  250/1077 - Train Accuracy: 0.7134, Validation Accuracy: 0.6754, Loss: 0.5075
Epoch   0 Batch  260/1077 - Train Accuracy: 0.7158, Validation Accuracy: 0.6776, Loss: 0.4984
Epoch   0 Batch  270/1077 - Train Accuracy: 0.6555, Validation Accuracy: 0.6903, Loss: 0.5373
Epoch   0 Batch  280/1077 - Train Accuracy: 0.7066, Validation Accuracy: 0.6886, Loss: 0.5074
Epoch   0 Batch  290/1077 - Train Accuracy: 0.7223, Validation Accuracy: 0.7248, Loss: 0.4928
Epoch   0 Batch  300/1077 - Train Accuracy: 0.7389, Validation Accuracy: 0.7021, Loss: 0.4521
Epoch   0 Batch  310/1077 - Train Accuracy: 0.7172, Validation Accuracy: 0.7116, Loss: 0.4421
Epoch   0 Batch  320/1077 - Train Accuracy: 0.7195, Validation Accuracy: 0.7092, Loss: 0.4165
Epoch   0 Batch  330/1077 - Train Accuracy: 0.7363, Validation Accuracy: 0.6925, Loss: 0.4237
Epoch   0 Batch  340/1077 - Train Accuracy: 0.7566, Validation Accuracy: 0.7205, Loss: 0.3899
Epoch   0 Batch  350/1077 - Train Accuracy: 0.7309, Validation Accuracy: 0.7649, Loss: 0.3929
Epoch   0 Batch  360/1077 - Train Accuracy: 0.7684, Validation Accuracy: 0.7667, Loss: 0.3473
Epoch   0 Batch  370/1077 - Train Accuracy: 0.7857, Validation Accuracy: 0.7440, Loss: 0.3365
Epoch   0 Batch  380/1077 - Train Accuracy: 0.7766, Validation Accuracy: 0.7521, Loss: 0.3243
Epoch   0 Batch  390/1077 - Train Accuracy: 0.7594, Validation Accuracy: 0.7848, Loss: 0.3250
Epoch   0 Batch  400/1077 - Train Accuracy: 0.8316, Validation Accuracy: 0.7646, Loss: 0.3179
Epoch   0 Batch  410/1077 - Train Accuracy: 0.8047, Validation Accuracy: 0.8040, Loss: 0.3140
Epoch   0 Batch  420/1077 - Train Accuracy: 0.8223, Validation Accuracy: 0.7766, Loss: 0.2794
Epoch   0 Batch  430/1077 - Train Accuracy: 0.8285, Validation Accuracy: 0.7919, Loss: 0.2653
Epoch   0 Batch  440/1077 - Train Accuracy: 0.8344, Validation Accuracy: 0.7798, Loss: 0.2671
Epoch   0 Batch  450/1077 - Train Accuracy: 0.8527, Validation Accuracy: 0.7898, Loss: 0.2427
Epoch   0 Batch  460/1077 - Train Accuracy: 0.8219, Validation Accuracy: 0.8065, Loss: 0.2466
Epoch   0 Batch  470/1077 - Train Accuracy: 0.8610, Validation Accuracy: 0.8409, Loss: 0.2321
Epoch   0 Batch  480/1077 - Train Accuracy: 0.8458, Validation Accuracy: 0.8171, Loss: 0.2241
Epoch   0 Batch  490/1077 - Train Accuracy: 0.8234, Validation Accuracy: 0.8107, Loss: 0.2035
Epoch   0 Batch  500/1077 - Train Accuracy: 0.8547, Validation Accuracy: 0.8139, Loss: 0.1888
Epoch   0 Batch  510/1077 - Train Accuracy: 0.8426, Validation Accuracy: 0.8221, Loss: 0.1821
Epoch   0 Batch  520/1077 - Train Accuracy: 0.8943, Validation Accuracy: 0.8494, Loss: 0.1662
Epoch   0 Batch  530/1077 - Train Accuracy: 0.8555, Validation Accuracy: 0.8406, Loss: 0.1672
Epoch   0 Batch  540/1077 - Train Accuracy: 0.8711, Validation Accuracy: 0.8335, Loss: 0.1450
Epoch   0 Batch  550/1077 - Train Accuracy: 0.8797, Validation Accuracy: 0.8384, Loss: 0.1390
Epoch   0 Batch  560/1077 - Train Accuracy: 0.8648, Validation Accuracy: 0.8462, Loss: 0.1417
Epoch   0 Batch  570/1077 - Train Accuracy: 0.8766, Validation Accuracy: 0.8686, Loss: 0.1524
Epoch   0 Batch  580/1077 - Train Accuracy: 0.9059, Validation Accuracy: 0.8555, Loss: 0.1141
Epoch   0 Batch  590/1077 - Train Accuracy: 0.8758, Validation Accuracy: 0.8725, Loss: 0.1427
Epoch   0 Batch  600/1077 - Train Accuracy: 0.8806, Validation Accuracy: 0.8775, Loss: 0.1337
Epoch   0 Batch  610/1077 - Train Accuracy: 0.8750, Validation Accuracy: 0.8501, Loss: 0.1235
Epoch   0 Batch  620/1077 - Train Accuracy: 0.9207, Validation Accuracy: 0.8665, Loss: 0.1070
Epoch   0 Batch  630/1077 - Train Accuracy: 0.9082, Validation Accuracy: 0.8938, Loss: 0.1034
Epoch   0 Batch  640/1077 - Train Accuracy: 0.8981, Validation Accuracy: 0.8754, Loss: 0.1047
Epoch   0 Batch  650/1077 - Train Accuracy: 0.9062, Validation Accuracy: 0.9002, Loss: 0.1030
Epoch   0 Batch  660/1077 - Train Accuracy: 0.9414, Validation Accuracy: 0.8828, Loss: 0.0974
Epoch   0 Batch  670/1077 - Train Accuracy: 0.9006, Validation Accuracy: 0.8704, Loss: 0.1054
Epoch   0 Batch  680/1077 - Train Accuracy: 0.8828, Validation Accuracy: 0.8860, Loss: 0.1044
Epoch   0 Batch  690/1077 - Train Accuracy: 0.9309, Validation Accuracy: 0.8938, Loss: 0.0891
Epoch   0 Batch  700/1077 - Train Accuracy: 0.9266, Validation Accuracy: 0.9023, Loss: 0.0749
Epoch   0 Batch  710/1077 - Train Accuracy: 0.9219, Validation Accuracy: 0.8803, Loss: 0.0748
Epoch   0 Batch  720/1077 - Train Accuracy: 0.9054, Validation Accuracy: 0.8928, Loss: 0.0924
Epoch   0 Batch  730/1077 - Train Accuracy: 0.9277, Validation Accuracy: 0.8750, Loss: 0.0967
Epoch   0 Batch  740/1077 - Train Accuracy: 0.9273, Validation Accuracy: 0.9173, Loss: 0.0676
Epoch   0 Batch  750/1077 - Train Accuracy: 0.9375, Validation Accuracy: 0.8977, Loss: 0.0771
Epoch   0 Batch  760/1077 - Train Accuracy: 0.8977, Validation Accuracy: 0.9261, Loss: 0.0924
Epoch   0 Batch  770/1077 - Train Accuracy: 0.9118, Validation Accuracy: 0.9176, Loss: 0.0763
Epoch   0 Batch  780/1077 - Train Accuracy: 0.9109, Validation Accuracy: 0.8938, Loss: 0.0951
Epoch   0 Batch  790/1077 - Train Accuracy: 0.8859, Validation Accuracy: 0.9322, Loss: 0.0680
Epoch   0 Batch  800/1077 - Train Accuracy: 0.9379, Validation Accuracy: 0.9158, Loss: 0.0755
Epoch   0 Batch  810/1077 - Train Accuracy: 0.9435, Validation Accuracy: 0.9123, Loss: 0.0531
Epoch   0 Batch  820/1077 - Train Accuracy: 0.9289, Validation Accuracy: 0.9006, Loss: 0.0599
Epoch   0 Batch  830/1077 - Train Accuracy: 0.9152, Validation Accuracy: 0.9087, Loss: 0.0753
Epoch   0 Batch  840/1077 - Train Accuracy: 0.9535, Validation Accuracy: 0.9169, Loss: 0.0539
Epoch   0 Batch  850/1077 - Train Accuracy: 0.9323, Validation Accuracy: 0.9244, Loss: 0.0936
Epoch   0 Batch  860/1077 - Train Accuracy: 0.9089, Validation Accuracy: 0.9009, Loss: 0.0783
Epoch   0 Batch  870/1077 - Train Accuracy: 0.9235, Validation Accuracy: 0.9194, Loss: 0.0599
Epoch   0 Batch  880/1077 - Train Accuracy: 0.9363, Validation Accuracy: 0.9165, Loss: 0.0776
Epoch   0 Batch  890/1077 - Train Accuracy: 0.9159, Validation Accuracy: 0.8906, Loss: 0.0647
Epoch   0 Batch  900/1077 - Train Accuracy: 0.9273, Validation Accuracy: 0.9201, Loss: 0.0708
Epoch   0 Batch  910/1077 - Train Accuracy: 0.9189, Validation Accuracy: 0.9233, Loss: 0.0679
Epoch   0 Batch  920/1077 - Train Accuracy: 0.9371, Validation Accuracy: 0.9212, Loss: 0.0566
Epoch   0 Batch  930/1077 - Train Accuracy: 0.9418, Validation Accuracy: 0.9208, Loss: 0.0522
Epoch   0 Batch  940/1077 - Train Accuracy: 0.9563, Validation Accuracy: 0.9126, Loss: 0.0452
Epoch   0 Batch  950/1077 - Train Accuracy: 0.9472, Validation Accuracy: 0.9066, Loss: 0.0447
Epoch   0 Batch  960/1077 - Train Accuracy: 0.9483, Validation Accuracy: 0.9187, Loss: 0.0435
Epoch   0 Batch  970/1077 - Train Accuracy: 0.9570, Validation Accuracy: 0.9016, Loss: 0.0589
Epoch   0 Batch  980/1077 - Train Accuracy: 0.9125, Validation Accuracy: 0.9233, Loss: 0.0579
Epoch   0 Batch  990/1077 - Train Accuracy: 0.9428, Validation Accuracy: 0.8910, Loss: 0.0595
Epoch   0 Batch 1000/1077 - Train Accuracy: 0.9382, Validation Accuracy: 0.9020, Loss: 0.0472
Epoch   0 Batch 1010/1077 - Train Accuracy: 0.9602, Validation Accuracy: 0.9297, Loss: 0.0449
Epoch   0 Batch 1020/1077 - Train Accuracy: 0.9531, Validation Accuracy: 0.8871, Loss: 0.0418
Epoch   0 Batch 1030/1077 - Train Accuracy: 0.9645, Validation Accuracy: 0.9180, Loss: 0.0468
Epoch   0 Batch 1040/1077 - Train Accuracy: 0.9437, Validation Accuracy: 0.9169, Loss: 0.0536
Epoch   0 Batch 1050/1077 - Train Accuracy: 0.9270, Validation Accuracy: 0.9183, Loss: 0.0438
Epoch   0 Batch 1060/1077 - Train Accuracy: 0.9332, Validation Accuracy: 0.9222, Loss: 0.0417
Epoch   0 Batch 1070/1077 - Train Accuracy: 0.9418, Validation Accuracy: 0.9297, Loss: 0.0510
Epoch   1 Batch   10/1077 - Train Accuracy: 0.9433, Validation Accuracy: 0.9080, Loss: 0.0534
Epoch   1 Batch   20/1077 - Train Accuracy: 0.9402, Validation Accuracy: 0.9205, Loss: 0.0377
Epoch   1 Batch   30/1077 - Train Accuracy: 0.9578, Validation Accuracy: 0.9212, Loss: 0.0408
Epoch   1 Batch   40/1077 - Train Accuracy: 0.9480, Validation Accuracy: 0.9229, Loss: 0.0426
Epoch   1 Batch   50/1077 - Train Accuracy: 0.9426, Validation Accuracy: 0.9375, Loss: 0.0466
Epoch   1 Batch   60/1077 - Train Accuracy: 0.9602, Validation Accuracy: 0.9187, Loss: 0.0401
Epoch   1 Batch   70/1077 - Train Accuracy: 0.9445, Validation Accuracy: 0.9400, Loss: 0.0447
Epoch   1 Batch   80/1077 - Train Accuracy: 0.9582, Validation Accuracy: 0.9304, Loss: 0.0397
Epoch   1 Batch   90/1077 - Train Accuracy: 0.9441, Validation Accuracy: 0.9229, Loss: 0.0466
Epoch   1 Batch  100/1077 - Train Accuracy: 0.9578, Validation Accuracy: 0.9382, Loss: 0.0369
Epoch   1 Batch  110/1077 - Train Accuracy: 0.9590, Validation Accuracy: 0.9386, Loss: 0.0345
Epoch   1 Batch  120/1077 - Train Accuracy: 0.9348, Validation Accuracy: 0.9322, Loss: 0.0468
Epoch   1 Batch  130/1077 - Train Accuracy: 0.9513, Validation Accuracy: 0.8885, Loss: 0.0424
Epoch   1 Batch  140/1077 - Train Accuracy: 0.9679, Validation Accuracy: 0.9265, Loss: 0.0356
Epoch   1 Batch  150/1077 - Train Accuracy: 0.9565, Validation Accuracy: 0.9368, Loss: 0.0432
Epoch   1 Batch  160/1077 - Train Accuracy: 0.9559, Validation Accuracy: 0.9226, Loss: 0.0335
Epoch   1 Batch  170/1077 - Train Accuracy: 0.9426, Validation Accuracy: 0.9439, Loss: 0.0416
Epoch   1 Batch  180/1077 - Train Accuracy: 0.9586, Validation Accuracy: 0.9457, Loss: 0.0324
Epoch   1 Batch  190/1077 - Train Accuracy: 0.9594, Validation Accuracy: 0.9478, Loss: 0.0383
Epoch   1 Batch  200/1077 - Train Accuracy: 0.9398, Validation Accuracy: 0.9208, Loss: 0.0413
Epoch   1 Batch  210/1077 - Train Accuracy: 0.9442, Validation Accuracy: 0.9506, Loss: 0.0475
Epoch   1 Batch  220/1077 - Train Accuracy: 0.9589, Validation Accuracy: 0.9528, Loss: 0.0357
Epoch   1 Batch  230/1077 - Train Accuracy: 0.9516, Validation Accuracy: 0.9233, Loss: 0.0412
Epoch   1 Batch  240/1077 - Train Accuracy: 0.9680, Validation Accuracy: 0.9581, Loss: 0.0332
Epoch   1 Batch  250/1077 - Train Accuracy: 0.9428, Validation Accuracy: 0.9258, Loss: 0.0332
Epoch   1 Batch  260/1077 - Train Accuracy: 0.9520, Validation Accuracy: 0.9031, Loss: 0.0291
Epoch   1 Batch  270/1077 - Train Accuracy: 0.9453, Validation Accuracy: 0.9407, Loss: 0.0395
Epoch   1 Batch  280/1077 - Train Accuracy: 0.9391, Validation Accuracy: 0.9137, Loss: 0.0446
Epoch   1 Batch  290/1077 - Train Accuracy: 0.9395, Validation Accuracy: 0.9403, Loss: 0.0563
Epoch   1 Batch  300/1077 - Train Accuracy: 0.9613, Validation Accuracy: 0.9382, Loss: 0.0334
Epoch   1 Batch  310/1077 - Train Accuracy: 0.9527, Validation Accuracy: 0.9144, Loss: 0.0384
Epoch   1 Batch  320/1077 - Train Accuracy: 0.9398, Validation Accuracy: 0.9549, Loss: 0.0559
Epoch   1 Batch  330/1077 - Train Accuracy: 0.9336, Validation Accuracy: 0.9258, Loss: 0.0414
Epoch   1 Batch  340/1077 - Train Accuracy: 0.9819, Validation Accuracy: 0.9432, Loss: 0.0343
Epoch   1 Batch  350/1077 - Train Accuracy: 0.9582, Validation Accuracy: 0.9570, Loss: 0.0371
Epoch   1 Batch  360/1077 - Train Accuracy: 0.9480, Validation Accuracy: 0.9496, Loss: 0.0298
Epoch   1 Batch  370/1077 - Train Accuracy: 0.9710, Validation Accuracy: 0.9247, Loss: 0.0379
Epoch   1 Batch  380/1077 - Train Accuracy: 0.9598, Validation Accuracy: 0.9574, Loss: 0.0293
Epoch   1 Batch  390/1077 - Train Accuracy: 0.9355, Validation Accuracy: 0.9585, Loss: 0.0539
Epoch   1 Batch  400/1077 - Train Accuracy: 0.9648, Validation Accuracy: 0.9055, Loss: 0.0410
Epoch   1 Batch  410/1077 - Train Accuracy: 0.9601, Validation Accuracy: 0.9421, Loss: 0.0470
Epoch   1 Batch  420/1077 - Train Accuracy: 0.9707, Validation Accuracy: 0.9421, Loss: 0.0240
Epoch   1 Batch  430/1077 - Train Accuracy: 0.9414, Validation Accuracy: 0.9123, Loss: 0.0340
Epoch   1 Batch  440/1077 - Train Accuracy: 0.9395, Validation Accuracy: 0.9283, Loss: 0.0462
Epoch   1 Batch  450/1077 - Train Accuracy: 0.9590, Validation Accuracy: 0.9492, Loss: 0.0364
Epoch   1 Batch  460/1077 - Train Accuracy: 0.9523, Validation Accuracy: 0.9300, Loss: 0.0378
Epoch   1 Batch  470/1077 - Train Accuracy: 0.9700, Validation Accuracy: 0.9382, Loss: 0.0360
Epoch   1 Batch  480/1077 - Train Accuracy: 0.9655, Validation Accuracy: 0.9467, Loss: 0.0285
Epoch   1 Batch  490/1077 - Train Accuracy: 0.9645, Validation Accuracy: 0.9325, Loss: 0.0327
Epoch   1 Batch  500/1077 - Train Accuracy: 0.9535, Validation Accuracy: 0.9503, Loss: 0.0278
Epoch   1 Batch  510/1077 - Train Accuracy: 0.9520, Validation Accuracy: 0.9315, Loss: 0.0440
Epoch   1 Batch  520/1077 - Train Accuracy: 0.9758, Validation Accuracy: 0.9634, Loss: 0.0299
Epoch   1 Batch  530/1077 - Train Accuracy: 0.9426, Validation Accuracy: 0.9222, Loss: 0.0398
Epoch   1 Batch  540/1077 - Train Accuracy: 0.9566, Validation Accuracy: 0.9251, Loss: 0.0280
Epoch   1 Batch  550/1077 - Train Accuracy: 0.9512, Validation Accuracy: 0.9293, Loss: 0.0307
Epoch   1 Batch  560/1077 - Train Accuracy: 0.9453, Validation Accuracy: 0.9396, Loss: 0.0315
Epoch   1 Batch  570/1077 - Train Accuracy: 0.9297, Validation Accuracy: 0.9574, Loss: 0.0473
Epoch   1 Batch  580/1077 - Train Accuracy: 0.9542, Validation Accuracy: 0.9364, Loss: 0.0284
Epoch   1 Batch  590/1077 - Train Accuracy: 0.9317, Validation Accuracy: 0.9414, Loss: 0.0404
Epoch   1 Batch  600/1077 - Train Accuracy: 0.9621, Validation Accuracy: 0.9677, Loss: 0.0392
Epoch   1 Batch  610/1077 - Train Accuracy: 0.9770, Validation Accuracy: 0.9421, Loss: 0.0345
Epoch   1 Batch  620/1077 - Train Accuracy: 0.9703, Validation Accuracy: 0.9361, Loss: 0.0287
Epoch   1 Batch  630/1077 - Train Accuracy: 0.9695, Validation Accuracy: 0.9553, Loss: 0.0281
Epoch   1 Batch  640/1077 - Train Accuracy: 0.9606, Validation Accuracy: 0.9553, Loss: 0.0267
Epoch   1 Batch  650/1077 - Train Accuracy: 0.9695, Validation Accuracy: 0.9332, Loss: 0.0291
Epoch   1 Batch  660/1077 - Train Accuracy: 0.9809, Validation Accuracy: 0.9144, Loss: 0.0313
Epoch   1 Batch  670/1077 - Train Accuracy: 0.9659, Validation Accuracy: 0.9151, Loss: 0.0337
Epoch   1 Batch  680/1077 - Train Accuracy: 0.9468, Validation Accuracy: 0.9165, Loss: 0.0419
Epoch   1 Batch  690/1077 - Train Accuracy: 0.9496, Validation Accuracy: 0.9411, Loss: 0.0332
Epoch   1 Batch  700/1077 - Train Accuracy: 0.9617, Validation Accuracy: 0.9371, Loss: 0.0233
Epoch   1 Batch  710/1077 - Train Accuracy: 0.9605, Validation Accuracy: 0.9492, Loss: 0.0232
Epoch   1 Batch  720/1077 - Train Accuracy: 0.9622, Validation Accuracy: 0.9489, Loss: 0.0363
Epoch   1 Batch  730/1077 - Train Accuracy: 0.9551, Validation Accuracy: 0.9233, Loss: 0.0409
Epoch   1 Batch  740/1077 - Train Accuracy: 0.9602, Validation Accuracy: 0.9535, Loss: 0.0258
Epoch   1 Batch  750/1077 - Train Accuracy: 0.9520, Validation Accuracy: 0.9499, Loss: 0.0292
Epoch   1 Batch  760/1077 - Train Accuracy: 0.9500, Validation Accuracy: 0.9666, Loss: 0.0403
Epoch   1 Batch  770/1077 - Train Accuracy: 0.9542, Validation Accuracy: 0.9375, Loss: 0.0269
Epoch   1 Batch  780/1077 - Train Accuracy: 0.9453, Validation Accuracy: 0.9499, Loss: 0.0393
Epoch   1 Batch  790/1077 - Train Accuracy: 0.9363, Validation Accuracy: 0.9432, Loss: 0.0372
Epoch   1 Batch  800/1077 - Train Accuracy: 0.9566, Validation Accuracy: 0.9496, Loss: 0.0330
Epoch   1 Batch  810/1077 - Train Accuracy: 0.9673, Validation Accuracy: 0.9442, Loss: 0.0202
Epoch   1 Batch  820/1077 - Train Accuracy: 0.9621, Validation Accuracy: 0.9276, Loss: 0.0236
Epoch   1 Batch  830/1077 - Train Accuracy: 0.9422, Validation Accuracy: 0.9560, Loss: 0.0355
Epoch   1 Batch  840/1077 - Train Accuracy: 0.9555, Validation Accuracy: 0.9393, Loss: 0.0236
Epoch   1 Batch  850/1077 - Train Accuracy: 0.9490, Validation Accuracy: 0.9489, Loss: 0.0499
Epoch   1 Batch  860/1077 - Train Accuracy: 0.9520, Validation Accuracy: 0.9226, Loss: 0.0442
Epoch   1 Batch  870/1077 - Train Accuracy: 0.9527, Validation Accuracy: 0.9169, Loss: 0.0258
Epoch   1 Batch  880/1077 - Train Accuracy: 0.9742, Validation Accuracy: 0.9524, Loss: 0.0447
Epoch   1 Batch  890/1077 - Train Accuracy: 0.9732, Validation Accuracy: 0.9371, Loss: 0.0323
Epoch   1 Batch  900/1077 - Train Accuracy: 0.9656, Validation Accuracy: 0.9545, Loss: 0.0385
Epoch   1 Batch  910/1077 - Train Accuracy: 0.9587, Validation Accuracy: 0.9435, Loss: 0.0293
Epoch   1 Batch  920/1077 - Train Accuracy: 0.9707, Validation Accuracy: 0.9403, Loss: 0.0234
Epoch   1 Batch  930/1077 - Train Accuracy: 0.9703, Validation Accuracy: 0.9418, Loss: 0.0265
Epoch   1 Batch  940/1077 - Train Accuracy: 0.9738, Validation Accuracy: 0.9588, Loss: 0.0211
Epoch   1 Batch  950/1077 - Train Accuracy: 0.9728, Validation Accuracy: 0.9375, Loss: 0.0226
Epoch   1 Batch  960/1077 - Train Accuracy: 0.9435, Validation Accuracy: 0.9457, Loss: 0.0271
Epoch   1 Batch  970/1077 - Train Accuracy: 0.9703, Validation Accuracy: 0.9535, Loss: 0.0281
Epoch   1 Batch  980/1077 - Train Accuracy: 0.9492, Validation Accuracy: 0.9418, Loss: 0.0333
Epoch   1 Batch  990/1077 - Train Accuracy: 0.9745, Validation Accuracy: 0.9308, Loss: 0.0285
Epoch   1 Batch 1000/1077 - Train Accuracy: 0.9587, Validation Accuracy: 0.9347, Loss: 0.0323
Epoch   1 Batch 1010/1077 - Train Accuracy: 0.9723, Validation Accuracy: 0.9581, Loss: 0.0260
Epoch   1 Batch 1020/1077 - Train Accuracy: 0.9805, Validation Accuracy: 0.9624, Loss: 0.0235
Epoch   1 Batch 1030/1077 - Train Accuracy: 0.9570, Validation Accuracy: 0.9499, Loss: 0.0253
Epoch   1 Batch 1040/1077 - Train Accuracy: 0.9642, Validation Accuracy: 0.9237, Loss: 0.0298
Epoch   1 Batch 1050/1077 - Train Accuracy: 0.9738, Validation Accuracy: 0.9403, Loss: 0.0212
Epoch   1 Batch 1060/1077 - Train Accuracy: 0.9656, Validation Accuracy: 0.9453, Loss: 0.0228
Epoch   1 Batch 1070/1077 - Train Accuracy: 0.9625, Validation Accuracy: 0.9556, Loss: 0.0275
Epoch   2 Batch   10/1077 - Train Accuracy: 0.9708, Validation Accuracy: 0.9450, Loss: 0.0291
Epoch   2 Batch   20/1077 - Train Accuracy: 0.9602, Validation Accuracy: 0.9670, Loss: 0.0209
Epoch   2 Batch   30/1077 - Train Accuracy: 0.9684, Validation Accuracy: 0.9439, Loss: 0.0243
Epoch   2 Batch   40/1077 - Train Accuracy: 0.9742, Validation Accuracy: 0.9535, Loss: 0.0208
Epoch   2 Batch   50/1077 - Train Accuracy: 0.9582, Validation Accuracy: 0.9318, Loss: 0.0290
Epoch   2 Batch   60/1077 - Train Accuracy: 0.9725, Validation Accuracy: 0.9396, Loss: 0.0234
Epoch   2 Batch   70/1077 - Train Accuracy: 0.9659, Validation Accuracy: 0.9506, Loss: 0.0284
Epoch   2 Batch   80/1077 - Train Accuracy: 0.9723, Validation Accuracy: 0.9439, Loss: 0.0230
Epoch   2 Batch   90/1077 - Train Accuracy: 0.9676, Validation Accuracy: 0.9414, Loss: 0.0303
Epoch   2 Batch  100/1077 - Train Accuracy: 0.9512, Validation Accuracy: 0.9450, Loss: 0.0308
Epoch   2 Batch  110/1077 - Train Accuracy: 0.9594, Validation Accuracy: 0.9553, Loss: 0.0273
Epoch   2 Batch  120/1077 - Train Accuracy: 0.9656, Validation Accuracy: 0.9666, Loss: 0.0314
Epoch   2 Batch  130/1077 - Train Accuracy: 0.9747, Validation Accuracy: 0.9208, Loss: 0.0262
Epoch   2 Batch  140/1077 - Train Accuracy: 0.9860, Validation Accuracy: 0.9496, Loss: 0.0185
Epoch   2 Batch  150/1077 - Train Accuracy: 0.9777, Validation Accuracy: 0.9368, Loss: 0.0240
Epoch   2 Batch  160/1077 - Train Accuracy: 0.9816, Validation Accuracy: 0.9421, Loss: 0.0192
Epoch   2 Batch  170/1077 - Train Accuracy: 0.9563, Validation Accuracy: 0.9268, Loss: 0.0292
Epoch   2 Batch  180/1077 - Train Accuracy: 0.9723, Validation Accuracy: 0.9453, Loss: 0.0196
Epoch   2 Batch  190/1077 - Train Accuracy: 0.9695, Validation Accuracy: 0.9350, Loss: 0.0305
Epoch   2 Batch  200/1077 - Train Accuracy: 0.9707, Validation Accuracy: 0.9453, Loss: 0.0300
Epoch   2 Batch  210/1077 - Train Accuracy: 0.9539, Validation Accuracy: 0.9524, Loss: 0.0337
Epoch   2 Batch  220/1077 - Train Accuracy: 0.9593, Validation Accuracy: 0.9347, Loss: 0.0312
Epoch   2 Batch  230/1077 - Train Accuracy: 0.9498, Validation Accuracy: 0.9325, Loss: 0.0261
Epoch   2 Batch  240/1077 - Train Accuracy: 0.9848, Validation Accuracy: 0.9510, Loss: 0.0207
Epoch   2 Batch  250/1077 - Train Accuracy: 0.9656, Validation Accuracy: 0.9325, Loss: 0.0246
Epoch   2 Batch  260/1077 - Train Accuracy: 0.9650, Validation Accuracy: 0.9407, Loss: 0.0237
Epoch   2 Batch  270/1077 - Train Accuracy: 0.9633, Validation Accuracy: 0.9300, Loss: 0.0323
Epoch   2 Batch  280/1077 - Train Accuracy: 0.9652, Validation Accuracy: 0.9396, Loss: 0.0295
Epoch   2 Batch  290/1077 - Train Accuracy: 0.9645, Validation Accuracy: 0.9435, Loss: 0.0449
Epoch   2 Batch  300/1077 - Train Accuracy: 0.9638, Validation Accuracy: 0.9403, Loss: 0.0282
Epoch   2 Batch  310/1077 - Train Accuracy: 0.9691, Validation Accuracy: 0.9343, Loss: 0.0293
Epoch   2 Batch  320/1077 - Train Accuracy: 0.9574, Validation Accuracy: 0.9450, Loss: 0.0384
Epoch   2 Batch  330/1077 - Train Accuracy: 0.9617, Validation Accuracy: 0.9268, Loss: 0.0280
Epoch   2 Batch  340/1077 - Train Accuracy: 0.9778, Validation Accuracy: 0.9393, Loss: 0.0287
Epoch   2 Batch  350/1077 - Train Accuracy: 0.9734, Validation Accuracy: 0.9453, Loss: 0.0252
Epoch   2 Batch  360/1077 - Train Accuracy: 0.9789, Validation Accuracy: 0.9496, Loss: 0.0202
Epoch   2 Batch  370/1077 - Train Accuracy: 0.9669, Validation Accuracy: 0.9545, Loss: 0.0331
Epoch   2 Batch  380/1077 - Train Accuracy: 0.9719, Validation Accuracy: 0.9567, Loss: 0.0194
Epoch   2 Batch  390/1077 - Train Accuracy: 0.9672, Validation Accuracy: 0.9631, Loss: 0.0352
Epoch   2 Batch  400/1077 - Train Accuracy: 0.9672, Validation Accuracy: 0.9474, Loss: 0.0328
Epoch   2 Batch  410/1077 - Train Accuracy: 0.9531, Validation Accuracy: 0.9414, Loss: 0.0427
Epoch   2 Batch  420/1077 - Train Accuracy: 0.9859, Validation Accuracy: 0.9460, Loss: 0.0184
Epoch   2 Batch  430/1077 - Train Accuracy: 0.9641, Validation Accuracy: 0.9428, Loss: 0.0294
Epoch   2 Batch  440/1077 - Train Accuracy: 0.9648, Validation Accuracy: 0.9545, Loss: 0.0330
Epoch   2 Batch  450/1077 - Train Accuracy: 0.9648, Validation Accuracy: 0.9418, Loss: 0.0274
Epoch   2 Batch  460/1077 - Train Accuracy: 0.9684, Validation Accuracy: 0.9407, Loss: 0.0280
Epoch   2 Batch  470/1077 - Train Accuracy: 0.9683, Validation Accuracy: 0.9428, Loss: 0.0287
Epoch   2 Batch  480/1077 - Train Accuracy: 0.9593, Validation Accuracy: 0.9283, Loss: 0.0277
Epoch   2 Batch  490/1077 - Train Accuracy: 0.9629, Validation Accuracy: 0.9407, Loss: 0.0280
Epoch   2 Batch  500/1077 - Train Accuracy: 0.9684, Validation Accuracy: 0.9435, Loss: 0.0183
Epoch   2 Batch  510/1077 - Train Accuracy: 0.9539, Validation Accuracy: 0.9545, Loss: 0.0357
Epoch   2 Batch  520/1077 - Train Accuracy: 0.9825, Validation Accuracy: 0.9371, Loss: 0.0237
Epoch   2 Batch  530/1077 - Train Accuracy: 0.9461, Validation Accuracy: 0.9418, Loss: 0.0332
Epoch   2 Batch  540/1077 - Train Accuracy: 0.9707, Validation Accuracy: 0.9361, Loss: 0.0253
Epoch   2 Batch  550/1077 - Train Accuracy: 0.9664, Validation Accuracy: 0.9237, Loss: 0.0239
Epoch   2 Batch  560/1077 - Train Accuracy: 0.9676, Validation Accuracy: 0.9503, Loss: 0.0301
Epoch   2 Batch  570/1077 - Train Accuracy: 0.9581, Validation Accuracy: 0.9570, Loss: 0.0440
Epoch   2 Batch  580/1077 - Train Accuracy: 0.9669, Validation Accuracy: 0.9592, Loss: 0.0195
Epoch   2 Batch  590/1077 - Train Accuracy: 0.9498, Validation Accuracy: 0.9453, Loss: 0.0337
Epoch   2 Batch  600/1077 - Train Accuracy: 0.9490, Validation Accuracy: 0.9496, Loss: 0.0398
Epoch   2 Batch  610/1077 - Train Accuracy: 0.9688, Validation Accuracy: 0.9371, Loss: 0.0361
Epoch   2 Batch  620/1077 - Train Accuracy: 0.9723, Validation Accuracy: 0.9492, Loss: 0.0281
Epoch   2 Batch  630/1077 - Train Accuracy: 0.9707, Validation Accuracy: 0.9403, Loss: 0.0261
Epoch   2 Batch  640/1077 - Train Accuracy: 0.9829, Validation Accuracy: 0.9645, Loss: 0.0216
Epoch   2 Batch  650/1077 - Train Accuracy: 0.9727, Validation Accuracy: 0.9350, Loss: 0.0261
Epoch   2 Batch  660/1077 - Train Accuracy: 0.9848, Validation Accuracy: 0.9446, Loss: 0.0230
Epoch   2 Batch  670/1077 - Train Accuracy: 0.9688, Validation Accuracy: 0.9403, Loss: 0.0280
Epoch   2 Batch  680/1077 - Train Accuracy: 0.9513, Validation Accuracy: 0.9542, Loss: 0.0312
Epoch   2 Batch  690/1077 - Train Accuracy: 0.9691, Validation Accuracy: 0.9641, Loss: 0.0280
Epoch   2 Batch  700/1077 - Train Accuracy: 0.9754, Validation Accuracy: 0.9680, Loss: 0.0275
Epoch   2 Batch  710/1077 - Train Accuracy: 0.9672, Validation Accuracy: 0.9332, Loss: 0.0231
Epoch   2 Batch  720/1077 - Train Accuracy: 0.9581, Validation Accuracy: 0.9570, Loss: 0.0340
Epoch   2 Batch  730/1077 - Train Accuracy: 0.9645, Validation Accuracy: 0.9332, Loss: 0.0364
Epoch   2 Batch  740/1077 - Train Accuracy: 0.9668, Validation Accuracy: 0.9620, Loss: 0.0272
Epoch   2 Batch  750/1077 - Train Accuracy: 0.9645, Validation Accuracy: 0.9513, Loss: 0.0264
Epoch   2 Batch  760/1077 - Train Accuracy: 0.9582, Validation Accuracy: 0.9652, Loss: 0.0288
Epoch   2 Batch  770/1077 - Train Accuracy: 0.9576, Validation Accuracy: 0.9499, Loss: 0.0279
Epoch   2 Batch  780/1077 - Train Accuracy: 0.9508, Validation Accuracy: 0.9535, Loss: 0.0368
Epoch   2 Batch  790/1077 - Train Accuracy: 0.9418, Validation Accuracy: 0.9503, Loss: 0.0296
Epoch   2 Batch  800/1077 - Train Accuracy: 0.9707, Validation Accuracy: 0.9659, Loss: 0.0279
Epoch   2 Batch  810/1077 - Train Accuracy: 0.9814, Validation Accuracy: 0.9442, Loss: 0.0200
Epoch   2 Batch  820/1077 - Train Accuracy: 0.9852, Validation Accuracy: 0.9506, Loss: 0.0220
Epoch   2 Batch  830/1077 - Train Accuracy: 0.9402, Validation Accuracy: 0.9474, Loss: 0.0325
Epoch   2 Batch  840/1077 - Train Accuracy: 0.9605, Validation Accuracy: 0.9684, Loss: 0.0257
Epoch   2 Batch  850/1077 - Train Accuracy: 0.9490, Validation Accuracy: 0.9698, Loss: 0.0543
Epoch   2 Batch  860/1077 - Train Accuracy: 0.9695, Validation Accuracy: 0.9585, Loss: 0.0362
Epoch   2 Batch  870/1077 - Train Accuracy: 0.9527, Validation Accuracy: 0.9531, Loss: 0.0284
Epoch   2 Batch  880/1077 - Train Accuracy: 0.9793, Validation Accuracy: 0.9638, Loss: 0.0365
Epoch   2 Batch  890/1077 - Train Accuracy: 0.9777, Validation Accuracy: 0.9389, Loss: 0.0258
Epoch   2 Batch  900/1077 - Train Accuracy: 0.9637, Validation Accuracy: 0.9549, Loss: 0.0376
Epoch   2 Batch  910/1077 - Train Accuracy: 0.9587, Validation Accuracy: 0.9393, Loss: 0.0317
Epoch   2 Batch  920/1077 - Train Accuracy: 0.9664, Validation Accuracy: 0.9357, Loss: 0.0287
Epoch   2 Batch  930/1077 - Train Accuracy: 0.9656, Validation Accuracy: 0.9379, Loss: 0.0265
Epoch   2 Batch  940/1077 - Train Accuracy: 0.9750, Validation Accuracy: 0.9276, Loss: 0.0199
Epoch   2 Batch  950/1077 - Train Accuracy: 0.9781, Validation Accuracy: 0.9602, Loss: 0.0190
Epoch   2 Batch  960/1077 - Train Accuracy: 0.9565, Validation Accuracy: 0.9371, Loss: 0.0255
Epoch   2 Batch  970/1077 - Train Accuracy: 0.9754, Validation Accuracy: 0.9577, Loss: 0.0294
Epoch   2 Batch  980/1077 - Train Accuracy: 0.9543, Validation Accuracy: 0.9513, Loss: 0.0312
Epoch   2 Batch  990/1077 - Train Accuracy: 0.9601, Validation Accuracy: 0.9176, Loss: 0.0332
Epoch   2 Batch 1000/1077 - Train Accuracy: 0.9669, Validation Accuracy: 0.9393, Loss: 0.0268
Epoch   2 Batch 1010/1077 - Train Accuracy: 0.9672, Validation Accuracy: 0.9411, Loss: 0.0274
Epoch   2 Batch 1020/1077 - Train Accuracy: 0.9844, Validation Accuracy: 0.9425, Loss: 0.0323
Epoch   2 Batch 1030/1077 - Train Accuracy: 0.9676, Validation Accuracy: 0.9677, Loss: 0.0356
Epoch   2 Batch 1040/1077 - Train Accuracy: 0.9564, Validation Accuracy: 0.9102, Loss: 0.0337
Epoch   2 Batch 1050/1077 - Train Accuracy: 0.9691, Validation Accuracy: 0.9268, Loss: 0.0248
Epoch   2 Batch 1060/1077 - Train Accuracy: 0.9719, Validation Accuracy: 0.9403, Loss: 0.0229
Epoch   2 Batch 1070/1077 - Train Accuracy: 0.9598, Validation Accuracy: 0.9531, Loss: 0.0250
Epoch   3 Batch   10/1077 - Train Accuracy: 0.9683, Validation Accuracy: 0.9524, Loss: 0.0294
Epoch   3 Batch   20/1077 - Train Accuracy: 0.9688, Validation Accuracy: 0.9719, Loss: 0.0268
Epoch   3 Batch   30/1077 - Train Accuracy: 0.9723, Validation Accuracy: 0.9478, Loss: 0.0284
Epoch   3 Batch   40/1077 - Train Accuracy: 0.9742, Validation Accuracy: 0.9425, Loss: 0.0271
Epoch   3 Batch   50/1077 - Train Accuracy: 0.9805, Validation Accuracy: 0.9510, Loss: 0.0269
Epoch   3 Batch   60/1077 - Train Accuracy: 0.9766, Validation Accuracy: 0.9368, Loss: 0.0190
Epoch   3 Batch   70/1077 - Train Accuracy: 0.9552, Validation Accuracy: 0.9531, Loss: 0.0349
Epoch   3 Batch   80/1077 - Train Accuracy: 0.9770, Validation Accuracy: 0.9506, Loss: 0.0250
Epoch   3 Batch   90/1077 - Train Accuracy: 0.9676, Validation Accuracy: 0.9471, Loss: 0.0254
Epoch   3 Batch  100/1077 - Train Accuracy: 0.9660, Validation Accuracy: 0.9492, Loss: 0.0241
Epoch   3 Batch  110/1077 - Train Accuracy: 0.9723, Validation Accuracy: 0.9723, Loss: 0.0235
Epoch   3 Batch  120/1077 - Train Accuracy: 0.9762, Validation Accuracy: 0.9570, Loss: 0.0317
Epoch   3 Batch  130/1077 - Train Accuracy: 0.9632, Validation Accuracy: 0.9293, Loss: 0.0257
Epoch   3 Batch  140/1077 - Train Accuracy: 0.9799, Validation Accuracy: 0.9680, Loss: 0.0266
Epoch   3 Batch  150/1077 - Train Accuracy: 0.9665, Validation Accuracy: 0.9606, Loss: 0.0256
Epoch   3 Batch  160/1077 - Train Accuracy: 0.9770, Validation Accuracy: 0.9606, Loss: 0.0191
Epoch   3 Batch  170/1077 - Train Accuracy: 0.9637, Validation Accuracy: 0.9361, Loss: 0.0328
Epoch   3 Batch  180/1077 - Train Accuracy: 0.9793, Validation Accuracy: 0.9574, Loss: 0.0200
Epoch   3 Batch  190/1077 - Train Accuracy: 0.9711, Validation Accuracy: 0.9379, Loss: 0.0185
Epoch   3 Batch  200/1077 - Train Accuracy: 0.9672, Validation Accuracy: 0.9659, Loss: 0.0260
Epoch   3 Batch  210/1077 - Train Accuracy: 0.9576, Validation Accuracy: 0.9272, Loss: 0.0367
Epoch   3 Batch  220/1077 - Train Accuracy: 0.9745, Validation Accuracy: 0.9435, Loss: 0.0234
Epoch   3 Batch  230/1077 - Train Accuracy: 0.9635, Validation Accuracy: 0.9208, Loss: 0.0325
Epoch   3 Batch  240/1077 - Train Accuracy: 0.9824, Validation Accuracy: 0.9560, Loss: 0.0260
Epoch   3 Batch  250/1077 - Train Accuracy: 0.9609, Validation Accuracy: 0.9524, Loss: 0.0301
Epoch   3 Batch  260/1077 - Train Accuracy: 0.9621, Validation Accuracy: 0.9382, Loss: 0.0227
Epoch   3 Batch  270/1077 - Train Accuracy: 0.9676, Validation Accuracy: 0.9421, Loss: 0.0305
Epoch   3 Batch  280/1077 - Train Accuracy: 0.9563, Validation Accuracy: 0.9450, Loss: 0.0351
Epoch   3 Batch  290/1077 - Train Accuracy: 0.9680, Validation Accuracy: 0.9485, Loss: 0.0418
Epoch   3 Batch  300/1077 - Train Accuracy: 0.9597, Validation Accuracy: 0.9393, Loss: 0.0246
Epoch   3 Batch  310/1077 - Train Accuracy: 0.9645, Validation Accuracy: 0.9506, Loss: 0.0236
Epoch   3 Batch  320/1077 - Train Accuracy: 0.9613, Validation Accuracy: 0.9641, Loss: 0.0379
Epoch   3 Batch  330/1077 - Train Accuracy: 0.9816, Validation Accuracy: 0.9428, Loss: 0.0316
Epoch   3 Batch  340/1077 - Train Accuracy: 0.9893, Validation Accuracy: 0.9691, Loss: 0.0278
Epoch   3 Batch  350/1077 - Train Accuracy: 0.9844, Validation Accuracy: 0.9506, Loss: 0.0255
Epoch   3 Batch  360/1077 - Train Accuracy: 0.9609, Validation Accuracy: 0.9396, Loss: 0.0211
Epoch   3 Batch  370/1077 - Train Accuracy: 0.9684, Validation Accuracy: 0.9442, Loss: 0.0299
Epoch   3 Batch  380/1077 - Train Accuracy: 0.9727, Validation Accuracy: 0.9567, Loss: 0.0197
Epoch   3 Batch  390/1077 - Train Accuracy: 0.9602, Validation Accuracy: 0.9503, Loss: 0.0342
Epoch   3 Batch  400/1077 - Train Accuracy: 0.9605, Validation Accuracy: 0.9357, Loss: 0.0358
Epoch   3 Batch  410/1077 - Train Accuracy: 0.9720, Validation Accuracy: 0.9524, Loss: 0.0432
Epoch   3 Batch  420/1077 - Train Accuracy: 0.9879, Validation Accuracy: 0.9524, Loss: 0.0184
Epoch   3 Batch  430/1077 - Train Accuracy: 0.9648, Validation Accuracy: 0.9315, Loss: 0.0263
Epoch   3 Batch  440/1077 - Train Accuracy: 0.9609, Validation Accuracy: 0.9563, Loss: 0.0265
Epoch   3 Batch  450/1077 - Train Accuracy: 0.9648, Validation Accuracy: 0.9453, Loss: 0.0278
Epoch   3 Batch  460/1077 - Train Accuracy: 0.9723, Validation Accuracy: 0.9499, Loss: 0.0318
Epoch   3 Batch  470/1077 - Train Accuracy: 0.9720, Validation Accuracy: 0.9574, Loss: 0.0319
Epoch   3 Batch  480/1077 - Train Accuracy: 0.9605, Validation Accuracy: 0.9421, Loss: 0.0250
Epoch   3 Batch  490/1077 - Train Accuracy: 0.9527, Validation Accuracy: 0.9343, Loss: 0.0273
Epoch   3 Batch  500/1077 - Train Accuracy: 0.9699, Validation Accuracy: 0.9521, Loss: 0.0137
Epoch   3 Batch  510/1077 - Train Accuracy: 0.9430, Validation Accuracy: 0.9560, Loss: 0.0304
Epoch   3 Batch  520/1077 - Train Accuracy: 0.9836, Validation Accuracy: 0.9595, Loss: 0.0190
Epoch   3 Batch  530/1077 - Train Accuracy: 0.9707, Validation Accuracy: 0.9563, Loss: 0.0362
Epoch   3 Batch  540/1077 - Train Accuracy: 0.9684, Validation Accuracy: 0.9453, Loss: 0.0226
Epoch   3 Batch  550/1077 - Train Accuracy: 0.9645, Validation Accuracy: 0.9315, Loss: 0.0225
Epoch   3 Batch  560/1077 - Train Accuracy: 0.9531, Validation Accuracy: 0.9276, Loss: 0.0292
Epoch   3 Batch  570/1077 - Train Accuracy: 0.9609, Validation Accuracy: 0.9549, Loss: 0.0340
Epoch   3 Batch  580/1077 - Train Accuracy: 0.9654, Validation Accuracy: 0.9602, Loss: 0.0220
Epoch   3 Batch  590/1077 - Train Accuracy: 0.9679, Validation Accuracy: 0.9549, Loss: 0.0244
Epoch   3 Batch  600/1077 - Train Accuracy: 0.9572, Validation Accuracy: 0.9396, Loss: 0.0362
Epoch   3 Batch  610/1077 - Train Accuracy: 0.9638, Validation Accuracy: 0.9702, Loss: 0.0317
Epoch   3 Batch  620/1077 - Train Accuracy: 0.9699, Validation Accuracy: 0.9702, Loss: 0.0188
Epoch   3 Batch  630/1077 - Train Accuracy: 0.9844, Validation Accuracy: 0.9680, Loss: 0.0216
Epoch   3 Batch  640/1077 - Train Accuracy: 0.9673, Validation Accuracy: 0.9727, Loss: 0.0204
Epoch   3 Batch  650/1077 - Train Accuracy: 0.9816, Validation Accuracy: 0.9524, Loss: 0.0200
Epoch   3 Batch  660/1077 - Train Accuracy: 0.9859, Validation Accuracy: 0.9510, Loss: 0.0222
Epoch   3 Batch  670/1077 - Train Accuracy: 0.9716, Validation Accuracy: 0.9659, Loss: 0.0273
Epoch   3 Batch  680/1077 - Train Accuracy: 0.9583, Validation Accuracy: 0.9599, Loss: 0.0285
Epoch   3 Batch  690/1077 - Train Accuracy: 0.9688, Validation Accuracy: 0.9609, Loss: 0.0258
Epoch   3 Batch  700/1077 - Train Accuracy: 0.9793, Validation Accuracy: 0.9492, Loss: 0.0195
Epoch   3 Batch  710/1077 - Train Accuracy: 0.9758, Validation Accuracy: 0.9538, Loss: 0.0214
Epoch   3 Batch  720/1077 - Train Accuracy: 0.9659, Validation Accuracy: 0.9588, Loss: 0.0278
Epoch   3 Batch  730/1077 - Train Accuracy: 0.9531, Validation Accuracy: 0.9268, Loss: 0.0380
Epoch   3 Batch  740/1077 - Train Accuracy: 0.9633, Validation Accuracy: 0.9542, Loss: 0.0240
Epoch   3 Batch  750/1077 - Train Accuracy: 0.9613, Validation Accuracy: 0.9375, Loss: 0.0272
Epoch   3 Batch  760/1077 - Train Accuracy: 0.9586, Validation Accuracy: 0.9553, Loss: 0.0336
Epoch   3 Batch  770/1077 - Train Accuracy: 0.9676, Validation Accuracy: 0.9510, Loss: 0.0234
Epoch   3 Batch  780/1077 - Train Accuracy: 0.9461, Validation Accuracy: 0.9542, Loss: 0.0359
Epoch   3 Batch  790/1077 - Train Accuracy: 0.9559, Validation Accuracy: 0.9634, Loss: 0.0295
Epoch   3 Batch  800/1077 - Train Accuracy: 0.9594, Validation Accuracy: 0.9513, Loss: 0.0242
Epoch   3 Batch  810/1077 - Train Accuracy: 0.9754, Validation Accuracy: 0.9652, Loss: 0.0188
Epoch   3 Batch  820/1077 - Train Accuracy: 0.9852, Validation Accuracy: 0.9357, Loss: 0.0225
Epoch   3 Batch  830/1077 - Train Accuracy: 0.9574, Validation Accuracy: 0.9563, Loss: 0.0336
Epoch   3 Batch  840/1077 - Train Accuracy: 0.9707, Validation Accuracy: 0.9439, Loss: 0.0211
Epoch   3 Batch  850/1077 - Train Accuracy: 0.9531, Validation Accuracy: 0.9631, Loss: 0.0476
Epoch   3 Batch  860/1077 - Train Accuracy: 0.9725, Validation Accuracy: 0.9400, Loss: 0.0373
Epoch   3 Batch  870/1077 - Train Accuracy: 0.9720, Validation Accuracy: 0.9386, Loss: 0.0281
Epoch   3 Batch  880/1077 - Train Accuracy: 0.9559, Validation Accuracy: 0.9528, Loss: 0.0415
Epoch   3 Batch  890/1077 - Train Accuracy: 0.9524, Validation Accuracy: 0.9318, Loss: 0.0347
Epoch   3 Batch  900/1077 - Train Accuracy: 0.9641, Validation Accuracy: 0.9379, Loss: 0.0391
Epoch   3 Batch  910/1077 - Train Accuracy: 0.9792, Validation Accuracy: 0.9183, Loss: 0.0261
Epoch   3 Batch  920/1077 - Train Accuracy: 0.9570, Validation Accuracy: 0.9442, Loss: 0.0270
Epoch   3 Batch  930/1077 - Train Accuracy: 0.9793, Validation Accuracy: 0.9297, Loss: 0.0210
Epoch   3 Batch  940/1077 - Train Accuracy: 0.9836, Validation Accuracy: 0.9553, Loss: 0.0127
Epoch   3 Batch  950/1077 - Train Accuracy: 0.9743, Validation Accuracy: 0.9389, Loss: 0.0240
Epoch   3 Batch  960/1077 - Train Accuracy: 0.9669, Validation Accuracy: 0.9613, Loss: 0.0280
Epoch   3 Batch  970/1077 - Train Accuracy: 0.9730, Validation Accuracy: 0.9290, Loss: 0.0259
Epoch   3 Batch  980/1077 - Train Accuracy: 0.9773, Validation Accuracy: 0.9421, Loss: 0.0266
Epoch   3 Batch  990/1077 - Train Accuracy: 0.9753, Validation Accuracy: 0.9670, Loss: 0.0293
Epoch   3 Batch 1000/1077 - Train Accuracy: 0.9594, Validation Accuracy: 0.9368, Loss: 0.0260
Epoch   3 Batch 1010/1077 - Train Accuracy: 0.9766, Validation Accuracy: 0.9215, Loss: 0.0269
Epoch   3 Batch 1020/1077 - Train Accuracy: 0.9781, Validation Accuracy: 0.9279, Loss: 0.0203
Epoch   3 Batch 1030/1077 - Train Accuracy: 0.9520, Validation Accuracy: 0.9556, Loss: 0.0255
Epoch   3 Batch 1040/1077 - Train Accuracy: 0.9638, Validation Accuracy: 0.9609, Loss: 0.0346
Epoch   3 Batch 1050/1077 - Train Accuracy: 0.9758, Validation Accuracy: 0.9513, Loss: 0.0175
Epoch   3 Batch 1060/1077 - Train Accuracy: 0.9793, Validation Accuracy: 0.9769, Loss: 0.0176
Epoch   3 Batch 1070/1077 - Train Accuracy: 0.9652, Validation Accuracy: 0.9670, Loss: 0.0164
Epoch   4 Batch   10/1077 - Train Accuracy: 0.9807, Validation Accuracy: 0.9641, Loss: 0.0271
Epoch   4 Batch   20/1077 - Train Accuracy: 0.9629, Validation Accuracy: 0.9645, Loss: 0.0191
Epoch   4 Batch   30/1077 - Train Accuracy: 0.9766, Validation Accuracy: 0.9599, Loss: 0.0163
Epoch   4 Batch   40/1077 - Train Accuracy: 0.9809, Validation Accuracy: 0.9396, Loss: 0.0192
Epoch   4 Batch   50/1077 - Train Accuracy: 0.9781, Validation Accuracy: 0.9521, Loss: 0.0225
Epoch   4 Batch   60/1077 - Train Accuracy: 0.9833, Validation Accuracy: 0.9361, Loss: 0.0202
Epoch   4 Batch   70/1077 - Train Accuracy: 0.9700, Validation Accuracy: 0.9492, Loss: 0.0321
Epoch   4 Batch   80/1077 - Train Accuracy: 0.9559, Validation Accuracy: 0.9471, Loss: 0.0236
Epoch   4 Batch   90/1077 - Train Accuracy: 0.9656, Validation Accuracy: 0.9489, Loss: 0.0218
Epoch   4 Batch  100/1077 - Train Accuracy: 0.9715, Validation Accuracy: 0.9581, Loss: 0.0190
Epoch   4 Batch  110/1077 - Train Accuracy: 0.9812, Validation Accuracy: 0.9684, Loss: 0.0251
Epoch   4 Batch  120/1077 - Train Accuracy: 0.9473, Validation Accuracy: 0.9627, Loss: 0.0316
Epoch   4 Batch  130/1077 - Train Accuracy: 0.9699, Validation Accuracy: 0.9577, Loss: 0.0223
Epoch   4 Batch  140/1077 - Train Accuracy: 0.9844, Validation Accuracy: 0.9489, Loss: 0.0213
Epoch   4 Batch  150/1077 - Train Accuracy: 0.9688, Validation Accuracy: 0.9592, Loss: 0.0194
Epoch   4 Batch  160/1077 - Train Accuracy: 0.9859, Validation Accuracy: 0.9581, Loss: 0.0171
Epoch   4 Batch  170/1077 - Train Accuracy: 0.9793, Validation Accuracy: 0.9542, Loss: 0.0238
Epoch   4 Batch  180/1077 - Train Accuracy: 0.9816, Validation Accuracy: 0.9549, Loss: 0.0192
Epoch   4 Batch  190/1077 - Train Accuracy: 0.9797, Validation Accuracy: 0.9542, Loss: 0.0190
Epoch   4 Batch  200/1077 - Train Accuracy: 0.9801, Validation Accuracy: 0.9744, Loss: 0.0263
Epoch   4 Batch  210/1077 - Train Accuracy: 0.9487, Validation Accuracy: 0.9645, Loss: 0.0351
Epoch   4 Batch  220/1077 - Train Accuracy: 0.9688, Validation Accuracy: 0.9393, Loss: 0.0338
Epoch   4 Batch  230/1077 - Train Accuracy: 0.9702, Validation Accuracy: 0.9506, Loss: 0.0280
Epoch   4 Batch  240/1077 - Train Accuracy: 0.9777, Validation Accuracy: 0.9531, Loss: 0.0193
Epoch   4 Batch  250/1077 - Train Accuracy: 0.9560, Validation Accuracy: 0.9442, Loss: 0.0268
Epoch   4 Batch  260/1077 - Train Accuracy: 0.9721, Validation Accuracy: 0.9339, Loss: 0.0232
Epoch   4 Batch  270/1077 - Train Accuracy: 0.9664, Validation Accuracy: 0.9517, Loss: 0.0335
Epoch   4 Batch  280/1077 - Train Accuracy: 0.9637, Validation Accuracy: 0.9499, Loss: 0.0363
Epoch   4 Batch  290/1077 - Train Accuracy: 0.9625, Validation Accuracy: 0.9513, Loss: 0.0586
Epoch   4 Batch  300/1077 - Train Accuracy: 0.9642, Validation Accuracy: 0.9531, Loss: 0.0294
Epoch   4 Batch  310/1077 - Train Accuracy: 0.9703, Validation Accuracy: 0.9595, Loss: 0.0303
Epoch   4 Batch  320/1077 - Train Accuracy: 0.9695, Validation Accuracy: 0.9396, Loss: 0.0320
Epoch   4 Batch  330/1077 - Train Accuracy: 0.9594, Validation Accuracy: 0.9663, Loss: 0.0279
Epoch   4 Batch  340/1077 - Train Accuracy: 0.9877, Validation Accuracy: 0.9524, Loss: 0.0313
Epoch   4 Batch  350/1077 - Train Accuracy: 0.9758, Validation Accuracy: 0.9379, Loss: 0.0275
Epoch   4 Batch  360/1077 - Train Accuracy: 0.9578, Validation Accuracy: 0.9553, Loss: 0.0232
Epoch   4 Batch  370/1077 - Train Accuracy: 0.9825, Validation Accuracy: 0.9588, Loss: 0.0287
Epoch   4 Batch  380/1077 - Train Accuracy: 0.9762, Validation Accuracy: 0.9638, Loss: 0.0287
Epoch   4 Batch  390/1077 - Train Accuracy: 0.9437, Validation Accuracy: 0.9592, Loss: 0.0342
Epoch   4 Batch  400/1077 - Train Accuracy: 0.9613, Validation Accuracy: 0.9464, Loss: 0.0348
Epoch   4 Batch  410/1077 - Train Accuracy: 0.9741, Validation Accuracy: 0.9517, Loss: 0.0344
Epoch   4 Batch  420/1077 - Train Accuracy: 0.9820, Validation Accuracy: 0.9428, Loss: 0.0254
Epoch   4 Batch  430/1077 - Train Accuracy: 0.9699, Validation Accuracy: 0.9300, Loss: 0.0275
Epoch   4 Batch  440/1077 - Train Accuracy: 0.9555, Validation Accuracy: 0.9435, Loss: 0.0356
Epoch   4 Batch  450/1077 - Train Accuracy: 0.9734, Validation Accuracy: 0.9393, Loss: 0.0278
Epoch   4 Batch  460/1077 - Train Accuracy: 0.9652, Validation Accuracy: 0.9563, Loss: 0.0314
Epoch   4 Batch  470/1077 - Train Accuracy: 0.9753, Validation Accuracy: 0.9478, Loss: 0.0291
Epoch   4 Batch  480/1077 - Train Accuracy: 0.9704, Validation Accuracy: 0.9457, Loss: 0.0235
Epoch   4 Batch  490/1077 - Train Accuracy: 0.9625, Validation Accuracy: 0.9336, Loss: 0.0232
Epoch   4 Batch  500/1077 - Train Accuracy: 0.9781, Validation Accuracy: 0.9421, Loss: 0.0187
Epoch   4 Batch  510/1077 - Train Accuracy: 0.9578, Validation Accuracy: 0.9425, Loss: 0.0266
Epoch   4 Batch  520/1077 - Train Accuracy: 0.9877, Validation Accuracy: 0.9308, Loss: 0.0217
Epoch   4 Batch  530/1077 - Train Accuracy: 0.9598, Validation Accuracy: 0.9364, Loss: 0.0428
Epoch   4 Batch  540/1077 - Train Accuracy: 0.9805, Validation Accuracy: 0.9258, Loss: 0.0270
Epoch   4 Batch  550/1077 - Train Accuracy: 0.9453, Validation Accuracy: 0.9343, Loss: 0.0209
Epoch   4 Batch  560/1077 - Train Accuracy: 0.9684, Validation Accuracy: 0.9254, Loss: 0.0268
Epoch   4 Batch  570/1077 - Train Accuracy: 0.9622, Validation Accuracy: 0.9311, Loss: 0.0353
Epoch   4 Batch  580/1077 - Train Accuracy: 0.9758, Validation Accuracy: 0.9187, Loss: 0.0188
Epoch   4 Batch  590/1077 - Train Accuracy: 0.9601, Validation Accuracy: 0.9432, Loss: 0.0297
Epoch   4 Batch  600/1077 - Train Accuracy: 0.9647, Validation Accuracy: 0.9482, Loss: 0.0373
Epoch   4 Batch  610/1077 - Train Accuracy: 0.9642, Validation Accuracy: 0.9343, Loss: 0.0305
Epoch   4 Batch  620/1077 - Train Accuracy: 0.9910, Validation Accuracy: 0.9645, Loss: 0.0236
Epoch   4 Batch  630/1077 - Train Accuracy: 0.9816, Validation Accuracy: 0.9339, Loss: 0.0260
Epoch   4 Batch  640/1077 - Train Accuracy: 0.9907, Validation Accuracy: 0.9528, Loss: 0.0218
Epoch   4 Batch  650/1077 - Train Accuracy: 0.9773, Validation Accuracy: 0.9276, Loss: 0.0246
Epoch   4 Batch  660/1077 - Train Accuracy: 0.9852, Validation Accuracy: 0.9322, Loss: 0.0213
Epoch   4 Batch  670/1077 - Train Accuracy: 0.9677, Validation Accuracy: 0.9531, Loss: 0.0255
Epoch   4 Batch  680/1077 - Train Accuracy: 0.9528, Validation Accuracy: 0.9535, Loss: 0.0303
Epoch   4 Batch  690/1077 - Train Accuracy: 0.9461, Validation Accuracy: 0.9478, Loss: 0.0431
Epoch   4 Batch  700/1077 - Train Accuracy: 0.9785, Validation Accuracy: 0.9531, Loss: 0.0218
Epoch   4 Batch  710/1077 - Train Accuracy: 0.9789, Validation Accuracy: 0.9283, Loss: 0.0228
Epoch   4 Batch  720/1077 - Train Accuracy: 0.9745, Validation Accuracy: 0.9553, Loss: 0.0294
Epoch   4 Batch  730/1077 - Train Accuracy: 0.9613, Validation Accuracy: 0.9393, Loss: 0.0366
Epoch   4 Batch  740/1077 - Train Accuracy: 0.9637, Validation Accuracy: 0.9499, Loss: 0.0225
Epoch   4 Batch  750/1077 - Train Accuracy: 0.9699, Validation Accuracy: 0.9375, Loss: 0.0249
Epoch   4 Batch  760/1077 - Train Accuracy: 0.9457, Validation Accuracy: 0.9545, Loss: 0.0468
Epoch   4 Batch  770/1077 - Train Accuracy: 0.9561, Validation Accuracy: 0.9173, Loss: 0.0278
Epoch   4 Batch  780/1077 - Train Accuracy: 0.9477, Validation Accuracy: 0.9482, Loss: 0.0467
Epoch   4 Batch  790/1077 - Train Accuracy: 0.9527, Validation Accuracy: 0.9411, Loss: 0.0284
Epoch   4 Batch  800/1077 - Train Accuracy: 0.9703, Validation Accuracy: 0.9357, Loss: 0.0307
Epoch   4 Batch  810/1077 - Train Accuracy: 0.9706, Validation Accuracy: 0.9524, Loss: 0.0225
Epoch   4 Batch  820/1077 - Train Accuracy: 0.9805, Validation Accuracy: 0.9492, Loss: 0.0247
Epoch   4 Batch  830/1077 - Train Accuracy: 0.9590, Validation Accuracy: 0.9684, Loss: 0.0309
Epoch   4 Batch  840/1077 - Train Accuracy: 0.9672, Validation Accuracy: 0.9766, Loss: 0.0217
Epoch   4 Batch  850/1077 - Train Accuracy: 0.9762, Validation Accuracy: 0.9577, Loss: 0.0401
Epoch   4 Batch  860/1077 - Train Accuracy: 0.9598, Validation Accuracy: 0.9531, Loss: 0.0329
Epoch   4 Batch  870/1077 - Train Accuracy: 0.9836, Validation Accuracy: 0.9670, Loss: 0.0214
Epoch   4 Batch  880/1077 - Train Accuracy: 0.9910, Validation Accuracy: 0.9549, Loss: 0.0293
Epoch   4 Batch  890/1077 - Train Accuracy: 0.9647, Validation Accuracy: 0.9513, Loss: 0.0285
Epoch   4 Batch  900/1077 - Train Accuracy: 0.9727, Validation Accuracy: 0.9588, Loss: 0.0288
Epoch   4 Batch  910/1077 - Train Accuracy: 0.9468, Validation Accuracy: 0.9524, Loss: 0.0333
Epoch   4 Batch  920/1077 - Train Accuracy: 0.9645, Validation Accuracy: 0.9585, Loss: 0.0199
Epoch   4 Batch  930/1077 - Train Accuracy: 0.9727, Validation Accuracy: 0.9627, Loss: 0.0214
Epoch   4 Batch  940/1077 - Train Accuracy: 0.9816, Validation Accuracy: 0.9446, Loss: 0.0178
Epoch   4 Batch  950/1077 - Train Accuracy: 0.9565, Validation Accuracy: 0.9474, Loss: 0.0200
Epoch   4 Batch  960/1077 - Train Accuracy: 0.9661, Validation Accuracy: 0.9368, Loss: 0.0269
Epoch   4 Batch  970/1077 - Train Accuracy: 0.9738, Validation Accuracy: 0.9524, Loss: 0.0304
Epoch   4 Batch  980/1077 - Train Accuracy: 0.9578, Validation Accuracy: 0.9542, Loss: 0.0251
Epoch   4 Batch  990/1077 - Train Accuracy: 0.9823, Validation Accuracy: 0.9652, Loss: 0.0237
Epoch   4 Batch 1000/1077 - Train Accuracy: 0.9661, Validation Accuracy: 0.9606, Loss: 0.0302
Epoch   4 Batch 1010/1077 - Train Accuracy: 0.9703, Validation Accuracy: 0.9453, Loss: 0.0241
Epoch   4 Batch 1020/1077 - Train Accuracy: 0.9730, Validation Accuracy: 0.9716, Loss: 0.0240
Epoch   4 Batch 1030/1077 - Train Accuracy: 0.9750, Validation Accuracy: 0.9705, Loss: 0.0198
Epoch   4 Batch 1040/1077 - Train Accuracy: 0.9593, Validation Accuracy: 0.9350, Loss: 0.0332
Epoch   4 Batch 1050/1077 - Train Accuracy: 0.9566, Validation Accuracy: 0.9407, Loss: 0.0213
Epoch   4 Batch 1060/1077 - Train Accuracy: 0.9734, Validation Accuracy: 0.9421, Loss: 0.0225
Epoch   4 Batch 1070/1077 - Train Accuracy: 0.9699, Validation Accuracy: 0.9570, Loss: 0.0185
Model Trained and Saved

Save Parameters

Save the batch_size and save_path parameters for inference.


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

Checkpoint


In [58]:
"""
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 [59]:
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 [60]:
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:      [153, 96, 115, 106, 89, 41, 148]
  English Words: ['he', 'saw', 'a', 'old', 'yellow', 'truck', '.']

Prediction
  Word Ids:      [121, 338, 171, 143, 111, 247, 62, 245, 1]
  French Words: il aime qu'il est difficile de football . <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.