Skip to content

Add analytic single-sample test for ELBO gradient estimators#2894

Merged
fritzo merged 7 commits into
pyro-ppl:devfrom
ordabayevy:fix-traceelbo-grad
Jul 5, 2021
Merged

Add analytic single-sample test for ELBO gradient estimators#2894
fritzo merged 7 commits into
pyro-ppl:devfrom
ordabayevy:fix-traceelbo-grad

Conversation

@ordabayevy
Copy link
Copy Markdown
Member

This test compares analytically derived pathwise and score function gradient estimators for a single particle to gradients obtained by Trace_ELBO and TraceEnum_ELBO. When reparameterization is disabled gradients obtained by Trace_ELBO don't match analytically derived score function gradients.

By comparing gradient values it seems like grads obtained by Trace_ELBO are missing a second term (delbo_dscale and delbo_dloc) in the derivative of a surrogate loss:

expected_grads = {
    "scale": -(dlogq_dscale * elbo + delbo_dscale).sum(0, keepdim=True),
    "loc": -(dlogq_dloc * elbo + delbo_dloc),
}
# actual_grads are missing delbo_dscale term in "scale" and delbo_dloc term in "loc"

On the other hand, TraceEnum_ELBO passes all tests.

@ordabayevy ordabayevy added the bug label Jul 4, 2021

elbo = Elbo(
max_plate_nesting=1, # set this to ensure rng agrees across runs
num_particles=1,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

From this test alone it is unclear whether there is a bug in stochastic gradient estimation; rather it is clear only that Trace_ELBO disagrees with a particular hand-coded estimator (at a single sample). That is, it still seems possible that Trace_ELBO might agree with your hand-coded estimator in expectation, but disagree at each sample. To confirm this is a true inference bug, we could also try setting num_particles=100000, vectorize_particles=True and see whether the expected gradients coming out of Trace_ELBO and TraceEnum_ELBO agree. WDYT?

Copy link
Copy Markdown
Member Author

@ordabayevy ordabayevy Jul 4, 2021

Choose a reason for hiding this comment

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

You are right! The missing term is a score function and its expectation is zero (which is cleverly removed by Trace_ELBO). So the expected gradients of Trace_ELBO and TraceEnum_ELBO agree (as tested in test_subsample_gradient).

I fixed the hand-coded formula for Trace_ELBO so the test should pass now. @fritzo do you want to add the test to the repo? If not, I'll close the PR.

@fritzo fritzo added this to the 1.7 release milestone Jul 4, 2021
@ordabayevy ordabayevy added question and removed bug labels Jul 4, 2021
@fritzo fritzo added the testing label Jul 5, 2021
@fritzo fritzo changed the title Score function gradient estimator by Trace_ELBO is missing a term Add analytic single-sample test for ELBO gradient estimators Jul 5, 2021
@fritzo fritzo merged commit 22a9396 into pyro-ppl:dev Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants