Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added vwmodel2ldamodel function #798

Merged
merged 1 commit into from
Jul 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions gensim/models/wrappers/ldavowpalwabbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import numpy

from gensim import utils, matutils
from gensim.models.ldamodel import LdaModel

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -559,3 +560,25 @@ def _run_vw_command(cmd):
def _bit_length(num):
"""Return number of bits needed to encode given number."""
return len(bin(num).lstrip('-0b'))

def vwmodel2ldamodel(vw_model, iterations=50):
"""
Function to convert vowpal wabbit model to gensim LdaModel. This works by
simply copying the training model weights (alpha, beta...) from a trained
vwmodel into the gensim model.

Args:
----
vw_model : Trained vowpal wabbit model.
iterations : Number of iterations to be used for inference of the new LdaModel.

Returns:
-------
model_gensim : LdaModel instance; copied gensim LdaModel.
"""
model_gensim = LdaModel(
num_topics=vw_model.num_topics, id2word=vw_model.id2word, chunksize=vw_model.chunksize,
passes=vw_model.passes, alpha=vw_model.alpha, eta=vw_model.eta, decay=vw_model.decay,
offset=vw_model.offset, iterations=iterations, gamma_threshold=vw_model.gamma_threshold)
model_gensim.expElogbeta[:] = vw_model._get_topics()
return model_gensim
12 changes: 12 additions & 0 deletions gensim/test/test_ldavowpalwabbit_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ def test_corpus_to_vw(self):
result = '\n'.join(ldavowpalwabbit.corpus_to_vw(corpus))
self.assertEqual(result, expected)

def testvwmodel2ldamodel(self):
"""Test copying of VWModel to LdaModel"""
if not self.vw_path:
return
tm1 = LdaVowpalWabbit(vw_path=self.vw_path, corpus=self.corpus, num_topics=2, id2word=self.dictionary)
tm2 = ldavowpalwabbit.vwmodel2ldamodel(tm1)
for document in self.corpus:
self.assertEqual(tm1[document][0], tm2[document][0])
self.assertEqual(tm1[document][1], tm2[document][1])
logging.debug('%d %d', tm1[document][0], tm2[document][0])
logging.debug('%d %d', tm1[document][1], tm2[document][1])


if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)
Expand Down