[英]Output shape of keras.layers.Embedding
I was watching a video on datacamp to learn about Keras, and the instructor used layers.Embedding with keras.layers.Flatten, but he did not really explain the output of the Embedding function properly. 我用谷歌搜索了 3-4 個小時,找不到任何可以幫助我的東西。
目標用語言解釋:所以這就是他試圖用普通英語做的事情。 輸入數據由一堆大學籃球隊 ID 組成。 由於 ID 並沒有真正告訴我們有關團隊的任何信息,因此他使用了 Embedding,輸入形狀為 1,output 形狀為 1,輸入維度等於團隊數量。 然后他說,我們的神經網絡將以這樣一種方式“學習”,將這個新的 output 從嵌入中關聯起來,作為籃球隊的評分,這將有助於預測兩支球隊之前沒有交手的結果。 然后,(這就是我迷路的地方)他說嵌入實際上會給數組添加一個額外的維度,所以我們必須使用 keras.layers.flatten() 進行展平。
據我了解,如果我在 Embedding 中輸入一個團隊 ID,則 output(一旦神經網絡學習了所有參數)將是([team_id],[team_rating]),在展平后,它將是([team_id,團隊評級])。 但是,按照他的描述,扁平化后,output 編號只有一個:team_rating。 當他添加一個減法層時,這一點尤其暗示,該減法層將從這個扁平層中減去兩個輸出(給出 team_rating 差異),這將用於預測游戲的結果。
這是從輸入層到 output 層的完整代碼(請注意,您可能不需要閱讀代碼來回答問題,但它有助於添加上下文)
n_teams = unique(games_season['team_1']).shape[0]
# Create an embedding layer
team_lookup = Embedding(input_dim=n_teams,
output_dim=1,
input_length=1,
name='Team-Strength')
# Create an input layer for the team ID
teamid_in = Input(shape=(1,))
# Lookup the input in the team strength embedding layer
strength_lookup = team_lookup(teamid_in)
# Flatten the output
strength_lookup_flat = Flatten()(strength_lookup)
# Combine the operations into a single, re-usable model
team_strength_model = Model(teamid_in, strength_lookup_flat, name='Team-Strength-Model')
# Input layer for team 1
team_in_1 = Input((1,),name='Team-1-In')
# Separate input layer for team 2
team_in_2 = Input((1,),name='Team-2-In')
# Lookup team 1 in the team strength model
team_1_strength = team_strength_model(team_in_1)
# Lookup team 2 in the team strength model
team_2_strength = team_strength_model(team_in_2)
# Create a subtract layer using the inputs from the previous exercise
score_diff = Subtract()([team_1_strength, team_2_strength])
# Subtraction layer from previous exercise
score_diff = Subtract()([team_1_strength, team_2_strength])
# Create the model
model = Model([team_in_1, team_in_2], score_diff)
我不明白的是
A. Embedding/Flatten 的輸入是一個團隊 ID,但 output 不是一個包含 team_id 和 team_rating 的列表(因為嵌入添加了一個額外的維度(team_rating),並且 flatten 將該維度中的值帶到與原始輸入(team_id)相同的維度。講師將其傳遞出去,好像只有一個 output:team_rating
B. 如果 output 實際上是一個包含 team_id 和 team_rating 的列表,難道我們不想只選擇 team_rating 以供將來處理,例如在不同團隊之間減去 team_rating 嗎?
任何幫助將不勝感激。 我已經嘗試檢查每一層的輸入形狀和 output 形狀,但對於其中大多數我只得到 (?,?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.