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

[Merged by Bors] - Fix initialization of parameters for algorithms that use SampleFromUniform #232

Closed
wants to merge 2 commits into from

Conversation

devmotion
Copy link
Member

This PR is a quick fix for TuringLang/Turing.jl#1563 and TuringLang/Turing.jl#1588.

As explained in TuringLang/Turing.jl#1588 (comment), the problem is that currently SampleFromUniform always resamples variables in every run, and hence also initial parameters provided by users are resampled in

model(rng, vi, _spl)
.

As mentioned in TuringLang/Turing.jl#1588 (comment), a better long term solution would be to fix this inconsistency and use dedicated evaluation and sampling contexts, as suggested in #80.

Comment on lines +80 to +88
# TODO: fix properly by using sampler and evaluation contexts
# This is a quick fix for https://github.com/TuringLang/Turing.jl/issues/1588
# and https://github.com/TuringLang/Turing.jl/issues/1563
# to avoid that existing variables are resampled
if _spl isa SampleFromUniform
model(rng, vi, SampleFromPrior())
else
model(rng, vi, _spl)
end
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better to maybe remove the check if it's SampleFromUniform here:

if spl isa SampleFromUniform || is_flagged(vi, vn, "del")

And instead we just provide a "resample!" or something with similar behavior as set_and_resample!(vi, ())?

Copy link
Member

Choose a reason for hiding this comment

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

Or will it be difficult to understand the downstream effects this might have?

Copy link
Member

Choose a reason for hiding this comment

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

In that case we could even allow init_params to be a NamedTuple and use set_and_resample! instead of initialize_parameters!.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think probably it will break downstream code if the sampler logic is changed. That's one motivation for temporarily introducing this workaround. The second motivation is that actually on the contrary it has been discussed to resample always when using a sampler, i.e., instead of not sampling with SampleFromUniform one would rather sample always also with e.g. SampleFromPrior. IMO this seems reasonable, in particular when one uses separate sampling and evaluation contexts since then it would be much clearer when the variables are affected by rerunning the model (and one could still avoid resampling).

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I get that; I just realized that what I mentioned above would also allow the use of NamedTuple as init_params which might be way more convenient for end-users. But maybe that's best resolved in a different PR once we have introduced the contexts you mentioned 👍

Copy link
Member

@torfjelde torfjelde left a comment

Choose a reason for hiding this comment

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

LGTM!

@torfjelde
Copy link
Member

bors try

bors bot added a commit that referenced this pull request Apr 17, 2021
@devmotion
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Apr 18, 2021
…niform` (#232)

This PR is a quick fix for TuringLang/Turing.jl#1563 and TuringLang/Turing.jl#1588.

As explained in TuringLang/Turing.jl#1588 (comment), the problem is that currently `SampleFromUniform` always resamples variables in every run, and hence also initial parameters provided by users are resampled in https://github.com/TuringLang/DynamicPPL.jl/blob/9d4137eb33e83f34c484bf78f9a57f828b3c92a0/src/sampler.jl#L80.

As mentioned in TuringLang/Turing.jl#1588 (comment), a better long term solution would be to fix this inconsistency and use dedicated evaluation and sampling contexts, as suggested in #80.
@bors bors bot changed the title Fix initialization of parameters for algorithms that use SampleFromUniform [Merged by Bors] - Fix initialization of parameters for algorithms that use SampleFromUniform Apr 18, 2021
@bors bors bot closed this Apr 18, 2021
@bors bors bot deleted the dw/initparams_uniform branch April 18, 2021 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants