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

External samplers docs #403

Merged
merged 94 commits into from
Oct 19, 2023
Merged

External samplers docs #403

merged 94 commits into from
Oct 19, 2023

Conversation

JaimeRZP
Copy link
Member

@JaimeRZP JaimeRZP commented Jul 7, 2023

Docs for Turing's #2008.

yebai and others added 30 commits April 17, 2023 12:16
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…ents_abstractmcmc.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…ents_abstractmcmc.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
tutorials/15-external-samplers/external-samplers.jmd Outdated Show resolved Hide resolved
So far we have used to Turing's support for external samplers to go beyond the capabilities of the wrappers.
Now we want to use this support to employ a sampler not supported within Turing's ecosystem yet.
We will use the recently developped Micro-Cannoncial Hamiltonian Monte Carlo (MCHMC) sampler to showcase this.
MCHMC[^3,^4] ((MCHMC's GitHub)[https://github.com/JaimeRZP/MicroCanonicalHMC.jl]) is HMC sampler that conserves energy along the exploration of the phase space.
Copy link
Member

Choose a reason for hiding this comment

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

is HMC sampler that conserves energy along the exploration of the phase space.

What does this actually mean? For example, HMC preserves energy along the trajectory; is this the same or not?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not extactly. hopefully this plot adds clarity.
image
I have rephrased the sentences.

tutorials/15-external-samplers/external-samplers.jmd Outdated Show resolved Hide resolved
As previously stated, in order to use external sampling libraries within `Turing` they must follow the `AbstractMCMC` API.
In this section we will briefly dwell in what this entails.
First and foremost, the inference method must be of the type `typeof(spl)<:AbstractMCMC.AbstractSampler`.
Second, the stepping function of the MCMC algorithm must be made into instances of `AbstractMCMC.step` and follow the structure below:
Copy link
Member

Choose a reason for hiding this comment

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

Usage of "instance" here is somewhat confusing; "instance" in this context usually refers to the instance of some type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Rephrased!

tutorials/15-external-samplers/external-samplers.jmd Outdated Show resolved Hide resolved
The last requirement is that the transition must be structure with a field named `θ` which constains the values of the parameters of the model for said transition.
This allows `Turing` to seemlessly extract the parameter values at each step of the chain when bundling the chains.

For a practical example of how to adapt a sampling library to the `AbstractMCMC` interface the readers can consult the following (code)[https://github.com/JaimeRZP/MicroCanonicalHMC.jl/blob/master/src/abstractmcmc.jl] within `MicroCanonicalHMC`.
Copy link
Member

Choose a reason for hiding this comment

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

Is this really the best example of an implementation of the interface?

Looking at the code, most of the complexity seems to be hidden in your Step type, and so it the AbstractMCMC.step function seems almost "useless", no?

Of course some implementations of the interface will indeed be like this, but it doesn't seem like the greatest example of how these things are usually done?

For example, something like https://github.com/TuringLang/AdvancedMH.jl/blob/458a602ac32a8514a117d4c671396a9ba8acbdab/src/mh-core.jl#L73-L115 is IMO a slightly better example.

https://github.com/TuringLang/AdvancedHMC.jl/blob/762e55f894d142495a41a6eba0eed9201da0a600/src/abstractmcmc.jl#L102-L170 might be even better because it demonstrates the difference between transition and state nicely.

But IMO, we should just have a simple example of how to implement something like a MH algorithm in the docs of AbstractMCMC; that would be ideal 😬

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 turned this into a list of examples.

Comment on lines 157 to 158
The last requirement is that the transition must be structure with a field named `θ` which constains the values of the parameters of the model for said transition.
This allows `Turing` to seemlessly extract the parameter values at each step of the chain when bundling the chains.
Copy link
Member

Choose a reason for hiding this comment

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

This part is still something I'm not too keen on. Basically, the current way of converting a transition to something that bundle_samples can handle is really not great and I don't expect anyone outside of Turing.jl to implement this 😕

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 for the time being it is quite an important consideration for devs who want to make the connection as seemless as possible.
We can change it as we change the code.

Copy link
Member

Choose a reason for hiding this comment

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

For certain, but it's an aspect of the implementation that is bound to change very rapidly from the current state, to the point where I really don't want to have downstream packages rely on it (the result is that we might end up making non-breaking release, which subsequently ends up breaking downstream packages because we encouraged usage of internal and volatile code 😕 )

So I'd rather just say "Right now, it's still non-trivial to make it so the resulting samples are also bundled nicely into a MCMCChains.Chains, but this is being worked on (see this issue)." and link to some issue where we keep track of this:)

JaimeRZP and others added 2 commits July 28, 2023 14:37
JaimeRZP and others added 4 commits July 28, 2023 16:44
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@yebai yebai force-pushed the external_samplers_docs branch from 2d36ff1 to 6ec06d3 Compare July 30, 2023 21:45
@torfjelde
Copy link
Member

I'Ill try to have a look at this during weekend, if need be:)

@JaimeRZP
Copy link
Member Author

For some reason it still is using [email protected] instead of the [email protected]

@yebai yebai merged commit e8dc563 into master Oct 19, 2023
1 check passed
@delete-merged-branch delete-merged-branch bot deleted the external_samplers_docs branch October 19, 2023 13:35
@yebai
Copy link
Member

yebai commented Oct 19, 2023

Thanks @JaimeRZP!

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.

5 participants