[WIP] Taking stochastic control flow seriously #105
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements the data structures and methods needed to handle models with stochastic control flow of the form described in #25. To this end, I implemented the
MixedVarInfo
data structure which combines aTypedVarInfo
and anUntypedVarInfo
using the latter for symbols which have not been seen before. In the future, we can consider moving more ofUntypedVarInfo
toTypedVarInfo
in an incrementalsample
function inTuring
, where new type information is specialized upon for faster sampling. Currently, this PR is still a proof of concept. More things fail than work but once I implement the following, things should be much better. I moved the Turing src from Turing to the test folder and kept it in sync with Turing#mt/mixed_varinfo which I am using locally for tests.For correctness and for things to work, the following still needs to be done:
MixedVarInfo
, namely_getvns
andset_namedtuple!
. Currently I just forward to thetvi::TypedVarInfo
implementation which is strictly speaking not correct.tonamedtuple
properly forUntypedVarInfo
transition_save!
.transition_save!
currently errors when it sees named tuples with different number of fields.MixedVarInfo
behaviour tests for dynamic models with fixed symbols and dynamic models with a stochastic number of symbols.For performance, the following needs to be done:
Model
refactor~
in aspace
field in theModel
structgetspace
forModel
to return the storedspace
fieldgetparams
for model to return the symbols that are:space
and not inargs
, ormissing
getobservations
to return the symbols that are:space
,args
, andmissing
.gethyperparameters
to return the symbols that are:space
, andargs
.NamedDist
refactorbijector
,reconstruct
andvectorize
forNamedDist
varinfo
as is - don't replace thevarname
inassume
as we do nowvarname
in theNamedDist
when converting thevarinfo
to aNamedTuple
/Chain
AbstractSampler
refactorSampler
fromalg
andmodel::Model
, wherealg
has no space defined, usegetparams(model)
as thespace
inSampler
.SampleFromUniform
andSampleFromPrior
have aspace
type parameter that defaults to()
.model(vi, ::Union{SampleFromPrior, SampleFromUniform}, ...)
to replace theSampleFromPrior{()}()
orSampleFromUniform{()}()
with an empty space bySampleFromPrior{getparams(model)}()
andSampleFromUniform{getparams(model)}()
respectively.sample
if sampling fromSampleFromPrior
.That's a long list, so I expect this to be a huge PR. But I think it's worth it :) The tests here should pass except for the test in #104. This is not a sign that everything is correct though, I still need to add and probably fix many more tests.