Skip to content

Don't include -np.inf in calculating average ELBO #1880

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

Merged
merged 2 commits into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions pymc3/variational/advi.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def advi(vars=None, start=None, model=None, n=5000, accurate_elbo=False,
if n < 10:
progress.set_description('ELBO = {:,.5g}'.format(elbos[i]))
elif i % (n // 10) == 0 and i > 0:
avg_elbo = elbos[i - n // 10:i].mean()
avg_elbo = infmean(elbos[i - n // 10:i])
progress.set_description('Average ELBO = {:,.5g}'.format(avg_elbo))

if i % eval_elbo == 0:
Expand Down Expand Up @@ -193,14 +193,14 @@ def advi(vars=None, start=None, model=None, n=5000, accurate_elbo=False,
pm._log.info('Interrupted at {:,d} [{:.0f}%]: ELBO = {:,.5g}'.format(
i, 100 * i // n, elbos[i]))
else:
avg_elbo = elbos[i - n // 10:i].mean()
avg_elbo = infmean(elbos[i - n // 10:i])
pm._log.info('Interrupted at {:,d} [{:.0f}%]: Average ELBO = {:,.5g}'.format(
i, 100 * i // n, avg_elbo))
else:
if n < 10:
pm._log.info('Finished [100%]: ELBO = {:,.5g}'.format(elbos[-1]))
else:
avg_elbo = elbos[-n // 10:].mean()
avg_elbo = infmean(elbos[-n // 10:])
pm._log.info('Finished [100%]: Average ELBO = {:,.5g}'.format(avg_elbo))
finally:
progress.close()
Expand Down Expand Up @@ -410,3 +410,8 @@ def rvs(x):
trace.record(point)

return MultiTrace([trace])


def infmean(input_array):
"""Return the mean of the finite values of the array"""
return np.mean(np.asarray(input_array)[np.isfinite(input_array)])
4 changes: 2 additions & 2 deletions pymc3/variational/advi_minibatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import pymc3 as pm
from pymc3.theanof import reshape_t, inputvars, floatX
from .advi import ADVIFit, adagrad_optimizer, gen_random_state
from .advi import ADVIFit, adagrad_optimizer, gen_random_state, infmean

__all__ = ['advi_minibatch']

Expand Down Expand Up @@ -529,7 +529,7 @@ def is_shared(t):
if n < 10:
progress.set_description('ELBO = {:,.2f}'.format(elbos[i]))
elif i % (n // 10) == 0 and i > 0:
avg_elbo = elbos[i - n // 10:i].mean()
avg_elbo = infmean(elbos[i - n // 10:i])
progress.set_description('Average ELBO = {:,.2f}'.format(avg_elbo))

pm._log.info('Finished minibatch ADVI: ELBO = {:,.2f}'.format(elbos[-1]))
Expand Down