訓練模型
你知道嗎?在 2017 年,Google 翻譯每天為超過 5 億名使用者提供服務。
在這裡,你會訓練你的第一個「Teacher Forcing」模型。Teacher Forcing 常用於序列到序列(sequence-to-sequence)模型,例如你的神經機器翻譯器,以獲得更好的表現。
你會拿到 sents2seqs() 函式、英文句子 en_text,以及法文句子 fr_text。
本練習屬於課程
使用 Keras 進行機器翻譯
練習說明
- 取得解碼器輸入(decoder input):包含法文字詞的 onehot 編碼序列,但不含每個序列的最後一個詞。
- 取得解碼器輸出(decoder output):包含法文字詞的 onehot 編碼序列,但不含每個序列的第一個詞。
- 在單一批次資料上訓練模型。
- 取得訓練資料
en_x、de_x與de_y的評估指標(loss 與 accuracy)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
n_epochs, bsize = 3, 250
for ei in range(n_epochs):
for i in range(0,data_size,bsize):
en_x = sents2seqs('source', en_text[i:i+bsize], onehot=True, reverse=True)
de_xy = sents2seqs('target', fr_text[i:i+bsize], onehot=True)
# Separate the decoder inputs from de_xy
de_x = ____[:,____,:]
# Separate the decoder outputs from de_xy
de_y = ____[____]
# Train the model on a single batch of data
nmt_tf.____([____,____], ____)
# Obtain the eval metrics for the training data
res = _____.evaluate([____,____], ____, batch_size=bsize, verbose=0)
print("{} => Train Loss:{}, Train Acc: {}".format(ei+1,res[0], res[1]*100.0))