開始使用免費開始

第 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)
編輯並執行程式碼