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

[WIP] DTM Tutorial Notebook and changes #831

Merged
merged 7 commits into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
59 changes: 53 additions & 6 deletions gensim/models/ldaseqmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def fit_lda_seq(self, corpus, lda_inference_max_iter, em_min_iter, em_max_iter):

while iter_ < em_min_iter or ((convergence > LDASQE_EM_THRESHOLD) and iter_ <= em_max_iter):

logger.info(" EM iter ", iter_)
logger.info(" EM iter %i", iter_)
logger.info("E Step")
# TODO: bound is initialized to 0
old_bound = bound
Expand Down Expand Up @@ -211,15 +211,15 @@ def fit_lda_seq(self, corpus, lda_inference_max_iter, em_min_iter, em_max_iter):
# if max_iter is too low, increase iterations.
if lda_inference_max_iter < LOWER_ITER:
lda_inference_max_iter *= ITER_MULT_LOW
logger.info("Bound went down, increasing iterations to", lda_inference_max_iter)
logger.info("Bound went down, increasing iterations to %i", lda_inference_max_iter)

# check for convergence
convergence = numpy.fabs((bound - old_bound) / old_bound)

if convergence < LDASQE_EM_THRESHOLD:

lda_inference_max_iter = MAX_ITER
logger.info("Starting final iterations, max iter is", lda_inference_max_iter)
logger.info("Starting final iterations, max iter is %i", lda_inference_max_iter)
convergence = 1.0

logger.info(iter_, "iteration lda seq bound is", bound, ", convergence is ", convergence)
Expand Down Expand Up @@ -318,7 +318,7 @@ def fit_lda_seq_topics(self, topic_suffstats):
lhood_term = 0

for k, chain in enumerate(self.topic_chains):
logger.info("Fitting topic number", k)
logger.info("Fitting topic number %i", k)
lhood_term = sslm.fit_sslm(chain, topic_suffstats[k])
lhood += lhood_term

Expand Down Expand Up @@ -371,6 +371,53 @@ def doc_topics(self, doc_number):
return doc_topic[doc_number]


def DTMvis(self, time, corpus):
"""
returns term_frequency, vocab, doc_lengths, topic-term distributions and doc_topic distributions, specified by pyLDAvis format.
all of these are needed to visualise topics for DTM for a particular time-slice via pyLDAvis.
input parameter is the year to do the visualisation.
"""

Copy link
Owner

Choose a reason for hiding this comment

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

Code style: remove blank line.

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

doc_topic = numpy.copy(self.gammas)
doc_topic /= doc_topic.sum(axis=1)[:, numpy.newaxis]
Copy link
Owner

Choose a reason for hiding this comment

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

Why not simply self.gammas / self.gammas.sum(axis=1)[:, numpy.newaxis]? Are you running out of memory?

If so, put a clear comment to that effect here, so somebody doesn't accidentally "fix" the code later.


topic_term = []
for chain in enumerate(self.topic_chains):
topic = numpy.transpose(chain.e_log_prob)
topic = topic[time]
topic = numpy.exp(topic)
topic = topic / topic.sum()
Copy link
Owner

Choose a reason for hiding this comment

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

Again, is this some memory optimization, or why is this expression split across so many lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had made it that way so it's clear to me what's going on cause I wasn't sure then- will change it.

topic_term.append(topic)

term_frequency = [0] * self.vocab_len
Copy link
Owner

Choose a reason for hiding this comment

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

Isn't a np.array better (and more compact and faster)? Same for topic_term list above.

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, will address.

doc_lengths = []
for doc_no, doc in enumerate(corpus):
doc_lengths.append(len(doc))
for pair in doc:
term_frequency[pair[0]] += pair[1]

vocab = []
for i in range(0, len(self.id2word)):
vocab.append(self.id2word[i])
Copy link
Owner

Choose a reason for hiding this comment

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

List comprehension.

