doc2vec的Python简单实现?

标签 python gensim word2vec doc2vec

我正在尝试从 gensim 实现 doc2vec,但遇到一些错误,并且网络上没有足够的文档或帮助。 这是我的工作代码的一部分:

from gensim.models import Doc2Vec
from gensim.models.doc2vec import LabeledSentence

class LabeledLineSentence(object):
    def __init__(self, filename):
        self.filename = filename
    def __iter__(self):
        with open(self.filename, 'r') as f:
            for uid, line in enumerate(f):
                print LabeledSentence(line.split(), tags=['TXT_%s' % uid])
                yield LabeledSentence(words=line.split(), tags=['TXT_%s' % uid])

sentences = LabeledLineSentence('myfile.txt')

我的 txt 文件是什么样的:

  1 hi how are you
  2 hi how are you
  3 hi how are you
  4 its such a great day
  5 its such a great day
  6 its such a great day
  7 i like dogs
  8 i like cats
  9 i like snakes
 10 the ice cream was yummy
 11 the cake was awesome  

初始化模型

model = Doc2Vec(alpha=0.025, min_alpha=0.025, size=50, window=5, min_count=5,
                dm=1, workers=8, sample=1e-5)       

打印输出示例:

LabeledSentence(['hi', 'how', 'are', 'you'], ['TXT_0'])
LabeledSentence(['hi', 'how', 'are', 'you'], ['TXT_1'])
LabeledSentence(['hi', 'how', 'are', 'you'], ['TXT_2'])
LabeledSentence(['its', 'such', 'a', 'great', 'day'], ['TXT_3'])
LabeledSentence(['its', 'such', 'a', 'great', 'day'], ['TXT_4'])

这是错误所在:

for epoch in range(500):
    try:
        print 'epoch %d' % (epoch)
        model.train(sentences)
        model.alpha *= 0.99
        model.min_alpha = model.alpha
    except (KeyboardInterrupt, SystemExit):
        break

RuntimeError: you must first build vocabulary before training the model

知道为什么吗?

最佳答案

调用 model.build_vocab 将修复该错误。

请参阅本教程 https://github.com/RaRe-Technologies/gensim/blob/develop/docs/notebooks/doc2vec-lee.ipynb

关于doc2vec的Python简单实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40315446/

相关文章:

Python如何使用ArgumentParser.convert_arg_line_to_args跳过文件中的注释行

python - 在 Pandas 中组合两个数据框时的意外行为

python - 如何安装 python gtkmozembed 包或者在 Windows 中是否有 python GtkMozEmbed 的替代品?

python - 使用gensim的Doc2Vec生成句子向量

c - 如何为其他语言(word2vec)制作预训练 vector ?

python - GStreamer 如何从流中提取视频帧?

python - Python 3.5 上的 Gensim 1.0.1 TypeError : object of type 'map' has no len()?

nlp - 使用 Gensim 减少 Google 的 Word2Vec 模型

python-3.x - word2vec - 通过特定向量查找单词

python - 找到与单词集最接近的单词