生成英文-法文翻译
您知道吗?汇丰银行曾因一句口号的翻译错误,斥资 1,000 万美元重新品牌定位。
我们将使用已训练好的模型,通过 model.predict() 来预测英文句子的法语翻译。您将获得训练好的模型(model)。该模型在 100,000 个句子上训练了 50 个 epoch,并在包含 35,000+ 个词的验证集上达到了约 90% 的准确率。由于在练习开始前需要加载已训练模型,本练习的加载可能会更久一些。此外,您还将获得一个字典(fr_id2word),可用于将词索引转换为对应的单词。最后,您将使用之前实现的 sents2seqs 函数,在将数据送入模型前完成预处理。
您可以使用 help(sents2seqs) 来回顾 sents2seqs() 函数所接受的输入。
本练习是课程的一部分
使用 Keras 的机器翻译
练习说明
- 预处理源
en_st,使用先前定义的sents2seqs函数将其转换为 onehot 编码的numpy数组。 - 使用提供的已训练
model对en_seq进行预测。 - 使用
np.argmax在fr_pred的每个预测上提取最大索引,并将其赋给fr_seq。 - 使用列表推导将法语序列 ID 转换为句子(记得忽略 0),并将其赋给
fr_sent。
交互式实操练习
通过完成这段示例代码来试试这个练习。
en_st = ['the united states is sometimes chilly during december , but it is sometimes freezing in june .']
print('English: {}'.format(en_st))
# Convert the English sentence to a sequence
en_seq = ____(____, en_st, ____=True, reverse=____)
# Predict probabilities of words using en_seq
fr_pred = ____.____(en_seq)
# Get the sequence indices (max argument) of fr_pred
fr_seq = ____.____(fr_pred, axis=____)[0]
# Convert the sequence of IDs to a sentence and print
fr_sent = [____[i] for i in ____ if i != ____]
print("French (Custom): {}".format(' '.join(fr_sent)))
print("French (Google Translate): les etats-unis sont parfois froids en décembre, mais parfois gelés en juin")