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

Change handling of transitions #12

Merged
merged 4 commits into from
Jan 30, 2020
Merged

Change handling of transitions #12

merged 4 commits into from
Jan 30, 2020

Conversation

devmotion
Copy link
Member

I've thought a bit more about transition_type, and how to improve on what I mentioned in #9 (comment).

In this PR I propose the following changes:

  • Remove transition_type and instead just obtain the first transition
  • Pass the initial transition to transitions_init, whose default implementation just creates a vector for N transitions of the type of the initial transition
  • Add a function transitions_save! to which (among others) the transitions container, the current iteration, and the current transition is passed; the default implementation for vector-type containers just saves the current transition in the container
  • Remove AbstractTransition
  • Do not export anything

The main advantages with this approach are:

  • Users/developers do not have to define transition_type - as long as their step! implementation is type stable it just works
  • It seems trivial to, e.g., add a wrapper sampler
    struct StreamingSampler{S<:AbstractSampler,I}
        io::I
        sampler::S
    end
    
    stream(io, sampler::AbstractSampler) = StreamingSampler(io, sampler)
    
    AbstractMCMC.sample_init!(rng::AbstractRNG, model::AbstractModel, sampler::StreamingSampler, N::Integer; kwargs...) = sample_init!(rng, model, sampler.sampler, N; kwargs...)
    AbstractMCMC.sample_end!(rng::AbstractRNG, model::AbstractModel, sampler::StreamingSampler, N::Integer, transitions; kwargs...) = sample_end!(rng, model, sampler.sampler, N, transitions; kwargs...)
    AbstractMCMC.bundle_samples!(rng::AbstractRNG, model::AbstractModel, sampler::StreamingSampler, N::Integer, ::Nothing; kwargs...) = nothing
    
    AbstractMCMC.transitions_init!(transition, ::AbstractModel, sampler::StreamingSampler, ::Integer; kwargs...) = nothing
    AbstractMCMC.transitions_save!(::Nothing, ::Integer, transition, ::AbstractModel, ::StreamingSampler, ::Integer) = write(sampler.io, transition)
    as suggested in stream sampler results to disk (or other output) - minor feature suggestion #107
  • One does not have to wrap parameter vectors or named tuples inside of a custom AbstractTransition subtype anymore - if step! just returns a parameter vector or a named tuple, the default implementation of sample will just yield a vector of parameter vectors or named tuples
  • It does not seem useful to pollute the namespace by exporting methods such as sample_init! or sample_end! (which many people do not want to call directly and have to be imported anyway if implemented for specific types). In general, I think in every concrete implementation of AbstractModel/AbstractSampler one just has to decide which methods of AbstractMCMC to export (probably just sample and psample).

@codecov
Copy link

codecov bot commented Jan 27, 2020

Codecov Report

Merging #12 into master will increase coverage by 3.57%.
The diff coverage is 94.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      TuringLang/Turing.jl#12      +/-   ##
==========================================
+ Coverage   77.27%   80.85%   +3.57%     
==========================================
  Files           1        1              
  Lines          44       47       +3     
==========================================
+ Hits           34       38       +4     
+ Misses         10        9       -1
Impacted Files Coverage Δ
src/AbstractMCMC.jl 80.85% <94.11%> (+3.57%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 67439eb...0986e92. Read the comment docs.

@cpfiffer
Copy link
Member

I'm a fan! I'll sit down with this sometime this evening and give some more thoughtful comments if I can find anything.

Copy link
Member

@cpfiffer cpfiffer left a comment

Choose a reason for hiding this comment

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

I've got no real comments, looks good to me. If nobody else has any comments, I'll merge this at the end of the day.

Edit: That's about ~12 hours from now in my time zone.

@devmotion
Copy link
Member Author

Should we merge it? 😃

@cpfiffer
Copy link
Member

Yeah, sorry. Got caught up in some stuff and it slipped my mind.

@cpfiffer cpfiffer merged commit ca622ed into master Jan 30, 2020
@delete-merged-branch delete-merged-branch bot deleted the transitions branch January 30, 2020 14:18
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