Skip to content

Commit

Permalink
Fix metric resizing (#232)
Browse files Browse the repository at this point in the history
* Fix metric resizing

* Make suggested changes

* Bump version number
  • Loading branch information
treigerm authored Dec 1, 2020
1 parent d435a0b commit e00b686
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 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.2.26"
version = "0.2.27"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
6 changes: 6 additions & 0 deletions src/metric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ renew(ue::DenseEuclideanMetric, M⁻¹) = DenseEuclideanMetric(M⁻¹)
Base.size(e::DenseEuclideanMetric, dim...) = size(e._temp, dim...)
Base.show(io::IO, dem::DenseEuclideanMetric) = print(io, "DenseEuclideanMetric(diag=$(_string_M⁻¹(dem.M⁻¹)))")

# getname functions
for T in (UnitEuclideanMetric, DiagEuclideanMetric, DenseEuclideanMetric)
@eval getname(::Type{<:$T}) = $T
end
getname(m::T) where {T<:AbstractMetric} = getname(T)

# `rand` functions for `metric` types.

function _rand(
Expand Down
2 changes: 1 addition & 1 deletion src/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end
function resize(h::Hamiltonian, θ::AbstractVecOrMat{T}) where {T<:AbstractFloat}
metric = h.metric
if size(metric) != size(θ)
metric = typeof(metric)(size(θ))
metric = getname(metric)(size(θ))
h = reconstruct(h, metric=metric)
end
return h
Expand Down
16 changes: 16 additions & 0 deletions test/metric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ using Test, Random, AdvancedHMC
@test all_same
end
end

@testset "Resize metric" begin
D = 10
rng = MersenneTwister(1)
θ = randn(rng, D)
ℓπ(θ) = 1
for metric in [
UnitEuclideanMetric(1),
DiagEuclideanMetric(1),
DenseEuclideanMetric(1)
]
h = Hamiltonian(metric, ℓπ, ℓπ)
h = AdvancedHMC.resize(h, θ)
@test size(h.metric) == size(θ)
end
end

2 comments on commit e00b686

@yebai
Copy link
Member

@yebai yebai commented on e00b686 Dec 1, 2020

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@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/25610

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.2.27 -m "<description of version>" e00b6867a6b28baec580e67f71ae05d79342defe
git push origin v0.2.27

Please sign in to comment.