Skip to content

Scan doesn't work with replay #1685

Closed
Closed

Description

@deoxyribose

Given a model adapted from the scan doctest:

def gaussian_hmm(y=None, T=T, N=N):
    def transition(x_prev, y_curr):
        with numpyro.plate('data', N):
            x_curr = numpyro.sample('x', dist.Normal(x_prev, 1.5))
            y_curr = numpyro.sample('y', dist.Normal(x_curr, 0.3), obs=y_curr)
        return x_curr, (x_curr, y_curr)
    with numpyro.plate('data', N):
        x0 = numpyro.sample('x_0', dist.Normal(jnp.zeros(N), 5.))
    _, (x, y) = scan(transition, x0, y, length=T)
    return (x, y)
    
with numpyro.handlers.seed(rng_seed=0):
    x, y = gaussian_hmm()

svi = SVI(gaussian_hmm, custom_guide, Adam(0.1), Trace_ELBO(num_particles=1), y=y, T=T, N=N)

n_steps = 5000
results = svi.run(random.PRNGKey(0), n_steps)

HMC gives the expected results. SVI runs without errors, but gives bad results, because the ELBO is not computed correctly. The scan operator blocks all handlers except substitute and condition, including the replay effect handler used in ELBO.

A preliminary implementation can be seen here master...aleatory-science:numpyro:master, which uses a similar logic to the way substitute is implemented for scan, and reuses the substitute_stack to store the replay trace.

Activity

  1. changed the title [-]Scan doesn[/-] [+]Scan doesn't work with replay[/+] on Nov 24, 2023
  2. added
    enhancement
    on Nov 28, 2023
  3. deoxyribose commented on Nov 30, 2023

    @deoxyribose
    ContributorAuthor

    I could make a PR as it is, but note that scan_enum hasn't been correspondingly edited (if it needs to be), and I'm not sure if and how the implementation otherwise needs to changed, or tested. @fehiepsi do you want to review what's there now, or is there something else I can add first?

  4. fehiepsi commented on Dec 3, 2023

    @fehiepsi
    Member

    I think scan enum is quite complicated to support. It's better to only support scan for Trace_ELBO in your PR.

  5. deoxyribose commented on Dec 4, 2023

    @deoxyribose
    ContributorAuthor

    Cool, I've submitted it at #1693

  6. fehiepsi commented on Jan 24, 2024

    @fehiepsi
    Member

    Thanks @deoxyribose !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions