MulaiMulai sekarang secara gratis

Training the model with validation

Here you will train the model using Teacher Forcing and also perform a validation step. You will train the model for multiple epochs and multiple iterations. Then at the end of each epoch, you will run the validation step and obtain the results.

For this, you have been provided with en_text (English sentences), fr_text (French sentences), the sents2seqs() function and nmt_tf (the compiled model). You also have tr_en and tr_fr (training data) and v_en and v_fr (validation data) loaded already.

Latihan ini adalah bagian dari kursus

Machine Translation with Keras

Lihat Kursus

Petunjuk latihan

  • Extract decoder inputs (all words except the last) and outputs (all words except the first) from de_xy.
  • Train the model on a single batch of data.
  • Create decoder inputs and outputs from the validation data similar to how you did for training data.
  • Evaluate the model on the validation dataset to obtain the validation loss and the accuracy.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

for ei in range(n_epochs):
  for i in range(0,train_size,bsize):    
    en_x = sents2seqs('source', tr_en[i:i+bsize], onehot=True, reverse=True)
    de_xy = sents2seqs('target', tr_fr[i:i+bsize], onehot=True)
    # Create a single batch of decoder inputs and outputs
    de_x, de_y = ____[:,____,:], de_xy[:,____,:]
    # Train the model on a single batch of data
    nmt_tf.____([____,____], de_y)      
  v_en_x = sents2seqs('source', v_en, onehot=True, reverse=True)
  # Create a single batch of validation decoder inputs and outputs
  v_de_xy = ____('target', ____, onehot=____)
  v_de_x, v_de_y = ____[____], v_de_xy[____]
  # Evaluate the trained model on the validation data
  res = nmt_tf.evaluate([____,____], ____, batch_size=valid_size, verbose=0)
  print("{} => Loss:{}, Val Acc: {}".format(ei+1,res[0], res[1]*100.0))
Edit dan Jalankan Kode