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

Add example illustrating how to run multiple chains. #698

Closed
trappmartin opened this issue Feb 25, 2019 · 13 comments
Closed

Add example illustrating how to run multiple chains. #698

trappmartin opened this issue Feb 25, 2019 · 13 comments
Labels

Comments

@trappmartin
Copy link
Member

We currently don't have an example that runs multiple chains. However, I think most examples should actually do this. Maybe we could change the tutorials in this respect?

Running multiple chains is currently possible using:

chains = mapreduce(c -> sample(model_fun, sampler), vcat, 1:num_chains)

which will create a Turing.Chains object that contains samples for all chains.

@cpfiffer
Copy link
Member

I feel like we should instead just add a keyword argument (or something) to sample where sample(model, sampler, chains=3) returns that number of chains. Is that a poor idea? I suspect we'd have to touch a lot of samplers due to the places we have sample defined.

@yebai
Copy link
Member

yebai commented Feb 28, 2019

Ideally, we can share the same sample function implementation for all samplers. We can probably do this a bit later, for example after we finish the refactoring in #689

@cpfiffer
Copy link
Member

cpfiffer commented Mar 1, 2019

I'll document this behavior for the moment until we get something more permanent.

@itsdfish
Copy link
Contributor

itsdfish commented Mar 1, 2019

I noticed that #582 was closed. Does this plan include running the chains in parallel?

@trappmartin
Copy link
Member Author

Currently this is not possible.

  1. You cannot set a random seed for Hamiltonian based samplers for now. However, this will very soon be possible and you will be able to sample chains in parallel if only Hamiltonian based samplers are used.

  2. The Distributions package does not implement all rand functions in Julia but used R as a backend. Therefore, it is not possible to seed the random number generator for all distributions making parallel particle based sampling difficult at the moment.

@cpfiffer
Copy link
Member

cpfiffer commented Mar 2, 2019

This will show up in the guide shortly. I'll be revisiting some of the tutorials soon after #695 goes through, and I'll add in this functionality to better show how to run multiple chains.

@yebai
Copy link
Member

yebai commented Mar 8, 2019

@Pindar777
Copy link

Pindar777 commented Apr 21, 2019

Why do mapreduce and reduce...pmap respond differently to @everywhere?
Although both approaches generate multiple chains only the first one uses parallel processes:

chns = reduce(chainscat, pmap(x->sample(model,sampler), 1:num_chains))
(from bdf5d8c)

chains = mapreduce(c -> sample(model, sampler), chainscat, 1:num_chains)
(from docs)

@cpfiffer
Copy link
Member

I'm not sure what you're asking, exactly. pmap (docs) is a mapping function that uses whatever worker processes are available, while mapreduce (docs) is explicitly serial.

The second case is slightly more numerically accurate, since I believe there's some cases where random numbers are not independent when sampling from Distributions. Not sure if that's been fixed yet.

@Pindar777
Copy link

@cpfiffer Thank you for your fast reply and explanation. I'm new to Julia and thought I'm making an error when working with the @everywhere macro. So, pmapstems from the Distrubuted-package and thus works with all workers where mapreduce is base-Julia and hence serial.

@cpfiffer
Copy link
Member

Yes, that's correct --- we're hoping soon to make it unnecessary to worry about mapreduce and pmap, but at the moment it's a bit clumsy.

@trappmartin
Copy link
Member Author

Both functions are from Julia Base. pmap is from the Distributed module of Base. You can read more on this in the Julia documentation.

@yebai
Copy link
Member

yebai commented May 15, 2019

Closed in favour of #746

@yebai yebai closed this as completed May 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants