-
Notifications
You must be signed in to change notification settings - Fork 241
Add code for AbstractMCMC Callbacks support #2755
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
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a24d042
Add glue code for AbstractMCMC Callbacks support
shravanngoswamii efb745d
use AbstractMCMC@5.11
shravanngoswamii 80caf44
update Callbacks to incorporate suggestion from discussion
shravanngoswamii 65f545b
format
shravanngoswamii 73924f5
Apply suggestions from code review
yebai 354244a
implement new suggestions
shravanngoswamii fb19bce
Merge branch 'main' of https://github.com/TuringLang/Turing.jl into s…
shravanngoswamii 1c0828e
implement all suggestions
shravanngoswamii d311a05
resolve merge conflicts
shravanngoswamii 03ff92d
update tests
shravanngoswamii 6a70f08
fix lp calculation
shravanngoswamii 1b4aad3
Merge branch 'main' of https://github.com/TuringLang/Turing.jl into s…
shravanngoswamii 364df74
pass MCMC states to getparams
shravanngoswamii 8965eef
nice cleanup
shravanngoswamii e5b1b3e
version bump: 0.42.6 --> 0.42.7
shravanngoswamii 6e6f44f
Merge branch 'main' into sg/AbstractMCMCCallbacks
yebai 58a4496
Update Project.toml
yebai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| module CallbacksTests | ||
|
|
||
| using Test, Turing, AbstractMCMC, Random, Distributions, LinearAlgebra | ||
|
|
||
| @model function test_normals() | ||
| x ~ Normal() | ||
| return y ~ MvNormal(zeros(3), I) | ||
| end | ||
|
|
||
| @testset "AbstractMCMC Callbacks Interface" begin | ||
| rng = Random.default_rng() | ||
| model = test_normals() | ||
|
|
||
| samplers = [ | ||
| ("NUTS", NUTS(10, 0.65)), | ||
| ("HMC", HMC(0.1, 5)), | ||
| ("MH", MH()), | ||
| ("ESS", ESS()), | ||
| ("Gibbs", Gibbs(:x => HMC(0.1, 5), :y => MH())), | ||
| ("SGHMC", SGHMC(; learning_rate=0.01, momentum_decay=1e-2)), | ||
| ("PG", PG(10)), | ||
| ] | ||
|
shravanngoswamii marked this conversation as resolved.
|
||
|
|
||
| for (name, sampler) in samplers | ||
| @testset "$name" begin | ||
| t1, s1 = AbstractMCMC.step( | ||
| rng, model, sampler; initial_params=Turing.Inference.init_strategy(sampler) | ||
| ) | ||
|
|
||
| # ParamsWithStats returns named params (not θ[i]) | ||
| pws = AbstractMCMC.ParamsWithStats( | ||
| model, sampler, t1, s1; params=true, stats=true | ||
| ) | ||
| pairs_dict = Dict(k => v for (k, v) in Base.pairs(pws)) | ||
| # Keys are Symbols since ParamsWithStats stores NamedTuple internally | ||
| @test haskey(pairs_dict, Symbol("x")) | ||
| @test haskey(pairs_dict, Symbol("y")) | ||
| @test pairs_dict[Symbol("y")] isa AbstractVector | ||
| @test length(pairs_dict[Symbol("y")]) == 3 | ||
|
|
||
| # Check stats contain lp | ||
| @test haskey(pairs_dict, :lp) || haskey(pairs_dict, :logjoint) | ||
| end | ||
| end | ||
|
|
||
| # NUTS second step has full AHMC transition metrics | ||
| @testset "NUTS Transition Metrics" begin | ||
| sampler = NUTS(10, 0.65) | ||
| t1, s1 = AbstractMCMC.step( | ||
| rng, model, sampler; initial_params=Turing.Inference.init_strategy(sampler) | ||
| ) | ||
| t2, s2 = AbstractMCMC.step(rng, model, sampler, s1) | ||
|
|
||
| pws = AbstractMCMC.ParamsWithStats(model, sampler, t2, s2; params=true, stats=true) | ||
| pairs_dict = Dict(k => v for (k, v) in Base.pairs(pws)) | ||
|
|
||
| # Keys are Symbols from NamedTuple | ||
| @test haskey(pairs_dict, :tree_depth) | ||
| @test haskey(pairs_dict, :n_steps) | ||
| @test haskey(pairs_dict, :acceptance_rate) | ||
| @test haskey(pairs_dict, :hamiltonian_energy) | ||
| end | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.