Python:gensim:RuntimeError:在训练模型之前必须先构建词汇表

时间:2015-11-30 00:30:38

标签: python gensim word2vec

我知道这问题已经被提出,但我仍然无法找到解决方案。

我想在自定义数据集上使用gensim的word2vec,但现在我仍在弄清楚数据集必须采用何种格式。我查看了this post,其中输入基本上是一个列表列表(一个包含其他列表的大列表,这些列表是来自NLTK Brown语料库的标记化句子)。所以我认为这是我必须用于命令word2vec.Word2Vec()的输入格式。但是,它不适用于我的小测试集,我不明白为什么。

我尝试过:

这有效

from gensim.models import word2vec
from nltk.corpus import brown
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

brown_vecs = word2vec.Word2Vec(brown.sents())

这不起作用

sentences = [ "the quick brown fox jumps over the lazy dogs","yoyoyo you go home now to sleep"]
vocab = [s.encode('utf-8').split() for s in sentences]
voc_vec = word2vec.Word2Vec(vocab)

我不明白为什么它不适用于“模拟”数据,即使它与布朗语料库中的句子具有相同的数据结构:

翻译

[['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dogs'], ['yoyoyo', 'you', 'go', 'home', 'now', 'to', 'sleep']]

brown.sents() :(开头)

[['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an', 'investigation', 'of', "Atlanta's", 'recent', 'primary', 'election', 'produced', '``', 'no', 'evidence', "''", 'that', 'any', 'irregularities', 'took', 'place', '.'], ['The', 'jury', 'further', 'said', 'in', 'term-end', 'presentments', 'that', 'the', 'City', 'Executive', 'Committee', ',', 'which', 'had', 'over-all', 'charge', 'of', 'the', 'election', ',', '``', 'deserves', 'the', 'praise', 'and', 'thanks', 'of', 'the', 'City', 'of', 'Atlanta', "''", 'for', 'the', 'manner', 'in', 'which', 'the', 'election', 'was', 'conducted', '.'], ...]

任何人都可以告诉我我做错了什么吗?

2 个答案:

答案 0 :(得分:43)

gensim的Word2Vec中的默认min_count设置为5.如果您的词汇中没有频率大于4的单词,则您的词汇将为空,因此错误。尝试

voc_vec = word2vec.Word2Vec(vocab, min_count=1)

答案 1 :(得分:0)

输入到gensim的Word2Vec可以是句子列表或单词列表或句子列表。

E.g。

1. sentences = ['I love ice-cream', 'he loves ice-cream', 'you love ice cream']
2. words = ['i','love','ice - cream', 'like', 'ice-cream']
3. sentences = [['i love ice-cream'], ['he loves ice-cream'], ['you love ice cream']]

在培训前建立词汇

model.build_vocab(sentences, update=False)

just check out the link for detailed info

相关问题