简体   繁体   English

Keras 中嵌入层的详细信息

[英]Exact details of Embedding layer in Keras

I want to understand the Embedding layer in Keras.我想了解 Keras 中的嵌入层。 Can anyone help?任何人都可以帮忙吗? I cannot find any good reference.我找不到任何好的参考。

  1. Which algorithm is working?哪种算法有效? I mean based on which paper or method?我的意思是基于哪篇论文或方法? I want details to implement it myself.我想要细节来自己实现它。
  2. how the training data for embedding is created?嵌入的训练数据是如何创建的? It is 2 words 2 words or more neighbors...它是 2 个词 2 个词或更多的邻居...
  3. How the model updates the weights? model 如何更新权重? is it first embedding then model, or both update together?是先嵌入然后再嵌入 model,还是同时更新?
  4. How the parameters is calculated?参数是如何计算的? For example i have this simple model:例如,我有这个简单的 model:
model = Sequential()

model.add(Embedding(input_dim=15, output_dim=8, input_length=4))

model.add(Flatten())

model.add(Dense(1, activation='sigmoid'))

model.summary()

and this is the summary:

Layer (type)                 Output Shape              Param 

=================================================================
embedding (Embedding)        (None, 4, 8)              120       
_________________________________________________________________
flatten (Flatten)            (None, 32)                0         
_________________________________________________________________
dense (Dense)                (None, 1)                 33  

how the 120 is calculated? 120是怎么计算的? Thanks in advance!提前致谢!

In terms of the number of parameters (120), that is the number of weights that need to be stored for the embedding layer specified.就参数数量(120)而言,即需要为指定的嵌入层存储的权重数量。 If the embedding layer has an input dimension of 15 (15 possible input categories or words) and an output dimension of 8 (each category represented by an 8-dimensional array), then the total number of weights or parameters stored by the embedding layer is 15x8=120.如果嵌入层的输入维度为 15(15 个可能的输入类别或词),output 维度为 8(每个类别由一个 8 维数组表示),则嵌入层存储的权重或参数的总数为15x8=120。

The embedding weights themselves can either be provided prior to training (using pretrained weights: https://keras.io/examples/nlp/pretrained_word_embeddings/ ) or the weights can be learned during the model training process.嵌入权重本身可以在训练之前提供(使用预训练的权重: https://keras.io/examples/nlp/pretrained_word_embeddings/ )或者可以在 model 训练过程中学习权重。 In the latter case the method of learning is the same as the rest of the network, presumably some form of gradient descent in an effort to minimize loss against the provided target.在后一种情况下,学习方法与网络的 rest 相同,可能是某种形式的梯度下降,以尽量减少对所提供目标的损失。 You can choose to train or freeze the weights using the 'trainable' keyword argument when initializing the embedding layer.在初始化嵌入层时,您可以选择使用“trainable”关键字参数来训练或冻结权重。

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

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