Word Mover's Distance (WMD) 如何使用 word2vec 嵌入 space?

How Word Mover's Distance (WMD) uses word2vec embedding space?

根据 WMD paper,它受到 word2vec 模型的启发,并使用 word2vec 向量 space 将文档 1 移动到文档 2(在 Earth Mover Distance 度量的上下文中)。来自论文:

Assume we are provided with a word2vec embedding matrix
X ∈ Rd×n for a finite size vocabulary of n words. The 
ith column, xi ∈ Rd, represents the embedding of the ith
word in d-dimensional space. We assume text documents
are represented as normalized bag-of-words (nBOW) vectors,
d ∈ Rn. To be precise, if word i appears ci times in
the document, we denote di = ci/cj (for j=1 to n). An nBOW vector
d is naturally very sparse as most words will not appear in
any given document. (We remove stop words, which are
generally category independent.)

我从论文中理解了这个概念,但是,我无法从 Gensim 的代码中理解 wmd 如何使用 word2vec 嵌入 space。

谁能简单解释一下?它是否以不同的方式计算单词向量,因为我无法理解这段代码中在哪里使用了 word2vec 嵌入矩阵?

WMD Fucntion from Gensim:

   def wmdistance(self, document1, document2):
    # Remove out-of-vocabulary words.
    len_pre_oov1 = len(document1)
    len_pre_oov2 = len(document2)
    document1 = [token for token in document1 if token in self]
    document2 = [token for token in document2 if token in self]

    dictionary = Dictionary(documents=[document1, document2])
    vocab_len = len(dictionary)

    # Sets for faster look-up.
    docset1 = set(document1)
    docset2 = set(document2)

    # Compute distance matrix.
    distance_matrix = zeros((vocab_len, vocab_len), dtype=double)
    for i, t1 in dictionary.items():
        for j, t2 in dictionary.items():
            if t1 not in docset1 or t2 not in docset2:
                continue
            # Compute Euclidean distance between word vectors.
            distance_matrix[i, j] = sqrt(np_sum((self[t1] - self[t2])**2))

    def nbow(document):
        d = zeros(vocab_len, dtype=double)
        nbow = dictionary.doc2bow(document)  # Word frequencies.
        doc_len = len(document)
        for idx, freq in nbow:
            d[idx] = freq / float(doc_len)  # Normalized word frequencies.
        return d

    # Compute nBOW representation of documents.
    d1 = nbow(document1)
    d2 = nbow(document2)

    # Compute WMD.
    return emd(d1, d2, distance_matrix)

就 WMD 而言,一段文字被认为是一堆 'piles' 的意思。这些堆放在文本单词的坐标处——这就是为什么大规模杀伤性武器的计算依赖于来自另一个来源的一组单词向量。这些向量定位文本的堆。

WMD 是移动一个文本堆以匹配另一个文本堆所需的最少工作量。从一堆转移到另一堆所需的工作量度是这些桩坐标之间的欧氏距离。

您可以尝试简单地移动字堆:查看文本 A 中的第一个单词,将其移动到文本 B 中的第一个单词,依此类推。但这不太可能是 最便宜的 转移——它可能会尝试匹配更近的单词,以在最短的可能路径上发送 'meaning'。所以实际上计算 WMD 是一个迭代优化问题——比简单的两点之间的欧氏距离或余弦距离要昂贵得多。

该优化是在您摘录的代码中的 emd() 调用中完成的。但这种优化需要的是文本 A 中所有单词与文本 B 中所有单词之间的成对距离——因为这些都是可能转移意义权重的候选路径。您可以看到在代码中计算的那些成对距离以填充 distance_matrix,使用已经加载到模型中并可通过 self[t1]self[t2] 等访问的词向量。