Skip to content

Commit

Permalink
Fix for #2205 (#2206)
Browse files Browse the repository at this point in the history
* fix for #2205

* added tests

* bump patch version

* relax ADVI tests a bit
  • Loading branch information
torfjelde authored Apr 24, 2024
1 parent 9be6b79 commit a328f8b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Turing"
uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
version = "0.31.0"
version = "0.31.1"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
7 changes: 5 additions & 2 deletions src/variational/VariationalInference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ function make_logjoint(model::DynamicPPL.Model; weight = 1.0)
DynamicPPL.DefaultContext(),
weight
)
model_contextualized = DynamicPPL.contextualize(model, ctx)
f = DynamicPPL.LogDensityFunction(model_contextualized)
f = DynamicPPL.LogDensityFunction(
model,
DynamicPPL.VarInfo(model),
ctx
)
return Base.Fix1(LogDensityProblems.logdensity, f)
end

Expand Down
20 changes: 20 additions & 0 deletions test/variational/advi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,24 @@
x = rand(q, 1000)
@test mean(eachcol(x)) [0.5, 0.5] atol=0.1
end

# Ref: https://github.com/TuringLang/Turing.jl/issues/2205
@turing_testset "with `condition` (issue #2205)" begin
@model function demo_issue2205()
x ~ Normal()
y ~ Normal(x, 1)
end

model = demo_issue2205() | (y = 1.0,)
q = vi(model, ADVI(10, 1000))
# True mean.
mean_true = 1 / 2
var_true = 1 / 2
# Check the mean and variance of the posterior.
samples = rand(q, 1000)
mean_est = mean(samples)
var_est = var(samples)
@test mean_est mean_true atol=0.2
@test var_est var_true atol=0.2
end
end

2 comments on commit a328f8b

@torfjelde
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/105523

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.31.1 -m "<description of version>" a328f8b8cfdfee38bcfdfee2712186e398fa5180
git push origin v0.31.1

Please sign in to comment.