簡體   English   中英

如何在 LDA model 中為主題建模指定 random_state

[英]how to specify random_state in LDA model for topic modelling

我閱讀了有關 random_state 的 gensim LDA model 文檔,其中指出:

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

– randomState object 或生成種子的種子。 對重現性有用。

我一直在嘗試將 random_state=42 或

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

這沒有用。 誰能建議我該怎么做

模型=ldaModel(語料庫=語料庫,id2word=字典,num_topics=num_topics,random_state=None)

我綁定在沒有 random_state 的情況下使用它 function 它可以工作,但是使用 random_state 我收到錯誤消息說 LDA model 未定義

def compute_coherence_values(字典、語料庫、文本、限制、隨機狀態、開始=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而不指定 label。 僅將變量傳遞給具有 int 編號的方法對ldaModel方法沒有任何意義,因為該方法不采用位置參數。 該方法采用命名參數。 所以它應該是這樣的:

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

我有一個使用來自LatentDirichletAllocationsklearn.decomposition的 LDA 實現,對於random_state它需要一個 integer。 這是一個例子:

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的好教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM