-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix: make importance resampling robust to importance samples with weight log(0) #68
Fix: make importance resampling robust to importance samples with weight log(0) #68
Conversation
@sritchie, sorry for the premature review request. The failing CLJS test makes sense to me and should be easy to fix. What's the Gen.clj-test-idiomatic way to test versions of dist that are compatible with both CLJ and CLJS? (Not sure I track why the linter is failing 🤷♂️) |
@Schaechtle in this case let's test with The linter's failing because Instead you could do (defn- neg-inf?
[v]
(= v ##-Inf)) which I believe will work in cljs as well. |
src/gen/inference/importance.cljc
Outdated
@@ -31,8 +35,8 @@ | |||
(let [candidate (gf/generate gf args observations) | |||
candidate-model-trace (:trace candidate) | |||
log-weight (:weight candidate)] | |||
(vswap! log-total-weight #(logsumexp [log-weight %])) | |||
(when (dist/bernoulli (math/exp (- log-weight @log-total-weight))) | |||
(when-not (neg-inf? log-weight) (vswap! log-total-weight #(logsumexp [log-weight %]))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about moving the lower when
into the when-not
block?
(when-not (neg-inf? log-weight)
(vswap! log-total-weight #(logsumexp [log-weight %]))
(when (dist/bernoulli (math/exp (- log-weight @log-total-weight)))
(vreset! model-trace candidate-model-trace)))
Also this makes me thing that dist/bernoulli
should really take the log-weight without running math/exp
on it again...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
4d2538e
to
67e382b
Compare
… in samples with weight 0 This crashed previously.
67e382b
to
e6b6fa6
Compare
@sritchie, thanks for the quick turnaround and the suggestions. All implemented.
maybe we add a |
Nice work! |
What does this do?
Fixes a bug in importance resampling. Before, importance resampling crashed if any samples had a weight = log(0). But that should be a fine case for importance resampling to handle. In this case, importance sampling should act like rejection sampling and never return those samples.
How was this tested?
I've added a test in a separate commit.