繁体   English   中英

Output keras.layers.Embedding 的形状

[英]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 形状,但对于其中大多数我只得到 (?,?)

Embedding 层的 output 是一个 2D 矩阵,在团队的输入序列中,每个 ID 都有一个嵌入,通过将此矩阵与表示团队索引的一个热向量编码相乘,这将产生一个表示team_rating的向量嵌入

因此,当教练说它增加了一个新维度时,他并不是说([team_id],[team_rating]) 他的意思是嵌入层将作为ID的 1D 输入转换为作为嵌入向量的 2D 表示。

那么为什么要扁平化呢? output 嵌入矩阵和一个热向量(假设上一张图片中的尺寸)之间的乘法将是V*1但我们需要它是1*v所以他将其展平。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM