gpt4 book ai didi

python - Gensim Doc2Vec 按文档作者访问向量

转载 作者:行者123 更新时间:2023-11-30 22:19:56 27 4
gpt4 key购买 nike

我在 df 中有三个文档:

id    author    document
12X john the cat sat
12Y jane the dog ran
12Z jane the hippo ate

这些文档被转换为 TaggedDocuments 语料库,其中标签是语义上无意义的整数的典型做法:

def read_corpus(documents):
for i, plot in enumerate(documents):
yield gensim.models.doc2vec.TaggedDocument(gensim.utils.simple_preprocess(plot, max_len=30), [i])

train_corpus = list(read_corpus(df.document))

然后使用该语料库来训练我的 Doc2Vec 模型:

model = gensim.models.doc2vec.Doc2Vec(vector_size=50, min_count=2, epochs=55)
model.build_vocab(train_corpus)
model.train(train_corpus, total_examples=model.corpus_count, epochs=model.epochs)

模型的结果向量可以这样访问:

model.docvecs.vectors_docs

如何将原始 df 与结果向量联系起来?现在所有文档都已经过训练并且每个文档都已识别出向量,我想按作者查询向量集。例如,如果我只想返回 Jane 的一组向量,我该怎么做?

我认为基本思想是识别与 Jane 相对应的 int 标签,然后执行以下操作来访问它们:

from operator import itemgetter 
a = model.docvecs.vectors_docs
b = [1, 2]
itemgetter(*b)(a)

我如何识别标签?它们仅对模型和标记文档有意义,因此它们不会连接回我的原始 df。

最佳答案

我使用 Gensim 尝试了一个简单的示例。我认为这里的方法应该适合你

import gensim
training_sentences = ['This is some document from Author {}'.format(i) for i in range(1,10)]
def read_corpus():
for i,line in enumerate(training_sentences):
# lets use the tag to identify the document and author
yield gensim.models.doc2vec.TaggedDocument(gensim.utils.simple_preprocess(line), ['Doc{}_Author{}'.format(i,i)])

您也可以直接从 pandas_df 准备训练语料库,如下所示

data_df = pd.DataFrame({'doc':training_sentences,'doc_id':[i for i in range (1,10)],'author_id':[10+i for i in range (1,10)]})
data_df.head()
tagged_docs = data_df.apply(lambda x:gensim.models.doc2vec.TaggedDocument(gensim.utils.simple_preprocess(x.doc),['doc{}_auth{}'.format(x.doc_id,x.author_id)]),axis=1)
training_corpus = tagged_docs.values

>> array([ TaggedDocument(words=['this', 'is', 'some', 'document', 'from', 'author'], tags=['doc1_auth11']),
TaggedDocument(words=['this', 'is', 'some', 'document', 'from', 'author'], tags=['doc2_auth12']),

# training
model = gensim.models.doc2vec.Doc2Vec(vector_size=50, min_count=2, epochs=55)
train_corpus = list(read_corpus())
model.build_vocab(train_corpus)
model.train(train_corpus, total_examples=model.corpus_count, epochs=model.epochs)
# indexing
model.docvecs.index2entity

>>
['Doc0_Author0',
'Doc1_Author1',
'Doc2_Author2',
'Doc3_Author3',
'Doc4_Author4',
'Doc5_Author5',
'Doc6_Author6',
'Doc7_Author7',
'Doc8_Author8']

现在,要访问author1的document1对应的向量,你可以这样做

model.docvecs[model.docvecs.index2entity.index('Doc1_Author1')]

array([ 8.08026362e-03, 4.27437993e-03, -7.73820514e-03,
-7.40669528e-03, 6.36066869e-03, 4.03292105e-03,
9.60215740e-03, -4.26750770e-03, -1.34797185e-03,
-9.02472902e-03, 6.25275355e-03, -2.49505695e-03,
3.18572600e-03, 2.56929174e-03, -4.17032139e-03,
-2.33384431e-03, -5.10744564e-03, -5.29057207e-03,
5.41675789e-03, 5.83767192e-03, -5.91145828e-03,
5.91885624e-03, -1.00465110e-02, 8.32535885e-03,
9.72494949e-03, -7.35746371e-03, -1.86231872e-03,
8.94813929e-05, -4.11528209e-03, -9.72509012e-03,
-6.52212929e-03, -8.83922912e-03, 9.46981460e-03,
-3.90578934e-04, 6.74136635e-03, -5.24599617e-03,
9.73031297e-03, -8.77021812e-03, -5.55411633e-03,
-7.21857697e-03, -4.50362219e-03, -4.06361837e-03,
2.57276138e-03, 1.76626759e-06, -8.08755495e-03,
-1.48400548e-03, -5.26673114e-03, -7.78301107e-03,
-4.24248137e-04, -7.99000356e-03], dtype=float32)

是的,这使用了文档-作者对排序,您可以单独使用 doc_id 并在 python 字典中维护一个单独的索引,例如 {doc_id:author_id} ,如果您想按作者过滤,则使用{author_id : [docids,...]}

关于python - Gensim Doc2Vec 按文档作者访问向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48953871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com