将葡萄牙语翻译成英语
这是本课程的最后一个练习,恭喜您完成到这里!
您将学习如何使用 NMT 模型进行翻译。
一个能够将葡萄牙语短句编码并解码为英语短句的模型已预训练完成,并加载在变量 model 中。
此外,函数 predict_one() 已加载完毕,可使用 help() 查看详情;数据集可通过变量 test(原始文本)和 X_test(已分词/标记化)获取。
您将定义一个函数来翻译句子列表。参数中,sentences 是待翻译的短句列表,index_to_word 是一个 dict,以数字索引为键、以英文单词为值,已加载在变量 en_index_to_word 中。
模型的摘要已为您打印,供参考。
本练习是课程的一部分
使用 Keras 构建语言建模的循环神经网络(RNN)
练习说明
- 遍历短句的枚举迭代器。
- 使用预加载的函数
predict_one()翻译单个短句。 - 打印翻译结果。
- 调用您定义的函数,翻译
X_test变量中的前 10 个短句。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Function to predict many phrases
def predict_many(model, sentences, index_to_word, raw_dataset):
for i, sentence in ____(sentences):
# Translate the Portuguese sentence
translation = ____(model, sentence, index_to_word)
# Get the raw Portuguese and English sentences
raw_target, raw_src = raw_dataset[i]
# Print the correct Portuguese and English sentences and the predicted
print('src=[%s], target=[%s], predicted=[%s]' % (raw_src, raw_target, ____))
____(model, X_test[____], en_index_to_word, test)