我使用 MySentences
类从目录中的所有文件中提取句子,并使用这些句子来训练 word2vec 模型。
我的数据集未标记。
class MySentences(object):
def __init__(self, dirname):
self.dirname = dirname
def __iter__(self):
for fname in os.listdir(self.dirname):
for line in open(os.path.join(self.dirname, fname)):
yield line.split()
sentences = MySentences('sentences')
model = gensim.models.Word2Vec(sentences)
现在我想使用该类来制作 doc2vec 模型。我读过Doc2Vec引用页。 Doc2Vec()
函数获取句子作为参数,但它不接受上述句子变量并返回错误:
AttributeError: 'list' object has no attribute 'words'
有什么问题吗?该参数的正确类型是什么?
更新:
我认为,未标记的数据是问题所在。看来doc2vec需要标记数据。
最佳答案
没有理由使用额外的类来解决问题。在库的新更新中,添加了一个新函数 TaggedLineDocument
将句子转换为向量。
sentences = TaggedLineDocument(INPUT_FILE)
然后训练模型
model = Doc2Vec(alpha=0.025, min_alpha=0.025)
model.build_vocab(sentences)
for epoch in range(10):
model.train(sentences)
model.alpha -= 0.002
model.min_alpha = model.alpha
print epoch
关于python - Gensim Doc2Vec - 将语料库句子传递给 Doc2Vec 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38245739/