Training the model with validation
Here you will learn how to train the neural machine translator model with a validation step.
You are provided with the nmt
model that you created in the last chapter. Furthermore, you will train the model on a English and French sentences obtained from the Udacity Github Repo. You are provided with training English text (tr_en
) and French text (tf_fr
) as well as validation English text (v_en
) and French text (v_fr
) from the previous exercise.
Training the model takes a bit of time so your code will take a little longer to run.
This exercise is part of the course
Machine Translation with Keras
Exercise instructions
- Create validation data by transforming
v_en
andv_fr
usingsents2seqs
function. - Get a properly transformed batch of inputs and outputs using the
sents2seqs
function. - Use the inputs (
en_x
) and outputs (de_y
) to train thenmt
on a single batch. - Use
v_en_x
andv_de_y
along withvalid_size
asbatch_size
to evaluate thenmt
model and obtain the validation accuracy.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Convert validation data to onehot
v_en_x = ____(____, ____, onehot=____, reverse=____)
v_de_y = ____(____, ____, onehot=True)
n_epochs, bsize = 3, 250
for ei in range(n_epochs):
for i in range(0,train_size,bsize):
# Get a single batch of inputs and outputs
en_x = ____(____, tr_en[____:____], onehot=____, reverse=____)
de_y = sents2seqs(____, tr_fr[____:____], onehot=True)
# Train the model on a single batch of data
nmt.____(____, ____)
# Evaluate the trained model on the validation data
res = nmt.evaluate(____, ____, batch_size=valid_size, verbose=0)
print("{} => Loss:{}, Val Acc: {}".format(ei+1,res[0], res[1]*100.0))