第 2 部分:定义完整模型
您知道吗?仅针对英法翻译任务,训练 Google Neural Machine Translator 的一个变体大约花了 6 天并使用了 96 块 GPU。
在本练习中,您将定义一个类似但要简单得多的、基于编码器—解码器的神经机器翻译模型。具体来说,您将使用之前定义的输入和输出,创建一个 Keras Model 对象,并使用给定的损失函数和优化器来编译模型。
这里已经为您提供了之前定义的 en_inputs(编码器输入层)、en_out 和 en_state(编码器 GRU 输出)、de_out(解码器 GRU 输出)以及 de_pred(解码器预测)。
本练习是课程的一部分
使用 Keras 的机器翻译
练习说明
- 定义一个 Keras
Model,其输入为en_inputs,输出为解码器预测(de_pred)。 - 通过调用
<model>.compile,使用'adam'优化器、交叉熵损失以及准确率(acc)作为评估指标来编译模型。 - 打印模型的摘要信息。
交互式实操练习
通过完成这段示例代码来试试这个练习。
from tensorflow.keras.models import Model
# Define a model with encoder input and decoder output
nmt = ____(____=____, outputs=____)
# Compile the model with an optimizer and a loss
nmt.____(optimizer=____, ____='categorical_crossentropy', metrics=[____])
# View the summary of the model
nmt.____()