開始使用免費開始

定義球隊模型

球隊強度查詢包含三個元件:一個輸入、一個嵌入向量(embedding)層,以及一個將輸出攤平成一維的 flatten 層。

如果你把這三層包成一個同時具有輸入與輸出的模型,就能在多個地方重複使用這個三層堆疊。

再次提醒:我們在任何使用到這個模組的地方,這三層的權重都會被共享。

本練習屬於課程

使用 Keras 的進階深度學習

檢視課程

練習說明

  • 建立一個用於球隊 ID(為整數)的 1D 輸入層。務必設定正確的輸入 shape!
  • 將此輸入傳入你先前建立的球隊強度查詢層。
  • 將球隊強度查詢的輸出做攤平(flatten)。
  • 建立一個以該 1D 輸入為輸入、以攤平後的球隊強度為輸出的模型。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Imports
from tensorflow.keras.layers import Input, Embedding, Flatten
from tensorflow.keras.models import Model

# Create an input layer for the team ID
teamid_in = ____(shape=(____,))

# Lookup the input in the team strength embedding layer
strength_lookup = team_lookup(____)

# Flatten the output
strength_lookup_flat = Flatten()(____)

# Combine the operations into a single, re-usable model
team_strength_model = ____(____, ____, name='Team-Strength-Model')
編輯並執行程式碼