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

Fix infinite diff in LdaModel.do_mstep #2344

Merged
merged 2 commits into from
Jan 28, 2019
Merged
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
26 changes: 19 additions & 7 deletions gensim/models/ldamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,19 @@ def __str__(self):
self.num_terms, self.num_topics, self.decay, self.chunksize
)

def sync_state(self):
"""Propagate the states topic probabilities to the inner object's attribute."""
self.expElogbeta = np.exp(self.state.get_Elogbeta())
def sync_state(self, current_Elogbeta=None):
"""Propagate the states topic probabilities to the inner object's attribute.

Parameters
----------
current_Elogbeta: numpy.ndarray
Posterior probabilities for each topic, optional.
If omitted, it will get Elogbeta from state.
"""

if current_Elogbeta is None:
menshikh-iv marked this conversation as resolved.
Show resolved Hide resolved
current_Elogbeta = self.state.get_Elogbeta()
self.expElogbeta = np.exp(current_Elogbeta)
assert self.expElogbeta.dtype == self.dtype

def clear(self):
Expand Down Expand Up @@ -1027,14 +1037,16 @@ def do_mstep(self, rho, other, extra_pass=False):
logger.debug("updating topics")
# update self with the new blend; also keep track of how much did
# the topics change through this update, to assess convergence
diff = np.log(self.expElogbeta)
previous_Elogbeta = self.state.get_Elogbeta()
menshikh-iv marked this conversation as resolved.
Show resolved Hide resolved
self.state.blend(rho, other)
diff -= self.state.get_Elogbeta()
self.sync_state()

current_Elogbeta = self.state.get_Elogbeta()
self.sync_state(current_Elogbeta)

# print out some debug info at the end of each EM iteration
self.print_topics(5)
logger.info("topic diff=%f, rho=%f", np.mean(np.abs(diff)), rho)
diff = mean_absolute_difference(previous_Elogbeta.ravel(), current_Elogbeta.ravel())
menshikh-iv marked this conversation as resolved.
Show resolved Hide resolved
logger.info("topic diff=%f, rho=%f", diff, rho)

if self.optimize_eta:
self.update_eta(self.state.get_lambda(), rho)
Expand Down