简体   繁体   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. 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. I have googled it for like 3-4 hours and could not find anything that could help me.我用谷歌搜索了 3-4 个小时,找不到任何可以帮助我的东西。

Objective explained in words: So here is what he was trying to do in normal English.目标用语言解释:所以这就是他试图用普通英语做的事情。 The input data consists of a bunch of college basketball team IDs.输入数据由一堆大学篮球队 ID 组成。 Since the ID does not really tell us anything about the team, he used Embedding with an input shape of 1, output shape of 1, and an input dimension equal to the number of teams.由于 ID 并没有真正告诉我们有关团队的任何信息,因此他使用了 Embedding,输入形状为 1,output 形状为 1,输入维度等于团队数量。 He then said that our neural network would "learn" in such a way to associate this new output from embedding as a rating of the basketball team which would be helpful to predict the outcome if two teams had not previously played against each other.然后他说,我们的神经网络将以这样一种方式“学习”,将这个新的 output 从嵌入中关联起来,作为篮球队的评分,这将有助于预测两支球队之前没有交手的结果。 Then, (and here is where I got lost) he said that Embedding would actually add an extra dimension to the array so we would have to flatten using keras.layers.flatten().然后,(这就是我迷路的地方)他说嵌入实际上会给数组添加一个额外的维度,所以我们必须使用 keras.layers.flatten() 进行展平。

As I understand it, if I input a team id into Embedding, the output (once the neural network has learned all its parameters) would be ([team_id],[team_rating]), and after flattening, it would be ([team_id,team_rating]).据我了解,如果我在 Embedding 中输入一个团队 ID,则 output(一旦神经网络学习了所有参数)将是([team_id],[team_rating]),在展平后,它将是([team_id,团队评级])。 However, the way he described it, after flattening, there would be only one output number: the team_rating.但是,按照他的描述,扁平化后,output 编号只有一个:team_rating。 This was especially implied when he added a subtracting layer which would subtract two outputs from this flattened layer (giving the team_rating difference) which would be used to predict the outcome of the game.当他添加一个减法层时,这一点尤其暗示,该减法层将从这个扁平层中减去两个输出(给出 team_rating 差异),这将用于预测游戏的结果。

Here is the full code from input layer to output layer (Note you probably don't need to read the code to answer the question, but it helps add context)这是从输入层到 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)

What I don't get is我不明白的是

A. The input into Embedding/Flatten is a team ID, but isn't the output a list consisting of both the team_id AND the team_rating (since embedding adds an extra dimension(team_rating), and flatten brings the value in that dimension to the same dimension as the original input(team_id). The instructor was passing it off as if there would only be one output: the team_rating A. Embedding/Flatten 的输入是一个团队 ID,但 output 不是一个包含 team_id 和 team_rating 的列表(因为嵌入添加了一个额外的维度(team_rating),并且 flatten 将该维度中的值带到与原始输入(team_id)相同的维度。讲师将其传递出去,好像只有一个 output:team_rating

B. If the output actually is a list of both the team_id and the team_rating, wouldn't we want to pick out ONLY the team_rating for future processing such as subtraction of team_rating between different teams? B. 如果 output 实际上是一个包含 team_id 和 team_rating 的列表,难道我们不想只选择 team_rating 以供将来处理,例如在不同团队之间减去 team_rating 吗?

Any help would be greatly appreciated.任何帮助将不胜感激。 I've already tried checking the input shape and output shape of every layer, but for most of them I just get (?,?)我已经尝试检查每一层的输入形状和 output 形状,但对于其中大多数我只得到 (?,?)

The output of the Embedding layer is a 2D Matrix with one embedding for each ID in the input sequence of teams, by multiplying this matrix with the one hot vector encoding that represents the index of the team this will result in a vector that represents the team_rating Embedding 层的 output 是一个 2D 矩阵,在团队的输入序列中,每个 ID 都有一个嵌入,通过将此矩阵与表示团队索引的一个热向量编码相乘,这将产生一个表示team_rating的向量嵌入

So when the instructor said that it adds a new dimension he didn't mean ([team_id],[team_rating]) .因此,当教练说它增加了一个新维度时,他并不是说([team_id],[team_rating]) He meant that the embedding layer converts a 1D input which is the ID into 2D representation which is the embedding vector.他的意思是嵌入层将作为ID的 1D 输入转换为作为嵌入向量的 2D 表示。

So why the flattening?那么为什么要扁平化呢? the output of the multiplication between the embedding matrix and the one hot vector(assuming the dimensions in the prev picture) will be V*1 but we need it to be 1*v so he flatten it. output 嵌入矩阵和一个热向量(假设上一张图片中的尺寸)之间的乘法将是V*1但我们需要它是1*v所以他将其展平。

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

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