Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,13 @@ function sample(
end
# Report end of sampling
if verbose
EBFMI_est = EBFMI(map(s -> s.hamiltonian_energy, stats))
average_acceptance_rate = mean(map(s -> s.acceptance_rate, stats))
if θ isa AbstractVector
n_chains = 1
EBFMI_est = length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : NaN
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
EBFMI_est = length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : NaN
EBFMI_est =
length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : NaN

else
n_chains = size(θ, 2)
EBFMI_est = length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : fill(NaN, n_chains)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
EBFMI_est = length(stats) > 1 ? EBFMI(map(s -> s.hamiltonian_energy, stats)) : fill(NaN, n_chains)
EBFMI_est = if length(stats) > 1
EBFMI(map(s -> s.hamiltonian_energy, stats))
else
fill(NaN, n_chains)
end

# Make sure that arrays are on CPU before printing.
EBFMI_est = convert(Vector{eltype(EBFMI_est)}, EBFMI_est)
average_acceptance_rate = convert(
Expand Down
11 changes: 11 additions & 0 deletions test/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,14 @@ end
@test length(stats) == n_samples
end
end

@testset "n_samples=1 does not crash" begin
# Regression test for https://github.com/TuringLang/AdvancedHMC.jl/issues/281
# verbose=true triggers EBFMI computation which failed on a 1-element stats vector
θ_init = rand(MersenneTwister(1), D)
h = Hamiltonian(DiagEuclideanMetric(D), ℓπ, ∂ℓπ∂θ)
κ = HMCKernel(Trajectory{MultinomialTS}(Leapfrog(0.1), GeneralisedNoUTurn()))
samples, stats = sample(h, κ, θ_init, 1; verbose=true, progress=false)
@test length(samples) == 1
@test length(stats) == 1
end
Loading