Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for initial_params #772

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 30 additions & 5 deletions src/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,22 @@
initialsampler(spl::Sampler) = SampleFromPrior()

function set_values!!(
willtebbutt marked this conversation as resolved.
Show resolved Hide resolved
varinfo::AbstractVarInfo,
initial_params::AbstractVector{<:Union{Real,Missing}},
spl::AbstractSampler,
)
varinfo::AbstractVarInfo, initial_params::AbstractVector{T}, spl::AbstractSampler
) where {T}
if T === Any
throw(
ArgumentError(
"`initial_params` must be a vector of type `Union{Real,Missing}`. " *
"If `initial_params` is a vector of vectors, please flatten it first using `vcat`.",
),
)
end

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 +191,23 @@
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(
"Variable $v not found in model, but it subsumes a variable ($vv) in the model. " *
"Please use AbstractVector for initial_params instead of NamedTuple.",
willtebbutt marked this conversation as resolved.
Show resolved Hide resolved
),
)
end
end
throw(ArgumentError("Variable $v not found in the model."))

Check warning on line 208 in src/sampler.jl

View check run for this annotation

Codecov / codecov/patch

src/sampler.jl#L208

Added line #L208 was not covered by tests
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
Loading