-
Notifications
You must be signed in to change notification settings - Fork 36
resetaccs!!
before evaluating
#1013
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
Changes from all commits
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 |
---|---|---|
|
@@ -32,6 +32,12 @@ function PointwiseLogProbAccumulator{whichlogprob,KeyType}() where {whichlogprob | |
return PointwiseLogProbAccumulator{whichlogprob,KeyType,typeof(logps)}(logps) | ||
end | ||
|
||
function Base.:(==)( | ||
acc1::PointwiseLogProbAccumulator{wlp1}, acc2::PointwiseLogProbAccumulator{wlp2} | ||
) where {wlp1,wlp2} | ||
return (wlp1 == wlp2 && acc1.logps == acc2.logps) | ||
end | ||
|
||
Comment on lines
+35
to
+40
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. Unfortunately the test that I wrote (checking that all the accs are 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 suspect not, not without some sketchy metaprogramming or doing a loop over fieldnames or something else that feels dangerous. |
||
function Base.copy(acc::PointwiseLogProbAccumulator{whichlogprob}) where {whichlogprob} | ||
return PointwiseLogProbAccumulator{whichlogprob}(copy(acc.logps)) | ||
end | ||
|
@@ -56,10 +62,11 @@ function accumulator_name( | |
return Symbol("PointwiseLogProbAccumulator{$whichlogprob}") | ||
end | ||
|
||
function split(acc::PointwiseLogProbAccumulator{whichlogprob}) where {whichlogprob} | ||
function _zero(acc::PointwiseLogProbAccumulator{whichlogprob}) where {whichlogprob} | ||
return PointwiseLogProbAccumulator{whichlogprob}(empty(acc.logps)) | ||
end | ||
|
||
reset(acc::PointwiseLogProbAccumulator) = _zero(acc) | ||
split(acc::PointwiseLogProbAccumulator) = _zero(acc) | ||
function combine( | ||
acc::PointwiseLogProbAccumulator{whichlogprob}, | ||
acc2::PointwiseLogProbAccumulator{whichlogprob}, | ||
|
@@ -223,23 +230,37 @@ function pointwise_logdensities( | |
# Get the data by executing the model once | ||
vi = VarInfo(model) | ||
|
||
# This accumulator tracks the pointwise log-probabilities in a single iteration. | ||
AccType = PointwiseLogProbAccumulator{whichlogprob,KeyType} | ||
vi = setaccs!!(vi, (AccType(),)) | ||
|
||
iters = Iterators.product(1:size(chain, 1), 1:size(chain, 3)) | ||
|
||
# Maintain a separate accumulator that isn't tied to a VarInfo but rather | ||
# tracks _all_ iterations. | ||
all_logps = AccType() | ||
for (sample_idx, chain_idx) in iters | ||
# Update the values | ||
setval!(vi, chain, sample_idx, chain_idx) | ||
|
||
# Execute model | ||
vi = last(evaluate!!(model, vi)) | ||
|
||
# Get the log-probabilities | ||
this_iter_logps = getacc(vi, Val(accumulator_name(AccType))).logps | ||
|
||
# Merge into main acc | ||
for (varname, this_lp) in this_iter_logps | ||
# Because `this_lp` is obtained from one model execution, it should only | ||
# contain one variable, hence `only()`. | ||
push!(all_logps, varname, only(this_lp)) | ||
end | ||
end | ||
|
||
logps = getacc(vi, Val(accumulator_name(AccType))).logps | ||
niters = size(chain, 1) | ||
nchains = size(chain, 3) | ||
logdensities = OrderedDict( | ||
varname => reshape(vals, niters, nchains) for (varname, vals) in logps | ||
varname => reshape(vals, niters, nchains) for (varname, vals) in all_logps.logps | ||
) | ||
return logdensities | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,15 +20,21 @@ function ValuesAsInModelAccumulator(include_colon_eq) | |
return ValuesAsInModelAccumulator(OrderedDict{VarName,Any}(), include_colon_eq) | ||
end | ||
|
||
function Base.:(==)(acc1::ValuesAsInModelAccumulator, acc2::ValuesAsInModelAccumulator) | ||
return (acc1.include_colon_eq == acc2.include_colon_eq && acc1.values == acc2.values) | ||
end | ||
|
||
function Base.copy(acc::ValuesAsInModelAccumulator) | ||
return ValuesAsInModelAccumulator(copy(acc.values), acc.include_colon_eq) | ||
end | ||
|
||
accumulator_name(::Type{<:ValuesAsInModelAccumulator}) = :ValuesAsInModel | ||
|
||
function split(acc::ValuesAsInModelAccumulator) | ||
function _zero(acc::ValuesAsInModelAccumulator) | ||
return ValuesAsInModelAccumulator(empty(acc.values), acc.include_colon_eq) | ||
end | ||
Comment on lines
+33
to
35
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 preferred using |
||
reset(acc::ValuesAsInModelAccumulator) = _zero(acc) | ||
split(acc::ValuesAsInModelAccumulator) = _zero(acc) | ||
function combine(acc1::ValuesAsInModelAccumulator, acc2::ValuesAsInModelAccumulator) | ||
if acc1.include_colon_eq != acc2.include_colon_eq | ||
msg = "Cannot combine accumulators with different include_colon_eq values." | ||
|
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.
Good spot.