Repository navigation
Add alpha to the existing stop-gradient decorator - #285
Conversation
|
Hi, thanks for the PR Given this generalizes the stop_gradient_decorator fully, I think we should avoid adding a new function and simply modify the current one by adding a parameter alpha to (defaulting to the old behaviour which is alpha=0. Similarly, there's no need to create new examples, and you can document the behaviour of alpha in the existing autodiff example rather than create a new example altogether. The same can be said about unittesting, which can be done in the same place as the test for the old non-discounted version. I don't mind you using AI for the code, but I would appreciate if you could avoid using AI to generate text for documentation and examples: these parts are supposed be readable by a human naturally, and at the moment, it's just LLM slang that's very hard to parse. Additionally, please avoid the automatic hardwrapping of prose text by Claude in the example markdown, it makes it unreadable. |
|
Thanks, Adrien. I added I used |
| Returns: | ||
| A Resampling function implementing stop gradient resampling. | ||
| """ | ||
| if isinstance(alpha, complex) or not math.isfinite(alpha) or not 0 <= alpha <= 1: |
There was a problem hiding this comment.
This cannot be parsed by JAX at run time. Simply use jnp.clip and let JAX fail and return its own message if the object is not a scalar.
There was a problem hiding this comment.
I switched to jnp.clip. The scalar check also uses JAX now, so alpha can be passed at runtime under jit.
| resampling_fn = adaptive.ess_decorator(resampling_fn, ess_threshold) | ||
| ``` | ||
|
|
||
| With the default `alpha=1`, Cuthbert applies the full gradient correction when it resamples. In [DMOP](https://arxiv.org/abs/2407.03085v2), `alpha` controls the bias–variance trade-off in the gradient estimates. |
There was a problem hiding this comment.
@SamDuffield have we settled on how we do references? I don't think we have. I'm not a big fan of hyperlinks like these. TBC
There was a problem hiding this comment.
Yeah we haven't got any unified approach to references. This is how we've been doing it in other examples so I'm fine with this here
|
|
||
| With the default `alpha=1`, Cuthbert applies the full gradient correction when it resamples. In [DMOP](https://arxiv.org/abs/2407.03085v2), `alpha` controls the bias–variance trade-off in the gradient estimates. | ||
|
|
||
| The DMOP analysis assumes resampling at every observation, while this example resamples only when ESS falls below 70% of the particle count. |
There was a problem hiding this comment.
(specifically the second paragraph)
There was a problem hiding this comment.
I removed that paragraph.
| @@ -8,36 +8,51 @@ | |||
| See [Scibior and Wood (2021)](https://arxiv.org/abs/2106.10314) for more details. | |||
There was a problem hiding this comment.
Probably add the reference here too
There was a problem hiding this comment.
I added the Tan et al. reference to the module docstring.
| Stop gradient resampling decorator. | ||
|
|
||
| This wrapper will call the provided resampling function, and then apply | ||
| the stop gradient trick of [Scibior and Wood (2021)](https://arxiv.org/abs/2106.10314). |
There was a problem hiding this comment.
Don't forget to reference the paper
There was a problem hiding this comment.
I added the citation where the wrapper describes the discount.
| log-likelihood with respect to model parameters) are unbiased, | ||
| corresponding to the classical Fisher estimate. | ||
| corresponding to the classical Fisher estimate. Values below 1 discount | ||
| the log-weight correction at each resampling step. |
There was a problem hiding this comment.
I clarified that alpha=0 removes the log-weight correction. Gradients still pass through the selected particle states.
There was a problem hiding this comment.
Main issue here is that you just didn't illustrate the new method at all. Ideally, the file should have been edited to show a similar figure, with the data and model here, as for Figure 1 in https://arxiv.org/abs/2407.03085v2
There was a problem hiding this comment.
I added a figure to the existing example showing how the bias and variance of the score estimates for log q and log r change with alpha.
There was a problem hiding this comment.
Can you make its background transparent please?
There was a problem hiding this comment.
Once done happy to approve
There was a problem hiding this comment.
Thanks, Adrien. I’ve updated the figure with a transparent background.
|
|
||
| @parameterized.parameters(-0.1, 1.1, float("nan")) | ||
| def test_invalid_discount(self, alpha): | ||
| with self.assertRaises(ValueError): |
There was a problem hiding this comment.
This would fail to raise for compiled code.
There was a problem hiding this comment.
or maybe it would but for the wrong reason. See comment there
There was a problem hiding this comment.
I replaced that test with a jax.jit test. It checks the gradient correction for alpha=-0.1, 0.5 and 1.1, and checks that JAX rejects a vector.
|
Happy to merge. @SamDuffield anything on your end? |
Nope, looks great! |
This adds an optional
alphaargument tostop_gradient_decorator. The default of1preserves the original behavior. Smaller values reduce the contribution of the selected log weights to the gradient at resampling.The existing autodiff example shows how to use
alpha. The resampling tests check that changingalphapreserves the forward results and scales the gradient correction as expected.The code and documentation were prepared with AI assistance.