# returns numpy arrays for doc_topic proportions, topic_term proportions, and document_lengths, term_frequency.
# these should be passed to the `pyLDAvis.prepare` method to visualise one time-slice of DTM topics.
return doc_topic, numpy.array(topic_term), doc_lengths, term_frequency, vocab


def DTMcoherence(self, time):
"""
returns all topics of a particular time-slice without probabilitiy values for it to be used
for either "u_mass" or "c_v" coherence.
"""
coherence_topics = []
for topics in self.print_topics(time):
coherence_topic = []
for word, dist in topics:
coherence_topic.append(word)
coherence_topics.append(coherence_topic)

return coherence_topics

def __getitem__(self, doc):
"""
Similar to the LdaModel __getitem__ function, it returns topic proportions of a document passed.
Expand Down Expand Up @@ -584,7 +631,7 @@ def fit_sslm(self, sstats):
if model == "DIM":
bound = self.compute_bound_fixed(sstats, totals)

logger.info("initial sslm bound is ", bound)
logger.info("initial sslm bound is %f", bound)

while converged > sslm_fit_threshold and iter_ < sslm_max_iter:
iter_ += 1
Expand All @@ -597,7 +644,7 @@ def fit_sslm(self, sstats):
bound = self.compute_bound_fixed(sstats, totals)

converged = numpy.fabs((bound - old_bound) / old_bound)
logger.info(iter_, " iteration lda seq bound is ", bound, " convergence is", converged)
logger.info("iteration %i iteration lda seq bound is %f convergence is %f", iter_, bound, converged)

self.e_log_prob = self.compute_expected_log_prob()
return bound
Expand Down
44 changes: 44 additions & 0 deletions gensim/models/wrappers/dtmmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,47 @@ def show_topic(self, topicid, time, num_words=50):
def print_topic(self, topicid, time, num_words=10):
"""Return the given topic, formatted as a string."""
return ' + '.join(['%.3f*%s' % v for v in self.show_topic(topicid, time, num_words)])

def DTMvis(self, corpus, time):
"""
returns term_frequency, vocab, doc_lengths, topic-term distributions and doc_topic distributions, specified by pyLDAvis format.
all of these are needed to visualise topics for DTM for a particular time-slice via pyLDAvis.
input parameter is the year to do the visualisation.
"""
topic_term = self.lambda_[:,:,time]
topic_term = np.exp(topic_term)
topic_term = topic_term / topic_term.sum()
topic_term = topic_term * self.num_topics

doc_topic = self.gamma_

term_frequency = [0] * self.num_terms
doc_lengths = []
for doc_num, doc in enumerate(corpus):
doc_lengths.append(len(doc))
for pair in doc:
term_frequency[pair[0]] += pair[1]

vocab = []
for i in range(0, len(self.id2word)):
vocab.append(self.id2word[i])
# returns numpy arrays for doc_topic proportions, topic_term proportions, and document_lengths, term_frequency.
# these should be passed to the `pyLDAvis.prepare` method to visualise one time-slice of DTM topics.
return doc_topic, topic_term, doc_lengths, term_frequency, vocab

def DTMcoherence(self, time, num_words=20):
Copy link
Owner

Choose a reason for hiding this comment

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

PEP8: functions in python start in lower case (dtm_coherence).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will change.

"""
returns all topics of a particular time-slice without probabilitiy values for it to be used
for either "u_mass" or "c_v" coherence.
TODO: because of print format right now can only return for 1st time-slice.
should we fix the coherence printing or make changes to the print statements to mirror DTM python?
"""
coherence_topics = []
for topic_no in range(0, self.num_topics):
topic = self.show_topic(topicid=topic_no, time=time, num_words=num_words)
coherence_topic = []
for prob, word in topic:
Copy link
Owner

Choose a reason for hiding this comment

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

List comprehension more Pythonic (and performant). This is C-style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah will fix!

coherence_topic.append(word)
coherence_topics.append(coherence_topic)

return coherence_topics