Skip to content

Commit

Permalink
Improve error message for initial_params (#772)
Browse files Browse the repository at this point in the history
* improve error message for `initial_params`

* Update src/sampler.jl

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix logic error

* apply Will's suggestions

* bump version

* Update src/sampler.jl

Co-authored-by: Penelope Yong <[email protected]>

* Update Project.toml

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Penelope Yong <[email protected]>
  • Loading branch information
3 people authored Jan 7, 2025
1 parent 3d18cfc commit 003ff2f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ By default, it returns an instance of [`SampleFromPrior`](@ref).
"""
initialsampler(spl::Sampler) = SampleFromPrior()

function set_values!!(
varinfo::AbstractVarInfo, initial_params::AbstractVector, spl::AbstractSampler
)
throw(
ArgumentError(
"`initial_params` must be a vector of type `Union{Real,Missing}`. " *
"If `initial_params` is a vector of vectors, please flatten it (e.g. using `vcat`) first.",
),
)
end

function set_values!!(
varinfo::AbstractVarInfo,
initial_params::AbstractVector{<:Union{Real,Missing}},
Expand All @@ -164,7 +175,8 @@ function set_values!!(
flattened_param_vals = varinfo[spl]
length(flattened_param_vals) == length(initial_params) || throw(
DimensionMismatch(
"Provided initial value size ($(length(initial_params))) doesn't match the model size ($(length(flattened_param_vals)))",
"Provided initial value size ($(length(initial_params))) doesn't match " *
"the model size ($(length(flattened_param_vals))).",
),
)

Expand All @@ -183,6 +195,24 @@ end
function set_values!!(
varinfo::AbstractVarInfo, initial_params::NamedTuple, spl::AbstractSampler
)
vars_in_varinfo = keys(varinfo)
for v in keys(initial_params)
vn = VarName{v}()
if !(vn in vars_in_varinfo)
for vv in vars_in_varinfo
if subsumes(vn, vv)
throw(
ArgumentError(
"The current model contains sub-variables of $v, such as ($vv). " *
"Using NamedTuple for initial_params is not supported in such a case. " *
"Please use AbstractVector for initial_params instead of NamedTuple.",
),
)
end
end
throw(ArgumentError("Variable $v not found in the model."))
end
end
initial_params = NamedTuple(k => v for (k, v) in pairs(initial_params) if v !== missing)
return update_values!!(
varinfo, initial_params, map(k -> VarName{k}(), keys(initial_params))
Expand Down
25 changes: 25 additions & 0 deletions test/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,30 @@
@test c1[1].metadata.s.vals == c2[1].metadata.s.vals
end
end

@testset "error handling" begin
# https://github.com/TuringLang/Turing.jl/issues/2452
@model function constrained_uniform(n)
Z ~ Uniform(10, 20)
X = Vector{Float64}(undef, n)
for i in 1:n
X[i] ~ Uniform(0, Z)
end
end

n = 2
initial_z = 15
initial_x = [0.2, 0.5]
model = constrained_uniform(n)
vi = VarInfo(model)

@test_throws ArgumentError DynamicPPL.initialize_parameters!!(
vi, [initial_z, initial_x], DynamicPPL.SampleFromPrior(), model
)

@test_throws ArgumentError DynamicPPL.initialize_parameters!!(
vi, (X=initial_x, Z=initial_z), DynamicPPL.SampleFromPrior(), model
)
end
end
end

6 comments on commit 003ff2f

@penelopeysm
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator register

Release notes:

  • Moved the implementation of predict from Turing.jl to DynamicPPL.jl; the user-facing behaviour is otherwise the same
  • values_as_in_model() now requires an extra boolean parameter, specifying whether variables on the lhs of := statements are to be included in the resulting OrderedDict of values. The type signature is now values_as_in_model([rng,] model, include_colon_eq::Bool [, varinfo, context])
  • Improved error message when a user tries to initialise a model with parameters that don't correspond strictly to the underlying VarInfo used

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

Tagging

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.33.0 -m "<description of version>" 003ff2f39747de4b59fe08e45c6dc42a51777828
git push origin v0.33.0

@penelopeysm
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator register

Release notes

Breaking changes

  • values_as_in_model() now requires an extra boolean parameter, specifying whether variables on the lhs of := statements are to be included in the resulting OrderedDict of values. The type signature is now values_as_in_model([rng,] model, include_colon_eq::Bool [, varinfo, context])

Other changes

  • Moved the implementation of predict from Turing.jl to DynamicPPL.jl; the user-facing behaviour is otherwise the same
  • Improved error message when a user tries to initialise a model with parameters that don't correspond strictly to the underlying VarInfo used

@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 updated: JuliaRegistries/General/122588

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.33.0 -m "<description of version>" 003ff2f39747de4b59fe08e45c6dc42a51777828
git push origin v0.33.0

@penelopeysm
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator register

Release notes:

Breaking changes

  • values_as_in_model() now requires an extra boolean parameter, specifying whether variables on the lhs of := statements are to be included in the resulting OrderedDict of values. The type signature is now values_as_in_model([rng,] model, include_colon_eq::Bool [, varinfo, context])

Other changes

  • Moved the implementation of predict from Turing.jl to DynamicPPL.jl; the user-facing behaviour is otherwise the same
  • Improved error message when a user tries to initialise a model with parameters that don't correspond strictly to the underlying VarInfo used

@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 updated: JuliaRegistries/General/122588

Tagging

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.33.0 -m "<description of version>" 003ff2f39747de4b59fe08e45c6dc42a51777828
git push origin v0.33.0

Please sign in to comment.