如何在 LDA 模型中指定 random_state 进行主题建模 [英] how to specify random_state in LDA model for topic modelling

查看:98
本文介绍了如何在 LDA 模型中指定 random_state 进行主题建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I read the gensim LDA model documentation about random_state which states that:

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

– 一个 randomState 对象或一个生成一个的种子.有助于重现性.

– Either a randomState object or a seed to generate one. Useful for reproducibility.

我一直在尝试把 random_state=42 或

I have been tring put random_state=42 or

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

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

which did not work. Can anyone suggest what should i do

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

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

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

I tied to use it without random_state the function it works but with random_state i got error message saying LDA model is not defined

def compute_coherence_values(dictionary, corpus, texts, limit, random_state, start=2,步骤=3):

def compute_coherence_values(dictionary, corpus, texts, limit, random_state, start=2, step=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

推荐答案

你的代码中的错误在这里:

The mistake in your code is in here:

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

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

You can't just pass the variable random_state without specifying the label. Just passing the variable to the method with an int number means nothing to the ldaModel method, since the method does not take positional parameter. The method takes named parameters. So it should be like this:

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

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

I have an implementation of the LDA that uses LatentDirichletAllocation from sklearn.decomposition, and for the random_state it takes an integer. Here is an example:

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

这里是一个关于如何实现和 LDA 的好教程

这篇关于如何在 LDA 模型中指定 random_state 进行主题建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