Doc2Vec:从 ConcatenatedDocvecs 推断出最相似的向量
Doc2Vec: infer most similar vector from ConcatenatedDocvecs
我正在按照 here
提供的指导生成 Pandas DataFrame 的 Doc2Vec 嵌入
from gensim.models import Doc2Vec
from gensim.models.doc2vec import TaggedDocument
from gensim.test.test_doc2vec import ConcatenatedDoc2Vec
import gensim.models.doc2vec
from collections import OrderedDict
import pandas as pd
import numpy as np
cube_embedded = # pandas cube
# convert the cube to documents
alldocs = [TaggedDocument(doc, [i]) for i, doc in enumerate(cube_embedded.values.tolist())]
# train models
simple_models = [
# PV-DBOW plain
Doc2Vec(dm=0, vector_size=100, negative=5, hs=0, min_count=2, sample=0, epochs=20, workers=cores),
# PV-DM w/ default averaging; a higher starting alpha may improve CBOW/PV-DM modes
Doc2Vec(dm=1, vector_size=100, window=10, negative=5, hs=0, min_count=2, sample=0, epochs=20, workers=cores, alpha=0.05, comment='alpha=0.05'),
# PV-DM w/ concatenation - big, slow, experimental mode window=5 (both sides) approximates paper's apparent 10-word total window size
Doc2Vec(dm=1, dm_concat=1, vector_size=100, window=5, negative=5, hs=0, min_count=2, sample=0, epochs=20, workers=cores),
]
for d2v_model in simple_models:
d2v_model.build_vocab(alldocs)
d2v_model.train(alldocs, total_examples=d2v_model.corpus_count, epochs=d2v_model.epochs)
models_by_name = OrderedDict((str(d2v_model), d2v_model) for d2v_model in simple_models)
models_by_name['dbow+dmm'] = ConcatenatedDoc2Vec([simple_models[0], simple_models[1]])
models_by_name['dbow+dmc'] = ConcatenatedDoc2Vec([simple_models[0], simple_models[2]])
给定文档向量 V,如果我尝试从 ConcatenatedDocvecs 模型中推断出与文档向量 V 最相似的文档,我会收到以下错误:
V = np.random.rand(200)
models_by_name['dbow+dmc'].docvecs.most_similar([V])
AttributeError: 'ConcatenatedDocvecs' object has no attribute 'most_similar'
当然,我不能使用简单模型来推断相似的文档,因为生成的向量嵌入的大小为 100(而不是像串联向量那样为 200)。
如何从 ConcatenatedDocvecs 模型中获取与文档向量最相似的文档列表?
ConcatenatedDocvecs
是一个简单的实用程序包装器 class,它允许您在多个基础 Doc2Vec
模型中访问标签向量的串联。它的存在是为了更容易地重现原始 'ParagraphVector' 论文中的一些分析。
它不会重现 Doc2Vec
模型(或一组键控向量)的所有功能,因此无法直接帮助您执行您想执行的 most_similar()
。
您可以改为在每个组成模型中执行最相似的操作,然后组合两个相似性度量(每个邻居)——例如通过对它们进行平均——以获得组合模型的可用相似性值(然后重新排序)。我怀疑,但我不确定,来自两个 100d 模型的这样一个值会表现得非常像来自串联的 200d 模型的真正余弦相似性。
或者,您可以查看各种 KeyedVectors
,而不是使用 ConcatenatedDoc2Vec
包装器 class(仅在请求时创建和 returns 串联的 200d 向量) class 在 gensim 中,并使用(或改编)一个来填充来自两个组成模型的所有连接的 200d 向量。然后,它的 most_similar()
就可以工作了。
我正在按照 here
提供的指导生成 Pandas DataFrame 的 Doc2Vec 嵌入from gensim.models import Doc2Vec
from gensim.models.doc2vec import TaggedDocument
from gensim.test.test_doc2vec import ConcatenatedDoc2Vec
import gensim.models.doc2vec
from collections import OrderedDict
import pandas as pd
import numpy as np
cube_embedded = # pandas cube
# convert the cube to documents
alldocs = [TaggedDocument(doc, [i]) for i, doc in enumerate(cube_embedded.values.tolist())]
# train models
simple_models = [
# PV-DBOW plain
Doc2Vec(dm=0, vector_size=100, negative=5, hs=0, min_count=2, sample=0, epochs=20, workers=cores),
# PV-DM w/ default averaging; a higher starting alpha may improve CBOW/PV-DM modes
Doc2Vec(dm=1, vector_size=100, window=10, negative=5, hs=0, min_count=2, sample=0, epochs=20, workers=cores, alpha=0.05, comment='alpha=0.05'),
# PV-DM w/ concatenation - big, slow, experimental mode window=5 (both sides) approximates paper's apparent 10-word total window size
Doc2Vec(dm=1, dm_concat=1, vector_size=100, window=5, negative=5, hs=0, min_count=2, sample=0, epochs=20, workers=cores),
]
for d2v_model in simple_models:
d2v_model.build_vocab(alldocs)
d2v_model.train(alldocs, total_examples=d2v_model.corpus_count, epochs=d2v_model.epochs)
models_by_name = OrderedDict((str(d2v_model), d2v_model) for d2v_model in simple_models)
models_by_name['dbow+dmm'] = ConcatenatedDoc2Vec([simple_models[0], simple_models[1]])
models_by_name['dbow+dmc'] = ConcatenatedDoc2Vec([simple_models[0], simple_models[2]])
给定文档向量 V,如果我尝试从 ConcatenatedDocvecs 模型中推断出与文档向量 V 最相似的文档,我会收到以下错误:
V = np.random.rand(200)
models_by_name['dbow+dmc'].docvecs.most_similar([V])
AttributeError: 'ConcatenatedDocvecs' object has no attribute 'most_similar'
当然,我不能使用简单模型来推断相似的文档,因为生成的向量嵌入的大小为 100(而不是像串联向量那样为 200)。
如何从 ConcatenatedDocvecs 模型中获取与文档向量最相似的文档列表?
ConcatenatedDocvecs
是一个简单的实用程序包装器 class,它允许您在多个基础 Doc2Vec
模型中访问标签向量的串联。它的存在是为了更容易地重现原始 'ParagraphVector' 论文中的一些分析。
它不会重现 Doc2Vec
模型(或一组键控向量)的所有功能,因此无法直接帮助您执行您想执行的 most_similar()
。
您可以改为在每个组成模型中执行最相似的操作,然后组合两个相似性度量(每个邻居)——例如通过对它们进行平均——以获得组合模型的可用相似性值(然后重新排序)。我怀疑,但我不确定,来自两个 100d 模型的这样一个值会表现得非常像来自串联的 200d 模型的真正余弦相似性。
或者,您可以查看各种 KeyedVectors
,而不是使用 ConcatenatedDoc2Vec
包装器 class(仅在请求时创建和 returns 串联的 200d 向量) class 在 gensim 中,并使用(或改编)一个来填充来自两个组成模型的所有连接的 200d 向量。然后,它的 most_similar()
就可以工作了。