Doc2Vec - 计算文档之间的相似性

本文旨在向您介绍 Doc2Vec 模型,以及它在计算文档之间的相似性时如何提供帮助。

目录

前言

一、Word2Vec

1.Skip-Gram

2.Continuous Bag-of-Words (CBOW)

二、Doc2Vec

1.Distributed Memory version of Paragraph Vector (PV-DM)

2.Words version of Paragraph Vector (PV-DBOW) 

三、程序实现

数据

安装 Gensim 

导入所有相关包

标记数据 

初始化 doc2vec

构建标记数据的词汇表 

训练 doc2vec 

保存模型

加载模型

计算最大相似度

计算两两相似度

推断向量

总结



前言

如果我们将上下文数据转换为低维向量,并且当我们谈论将文本文档转换为其数字表示时,这就是 doc2vec 模型发挥作用的地方,自然语言处理领域有许多具有挑战性的任务可以完成。但是,我们可以使用 doc2vec 完成许多任务,但今天我们只专注于计算文档之间的相似性,以便您能够识别抄袭文档,获得相似文章的推荐等等。

Doc2vec 是一种无监督机器学习算法,用于将文档转换为向量。这个概念是由Mikilov 和 Le本文中提出的。现在,由于您已经对 doc2vec 进行了温和的介绍,我想将您的注意力转向 word2vec,因为 doc2vec 严重依赖于 word2vec,而在没有 word2vec 的情况下描述 doc2vec 实际上会错过重点。


一、Word2Vec

正如名称本身描述的算法,word2vec 模型生成单词的向量。有时很容易通过简单地使用 one-hot 编码技术来开发使用单词的模型,但在这种方法中,句子中的单词不会保持其含义。例如,如果我们将单词 king 编码为id_2,将 man编码为id_4 ,将France编码为id_6 ,那么所有这些单词之间的关系都相同,但是如果我们想保持它们之间的关系会发生什么,即 king 应该有与人而不是法国的牢固关系?这就是 word2vec 模型真正有用的地方,因为它可以保持单词之间的关系。

 Word2vec 表示是使用 2 种算法开发的:

1. Skip-Gram

2. Continuous Bag-of-Words

1.Skip-Gram

skip-gram 模型试图用给定的目标词预测周围的上下文。如下图所示,当目标词“sat”被发送到该模型时,它会尝试预测其周围的上下文,即“The cat sat on the mat”。

2.Continuous Bag-of-Words (CBOW)

正如我们在上面定义的 skip-gram 的过程,请记住它与 CBOW 所做的完全相反。CBOW 模型尝试根据上下文预测下一个单词。如下图所示,当上下文“the cat sat”被发送到该模型时,它会尝试根据上下文预测下一个单词,在我们的示例中,下一个预测的单词将是“on”。

二、Doc2Vec

  在对 word2vec 进行了简单的介绍之后,现在将更容易理解 doc2vec 的工作原理。

正如我上面提到的,doc2vec 的目标是计算文档的数字表示。Doc2vec 与 word2vec 几乎相似,但与 word 不同的是,文档中不维护逻辑结构,因此在开发 doc2vec 时,在其中添加了另一个名为Paragraph ID的向量。

1.Distributed Memory version of Paragraph Vector (PV-DM)

通过查看上图,您一定认为它几乎类似于 CBOW 模型的视觉表示。嗯,你是对的,但是添加了一个额外的特征向量,通过它可以识别文档的唯一性。在训练这样的模型时,名为“W”的向量是保存数字表示并表示单词概念的单词向量。类似地,名为“D”的向量是包含数字表示并表示文档概念的文档向量。

2.Words version of Paragraph Vector (PV-DBOW) 

 如果 doc2vec 中有 CBOW 模型的扩展,那么 doc2vec 中是否有 Skip-Gram 模型的扩展?是的,有一个类似于 skip-gram 的算法,称为段落向量的单词版本 (PV-DBOW)。

很重要的一点

在使用 word2vec 时,CBOW 比 Skip-Gram 快得多,但在 doc2vec 的情况下,PV-DM(CBOW 的扩展)的计算速度比 PV-DBOW(Skip-Gram 的扩展)慢得多,因为它消耗的内存比单词少向量没有被保存。

现在我们已经了解了word2vec和doc2vec的整个过程,让我们开始实现doc2vec。

三、程序实现

文档之间的相似度计算是自然语言处理领域中一项非常具有挑战性的任务。如果两个文档的语义上下文相似,则它们可能是相似的,并且手动识别大量文档之间的相似性可能非常困难。为了使这项工作变得简单,我们将让我们的机器使用 doc2vec 找出文档之间的相似性。

数据

我将仅使用 3 个句子的数据作为 doc2vec 演示的示例,以便我们可以轻松识别文本之间的相似性,但使用 doc2vec 我们可以使用庞大的数据集并相应地训练我们的模型。

安装 Gensim 

对于 doc2vec 的实现,我们将使用一个流行的开源自然语言处理库,称为 Gensim (Generate Similar),用于无监督主题建模。您可以使用以下命令将其安装在您的机器上。

