开始使用免费开始使用

第 1 部分:定义完整模型

在本练习中,您将实现编码器-解码器模型的最后几层。您将使用 DenseTimeDistributed 层来得到模型的最终预测(即法语词的预测概率)。

我们已为您提供到目前为止实现的编码器和解码器(不含顶部输出部分)。解码器 GRU 层的输出 de_out 已提供。我们使用前缀 en(例如 en_gru)表示与编码器相关的内容,使用前缀 de 表示与解码器相关的内容(例如 de_gru)。

本练习是课程的一部分

使用 Keras 的机器翻译

查看课程

练习说明

  • 从 Keras 导入 DenseTimeDistributed 层。
  • 定义一个带有 softmax 激活函数的 Dense 层,输出维度为 fr_vocab
  • 将该 Dense 层包裹在 TimeDistributed 层中。
  • de_out 传入 de_dense_time 层,获得模型的最终预测。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import Dense and TimeDistributed layers
from tensorflow.keras.____ import ____, ____
# Define a softmax dense layer that has fr_vocab outputs
de_dense = ____(____, ____)
# Wrap the dense layer in a TimeDistributed layer
de_dense_time = ____(de_dense)
# Get the final prediction of the model
de_pred = ____(de_out)
print("Prediction shape: ", de_pred.shape)
编辑并运行代码