-
Notifications
You must be signed in to change notification settings - Fork 32
[Merged by Bors] - Fix for #371 #372
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
Conversation
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.
It looks sensible to me!
Another solution would be to remove |
Allows one to use a different variable-name, e.g. |
@devmotion Is there another way besides I am surprised that this is such an uncommon use-case. For large systems with dozens or more parameters, it's a pain to write and maintain Turing models entirely by hand. It's only natural to want to automate this and build models programmatically. |
Sure, I know this use case, eg from DiffEqBayes. But to me |
It sounds like a good idea! |
I’m curious; what models are you working with? Some examples would help us to improve DynamicPPL / AbstractPPL. |
That would also be good, maybe even preferable. I actually only learned about
I am working on a permafrost model (at the moment basically a 1-D heat PDE with phase change) which uses a kind of "modular" design philosophy; i.e. one builds a model by stacking various components that may include one or more physical processes and each component may have an arbitrary number of parameters associated with it. I am interested in solving the Bayesian inverse problem (and also obtaining predictive uncertainty) for a subset of these parameters. I actually don't use Turing at the moment for posterior sampling because I found I can give you a code snippet of how I specify the Turing model at present: function makepriors(ps::CryoGridParams)
return map(ps[:component], ps[:fieldname], ps[:val]) do comp, name, value
prior(comp, Val{name}(), value)
end
end
# `NamedDist` comes from DynamicPPL.jl. (this part comes from Tor)
@model priormodel(shared::NamedTuple, name::Symbol, prior::Distribution) = x ~ NamedDist(prior, name)
@model priormodel(shared::NamedTuple, name::Symbol, nt::NamedTuple) = @submodel prefix=$name priormodel(shared, nt)
@model priormodel(shared::NamedTuple, name::Symbol, val::Symbol) = shared[val]
@model priormodel(shared::NamedTuple, name::Symbol, val) = val
@model function priormodel(shared, priors::NamedTuple)
# NOTE: The below will update `__varinfo__` for every `k`.
vals = map(keys(priors)) do k
@submodel priormodel(shared, k, priors[k])
end
return NamedTuple{keys(priors)}(vals)
end
@model function cryogridparams(ps::CryoGridParams, sharedpriors::NamedTuple=(;), ::Type{T}=Float64) where T
shared = @submodel priormodel((;), sharedpriors)
pvals = @submodel priormodel(shared, (; map(Pair, ps[:pid], ps[:prior])...))
# create copy of parameter array with compatible type (for autodiff)
p = copyto!(similar(collect(values(ps)), T), pvals)
return p
end where I hope that helps! Edit: I should note that I just changed this very recently; the parameters used to be organized into a hierarchy/tree and then |
One main advantage of @phipsgabler can probably confirm or falsify this claim |
I think $name[i] to (VarName){name}((Setfield.compose)((Setfield.IndexLens)((i,)))) OK, actually, the LHS is transformed as $vn = $(AbstractPPL.drop_escape(varname(left))) so it might actually work... but I haven't tried it! |
@torfjelde a gentle reminder on this |
As @phipsgabler originally said, This is because we explicitly "unwrap" the interpolated expressions in the model macro: Lines 321 to 322 in d222316
As a result, even if If we keep the |
bors try |
It would be great to get this merged, if everything is working ok! I currently have my project pointed at this PR branch. |
@torfjelde pls, feel free to go ahead and merge it if you think it is ready. |
bors r+ |
bors cancel |
Canceled. |
bors r+ |
This PR adds a method called `resolve_varnames(varname, dist)` and adds an additional generated variable for each `~` which now holds the RHS of `~`. It does address #371 but uncertain if this is the best way, so wouldn't recommend merging this just yet. But putting it here so we can colab on it. Co-authored-by: Hong Ge <[email protected]>
Pull request successfully merged into master. Build succeeded: |
This PR adds a method called
resolve_varnames(varname, dist)
and adds an additional generated variable for each~
which now holds the RHS of~
.It does address #371 but uncertain if this is the best way, so wouldn't recommend merging this just yet. But putting it here so we can colab on it.