【问题标题】:how to specify random_state in LDA model for topic modelling如何在 LDA 模型中为主题建模指定 random_state
【发布时间】:2020-08-05 23:56:17
【问题描述】:

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

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

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

我一直在尝试把 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(字典、语料库、文本、限制、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

【问题讨论】:

    标签: python text model word


    【解决方案1】:

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

     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.decompositionLatentDirichletAllocation 的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

    【讨论】:

    • 嗨,安娜,是的,如果我像你一样指定 random_state ,它就可以工作。当我在函数中定义它时,它似乎有问题。任何想法,我刚刚更新了帖子。谢谢
    • 我试图从这个 [ 链接 ] (datascienceplus.com/…) 修改函数
    • 你不能只传递random_state变量,你必须指定参数名称和它的值。让我更新我的评论
    • 我确实尝试过这样定义函数:def compute_coherence_values(dictionary, corpus, texts, limit, random_state=100, start=2, step=3):
    • 在你建议的函数中:model=ldaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics, random_state = random_state)
    猜你喜欢
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    相关资源
    最近更新 更多