tensorflow - Tensorflow 嵌入层内部的网络结构是什么?

标签 tensorflow word2vec embedding

Tensoflow 嵌入层 ( https://www.tensorflow.org/api_docs/python/tf/keras/layers/Embedding ) 易于使用,
并且有大量的文章在谈论
“如何使用”嵌入( https://machinelearningmastery.com/what-are-word-embeddings/https://www.sciencedirect.com/topics/computer-science/embedding-method )
.
但是,我想知道 Tensorflow 或 Pytorch 中非常“嵌入层”的实现。
它是一个 word2vec 吗?
是Cbow吗?
是特殊的密集层吗?

最佳答案

结构明智,两者都是 Dense层和 Embedding层是带有神经元的隐藏层。不同之处在于它们对给定输入和权重矩阵的操作方式。
一个 Dense层对给定的权重矩阵执行运算,方法是将输入相乘,为其添加偏差并对其应用激活函数。而Embedding层使用权重矩阵作为查找字典。
嵌入层最好理解为将整数索引(代表特定单词)映射到密集向量的字典。它接受整数作为输入,在内部字典中查找这些整数,并返回相关的向量。它实际上是一个字典查找。

from keras.layers import Embedding

embedding_layer = Embedding(1000, 64)
这里 1000 表示字典中的单词数,64 表示这些单词的维度。直观地说,嵌入层就像任何其他层一样会尝试找到 64 维的向量(实数)[ n1, n2, ..., n64]对于任何词。该向量将表示该特定单词的语义。它将在使用反向传播进行训练时学习这个向量,就像任何其他层一样。

When you instantiate an Embedding layer, its weights (its internal dictionary of token vectors) are initially random, just as with any other layer. During training, these word vectors are gradually adjusted via backpropagation, structuring the space into something the downstream model can exploit. Once fully trained, the embedding space will show a lot of structure—a kind of structure specialized for the specific problem for which you’re training your model.


-- 深度学习 Python,作者 F. Chollet

编辑 - 如何使用“反向传播”来训练 Embedding Layer 的查找矩阵? Embedding层类似于线性层,没有任何激活函数。理论上,Embedding layer 还执行矩阵乘法,但不会通过使用任何类型的激活函数为其添加任何非线性。所以 Embedding 中的反向传播层类似于任何线性层。但实际上,我们不会在嵌入层中进行任何矩阵乘法,因为输入通常是单热编码的,而权重矩阵与单热编码向量的乘法就像查找一样简单。

关于tensorflow - Tensorflow 嵌入层内部的网络结构是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67896966/

相关文章:

python - 我会尝试更改 keras 预训练模型的 channel

python - 如何添加 keras dropout 层?

gensim - gensim LabeledSentence 和 TaggedDocument 有什么区别

python - 正则表达式替换需要花费数百万个文档的时间,如何让它更快?

python - 将 word2vec bin 文件转换为文本

php - 如何在 HTML 中运行 PHP?

python - In[0] 不是矩阵。相反,它具有形状 [100] [Op :MatMul]

python - Tensorflow 模型中的 set_weights()

c# - 在 C# 中嵌入 Julia

java - 不同上下文中同一类的变量