Skip to content
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

Cannot reuse model when using ReverseDiff and Memoization #1393

Closed
hstrey opened this issue Aug 26, 2020 · 1 comment · Fixed by #1414
Closed

Cannot reuse model when using ReverseDiff and Memoization #1393

hstrey opened this issue Aug 26, 2020 · 1 comment · Fixed by #1414

Comments

@hstrey
Copy link

hstrey commented Aug 26, 2020

Here is a little program that illustrates the problem:

using Turing, ReverseDiff, Memoization
using Statistics
using Random
Turing.setadbackend(:reversediff)
turing.setrdcache(true)
Turing.turnprogress(false)
using Distributions
for i in 1:10
    N = 1000
    d = Normal(0.0, i)
    data=rand(d,N)
    @model g(rn,T) = begin
        sigma ~ Uniform(0.0,20.0)
        rn ~ MvNormal(zeros(T),sigma)
    end
    chn = sample(g(data,N),NUTS(0.65),1000)
    println("Std of chain: ",mean(Array(chn[:sigma])),"Std of data: ",std(data))
end

I am creating a dataset that samples from a Normal distributions and our model is estimating the sigma of the distribution. When you run the code you can see that the sampler samples only from the first dataset and therefore the estimated mean for sigma is always around 1. I tried to run Turing.emptyrdcache() after sampling but that did not solve the problem.

Here is my Project.toml:
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Memoization = "6fafb56a-5788-4b4e-91ca-c0cea6611c73"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

@mohamed82008
Copy link
Member

This issue is a bit weird. If emptying the cache is not helping then it is probably not emptying it properly. Maybe something changed with Memoization.jl. The second unusual thing here is that the same tape should be good enough for all the iterations and it should give the correct gradient. So I am suspecting something maybe wrong with ReverseDiff here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants