嵌入层 - Pytorch 中的 torch.nn.Embedding

Embedding Layer - torch.nn.Embedding in Pytorch

我对 NN 很陌生,如果我的问题很愚蠢,我深表歉意。我只是在阅读 github 上的代码,发现专业人士使用嵌入(在那种情况下不是词嵌入)但我可以问一下一般情况:

  1. 嵌入层是否具有可随着时间学习以改进嵌入的可训练变量?
  2. 能否请您提供一下直觉以及在什么情况下使用,比如房价回归是否会从中受益?
  3. 如果是这样(它学习)与仅使用线性层有什么区别?
>>> embedding = nn.Embedding(10, 3)
>>> input = torch.LongTensor([[1,2,4,5],[4,3,2,9]])
>>> input
tensor([[1, 2, 4, 5],
        [4, 3, 2, 9]])

>>> embedding(input)
tensor([[[-0.0251, -1.6902,  0.7172],
         [-0.6431,  0.0748,  0.6969],
         [ 1.4970,  1.3448, -0.9685],
         [-0.3677, -2.7265, -0.1685]],

        [[ 1.4970,  1.3448, -0.9685],
         [ 0.4362, -0.4004,  0.9400],
         [-0.6431,  0.0748,  0.6969],
         [ 0.9124, -2.3616,  1.1151]]])

简而言之,嵌入层具有可学习的参数,该层的有用性取决于您希望对数据产生什么样的归纳偏差。

Does Embedding Layer has trainable variables that learn over time as to improve in embedding?

是的,如变量部分下的 docs 所述,它具有在训练过程中改变的嵌入权重。

May you please provide an intuition on it and what circumstances to use, like would the house price regression benefit from it?

嵌入层通常用于输入被标记化的 NLP 任务。这意味着输入在某种意义上是离散的,可用于索引权重(这基本上是嵌入层在前向模式中的作用)。这种离散归因意味着像 1242 这样的输入是完全不同的(直到学习了语义相关性)。房价回归具有连续输入 space,1.01.1 等值可能比值 1.042.0 更相关。这种关于假设 space 的假设称为归纳偏差,几乎每个机器学习架构都符合某种归纳偏差。我相信 可能 将嵌入层用于需要某种离散化的回归问题,但不会从中受益。

If so (that it learns) what is the difference than just using Linear Layers?

有很大的不同,线性层与权重进行矩阵乘法而不是将其用作查找 table。在嵌入层的反向传播过程中,梯度只会传播到查找中使用的相应索引,并且会累积重复索引。