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

add namespace feature? #593

Closed
dsheldon opened this issue May 9, 2020 · 2 comments · Fixed by #642
Closed

add namespace feature? #593

dsheldon opened this issue May 9, 2020 · 2 comments · Fixed by #642
Labels
enhancement New feature or request

Comments

@dsheldon
Copy link
Contributor

dsheldon commented May 9, 2020

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:

def BrownianBridge(loc1=0., loc2=0., scale=1., num_steps=100):
    
    # Get rw with num_steps-1 random increments
    rw = numpyro.sample("rw", dist.GaussianRandomWalk(num_steps=num_steps-1))

    # Shift/scale and append first location
    x = np.append(loc1, loc1 + rw*scale)

    # Now adjust to end at loc2
    t = np.linspace(0., 1., num_steps)
    x += (loc2 - x[-1])*t

    return x

def model():
    with numpyro.handlers.namespace("a"):
        a = BrownianBridge(0., 5.)
    
    with numpyro.handlers.namespace("b"):
        b = BrownianBridge(1, 10)

    return a, b

with numpyro.handlers.seed(rng_seed=1):
    trace = numpyro.handlers.trace(model).get_trace()
    
print(trace['a::rw']['value'])
print(trace['b::rw']['value'])

And the handler:

class namespace(Messenger):
    def __init__(self, name):
        self.name = name

    def process_message(self, msg):
        if msg.get('name'):
            msg['name'] = self.name + '::' + msg['name']

What do you think? Is there an existing way or better way to handle this? Would it break something else?

@fehiepsi
Copy link
Member

fehiepsi commented May 9, 2020

This looks like scope handler in Pyro. I think it would be great to have. 👍

cc @eb8680

@fehiepsi fehiepsi added the enhancement New feature or request label May 9, 2020
@eb8680
Copy link
Member

eb8680 commented May 10, 2020

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.

This was referenced Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants