Skip to content

Commit

Permalink
Add getparams and setparams!! following AbstractMCMC v5.5 and v5.6 (
Browse files Browse the repository at this point in the history
#378)

* add `getparams` and `setparams!!`

* undo formatting

* Update test/abstractmcmc.jl

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

* add some new tests

* update `setparams!!`

* Update src/abstractmcmc.jl

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

* add comment

* Update abstractmcmc.jl

Co-authored-by: Hong Ge <[email protected]>

* format

* update implementation

* bump AbstractMCMC

* update test

* Update src/abstractmcmc.jl

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

* fix method ambiguity

* Update src/abstractmcmc.jl

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

* Update src/abstractmcmc.jl

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

* fix test error

* fix more test error

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Hong Ge <[email protected]>
  • Loading branch information
3 people authored Oct 31, 2024
1 parent 47b212a commit 5d56902
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AdvancedHMC"
uuid = "0bf59076-c3b1-5ca4-86bd-e02cd72cde3d"
version = "0.6.2"
version = "0.6.3"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down Expand Up @@ -30,7 +30,7 @@ AdvancedHMCMCMCChainsExt = "MCMCChains"
AdvancedHMCOrdinaryDiffEqExt = "OrdinaryDiffEq"

[compat]
AbstractMCMC = "5"
AbstractMCMC = "5.6"
ArgCheck = "1, 2"
CUDA = "3, 4, 5"
DocStringExtensions = "0.8, 0.9"
Expand Down
2 changes: 1 addition & 1 deletion research/tests/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ include("../src/riemannian_hmc.jl")
include("relativistic_hmc.jl")
include("riemannian_hmc.jl")

@main function runtests(patterns...; dry::Bool = false)
Comonicon.@main function runtests(patterns...; dry::Bool = false)
retest(patterns...; dry = dry, verbose = Inf)
end
18 changes: 18 additions & 0 deletions src/abstractmcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ getadaptor(state::HMCState) = state.adaptor
getmetric(state::HMCState) = state.metric
getintegrator(state::HMCState) = state.κ.τ.integrator

function AbstractMCMC.getparams(state::HMCState)
return state.transition.z.θ
end

function AbstractMCMC.setparams!!(
model::AbstractMCMC.LogDensityModel,
state::HMCState,
params,
)
hamiltonian = AdvancedHMC.Hamiltonian(state.metric, model)
return Setfield.@set state.transition.z = AdvancedHMC.phasepoint(
hamiltonian,
params,
state.transition.z.r;
ℓκ = state.transition.z.ℓκ,
)
end

"""
$(TYPEDSIGNATURES)
Expand Down
20 changes: 20 additions & 0 deletions test/abstractmcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ using Statistics: mean
LogDensityProblemsAD.ADgradient(Val(:ForwardDiff), ℓπ_gdemo),
)

@testset "getparams and setparams!!" begin
t, s = AbstractMCMC.step(rng, model, nuts;)

θ = AbstractMCMC.getparams(s)
@test θ == t.z.θ
new_state = AbstractMCMC.setparams!!(model, s, θ)
@test new_state.transition.z.θ == θ
new_state_logπ = new_state.transition.z.ℓπ
@test new_state_logπ.value == s.transition.z.ℓπ.value
@test new_state_logπ.gradient == s.transition.z.ℓπ.gradient
new_state_logκ = new_state.transition.z.ℓκ
@test new_state_logκ.value == s.transition.z.ℓκ.value
@test new_state_logκ.gradient == s.transition.z.ℓκ.gradient
@test new_state.transition.z.r == s.transition.z.r

new_θ = randn(rng, 2)
new_state = AbstractMCMC.setparams!!(model, s, new_θ)
@test AbstractMCMC.getparams(new_state) == new_θ
end

samples_nuts = AbstractMCMC.sample(
rng,
model,
Expand Down

4 comments on commit 5d56902

@sunxd3
Copy link
Member Author

@sunxd3 sunxd3 commented on 5d56902 Oct 31, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Error while trying to register: Register Failed
@sunxd3, it looks like you are not a publicly listed member/owner in the parent organization (TuringLang).
If you are a member/owner, you will need to change your membership to public. See GitHub Help

@sunxd3
Copy link
Member Author

@sunxd3 sunxd3 commented on 5d56902 Oct 31, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/118450

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.3 -m "<description of version>" 5d5690271077826d9c0ddb2b35a5af74efc815fc
git push origin v0.6.3

Please sign in to comment.