NMT 示例
本练习旨在在您在课程开头对 NMT 的初步了解基础上继续深入。您将继续把葡萄牙语的短语翻译成英语。
一些示例句子已存放在变量 sentences 中,并已打印到控制台。
此外,变量 model 中提供了一个预训练模型,您还将使用两个自定义函数来简化部分步骤:
encode_sequences():将文本转换为数值索引序列并进行填充。translate_many():使用预训练模型将葡萄牙语句子列表翻译为英语。稍后您将亲自编写此函数。
如需了解函数的更多细节,请使用 help()。已将 pandas 库按 pd 导入。
本练习是课程的一部分
使用 Keras 构建语言建模的循环神经网络(RNN)
练习说明
- 使用
encode_sequences()函数对文本进行预处理,并将结果保存到变量X中。 - 使用
translate_many()函数翻译sentences,并传入X作为参数。 - 使用
pd.DataFrame()创建一个数据框,将原始列表和翻译后的列表作为列。 - 打印该数据框。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Transform text into sequence of indexes and pad
X = ____(sentences)
# Print the sequences of indexes
print(X)
# Translate the sentences
translated = translate_many(model, ____)
# Create pandas DataFrame with original and translated
df = pd.DataFrame({'Original': ____, 'Translated': ____})
# Print the DataFrame
print(____)