2016-09-21 46 views
0

当使用LDA模型时,每次都会得到不同的主题,我想要复制同一组。我在Google搜索了类似的问题,例如this未能在gensim中修复LDA模型中的种子值

我按照num.random.seed(1000)的文章所述修复了种子,但它不起作用。我读了ldamodel.py,找到下面的代码:

def get_random_state(seed): 

    """ 
    Turn seed into a np.random.RandomState instance. 
    Method originally from maciejkula/glove-python, and written by @joshloyal 
    """ 
    if seed is None or seed is numpy.random: 
     return numpy.random.mtrand._rand 
    if isinstance(seed, (numbers.Integral, numpy.integer)): 
     return numpy.random.RandomState(seed) 
    if isinstance(seed, numpy.random.RandomState): 
     return seed 
    raise ValueError('%r cannot be used to seed a numpy.random.RandomState' 
         ' instance' % seed) 

所以我使用的代码:

lda = models.LdaModel(
    corpus_tfidf, 
    id2word=dic, 
    num_topics=2, 
    random_state=numpy.random.RandomState(10) 
) 

但它仍然没有工作。

回答

1

corpora.Dictionary生成的字典可能与相同的语料库不同(例如相同的词语,但顺序不同)。因此,每个人都应该修正词典以及种子以获得相同的主题。下面的代码可能有助于修复词典:

dic = corpora.Dictionary(corpus) 
dic.save("filename") 
dic=corpora.Dictionary.load("filename")