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

inline a barebones version of logsumexp for improved performance #1745

Merged
merged 3 commits into from
Nov 30, 2017
Merged
Changes from 1 commit
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
24 changes: 18 additions & 6 deletions gensim/models/ldamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,28 @@
from gensim.models import basemodel, CoherenceModel
from gensim.models.callbacks import Callback

# log(sum(exp(x))) that tries to avoid overflow
try:
from scipy.special import logsumexp
except ImportError:
from scipy.misc import logsumexp


logger = logging.getLogger('gensim.models.ldamodel')


def logsumexp(x):
"""
barebones log-sum-exp that tries to avoid overflows
Copy link
Contributor

Choose a reason for hiding this comment

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


Args:
x: 1d ndarray

Note:
does not support NaNs

"""
x_max = np.max(x)
x = np.log(np.sum(np.exp(x - x_max)))
x += x_max

return x


def update_dir_prior(prior, N, logphat, rho):
"""
Updates a given prior using Newton's method, described in
Expand Down