开始使用免费开始使用

数据预处理

现在,您需要为我们的新模型处理数据。该模型有 2 个输入和 1 个输出。两个输入分别是:独热编码(one-hot)的英文单词,以及去掉最后一个词后的独热编码法文单词。

输出是去掉第一个词后的独热编码法文单词。也就是说,在解码器中,每个输入的法文单词对应的输出是其下一个单词。这里您将学习如何实现这一点。

您已经获得了 sents2seqs() 函数、en_textfr_text

本练习是课程的一部分

使用 Keras 的机器翻译

查看课程

练习说明

  • 使用 sents2seqs() 函数(独热编码并反转)获取一批编码器输入(从 ii+bsize)。
  • 使用 sents2seqs() 函数(独热编码)获取一批解码器输入和输出(从 ii+bsize)。
  • 通过在时间维度上切片,将解码器输入(除最后一个法文单词外的所有词)从 de_xy 中分离出来。
  • 将解码器输出(除第一个法文单词外的所有词)从 de_xy 中分离出来。

交互式实操练习

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

bsize = 250
for i in range(0, len(en_text), bsize):
  # Get the encoder inputs using the sents2seqs() function
  en_x = ____('source', ____[____:____], onehot=True, reverse=____)
  # Get the decoder inputs/outputs using the sents2seqs() function
  de_xy = sents2seqs('target', ____[____:____], onehot=True)
  # Separate the decoder inputs from de_xy
  de_x = de_xy[:,____,:]
  # Separate the decoder outputs from de_xy
  de_y = de_xy[:,____,:]
  
  print("Data from ", i, " to ", i+bsize)
  print("\tnp.argmax() => en_x[0]: ", np.argmax(en_x[0], axis=-1))
  print("\tnp.argmax() => de_x[0]: ", np.argmax(de_x[0], axis=-1))
  print("\tnp.argmax() => de_y[0]: ", np.argmax(de_y[0], axis=-1))
编辑并运行代码