如何在主题建模的LDA模型中指定random_state

how to specify random_state in LDA model for topic modelling

我阅读了关于 random_state 的 gensim LDA 模型文档,其中指出:

random_state ({np.random.RandomState, int}, optional) 

– 随机状态对象或种子生成一个。对再现性有用。

我一直在尝试 random_state=42 或

random_seed=42
state=np.random.RandomState(random_seed)
state.randn(1)
random_state=state.randn(1) 

这没有用。谁能建议我该怎么做

model=ldaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics, random_state=None)

我想在没有 random_state 它有效的功能的情况下使用它,但是 random_state 我收到错误消息说 LDA 模型未定义

def compute_coherence_values(词典、语料库、正文、limit, random_state, start=2, 步骤=3):

coherence_values = []
model_list = []
for num_topics in range(start, limit, step):
    #model=LdaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics)
    model=ldaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics, 
                                                  random_state)
    model_list.append(model)
    coherencemodel = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence='c_v')
    coherence_values.append(coherencemodel.get_coherence())

return model_list, coherence_values

你的代码错误在这里:

 model=ldaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics, 
                                                  random_state)

您不能只传递变量 random_state 而不指定标签。仅将变量传递给带有 int 数字的方法对 ldaModel 方法没有任何意义,因为该方法不采用位置参数。该方法采用命名参数。所以应该是这样的:

model=ldaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics, 
                                                  random_state = random_state)

我有一个使用 sklearn.decomposition 中的 LatentDirichletAllocation 的 LDA 实现,对于 random_state 它需要一个整数。这是一个例子:

lda_model = LatentDirichletAllocation(n_components=10,        
                                  max_iter=10,               
                                  learning_method='online',   
                                  random_state=100,          
                                  batch_size=128,            
                                  evaluate_every = -1,       
                                  n_jobs = -1 )

Here is a good tutorial关于如何实现和LDA