第 2 部分:文本反转模型 - 编码器
现在,您将实现文本反转模型的编码器剩余部分。编码器以您之前实现的 words2onehot() 函数产生的 one-hot 向量为输入。
此处您需要实现 encoder() 函数。encoder() 函数接收一组 one-hot 向量,并将它们转换为词 ID 列表。
在本练习中,已为您提供 words2onehot() 函数和 word2index 字典(包含单词 We、like 和 dogs)。
本练习是课程的一部分
使用 Keras 的机器翻译
练习说明
- 使用
np.argmax()函数将onehot转换为词 ID 数组,并返回这些词 ID。 - 定义包含单词
We、like、dogs的列表。 - 使用
words2onehot()函数将该单词列表转换为 one-hot 向量。请记住,words2onehot()的参数是一个单词列表和一个 Python 字典。 - 使用
encoder()函数为这些 one-hot 向量获取上下文向量。
交互式实操练习
通过完成这段示例代码来试试这个练习。
def encoder(onehot):
# Get word IDs from onehot vectors and return the IDs
word_ids = np.____(____, axis=____)
return ____
# Define "We like dogs" as words
words = ____
# Convert words to onehot vectors using words2onehot
onehot = ____(____, ____)
# Get the context vector by using the encoder function
context = encoder(____)
print(context)