开始使用免费开始使用

训练模型

您知道吗?在 2017 年,Google Translate 每天为超过 5 亿用户提供服务。

在本练习中,您将训练第一个使用 Teacher Forcing 的模型。Teacher Forcing 常用于序列到序列模型(例如您的神经机器翻译器)以获得更好的性能。

我们已提供 sents2seqs() 函数、英文句子 en_text 和法文句子 fr_text

本练习是课程的一部分

使用 Keras 的机器翻译

查看课程

练习说明

  • 获取解码器输入:它应包含法语词的一次性编码序列(每个序列中不包含最后一个词)。
  • 获取解码器输出:它应包含法语词的一次性编码序列(每个序列中不包含第一个词)。
  • 在单个批量数据上训练模型。
  • 对训练数据 en_xde_xde_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))  
编辑并运行代码