Closed yebai closed 7 years ago
UPDATE: after modifying the model to return states
, I get the following error:
MethodError: Cannot `convert` an object of type Array{Int64,1} to an object of type ForwardDiff.Dual{N,T<:Real}
This may have arisen from a call to the constructor ForwardDiff.Dual{N,T<:Real}(...),
since type constructors fall back to convert methods.
in copy!(::Base.LinearFast, ::Array{ForwardDiff.Dual,1}, ::Base.LinearFast, ::Array{Array{Int64,1},1}) at abstractarray.jl:559
in vectorize(::Distributions.Categorical{Float64}, ::Array{Int64,1}) at hmc_helper.jl:28
in update(::Turing.VarInfo, ::Turing.Sample, ::Set{Symbol}) at gibbs_helper.jl:7
in run(::Function, ::Dict{Any,Any}, ::Turing.GibbsSampler{Turing.Gibbs}) at gibbs.jl:67
in sample(::Function, ::Dict{Any,Any}, ::Turing.Gibbs) at gibbs.jl:81
in macro expansion; at util.jl:184 [inlined]
in anonymous at <missing>:?
in include_string(::String, ::String) at loading.jl:441
in include_string(::String, ::String, ::Int64) at eval.jl:28
in include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N}) at eval.jl:32
in (::Atom.##53#56{String,Int64,String})() at eval.jl:40
in withpath(::Atom.##53#56{String,Int64,String}, ::String) at utils.jl:30
in withpath(::Function, ::String) at eval.jl:46
in macro expansion at eval.jl:57 [inlined]
in (::Atom.##52#55{Dict{String,Any}})() at task.jl:60
This error also occur in another mixture model example. It seems that the Gibbs sampler try to differentiate through a discrete variable.
Yeah. I saw the same error as well. I'm working on fix this.
Fixed by #103
After updating the model with a prior over transitioning matrix, I run into another error
using Turing, Distributions
using ForwardDiff: Dual
using PyPlot, PyCall
K = 10
N = 51
obs = [ 0.0, 7.72711051, 2.76189162, 8.8216901 ,
10.80174329, 8.87655587, 0.47685358, 9.51892527,
7.82538035, 5.52629325, 10.75167786, 5.94925434,
-0.96912603, 1.65160838, 1.65005965, -0.99642713,
7.37803004, 5.40821392, 9.44046498, 8.51761132,
9.76981763, 5.980154 , 9.19558142, 5.33965621,
6.2388448 , 2.77755879, 6.67731151, 8.52411613,
11.31057577, 8.11554144, 6.64705471, 8.02025435,
9.84003587, 3.03943679, -2.93966727, 2.04372567,
-0.93734763, 3.66943525, 6.12876571, -2.07758649,
1.10420963, -0.23197037, 3.64908206, 14.14671815,
6.96651114, 7.28554932, 9.06049355, 6.54246834,
11.22672275, 7.41962631, 8.45635411 ];
@model bayeshmm(y) = begin
s = tzeros(Int64, N)
m = tzeros(Dual, K)
T = Array{Array}(K)
for i = 1:K
m[i] ~ Normal(0, 10)
T[i] ~ Dirichlet(ones(K))
end
s[1] ~ Categorical(ones(Float64, K)/K)
for i = 2:N
s[i] ~ Categorical(vec(T[s[i-1]]))
y[i] ~ Normal(m[s[i-1]], 4)
end
res = m[s[:]]
return(res, s, m)
end
g = Gibbs(300, HMC(2, 0.2, 5, :m, :T), PG(100,2, :s))
c = @sample(bayeshmm(obs), g);
ERROR
MethodError: Cannot `convert` an object of type Array{ForwardDiff.Dual{0,Float64},1} to an object of type ForwardDiff.Dual{0,Float64}
This may have arisen from a call to the constructor ForwardDiff.Dual{0,Float64}(...),
since type constructors fall back to convert methods.
in push!(::Array{ForwardDiff.Dual{0,Float64},1}, ::Array{ForwardDiff.Dual{0,Float64},1}) at ./array.jl:479
in varInfo2samples(::Turing.VarInfo) at /Users/yebai/.julia/v0.5/Turing/src/samplers/support/gibbs_helper.jl:41
in run(::Function, ::Dict{Any,Any}, ::Turing.GibbsSampler{Turing.Gibbs}) at /Users/yebai/.julia/v0.5/Turing/src/samplers/gibbs.jl:71
in sample(::Function, ::Dict{Any,Any}, ::Turing.Gibbs) at /Users/yebai/.julia/v0.5/Turing/src/samplers/gibbs.jl:81
in anonymous at ./<missing>:?
Running this example produces the following error: