私は、ハーバード図書館の本のタイトルと主題をモデル化するトピックです。
Gensim Mallet Wrapper を使用して、Mallet の LDA でモデル化します。モデルの良さを確認するために Coherence と Perplexity の値を取得しようとすると、以下の例外で perplexity の計算に失敗します。Mallet の代わりに Gensim の組み込み LDA モデルを使用すると、同じエラーは発生しません。私のコーパスには、長さが最大 50 語、平均 20 語の 700 万以上のドキュメントが含まれています。したがって、ドキュメントは短いものです。
以下は私のコードの関連部分です:
# TOPIC MODELING
from gensim.models import CoherenceModel
num_topics = 50
# Build Gensim's LDA model
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
id2word=id2word,
num_topics=num_topics,
random_state=100,
update_every=1,
chunksize=100,
passes=10,
alpha='auto',
per_word_topics=True)
# Compute Perplexity
print('\nPerplexity: ', lda_model.log_perplexity(corpus))
# a measure of how good the model is. lower the better.
困惑: -47.91929228302663
# Compute Coherence Score
coherence_model_lda = CoherenceModel(model=lda_model,
texts=data_words_trigrams, dictionary=id2word, coherence='c_v')
coherence_lda = coherence_model_lda.get_coherence()
print('\nCoherence Score: ', coherence_lda)
コヒーレンス スコア: 0.28852857563541856
LDA は問題なくスコアを出しました。今、私はMALLETで同じ言葉の袋をモデル化しています
# Building LDA Mallet Model
mallet_path = '~/mallet-2.0.8/bin/mallet' # update this path
ldamallet = gensim.models.wrappers.LdaMallet(mallet_path,
corpus=corpus, num_topics=num_topics, id2word=id2word)
# Convert mallet to gensim type
mallet_model =
gensim.models.wrappers.ldamallet.malletmodel2ldamodel(ldamallet)
# Compute Coherence Score
coherence_model_ldamallet = CoherenceModel(model=mallet_model,
texts=data_words_trigrams, dictionary=id2word, coherence='c_v')
coherence_ldamallet = coherence_model_ldamallet.get_coherence()
print('\nCoherence Score: ', coherence_ldamallet)
コヒーレンス スコア: 0.5994123896865993
次に、Perplexity 値を要求すると、警告と NaN 値を下回ります。
# Compute Perplexity
print('\nPerplexity: ', mallet_model.log_perplexity(corpus))
/app/app-py3/lib/python3.5/site-packages/gensim/models/ldamodel.py:1108: RuntimeWarning: 乗算スコアで無効な値が検出されました += np.sum((self.eta - _lambda) * Elogbeta )
困惑:ナン
/app/app-py3/lib/python3.5/site-packages/gensim/models/ldamodel.py:1109: RuntimeWarning: 減算スコアで無効な値が検出されました += np.sum(gammaln(_lambda) - gammaln(self.イータ))
これは非常に Gensim 固有の質問であり、この関数のより深い知識が必要であることを認識しています: gensim.models.wrappers.ldamalet.malletmodel2ldamodel(ldamalet)
したがって、警告と Gensim ドメインに関するコメントをいただければ幸いです。