Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "SliceSampling"
uuid = "43f4d3e8-9711-4a8c-bd1b-03ac73a255cf"
version = "0.7.8"
version = "0.7.9"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand All @@ -21,7 +21,7 @@ Distributions = "0.25"
LinearAlgebra = "1"
LogDensityProblems = "2"
Random = "1"
Turing = "0.40"
Turing = "0.41"
julia = "1.10"

[extras]
Expand Down
15 changes: 7 additions & 8 deletions ext/SliceSamplingTuringExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ end
function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDensityFunction)
n_max_attempts = 1000

model = ℓ.model
vi = Turing.DynamicPPL.VarInfo(rng, model, Turing.SampleFromUniform())
vi_spl = last(Turing.DynamicPPL.evaluate_and_sample!!(rng, model, vi, Turing.SampleFromUniform()))
θ = vi_spl[:]
ℓp = LogDensityProblems.logdensity(ℓ, θ)
Copy link
Member Author

Choose a reason for hiding this comment

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

calling logdensity leads to an extra model evaluation, we can skip this by using the logjoint which is already in the varinfo

model, vi = ℓ.model, ℓ.varinfo
vi_spl = last(Turing.DynamicPPL.init!!(rng, model, vi, Turing.DynamicPPL.InitFromUniform()))
ℓp = Turing.DynamicPPL.getlogjoint_internal(vi_spl)

init_attempt_count = 1
for attempts in 1:n_max_attempts
Expand All @@ -52,19 +50,20 @@ function SliceSampling.initial_sample(rng::Random.AbstractRNG, ℓ::Turing.LogDe

# NOTE: This will sample in the unconstrained space.
vi_spl = last(
Turing.DynamicPPL.evaluate_and_sample!!(
rng, model, vi, Turing.SampleFromUniform()
Turing.DynamicPPL.init!!(
rng, model, vi_spl, Turing.InitFromUniform()
),
)
ℓp = Turing.DynamicPPL.getlogjoint_internal(vi_spl)
θ = vi_spl[:]
ℓp = LogDensityProblems.logdensity(ℓ, θ)

if all(isfinite.(θ)) && isfinite(ℓp)
return θ
end
end

@error "Failed to find valid initial parameters after $(n_max_attempts) attempts; consider providing explicit initial parameters using the `initial_params` keyword"
θ = vi_spl[:]
return θ
end

Expand Down
Loading