无法使用 gensim 加载 Doc2vec 对象

Cannot load Doc2vec object using gensim

我正在尝试使用 gensim 加载预训练的 Doc2vec 模型,并使用它将段落映射到矢量。我指的是https://github.com/jhlau/doc2vec,我下载的预训练模型是英文维基百科的DBOW,同样在link里面。但是,当我在维基百科上加载 Doc2vec 模型并使用以下代码推断向量时:

import gensim.models as g
import codecs

model="wiki_sg/word2vec.bin"
test_docs="test_docs.txt"
output_file="test_vectors.txt"

#inference hyper-parameters
start_alpha=0.01
infer_epoch=1000

#load model
test_docs = [x.strip().split() for x in codecs.open(test_docs, "r", "utf-8").readlines()]
m = g.Doc2Vec.load(model)

#infer test vectors
output = open(output_file, "w")
for d in test_docs:
    output.write(" ".join([str(x) for x in m.infer_vector(d, alpha=start_alpha, steps=infer_epoch)]) + "\n")
output.flush()
output.close()

我得到一个错误:

/Users/zhangji/Desktop/CSE547/Project/NLP/venv/lib/python2.7/site-packages/smart_open/smart_open_lib.py:402: UserWarning: This function is deprecated, use smart_open.open instead. See the migration notes for details: https://github.com/RaRe-Technologies/smart_open/blob/master/README.rst#migrating-to-the-new-open-function
  'See the migration notes for details: %s' % _MIGRATION_NOTES_URL
Traceback (most recent call last):
  File "/Users/zhangji/Desktop/CSE547/Project/NLP/AbstractMapping.py", line 19, in <module>
    output.write(" ".join([str(x) for x in m.infer_vector(d, alpha=start_alpha, steps=infer_epoch)]) + "\n")
AttributeError: 'Word2Vec' object has no attribute 'infer_vector'

我知道有几个线程关于堆栈溢出的 infer_vector 问题,但其中 none 解决了我的问题。我使用

下载了 gensim 包
pip install git+https://github.com/jhlau/gensim

另外,我看了gensim包里的源码,发现当我使用Doc2vec.load()时,Doc2vec class并没有真正的load()函数本身,但由于它是Word2vec的subclass,所以它调用了Word2vec中load()的super方法,然后使模型成为Word2vec对象。但是,infer_vector() 函数是 Doc2vec 独有的,在 Word2vec 中不存在,这就是它导致错误的原因。我还尝试将模型 m 转换为 Doc2vec,但出现此错误:

>>> g.Doc2Vec(m)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/zhangji/Library/Python/2.7/lib/python/site-packages/gensim/models/doc2vec.py", line 599, in __init__
    self.build_vocab(documents, trim_rule=trim_rule)
  File "/Users/zhangji/Library/Python/2.7/lib/python/site-packages/gensim/models/word2vec.py", line 513, in build_vocab
    self.scan_vocab(sentences, trim_rule=trim_rule)  # initial survey
  File "/Users/zhangji/Library/Python/2.7/lib/python/site-packages/gensim/models/doc2vec.py", line 635, in scan_vocab
    for document_no, document in enumerate(documents):
  File "/Users/zhangji/Library/Python/2.7/lib/python/site-packages/gensim/models/word2vec.py", line 1367, in __getitem__
    return vstack([self.syn0[self.vocab[word].index] for word in words])
TypeError: 'int' object is not iterable

事实上,我现在想要的 gensim 只是使用在学术文章上运行良好的预训练模型将段落转换为向量。由于某些原因,我不想自己训练模型。如果有人能帮我解决问题,我将不胜感激。

顺便说一句,我用的是python2.7,目前的gensim版本是0.12.4.

谢谢!

我会避免使用 https://github.com/jhlau/doc2vec 上已有 4 年历史的非标准 gensim 分支,或任何仅加载此类代码的已有 4 年历史的已保存模型。

那里的维基百科 DBOW 模型也小得令人怀疑,只有 1.4GB。甚至在 4 年前,维基百科就有超过 400 万篇文章,一个 300 维 Doc2Vec 模型被​​训练为拥有 400 万篇文章的文档向量,其大小至少为 4000000 articles * 300 dimensions * 4 bytes/dimension = 4.8GB,而不是甚至计算模型的其他部分。 (因此,该下载显然 不是 相关论文中提到的 4.3M 文档、300 维模型——而是以其他不清楚的方式被截断的内容。)

目前的gensim版本是3.8.3,几周前发布了。

使用当前代码和当前 Wikipedia 转储构建您自己的 Doc2Vec 模型可能需要一些修修补补,以及一整夜或更长时间的运行时间 - 但随后您将获得现代支持代码,具有现代模型,可以更好地理解过去 4 年中使用的单词。 (而且,如果你在你感兴趣的文档类型的语料库上训练模型——比如学术文章——词汇、词义和与你自己的文本预处理的匹配将用于以后推断的文档一切都会更好。)

gensim 源代码树中有一个构建 Doc2Vec 维基百科模型的 Jupyter notebook 示例:

https://github.com/RaRe-Technologies/gensim/blob/develop/docs/notebooks/doc2vec-wikipedia.ipynb