2013-02-25 56 views
9

Tôi đang sử dụng python gensim để đào tạo mô hình Phân bổ cấp bậc LAV (Latent Dirichlet Allocation - LDA) từ một kho nhỏ 231 câu. Tuy nhiên, mỗi lần tôi lặp lại quá trình, nó tạo ra các chủ đề khác nhau.Mô hình LDA tạo ra các chủ đề khác nhau mỗi khi tôi đào tạo trên cùng một tập mẫu

Tại sao các tham số và tập hợp LDA giống nhau lại tạo ra các chủ đề khác nhau mỗi lần?

Và làm thế nào để tôi ổn định việc tạo chủ đề?

Tôi đang sử dụng corpus này (http://pastebin.com/WptkKVF0) và danh sách này của các tệp từ dừng (http://pastebin.com/LL7dqLcj) và đây là mã của tôi:

from gensim import corpora, models, similarities 
from gensim.models import hdpmodel, ldamodel 
from itertools import izip 
from collections import defaultdict 
import codecs, os, glob, math 

stopwords = [i.strip() for i in codecs.open('stopmild','r','utf8').readlines() if i[0] != "#" and i != ""] 

def generateTopics(corpus, dictionary): 
    # Build LDA model using the above corpus 
    lda = ldamodel.LdaModel(corpus, id2word=dictionary, num_topics=50) 
    corpus_lda = lda[corpus] 

    # Group topics with similar words together. 
    tops = set(lda.show_topics(50)) 
    top_clusters = [] 
    for l in tops: 
     top = [] 
     for t in l.split(" + "): 
      top.append((t.split("*")[0], t.split("*")[1])) 
     top_clusters.append(top) 

    # Generate word only topics 
    top_wordonly = [] 
    for i in top_clusters: 
     top_wordonly.append(":".join([j[1] for j in i])) 

    return lda, corpus_lda, top_clusters, top_wordonly 

####################################################################### 

# Read textfile, build dictionary and bag-of-words corpus 
documents = [] 
for line in codecs.open("./europarl-mini2/map/coach.en-es.all","r","utf8"): 
    lemma = line.split("\t")[3] 
    documents.append(lemma) 
texts = [[word for word in document.lower().split() if word not in stopwords] 
      for document in documents] 
dictionary = corpora.Dictionary(texts) 
corpus = [dictionary.doc2bow(text) for text in texts] 

lda, corpus_lda, topic_clusters, topic_wordonly = generateTopics(corpus, dictionary) 

for i in topic_wordonly: 
    print i 

Trả lời

25

Tại sao các thông số và corpus LDA cùng tạo ra chủ đề khác nhau mỗi lần?

Vì LDA sử dụng ngẫu nhiên trong cả hai bước đào tạo và suy luận.

Và làm thế nào để tôi ổn định việc tạo chủ đề?

Bằng cách đặt hạt giống numpy.random với giá trị giống nhau mỗi khi một mô hình được đào tạo hoặc suy luận được thực hiện, với numpy.random.seed:

SOME_FIXED_SEED = 42 

# before training/inference: 
np.random.seed(SOME_FIXED_SEED) 

(Đây là xấu xí, và nó làm cho kết quả Gensim khó để tái tạo; hãy xem xét việc gửi một bản vá. Tôi đã mở một số issue.)

+2

Nếu dữ liệu Traing là đủ, kết quả nên hội tụ trong vòng hạn chế. Phải không? –

+0

Tôi có thể biết làm cách nào để đặt 'numpy.random' thành' numpy.random.seed'? bạn có thể chỉ cho tôi một ví dụ về cách gọi 'ldamodel' với 'numpy.random.seed' không? – alvas

+1

@ 2er0 Bạn không đặt 'np.random' * thành *' np.random.seed', bạn đặt hạt giống * bằng * 'np.random.seed'. –

1

Tôi có cùng một vấn đề, ngay cả với khoảng 50.000 nhận xét. Nhưng bạn có thể nhận được nhiều chủ đề nhất quán hơn bằng cách tăng số lần lặp mà LDA chạy. Ban đầu nó được thiết lập là 50 và khi tôi nâng nó lên 300, nó thường mang lại cho tôi những kết quả tương tự, có lẽ vì nó gần gũi hơn với sự hội tụ.

Cụ thể, bạn chỉ cần thêm các tùy chọn sau đây:

ldamodel.LdaModel(corpus, ..., iterations = <your desired iterations>): 
Các vấn đề liên quan