-
Notifications
You must be signed in to change notification settings - Fork 36
InitContext
, part 3 - Introduce InitContext
#981
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
base: breaking
Are you sure you want to change the base?
Conversation
Benchmark Report for Commit ab3e8daComputer Information
Benchmark Results
|
DynamicPPL.jl documentation for PR #981 is available at: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## breaking #981 +/- ##
============================================
+ Coverage 82.16% 82.41% +0.25%
============================================
Files 38 39 +1
Lines 3935 3992 +57
============================================
+ Hits 3233 3290 +57
Misses 702 702 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
""" | ||
prefix(model::Model, x::VarName) | ||
prefix(model::Model, x::Val{sym}) | ||
prefix(model::Model, x::Any) | ||
|
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.
This code was shifted verbatim to src/model.jl
to avoid circular dependencies between files.
90e95e1
to
21cf568
Compare
InitContext
InitContext
, part 3 - Introduce InitContext
025aa8b
to
b55c1e1
Compare
5f60c46
to
4408efb
Compare
4408efb
to
9c07727
Compare
9c07727
to
ef038c6
Compare
ef038c6
to
5961ca9
Compare
5961ca9
to
0656487
Compare
fd78d42
to
bfcdbb9
Compare
Note that, apart from being simpler code, Distributions.Uniform also doesn't allow the lower and upper bounds to be exactly equal (but we might like to keep that option open in DynamicPPL, e.g. if the user wants to initialise all values to the same value in linked space).
This should have been changed in #940, but slipped through as the file wasn't listed as one of the changed files.
bfcdbb9
to
ab3e8da
Compare
Part 1: Adding
hasvalue
andgetvalue
to AbstractPPLPart 2: Removing
hasvalue
andgetvalue
from DynamicPPLThis is Part 3/N of splitting up #967.
This PR solely adds the code for a new leaf context,
InitContext
. Its behaviour is to always override existing values inside a VarInfo.A long-held goal of mine is to split up
contexts.jl
andcontext_implementations.jl
such that each context is in its own file. To this end, I've put all theInitContext
-related code insidesrc/contexts/init.jl
. This should, hopefully, also make reviewing easier.InitContext
comes in three forms:PriorInit
: Samples values from the prior;UniformInit(min, max)
: Samples values from betweenmin
andmax
before invlinking them back into the support of the original distribution;ParamsInit(params)
: Takes values fromparams
, which can be a NamedTuple or a Dict. Also takes a fallback strategy, which is used in case the varname is not found inparams
.PriorInit
andUniformInit
have almost one-to-one correspondence withSampleFromPrior
andSampleFromUniform
. I haven't removed them yet because a LOT of stuff depends onSampleFromPrior
. We will eventually remove all of it but doing it in a single PR would make it way too big.However,
ParamsInit
is new. Its purpose is to set fixed values in a VarInfo in a principled manner. Right now, there is a fair amount of code that does low-level VarInfo manipulation by reaching inside and modifying the underlying arrays, such asinitialise_values!!
, andsetval_and_resample!
. This is prone to bugs, and also leads to (for example) the VarInfo's logp being out of sync. (All of that code will be removed in subsequent PRs.)ParamsInit
accomplishes the same in a much cleaner way, although in some cases (specifically, when setting initial values for sampling) it may necessitate one extra model evaluation. Because initialisation is not meant to happen within a tight loop (by definition it happens once), I don't consider performance to be an important issue.One might ask what's the difference between
InitContext{ParamsInit}
,ConditionContext
, andFixedContext
. Consider the model@model f() = x ~ Normal()
. Evaluating this with each of the contexts gives the following behaviour:x ~ Normal()
x
in VarInfoIn other words, InitContext behaves as if you had run the model and sampled a new value for
x
, but that value just so happened to be the one that you provided. In contrast, Condition and FixedContext fundamentally change the model that is being run in thatx
is no longer a parameter.Closes #375.