开始使用免费开始使用

第 2 部分:理解 GRU 模型

现在,您将了解如何让 Keras 模型接受任意大小的输入批次。能够接受任意大小的批次非常重要。例如,这使您可以只定义一个 Keras 模型,并在训练阶段尝试不同的批大小,而无需更改模型本身。

本练习中,已为您导入 kerasnumpy(简称 np)。

本练习是课程的一部分

使用 Keras 的机器翻译

查看课程

练习说明

  • 定义一个输入层,可接受任意大小的批次,序列长度为 3,输入维度为 4。
  • 定义一个具有 10 个隐藏单元的 GRU 层,接收上述输入并生成输出。
  • 定义一个名为 model 的 Model,以该输入层为输入,以 GRU 层的输出为输出。请记住,您可以使用 keras.models.Model(<argument>=<value>) 语法来定义模型。
  • x1x2 都进行模型输出预测。

交互式实操练习

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

# Define an input layer
inp = keras.layers.____(____=(____))
# Define a GRU layer that takes in the input
gru_out = keras.layers.____(____)(____)
# Define a model that outputs the GRU output
____ = keras.models.____(inputs=____, outputs=____)

x1 = np.random.normal(size=(2,3,4))
x2 = np.random.normal(size=(5,3,4))

# Get the output of the model and print the result
y1 = ____.____(____)
y2 = ____.____(____)
print("shape (y1) = ", y1.shape, " shape (y2) = ", y2.shape)
编辑并运行代码