You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Closes#2739.
As a nice by-product of using `rand(ldf)` rather than `vi[:]`, we also
avoid accidentally promoting Float32 to Float64. This means that
(together with TuringLang/DynamicPPL.jl#1328 and
tpapp/DynamicHMC.jl#199) one can do
```julia
julia> using DynamicPPL; DynamicPPL.set_logprob_type!(Float32)
┌ Info: DynamicPPL's log probability type has been set to Float32.
└ Please note you will need to restart your Julia session for this change to take effect.
```
and then after restarting
```julia
julia> using Turing, FlexiChains, DynamicHMC
julia> @model function f()
x ~ Normal(0.0f0, 1.0f0)
end
f (generic function with 2 methods)
julia> chn = sample(f(), externalsampler(DynamicHMC.NUTS()), 100; chain_type=VNChain)
Sampling 100%|████████████████████████████████████████████| Time: 0:00:02
FlexiChain (100 iterations, 1 chain)
↓ iter=1:100 | → chain=1:1
Parameter type VarName
Parameters x
Extra keys :logprior, :loglikelihood, :logjoint
julia> eltype(chn[@varname(x)])
Float32
julia> eltype(chn[:logjoint])
Float32
```
(Previously, the values of `x` would be Float32, but logjoint would be
Float64. And if you used MCMCChains, everything would be Float64.)
Copy file name to clipboardExpand all lines: HISTORY.md
+12Lines changed: 12 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,15 @@
1
+
# 0.43.3
2
+
3
+
Unify parameter initialisation for HMC and external samplers.
4
+
External samplers (like HMC) now attempt multiple times to generate valid initial parameters, instead of just taking the first set of parameters.
5
+
6
+
Re-exports `set_logprob_type!` from DynamicPPL to allow users to control the base log-probability type used when evaluating Turing models.
7
+
For example, calling `set_logprob_type!(Float32)` will mean that Turing will use `Float32` for log-probability calculations, only promoting if there is something in the model that causes it to be (e.g. a distribution that returns `Float64` log-probabilities).
8
+
Note that this is a compile-time preference: for it to take effect you will have to restart your Julia session after calling `set_logprob_type!`.
9
+
10
+
Furthermore, note that sampler support for non-`Float64` log-probabilities is currently limited.
11
+
Although DynamicPPL promises not promote float types unnecessarily, many samplers, including HMC and NUTS, still use `Float64` internally and thus will cause log-probabilities and parameters to be promoted to `Float64`, even if the model itself uses `Float32`.
12
+
1
13
# 0.43.2
2
14
3
15
Throw an `ArgumentError` when a `Gibbs` sampler is missing component samplers for any variable in the model.
Given a `LogDensityFunction` and an initialization strategy, attempt to find valid initial
38
+
parameters by sampling from the initialization strategy and checking that the log density
39
+
(and gradient, if available) are finite. If valid parameters are not found after
40
+
`max_attempts`, throw an error.
41
+
"""
42
+
functionfind_initial_params_ldf(
43
+
rng::Random.AbstractRNG,
44
+
ldf::DynamicPPL.LogDensityFunction,
45
+
init_strategy::DynamicPPL.AbstractInitStrategy;
46
+
max_attempts::Int=1000,
47
+
)
48
+
for attempts in1:max_attempts
49
+
# Get new parameters
50
+
x =rand(rng, ldf, init_strategy)
51
+
is_valid =if ldf.adtype ===nothing
52
+
logp = LogDensityProblems.logdensity(ldf, x)
53
+
isfinite(logp)
54
+
else
55
+
logp, grad = LogDensityProblems.logdensity_and_gradient(ldf, x)
56
+
isfinite(logp) &&all(isfinite, grad)
57
+
end
58
+
59
+
# If they're OK, return them
60
+
is_valid &&return x
61
+
62
+
attempts ==10&&
63
+
@warn"failed to find valid initial parameters in $(attempts) tries; consider providing a different initialisation strategy with the `initial_params` keyword"
64
+
end
65
+
66
+
# if we failed to find valid initial parameters, error
67
+
returnerror(
68
+
"failed to find valid initial parameters in $(max_attempts) tries. See https://turinglang.org/docs/uri/initial-parameters for common causes and solutions. If the issue persists, please open an issue at https://github.com/TuringLang/Turing.jl/issues",
Copy file name to clipboardExpand all lines: src/mcmc/hmc.jl
+15-53Lines changed: 15 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -150,41 +150,10 @@ function AbstractMCMC.sample(
150
150
end
151
151
end
152
152
153
-
functionfind_initial_params(
154
-
rng::Random.AbstractRNG,
155
-
model::DynamicPPL.Model,
156
-
varinfo::DynamicPPL.AbstractVarInfo,
157
-
hamiltonian::AHMC.Hamiltonian,
158
-
init_strategy::DynamicPPL.AbstractInitStrategy;
159
-
max_attempts::Int=1000,
160
-
)
161
-
varinfo =deepcopy(varinfo) # Don't mutate
162
-
163
-
for attempts in1:max_attempts
164
-
theta = varinfo[:]
165
-
z = AHMC.phasepoint(rng, theta, hamiltonian)
166
-
isfinite(z) &&return varinfo, z
167
-
168
-
attempts ==10&&
169
-
@warn"failed to find valid initial parameters in $(attempts) tries; consider providing a different initialisation strategy with the `initial_params` keyword"
# if we failed to find valid initial parameters, error
178
-
returnerror(
179
-
"failed to find valid initial parameters in $(max_attempts) tries. See https://turinglang.org/docs/uri/initial-parameters for common causes and solutions. If the issue persists, please open an issue at https://github.com/TuringLang/Turing.jl/issues",
180
-
)
181
-
end
182
-
183
-
function Turing.Inference.initialstep(
153
+
function AbstractMCMC.step(
184
154
rng::AbstractRNG,
185
155
model::DynamicPPL.Model,
186
-
spl::Hamiltonian,
187
-
vi_original::AbstractVarInfo;
156
+
spl::Hamiltonian;
188
157
# the initial_params kwarg is always passed on from sample(), cf. DynamicPPL
189
158
# src/sampler.jl, so we don't need to provide a default value here
190
159
initial_params::DynamicPPL.AbstractInitStrategy,
@@ -193,32 +162,19 @@ function Turing.Inference.initialstep(
193
162
verbose::Bool=true,
194
163
kwargs...,
195
164
)
196
-
# Transform the samples to unconstrained space and compute the joint log probability.
0 commit comments