-
Notifications
You must be signed in to change notification settings - Fork 31
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
Allowing using NamedTuple
as initial_params
#632
Changes from 6 commits
52a9b76
94c2ef7
5b5f4dd
4693fa2
bab789e
c3ce3fb
aab6606
22942d4
a3969d8
1b2ce47
14bb2cf
8b79ac0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -142,38 +142,59 @@ By default, it returns an instance of [`SampleFromPrior`](@ref). | |||
""" | ||||
initialsampler(spl::Sampler) = SampleFromPrior() | ||||
|
||||
function initialize_parameters!!( | ||||
vi::AbstractVarInfo, initial_params, spl::Sampler, model::Model | ||||
function set_values!!( | ||||
varinfo::AbstractVarInfo, | ||||
initial_params::AbstractVector{<:Union{Real,Missing}}, | ||||
spl::AbstractSampler, | ||||
) | ||||
@debug "Using passed-in initial variable values" initial_params | ||||
|
||||
# Flatten parameters. | ||||
init_theta = mapreduce(vcat, initial_params) do x | ||||
vec([x;]) | ||||
end | ||||
|
||||
# Get all values. | ||||
linked = islinked(vi, spl) | ||||
if linked | ||||
vi = invlink!!(vi, spl, model) | ||||
end | ||||
theta = vi[spl] | ||||
length(theta) == length(init_theta) || throw( | ||||
theta = varinfo[spl] | ||||
sunxd3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
length(theta) == length(initial_params) || throw( | ||||
DimensionMismatch( | ||||
"Provided initial value size ($(length(init_theta))) doesn't match the model size ($(length(theta)))", | ||||
"Provided initial value size ($(length(initial_params))) doesn't match the model size ($(length(theta)))", | ||||
), | ||||
) | ||||
|
||||
# Update values that are provided. | ||||
for i in eachindex(init_theta) | ||||
x = init_theta[i] | ||||
for i in eachindex(initial_params) | ||||
x = initial_params[i] | ||||
if x !== missing | ||||
theta[i] = x | ||||
end | ||||
end | ||||
|
||||
# Update in `vi`. | ||||
vi = setindex!!(vi, theta, spl) | ||||
# Update in `varinfo`. | ||||
return setindex!!(varinfo, theta, spl) | ||||
end | ||||
|
||||
# if initialize with scalar, convert to vector | ||||
function set_values!!(varinfo::AbstractVarInfo, initial_params::Real, spl::AbstractSampler) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason of adding this method is that, before this PR,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was not aware we allowed this either 🤷 Are we testing this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah Line 87 in dfdc155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, I say we drop this. Seems unnecessarily complicated when it's just a difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see @torfjelde's point of simplicity of interface, but it is a breaking change, which makes me wonder if the small effort of keeping it supported is worth it. If we do change it, maybe bundle it with some other breaking changes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though it's technically breaking, I highly doubt this piece of code exists anywhere else, i.e. it would be a matter of bumping compat bounds 🤔 |
||||
return set_values!!(varinfo, [initial_params], spl) | ||||
end | ||||
|
||||
function set_values!!( | ||||
varinfo::AbstractVarInfo, initial_params::NamedTuple, spl::AbstractSampler | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we happy with |
||||
) | ||||
initial_params = NamedTuple(k => v for (k, v) in pairs(initial_params) if v !== missing) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this is good, but changed to support a more uniform interface: allowing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as in last comment: when are we initializing using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see reply above |
||||
return DynamicPPL.TestUtils.update_values!!( | ||||
sunxd3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
varinfo, initial_params, map(k -> VarName{k}(), keys(initial_params)) | ||||
) | ||||
end | ||||
|
||||
function initialize_parameters!!( | ||||
vi::AbstractVarInfo, initial_params, spl::AbstractSampler, model::Model | ||||
) | ||||
@debug "Using passed-in initial variable values" initial_params | ||||
|
||||
# `link` the varinfo if needed. | ||||
linked = islinked(vi, spl) | ||||
if linked | ||||
vi = invlink!!(vi, spl, model) | ||||
end | ||||
|
||||
# Set the values in `vi`. | ||||
vi = set_values!!(vi, initial_params, spl) | ||||
|
||||
# `invlink` if needed. | ||||
if linked | ||||
vi = link!!(vi, spl, model) | ||||
end | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -892,6 +892,12 @@ Base.keys(vi::TypedVarInfo{<:NamedTuple{()}}) = VarName[] | |
return expr | ||
end | ||
|
||
# FIXME(torfjelde): Don't use `_getvns`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still relevant? Don't know why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just unnecessary; |
||
Base.keys(vi::UntypedVarInfo, spl::AbstractSampler) = _getvns(vi, spl) | ||
function Base.keys(vi::TypedVarInfo, spl::AbstractSampler) | ||
return mapreduce(values, vcat, _getvns(vi, spl)) | ||
end | ||
|
||
""" | ||
setgid!(vi::VarInfo, gid::Selector, vn::VarName) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the array element type from
Real
toUnion{Real, Missing}
because we allow initialization vector withmissing
in it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really? In what scenario would this be used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DynamicPPL.jl/test/sampler.jl
Line 135 in dfdc155
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, not sure how I feel about that. I guess it means to sample that parameter from the prior? But that's so simple to do by hand anyways these days.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose so, although it made sense to me because the initialization vector need to be the same dimension as the model, so it makes sense for someone to say "I don't care about these" by setting them to
missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but you can also just do
rand(Vector, model)
and alter those values 😕
Basically, it's a question of whether we want to maintain this or just leave it up to the user when it is a simple one-liner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got your argument, 👍
But in this case it's not really a one-liner, as user also need to set individual values.
For models with small dimensions, current syntax can still be useful. I am for keeping this option.