cscherrer / Soss.jl

Probabilistic programming via source rewriting
https://cscherrer.github.io/Soss.jl/stable/
MIT License
413 stars 30 forks source link

Readme #52

Closed zenon closed 4 years ago

zenon commented 5 years ago

Hi,

in the Readme, it says

using Soss 

m = @model X begin
   β ~ Normal() |> iid(size(X,2))
   y ~ For(eachrow(X)) do x
      Normal(x' * β, 1)
   end
end;

and then

truth = rand(m(x=randn(6)));

This gives; ERROR: UndefVarError: X not defined

zenon commented 5 years ago

I thougt that using a upper case X might help,

truth = rand(m(X=randn(6)));

ERROR: ArgumentError: Sampler for this object is not defined

Complete error message:

ERROR: ArgumentError: Sampler for this object is not defined Stacktrace: [1] Random.Sampler(::Type{Random.MersenneTwister}, ::Random.SamplerTrivial{For{GeneralizedGenerated.Closure{(β, x;) -> let (Soss).Normal((Soss).:(x', β), 1) end,Tuple{Array{Float64,1}}},1,Base.Generator{Base.OneTo{Int64},getfield(Base, Symbol("##176#177")){Array{Float64,1}}}},Any}, ::Val{1}) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\Random\src\Random.jl:143 [2] Random.Sampler(::Random.MersenneTwister, ::Random.SamplerTrivial{For{GeneralizedGenerated.Closure{(β, x;) -> let (Soss).Normal((Soss).:(x', β), 1) end,Tuple{Array{Float64,1}}},1,Base.Generator{Base.OneTo{Int64},getfield(Base, Symbol("##176#177")){Array{Float64,1}}}},Any}, ::Val{1}) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\Random\src\Random.jl:140 [3] rand(::Random.MersenneTwister, ::Random.SamplerTrivial{For{GeneralizedGenerated.Closure{(β, x;) -> let (Soss).Normal((Soss).:(x', β), 1) end,Tuple{Array{Float64,1}}},1,Base.Generator{Base.OneTo{Int64},getfield(Base, Symbol("##176#177")){Array{Float64,1}}}},Any}) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\Random\src\Random.jl:230 (repeats 2 times) [4] rand(::For{GeneralizedGenerated.Closure{(β, x;) -> let (Soss).Normal((Soss).:(x', β), 1) end,Tuple{Array{Float64,1}}},1,Base.Generator{Base.OneTo{Int64},getfield(Base, Symbol("##176#177")){Array{Float64,1}}}}) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\Random\src\Random.jl:235 [5] _rand(::Model{NamedTuple{(:X,),T} where T<:Tuple,begin β ~ Normal() |> iid(size(X, 2)) y ~ For(eachrow(X)) do x Normal(x' β, 1) end end}, ::NamedTuple{(:X,),Tuple{Array{Float64,1}}}) at C:\Users\Zenon.julia\packages\GeneralizedGenerated\x3uMp\src\closure_conv.jl:102 [6] rand(::Soss.JointDistribution{NamedTuple{(:X,),Tuple{Array{Float64,1}}},NamedTuple{(:X,),T} where T<:Tuple,begin β ~ Normal() |> iid(size(X, 2)) y ~ For(eachrow(X)) do x Normal(x' β, 1) end end}) at C:\Users\Zenon.julia\packages\Soss\JSNdl\src\rand.jl:8

zenon commented 5 years ago

The rest of the example code depends on it, so I'm stuck.

Except

weightedSample(m(σ=1), (μ=0.0,))

and

weightedSample(m(σ=1), (x=[-1,0,1],))

which both give: ERROR: UndefVarError: X not defined

and

nuts(m(σ=1),(x=[-1,0,1],)) |> particles

It leads to: ERROR: UndefVarError: nuts not defined

What am I doing wrong??

cscherrer commented 5 years ago

Hi @zenon ,

Sorry for the delay, I had missed the notification for this. I seem to have introduced a bug in the definition of For. I should have it fixed within a day or two. More details soon...

zenon commented 5 years ago

Cool, thank you! :-)

cscherrer commented 5 years ago

I made some code and README updates; it should work now in master. Thanks again for letting me know about this!

zenon commented 5 years ago

Hello Chad! Great, thank you!

I can repeat everything until

 symlogpdf(m2) |> expandSums

There are still issues in

 weightedSample(m(σ=1), (μ=0.0,))

weightedSample(m(σ=1), (x=[-1,0,1],))

The latter seems to just need an uppercasing of X. The first one wants an X too, and I don't know yet where.

cscherrer commented 5 years ago

I can repeat everything until

 symlogpdf(m2) |> expandSums

Hmm, that's weird. Does it throw an error? If you just type expandSums at the prompt, does it recognize it?

There are still issues in...

Oops, I still need to update that, it's referring to a different model. I think there's a way to have doctests, I need to get around to that

zenon commented 5 years ago

Ehm, with "until" I indended to mean "until and including" (this is not my native language)

A suggestion, helpful for readers that are unsure about what happens (like me), and used e.g. by Turing: Add a

 Random.seed!(...)

near the start of the readme, so we can reproduce your results exactly.

Do doctests exist for Julia? I agree this should make things easier :-)

cscherrer commented 5 years ago

I had forgotten about Weave! https://github.com/JunoLab/Weave.jl

cscherrer commented 5 years ago

Updated! Now it's in README.jmd, which produces README.md. It's not yet automated, for now I'm just calling weave on the command line

zenon commented 5 years ago

Cool.

zenon commented 5 years ago

I'm sorry, I feel like a gadfly, tbh. I updated Soss again, and in

symlogpdf(m2) |> expandSums |> foldConstants

foldConstants isn't found, and in the second benchmark line

 @btime logpdf($m2(X=X), $truth, $codegen)

I get

 ERROR: MethodError: no method matching 
  logpdf(::Soss.JointDistribution{NamedTuple{(:X,),Tuple{Array{Float64,2}}},NamedTuple{(:X,),T} where T<:Tuple,begin
     k = size(X, 2)
     β ~ Normal() |> iid(k)
     yhat = X * β
     N = size(X, 1)
     y ~ For(N) do j
             Normal(yhat[j], 1)
         end
 end}, ::NamedTuple{(:X, :β, :y),Tuple{Array{Float64,2},Array{Float64,1},Array{Float64,1}}}, 
 ::typeof(codegen))

Kind regards, and I really appreciate your patience. (I didn't try weave yet, I just tried the code by copy and paste. In case this makes a difference.)

cscherrer commented 5 years ago

No worries! I appreciate your help making sure it's all working properly.

Are you on master? The symbolic stuff is very recently updated

zenon commented 4 years ago

Master, update, and now everything works fine. Thank you!

zenon commented 4 years ago

If you got more documentation, I'll have a go at it too :-)