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

load_word_topics() returns value #767

Merged
merged 6 commits into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 7 additions & 5 deletions gensim/models/wrappers/ldamallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __getitem__(self, bow, iterations=100):

def load_word_topics(self):
logger.info("loading assigned topics from %s", self.fstate())
wordtopics = numpy.zeros((self.num_topics, self.num_terms), dtype=numpy.float32)
word_topics = numpy.zeros((self.num_topics, self.num_terms), dtype=numpy.float32)
if hasattr(self.id2word, 'token2id'):
word2id = self.id2word.token2id
else:
Expand All @@ -199,10 +199,10 @@ def load_word_topics(self):
if token not in word2id:
continue
tokenid = word2id[token]
wordtopics[int(topic), tokenid] += 1.0
logger.info("loaded assigned topics for %i tokens", wordtopics.sum())
self.wordtopics = wordtopics
word_topics[int(topic), tokenid] += 1.0
logger.info("loaded assigned topics for %i tokens", word_topics.sum())
self.print_topics(15)
Copy link
Owner

@piskvorky piskvorky Jul 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This print makes no sense without the self.wordtopics assignment.

@tmylk did you review this before merging??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - the print was there in the code earlier and I removed the self.wordtopics assignment so now it doesn't make sense.

return word_topics

def print_topics(self, num_topics=10, num_words=10):
return self.show_topics(num_topics, num_words, log=True)
Expand Down Expand Up @@ -242,7 +242,9 @@ def show_topics(self, num_topics=10, num_words=10, log=False, formatted=True):
return shown

def show_topic(self, topicid, topn=10):
topic = self.wordtopics[topicid]
if self.word_topics is None:
logger.info("Run train or load_word_topics before showing topics.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warn instead of info

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, did.

topic = self.word_topics[topicid]
topic = topic / topic.sum() # normalize to probability dist
bestn = matutils.argsort(topic, topn, reverse=True)
beststr = [(topic[id], self.id2word[id]) for id in bestn]
Expand Down
8 changes: 4 additions & 4 deletions gensim/test/test_ldamallet_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def testPersistence(self):
model.save(fname)
model2 = ldamallet.LdaMallet.load(fname)
self.assertEqual(model.num_topics, model2.num_topics)
self.assertTrue(numpy.allclose(model.wordtopics, model2.wordtopics))
self.assertTrue(numpy.allclose(model.word_topics, model2.word_topics))
tstvec = []
self.assertTrue(numpy.allclose(model[tstvec], model2[tstvec])) # try projecting an empty vector

Expand All @@ -116,7 +116,7 @@ def testPersistenceCompressed(self):
model.save(fname)
model2 = ldamallet.LdaMallet.load(fname, mmap=None)
self.assertEqual(model.num_topics, model2.num_topics)
self.assertTrue(numpy.allclose(model.wordtopics, model2.wordtopics))
self.assertTrue(numpy.allclose(model.word_topics, model2.word_topics))
tstvec = []
self.assertTrue(numpy.allclose(model[tstvec], model2[tstvec])) # try projecting an empty vector

Expand All @@ -132,8 +132,8 @@ def testLargeMmap(self):
# test loading the large model arrays with mmap
model2 = ldamodel.LdaModel.load(testfile(), mmap='r')
self.assertEqual(model.num_topics, model2.num_topics)
self.assertTrue(isinstance(model2.wordtopics, numpy.memmap))
self.assertTrue(numpy.allclose(model.wordtopics, model2.wordtopics))
self.assertTrue(isinstance(model2.word_topics, numpy.memmap))
self.assertTrue(numpy.allclose(model.word_topics, model2.word_topics))
tstvec = []
self.assertTrue(numpy.allclose(model[tstvec], model2[tstvec])) # try projecting an empty vector

Expand Down