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

Fixes random_flax_module with flax.linen.BatchNorm #1823

Merged
merged 8 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion numpyro/infer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def initialize_model(
data={
k: site["value"]
for k, site in model_trace.items()
if site["type"] in ["param"]
if site["type"] in ["param", "mutable"]
juanitorduz marked this conversation as resolved.
Show resolved Hide resolved
},
)
constrained_values = {
Expand Down
17 changes: 14 additions & 3 deletions test/contrib/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
random_haiku_module,
)
import numpyro.distributions as dist
from numpyro.infer import MCMC, NUTS
from numpyro.infer import MCMC, NUTS, SVI, Trace_ELBO
from numpyro.infer.autoguide import AutoDelta

pytestmark = pytest.mark.filterwarnings(
"ignore:jax.tree_.+ is deprecated:FutureWarning"
Expand Down Expand Up @@ -242,7 +243,7 @@ def model():
nn = haiku_module("nn", transform(fn), apply_rng=dropout, input_shape=(4, 3))
x = numpyro.sample("x", dist.Normal(0, 1).expand([4, 3]).to_event(2))
if dropout:
y = nn(numpyro.prng_key(), x)
y = nn(random.PRNGKey(0), x)
else:
y = nn(x)
numpyro.deterministic("y", y)
Expand All @@ -256,6 +257,11 @@ def model():
else:
assert set(tr.keys()) == {"nn$params", "x", "y"}

# test svi
guide = AutoDelta(model)
svi = SVI(model, guide, numpyro.optim.Adam(0.01), Trace_ELBO())
svi.run(random.PRNGKey(100), 10)


@pytest.mark.parametrize("dropout", [True, False])
@pytest.mark.parametrize("batchnorm", [True, False])
Expand Down Expand Up @@ -287,7 +293,7 @@ def model():
)
x = numpyro.sample("x", dist.Normal(0, 1).expand([4, 3]).to_event(2))
if dropout:
y = net(x, rngs={"dropout": numpyro.prng_key()})
y = net(x, rngs={"dropout": random.PRNGKey(0)})
else:
y = net(x)
numpyro.deterministic("y", y)
Expand All @@ -300,3 +306,8 @@ def model():
assert tr["nn$state"]["type"] == "mutable"
else:
assert set(tr.keys()) == {"nn$params", "x", "y"}

# test svi
guide = AutoDelta(model)
svi = SVI(model, guide, numpyro.optim.Adam(0.01), Trace_ELBO())
svi.run(random.PRNGKey(100), 10)
Comment on lines +310 to +313
Copy link
Contributor Author

@juanitorduz juanitorduz Jun 30, 2024

Choose a reason for hiding this comment

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

Without these SVI lines, the tests were passing in the master branch. Once added, we got the error. The suggested fix in the issue does solve it.

Loading