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 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
33 changes: 27 additions & 6 deletions gensim/models/ldamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,37 @@
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):
"""Log of sum of exponentials

Parameters
----------
x : array_like
Input data

Returns
-------
float
log of sum of exponentials of elements in `x`

Notes
-----
for performance, does not support NaNs or > 1d arrays like
scipy.special.logsumexp()

"""

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