-
Notifications
You must be signed in to change notification settings - Fork 256
Unify constraint handling for AutoContinuous
, AutoDelta
, AutoNormal
.
#2015
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
Open
tillahoffmann
wants to merge
5
commits into
pyro-ppl:master
Choose a base branch
from
tillahoffmann:autosample
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c19962b
Note that `initialize_model` returns unconstrained parameters.
tillahoffmann 48dd429
Unify constraint handling for `AutoContinuous`, `AutoDelta`, `AutoNor…
tillahoffmann bb58d77
Fix `event_dim` for `AutoDelta` for guide subsampling and add test.
tillahoffmann d861328
Log `stdout` and `stderr` for failed examples.
tillahoffmann 8be07a1
Halve default number of hidden factors in `neutra` example.
tillahoffmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -339,6 +339,28 @@ def quantiles(self, params, quantiles): | |
return result | ||
|
||
|
||
def _maybe_constrain_dist_for_site( | ||
site: dict, base_distribution: dist.Distribution | ||
) -> dist.Distribution: | ||
support = site["fn"].support | ||
|
||
# Short-circuit if the support is real and return the base distribution with the | ||
# correct number of event dimensions. | ||
base_support = support | ||
while isinstance(base_support, constraints.independent): | ||
base_support = base_support.base_constraint | ||
if base_support is constraints.real: | ||
if support.event_dim: | ||
return base_distribution.to_event(support.event_dim) | ||
else: | ||
return base_distribution | ||
|
||
# Transform the distribution to the support of the site. | ||
with helpful_support_errors(site): | ||
transform = biject_to(support) | ||
return dist.TransformedDistribution(base_distribution, transform) | ||
|
||
|
||
class AutoNormal(AutoGuide): | ||
""" | ||
This implementation of :class:`AutoGuide` uses Normal distributions | ||
|
@@ -431,18 +453,11 @@ def __call__(self, *args, **kwargs): | |
constraint=self.scale_constraint, | ||
event_dim=event_dim, | ||
) | ||
|
||
site_fn = dist.Normal(site_loc, site_scale).to_event(event_dim) | ||
if site["fn"].support is constraints.real or ( | ||
isinstance(site["fn"].support, constraints.independent) | ||
and site["fn"].support.base_constraint is constraints.real | ||
): | ||
result[name] = numpyro.sample(name, site_fn) | ||
else: | ||
with helpful_support_errors(site): | ||
transform = biject_to(site["fn"].support) | ||
guide_dist = dist.TransformedDistribution(site_fn, transform) | ||
result[name] = numpyro.sample(name, guide_dist) | ||
unconstrained_dist = dist.Normal(site_loc, site_scale) | ||
constrained_dist = _maybe_constrain_dist_for_site( | ||
site, unconstrained_dist | ||
) | ||
result[name] = numpyro.sample(name, constrained_dist) | ||
|
||
return result | ||
|
||
|
@@ -528,17 +543,15 @@ def __init__( | |
|
||
def _setup_prototype(self, *args, **kwargs): | ||
super()._setup_prototype(*args, **kwargs) | ||
with numpyro.handlers.block(): | ||
self._init_locs = { | ||
k: v | ||
for k, v in self._postprocess_fn(self._init_locs).items() | ||
if k in self._init_locs | ||
} | ||
for name, site in self.prototype_trace.items(): | ||
if site["type"] != "sample" or site["is_observed"]: | ||
continue | ||
|
||
event_dim = site["fn"].event_dim | ||
event_dim = ( | ||
site["fn"].event_dim | ||
+ jnp.ndim(self._init_locs[name]) | ||
- jnp.ndim(site["value"]) | ||
) | ||
self._event_dims[name] = event_dim | ||
|
||
# If subsampling, repeat init_value to full size. | ||
|
@@ -561,26 +574,25 @@ def __call__(self, *args, **kwargs): | |
if site["type"] != "sample" or site["is_observed"]: | ||
continue | ||
|
||
event_dim = self._event_dims[name] | ||
init_loc = self._init_locs[name] | ||
event_dim = self._event_dims[name] | ||
with ExitStack() as stack: | ||
for frame in site["cond_indep_stack"]: | ||
stack.enter_context(plates[frame.name]) | ||
|
||
site_loc = numpyro.param( | ||
"{}_{}_loc".format(name, self.prefix), | ||
init_loc, | ||
constraint=site["fn"].support, | ||
event_dim=event_dim, | ||
f"{name}_{self.prefix}_loc", init_loc, event_dim=event_dim | ||
) | ||
|
||
site_fn = dist.Delta(site_loc).to_event(event_dim) | ||
result[name] = numpyro.sample(name, site_fn) | ||
unconstrained_dist = dist.Delta(site_loc) | ||
constrained_dist = _maybe_constrain_dist_for_site( | ||
site, unconstrained_dist | ||
) | ||
result[name] = numpyro.sample(name, constrained_dist) | ||
|
||
return result | ||
|
||
def sample_posterior(self, rng_key, params, *args, sample_shape=(), **kwargs): | ||
locs = {k: params["{}_{}_loc".format(k, self.prefix)] for k in self._init_locs} | ||
locs = self.median(params) | ||
latent_samples = { | ||
k: jnp.broadcast_to(v, sample_shape + jnp.shape(v)) for k, v in locs.items() | ||
} | ||
|
@@ -600,7 +612,11 @@ def sample_posterior(self, rng_key, params, *args, sample_shape=(), **kwargs): | |
return {**latent_samples, **deterministic_samples} | ||
|
||
def median(self, params): | ||
locs = {k: params["{}_{}_loc".format(k, self.prefix)] for k in self._init_locs} | ||
locs = {} | ||
for name in self._init_locs: | ||
unconstrained = params[f"{name}_{self.prefix}_loc"] | ||
transform = biject_to(self.prototype_trace[name]["fn"].support) | ||
locs[name] = transform(unconstrained) | ||
return locs | ||
|
||
|
||
|
@@ -708,26 +724,11 @@ def __call__(self, *args, **kwargs): | |
|
||
# unpack continuous latent samples | ||
result = {} | ||
|
||
for name, unconstrained_value in self._unpack_latent(latent).items(): | ||
site = self.prototype_trace[name] | ||
with helpful_support_errors(site): | ||
transform = biject_to(site["fn"].support) | ||
value = transform(unconstrained_value) | ||
event_ndim = site["fn"].event_dim | ||
if numpyro.get_mask() is False: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need this logic to save computation for prediction. |
||
log_density = 0.0 | ||
else: | ||
log_density = -transform.log_abs_det_jacobian( | ||
unconstrained_value, value | ||
) | ||
log_density = sum_rightmost( | ||
log_density, jnp.ndim(log_density) - jnp.ndim(value) + event_ndim | ||
) | ||
delta_dist = dist.Delta( | ||
value, log_density=log_density, event_dim=event_ndim | ||
) | ||
result[name] = numpyro.sample(name, delta_dist) | ||
unconstrained_dist = dist.Delta(unconstrained_value) | ||
constrained_dist = _maybe_constrain_dist_for_site(site, unconstrained_dist) | ||
result[name] = numpyro.sample(name, constrained_dist) | ||
|
||
return result | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you are finding the MAP point in unconstrained space. This class gets MAP point in constrained space.