结合词嵌入以获得句子嵌入的更好方法

Better way to combine Word embedding to get embedding of a sentence

我在很多kaggle kernels和教程中看到,average word embeddings被认为是得到一个句子的embedding。但是,我想知道这是否正确 approach.Since 它丢弃了句子中单词的位置信息。有没有更好的方法来组合嵌入?也许以特定方式分层组合它们?

如果您需要一种简单但有效的方法,Sif embedding is perfectly fine. It averages word vector in a sentence and removes its first principal component. It is much superior to averaging word vectors. The code available online here。这是主要部分:

svd = TruncatedSVD(n_components=1, random_state=rand_seed, n_iter=20)
svd.fit(all_vector_representation)
svd = svd.components_

XX2 = all_vector_representation - all_vector_representation.dot(svd.transpose()) * svd

其中 all_vector_representation 是数据集中所有句子的平均嵌入。

其他复杂的方法也存在,如 ELMO, Transformer

我从上面 post Amir 的论文中学到了我所知道的一切。我不喜欢所链接的代码库,也不认为 Amir 的代码反映了论文的内容,因此我将添加非常简单的代码来完全按照论文的建议进行操作。

def SIF_Embedding(X,p,a=1e-3):
    # X is the concatenated word embeddings (n,m) 
    # p is the probability of each word     (n,1)
            
    U,_,_ = np.linalg.svd(np.matmul(X.transpose(),X))
    U = U[:,0]
        
    mV = np.matmul( ( a / (a + p) ) , X)
    mV /= X.shape[0]
    mV -= U * np.matmul(U.transpose(), mV)
    
    return mV