pip install gensim

导入所有相关包

import gensim
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
from nltk.tokenize import word_tokenize
from gensim.models.doc2vec import Doc2Vec

标记我们的数据后的输出如下 

data = ["The process of searching for a job can be very stressful, but it doesn’t have to be. Start with a\
        well-written resume that has appropriate keywords for your occupation. Next, conduct a targeted job search\
        for positions that meet your needs.",
        "Gardening in mixed beds is a great way to get the most productivity from a small space. Some investment\
        is required, to purchase materials for the beds themselves, as well as soil and compost. The\
        investment will likely pay-off in terms of increased productivity.",
        "Looking for a job can be very stressful, but it doesn’t have to be. Begin by writing a good resume with\
        appropriate keywords for your occupation. Second, target your job search for positions that match your\
        needs."]

标记数据 

tagged_data = [TaggedDocument(words=word_tokenize(_d.lower()), tags=[str(i)]) for i, _d in enumerate(data)]

标记我们的数据后的输出如下 

print (tagged_data)
Output:
[TaggedDocument(words=['the', 'process', 'of', 'searching', 'for', 'a', 'job', 'can', 'be', 'very', 'stressful', ',', 'but', 'it', 'doesn', '’', 't', 'have', 'to', 'be', '.', 'start', 'with', 'a', 'well-written', 'resume', 'that', 'has', 'appropriate', 'keywords', 'for', 'your', 'occupation', '.', 'next', ',', 'conduct', 'a', 'targeted', 'job', 'search', 'for', 'positions', 'that', 'meet', 'your', 'needs', '.'], tags=['0']), TaggedDocument(words=['gardening', 'in', 'mixed', 'beds', 'is', 'a', 'great', 'way', 'to', 'get', 'the', 'most', 'productivity', 'from', 'a', 'small', 'space', '.', 'some', 'investment', 'is', 'required', ',', 'to', 'purchase', 'materials', 'for', 'the', 'beds', 'themselves', ',', 'as', 'well', 'as', 'soil', 'and', 'compost', '.', 'the', 'investment', 'will', 'likely', 'pay-off', 'in', 'terms', 'of', 'increased', 'productivity', '.'], tags=['1']), TaggedDocument(words=['looking', 'for', 'a', 'job', 'can', 'be', 'very', 'stressful', ',', 'but', 'it', 'doesn', '’', 't', 'have', 'to', 'be', '.', 'begin', 'by', 'writing', 'a', 'good', 'resume', 'with', 'appropriate', 'keywords', 'for', 'your', 'occupation', '.', 'second', ',', 'target', 'your', 'job', 'search', 'for', 'positions', 'that', 'match', 'your', 'needs', '.'], tags=['2'])]

 现在我们已经标记了我们的数据,让我们开始训练我们的模型

初始化 doc2vec

model = gensim.models.doc2vec.Doc2Vec(vector_size=30, min_count=2, epochs=80)

构建标记数据的词汇表 

model.build_vocab(tagged_data)

训练 doc2vec 

model.train(tagged_data,total_examples=model.corpus_count,epochs=80)

保存模型

doc2vec训练好后,保存如下 

model.save("d2v.model")

当我们保存了我们的模型后,它就可以实现了。加载模型,让我们计算句子之间的最大相似度 

加载模型

gensim.models.Doc2Vec.load("d2v.model")

计算最大相似度

similar_doc = model.docvecs.most_similar('0')
print(similar_doc[0])
Output:
('2', 0.9393066167831421)

计算两两相似度

model.docvecs.similarity('0','1')
model.docvecs.similarity('0','2')
model.docvecs.similarity('1','2')

推断向量

推断向量用于找出不属于我们训练数据的文档的向量

因为vector_size设置为30,所以推断向量的维度为30

test_data = word_tokenize("When your focus is to improve employee performance, it’s essential to encourage ongoing\
                        dialogue between managers and their direct reports. Some companies encourage supervisors\
                        to hold one-on-one meetings with employees as a way to facilitate\
                        two-way communication.".lower())
v1 = model.infer_vector(test_data)
print("V1_infer", v1)
Output:
V1_infer [-0.06755273  0.0633966   0.06744069  0.01091933 -0.01968639 -0.01889984
 -0.04448636 -0.00854152 -0.25066498 -0.03219931  0.03350157 -0.02680573
 -0.04993293 -0.2456862  -0.02887128 -0.12966427  0.04222799 -0.02136624
 -0.10524843 -0.07345396  0.07305007  0.00686409 -0.09619413  0.06575447
  0.15723655  0.05926161  0.06410413  0.00242155  0.01862393 -0.11729769]

总结

我们已经看到使用doc2vec模型可以获得很多帮助。此外,这向我们展示了文本文档的数字表示如何有助于 Web 搜索、垃圾邮件过滤、文档检索、检测抄袭文档等。

猜你喜欢

转载自blog.csdn.net/qq_43483899/article/details/125957125