You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've found myself writing small model components that include numpyro.sample statements and are reused in several places within a model. To avoid duplicate names I need to pass unique prefixes or suffixes into the components.
I had the idea that this might be more cleanly handled with a namespace handler. Here's an example of what I'm thinking:
defBrownianBridge(loc1=0., loc2=0., scale=1., num_steps=100):
# Get rw with num_steps-1 random incrementsrw=numpyro.sample("rw", dist.GaussianRandomWalk(num_steps=num_steps-1))
# Shift/scale and append first locationx=np.append(loc1, loc1+rw*scale)
# Now adjust to end at loc2t=np.linspace(0., 1., num_steps)
x+= (loc2-x[-1])*treturnxdefmodel():
withnumpyro.handlers.namespace("a"):
a=BrownianBridge(0., 5.)
withnumpyro.handlers.namespace("b"):
b=BrownianBridge(1, 10)
returna, bwithnumpyro.handlers.seed(rng_seed=1):
trace=numpyro.handlers.trace(model).get_trace()
print(trace['a::rw']['value'])
print(trace['b::rw']['value'])
I think @dsheldon's namespace implementation is fine. There's some extra weirdness in pyro.contrib.autoname.scope involving adding a counter suffix to sites and namespaces that appear more than once that hasn't proven to be all that useful in hindsight.
I've found myself writing small model components that include
numpyro.sample
statements and are reused in several places within a model. To avoid duplicate names I need to pass unique prefixes or suffixes into the components.I had the idea that this might be more cleanly handled with a
namespace
handler. Here's an example of what I'm thinking:And the handler:
What do you think? Is there an existing way or better way to handle this? Would it break something else?
The text was updated successfully, but these errors were encountered: