-
Notifications
You must be signed in to change notification settings - Fork 246
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
Validate the model when cannot find valid initial params. #733
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,6 +427,21 @@ def initialize_model(rng_key, model, | |
|
||
if not_jax_tracer(is_valid): | ||
if device_get(~jnp.all(is_valid)): | ||
with numpyro.validation_enabled(), trace() as tr: | ||
# validate parameters | ||
substituted_model(*model_args, **model_kwargs) | ||
# validate values | ||
for site in tr.values(): | ||
if site['type'] == 'sample': | ||
with warnings.catch_warnings(record=True) as ws: | ||
site['fn']._validate_sample(site['value']) | ||
if len(ws) > 0: | ||
for w in ws: | ||
# at site information to the warning message | ||
w.message.args = ("Site {}: {}".format(site["name"], w.message.args[0]),) \ | ||
+ w.message.args[1:] | ||
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. What does a sample warning message look like? 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. Thanks for reviewing, @neerajprad! I'm a bit busy today but will run this again to display the warning message tomorrow. Here I just want to add site information to the warning message, because not many users know how to use 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. @neerajprad the following model import numpyro
import numpy as np
def model():
x = numpyro.sample("x", numpyro.distributions.Normal())
numpyro.sample("obs", numpyro.distributions.Normal(x), obs=float('nan'))
mcmc = numpyro.infer.MCMC(numpyro.infer.NUTS(model), 10, 10)
mcmc.run(np.array([0, 0], dtype='uint32')) gives the warning
|
||
warnings.showwarning(w.message, w.category, w.filename, w.lineno, | ||
file=w.file, line=w.line) | ||
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.") | ||
return ModelInfo(ParamInfo(init_params, pe, grad), potential_fn, postprocess_fn, model_trace) | ||
|
||
|
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.
A more informative warning / error message is definitely needed. I am thinking that can we simply run
initialize_model
withvalidation_enabled
(I wouldn't expect that to add any material overhead)? Is the resulting warning message not informative enough?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.
Yes, it is simpler (I also don't worry about the overhead) but there are two issues with that:
What do you think?
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.
Thanks for explaining, @fehiepsi, both of your points make a lot of sense.