pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 8decoder_in = tf.expand_dims(tf.constant([word2idx_fr["BOS"]]), axis=0)pred_sent_fr = []while True:decoder_pred, decoder_state = decoder(decoder_in, decoder_state)decoder_pred = tf.argmax(decoder_pred, axis=-1)pred_word = idx2word_fr[decoder_pred.numpy()[0][0]]pred_sent_fr.append(pred_word)if pred_word == "EOS":breakdecoder_in = decoder_predprint("predicted: ", " ".join(pred_sent_fr))def evaluate_bleu_score(encoder, decoder, test_dataset,word2idx_fr, idx2word_fr):bleu_scores = []smooth_fn = SmoothingFunction()for encoder_in, decoder_in, decoder_out in test_dataset:encoder_state = encoder.init_state(batch_size)encoder_out, encoder_state = encoder(encoder_in, encoder_state)decoder_state = encoder_statedecoder_pred, decoder_state = decoder(decoder_in, decoder_state)# compute argmaxdecoder_out = decoder_out.numpy()decoder_pred = tf.argmax(decoder_pred, axis=-1).numpy()for i in range(decoder_out.shape[0]):ref_sent = [idx2word_fr[j] for j indecoder_out[i].tolist() if j > 0]hyp_sent = [idx2word_fr[j] for j indecoder_pred[i].tolist() if j > 0]# remove trailing EOSref_sent = ref_sent[0:-1]hyp_sent = hyp_sent[0:-1]bleu_score = sentence_bleu([ref_sent], hyp_sent,smoothing_function=smooth_fn.method1)bleu_scores.append(bleu_score)[ 325 ]

Recurrent Neural Networksreturn np.mean(np.array(bleu_scores))The training loop is shown as follows. We will use the Adam optimizer for ourmodel. We also set up a checkpoint so we can save our model after every 10 epochs.We then train the model for 250 epochs, and print out the loss, an example sentenceand its translation, and the BLEU score computed over the entire test set:optimizer = tf.keras.optimizers.Adam()checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")checkpoint = tf.train.Checkpoint(optimizer=optimizer,encoder=encoder,decoder=decoder)num_epochs = 250eval_scores = []for e in range(num_epochs):encoder_state = encoder.init_state(batch_size)for batch, data in enumerate(train_dataset):encoder_in, decoder_in, decoder_out = data# print(encoder_in.shape, decoder_in.shape, decoder_out.shape)loss = train_step(encoder_in, decoder_in, decoder_out, encoder_state)print("Epoch: {}, Loss: {:.4f}".format(e + 1, loss.numpy()))if e % 10 == 0:checkpoint.save(file_prefix=checkpoint_prefix)predict(encoder, decoder, batch_size, sents_en, data_en,sents_fr_out, word2idx_fr, idx2word_fr)eval_score = evaluate_bleu_score(encoder, decoder,test_dataset, word2idx_fr, idx2word_fr)print("Eval Score (BLEU): {:.3e}".format(eval_score))# eval_scores.append(eval_score)checkpoint.save(file_prefix=checkpoint_prefix)The results from the first 5 and last 5 epochs of training are shown as follows. Noticethat the loss has gone down from about 1.5 to around 0.07 in epoch 247. The BLEUscores have also gone up by around 2.5 times. Most impressive, however, is thedifference in translation quality between the first 5 and last 5 epochs:[ 326 ]

Chapter 8

decoder_in = tf.expand_dims(

tf.constant([word2idx_fr["BOS"]]), axis=0)

pred_sent_fr = []

while True:

decoder_pred, decoder_state = decoder(

decoder_in, decoder_state)

decoder_pred = tf.argmax(decoder_pred, axis=-1)

pred_word = idx2word_fr[decoder_pred.numpy()[0][0]]

pred_sent_fr.append(pred_word)

if pred_word == "EOS":

break

decoder_in = decoder_pred

print("predicted: ", " ".join(pred_sent_fr))

def evaluate_bleu_score(encoder, decoder, test_dataset,

word2idx_fr, idx2word_fr):

bleu_scores = []

smooth_fn = SmoothingFunction()

for encoder_in, decoder_in, decoder_out in test_dataset:

encoder_state = encoder.init_state(batch_size)

encoder_out, encoder_state = encoder(encoder_in, encoder_state)

decoder_state = encoder_state

decoder_pred, decoder_state = decoder(

decoder_in, decoder_state)

# compute argmax

decoder_out = decoder_out.numpy()

decoder_pred = tf.argmax(decoder_pred, axis=-1).numpy()

for i in range(decoder_out.shape[0]):

ref_sent = [idx2word_fr[j] for j in

decoder_out[i].tolist() if j > 0]

hyp_sent = [idx2word_fr[j] for j in

decoder_pred[i].tolist() if j > 0]

# remove trailing EOS

ref_sent = ref_sent[0:-1]

hyp_sent = hyp_sent[0:-1]

bleu_score = sentence_bleu([ref_sent], hyp_sent,

smoothing_function=smooth_fn.method1)

bleu_scores.append(bleu_score)

[ 325 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!