Skip to content

Commit

Permalink
Fixes piskvorky#2069: wrong malletmodel2ldamodel
Browse files Browse the repository at this point in the history
`malletmodel2ldamodel` sets up expElogbeta attribute
but LdaModel.show_topics uses inner not dirichleted state instead.
And moreover LdaState and LdaModel were not synced.
  • Loading branch information
horpto committed Dec 12, 2018
1 parent 54e2164 commit 532072d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions gensim/models/wrappers/ldamallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,11 @@ def malletmodel2ldamodel(mallet_model, gamma_threshold=0.001, iterations=50):
"""
model_gensim = LdaModel(
id2word=mallet_model.id2word, num_topics=mallet_model.num_topics,
alpha=mallet_model.alpha, iterations=iterations,
alpha=mallet_model.alpha, eta=0,
iterations=iterations,
gamma_threshold=gamma_threshold,
dtype=numpy.float64 # don't loose precision when converting from MALLET
)
model_gensim.expElogbeta[:] = mallet_model.wordtopics
model_gensim.state.sstats[...] = mallet_model.wordtopics
model_gensim.sync_state()
return model_gensim
6 changes: 5 additions & 1 deletion gensim/test/test_ldamallet_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def testMallet2Model(self):

tm1 = ldamallet.LdaMallet(self.mallet_path, corpus=corpus, num_topics=2, id2word=dictionary)
tm2 = ldamallet.malletmodel2ldamodel(tm1)

# set num_topics=-1 to exclude random influence
self.assertEqual(tm1.show_topics(-1, 10), tm2.show_topics(-1, 10))

for document in corpus:
element1_1, element1_2 = tm1[document][0]
element2_1, element2_2 = tm2[document][0]
Expand All @@ -101,7 +105,7 @@ def testMallet2Model(self):
self.assertAlmostEqual(element1_2, element2_2, 1)
logging.debug('%d %d', element1_1, element2_1)
logging.debug('%d %d', element1_2, element2_2)
logging.debug('%d %d', tm1[document][1], tm2[document][1])
logging.debug('%s %s', tm1[document][1], tm2[document][1])

def testPersistence(self):
if not self.mallet_path:
Expand Down

0 comments on commit 532072d

Please sign in to comment.