diff --git a/HISTORY.md b/HISTORY.md index 20e9bfa38b..c6569dacc9 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +# 0.42.2 + +`InitFromParams(mode_estimate)`, where `mode_estimate` was obtained from an optimisation on a Turing model, now accepts a second optional argument which provides a fallback initialisation strategy if some parameters are missing from `mode_estimate`. + +This also means that you can now invoke `returned(model, mode_estimate)` to calculate a model's return values given the parameters in `mode_estimate`. + # 0.42.1 Avoid passing a full VarInfo to `check_model`, which allows more models to be checked safely for validity. diff --git a/Project.toml b/Project.toml index bdaa1bff3e..ee14aac4d0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Turing" uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" -version = "0.42.1" +version = "0.42.2" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" diff --git a/src/optimisation/Optimisation.jl b/src/optimisation/Optimisation.jl index 2c307f7a14..bc897f0646 100644 --- a/src/optimisation/Optimisation.jl +++ b/src/optimisation/Optimisation.jl @@ -128,12 +128,18 @@ function Base.show(io::IO, m::ModeResult) end """ - InitFromParams(m::ModeResult) + InitFromParams( + m::ModeResult, + fallback::Union{AbstractInitStrategy,Nothing}=InitFromPrior() + ) -Initialize a model from the parameters stored in a `ModeResult`. +Initialize a model from the parameters stored in a `ModeResult`. The `fallback` is used if +some parameters are missing from the `ModeResult`. """ -function DynamicPPL.InitFromParams(m::ModeResult) - return DynamicPPL.InitFromParams(m.params) +function DynamicPPL.InitFromParams( + m::ModeResult, fallback::Union{DynamicPPL.AbstractInitStrategy,Nothing}=InitFromPrior() +) + return DynamicPPL.InitFromParams(m.params, fallback) end # Various StatsBase methods for ModeResult diff --git a/test/optimisation/Optimisation.jl b/test/optimisation/Optimisation.jl index 39c2aef03a..248335f328 100644 --- a/test/optimisation/Optimisation.jl +++ b/test/optimisation/Optimisation.jl @@ -587,6 +587,19 @@ using Turing @test chain[:σ][1] == mle.params[@varname(σ)] end + @testset "returned on ModeResult" begin + @model function f() + x ~ Normal() + 2.0 ~ Normal(x) + return x + 1.0 + end + model = f() + result = maximum_a_posteriori(model) + @test returned(model, result) == result.params[@varname(x)] + 1.0 + result = maximum_likelihood(model) + @test returned(model, result) == result.params[@varname(x)] + 1.0 + end + # Issue: https://discourse.julialang.org/t/turing-mixture-models-with-dirichlet-weightings/112910 @testset "Optimization with different linked dimensionality" begin @model demo_dirichlet() = x ~ Dirichlet(2 * ones(3))