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

SVI shared parameter initialization #1553

Merged
merged 2 commits into from
Mar 8, 2023
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
14 changes: 10 additions & 4 deletions numpyro/infer/svi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from numpyro.distributions import constraints
from numpyro.distributions.transforms import biject_to
from numpyro.handlers import replay, seed, trace
from numpyro.handlers import replay, seed, substitute, trace
from numpyro.infer.util import helpful_support_errors, transform_fn
from numpyro.optim import _NumPyroOptim, optax_to_numpyro

Expand Down Expand Up @@ -184,9 +184,15 @@ def init(self, rng_key, *args, **kwargs):
model_init = seed(self.model, model_seed)
guide_init = seed(self.guide, guide_seed)
guide_trace = trace(guide_init).get_trace(*args, **kwargs, **self.static_kwargs)
model_trace = trace(replay(model_init, guide_trace)).get_trace(
*args, **kwargs, **self.static_kwargs
)
init_guide_params = {
name: site["value"]
for name, site in guide_trace.items()
if site["type"] == "param"
}
model_trace = trace(
substitute(replay(model_init, guide_trace), init_guide_params)
).get_trace(*args, **kwargs, **self.static_kwargs)

params = {}
inv_transforms = {}
mutable_state = {}
Expand Down
34 changes: 34 additions & 0 deletions test/infer/test_svi.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,40 @@ def guide():
assert_allclose(actual_loss, expected_loss, rtol=1e-6)


def test_shared_param_init():
shared_init = 1.0

def model():
# should receive initial value from guide when used in SVI
shared = numpyro.param("shared")
assert_allclose(shared, shared_init)

def guide():
numpyro.param("shared", lambda _: shared_init)

svi = SVI(model, guide, optim.Adam(0.01), Trace_ELBO())
svi_state = svi.init(random.PRNGKey(0))
params = svi.get_params(svi_state)
# make sure the correct init ended up in the SVI state
assert_allclose(params["shared"], shared_init)


def test_shared_param():
target_value = 5.0

def model():
shared = numpyro.param("shared")
# drive the shared parameter toward a target value
numpyro.factor("neg_loss", -((shared - target_value) ** 2))

def guide():
numpyro.param("shared", 1.0)

svi = SVI(model, guide, optim.Adam(0.01), Trace_ELBO())
svi_result = svi.run(random.PRNGKey(0), 1000)
assert_allclose(svi_result.params["shared"], target_value, atol=0.1)


def test_elbo_dynamic_support():
x_prior = dist.TransformedDistribution(
dist.Normal(),
Expand Down