Combining DiffEqFlux with Turing for Bayesian Inference and Neural ODEs

Hi all, I’m trying to implement a Bayesian Neural Network. The catch is that this neural network is used to approximate a function that is then integrated to predict time series generated by an ODE.

Reading through Turing’s tutorials I naively implemented the following:


ann = FastChain(FastDense(2, 5, sigmoid), FastDense(5,2, sigmoid))
nn_ode = NeuralODE(ann, (0.0, 4.9), alg = Vern7(), saveat = 0.1)

@model function fitlv(data)
    σ ~ InverseGamma(2, 3)
    p ~ MvNormal(zeros(27), 0.5)
    predict = Array(nn_ode(u0, p))
  
    for i = 1:size(data,2)
    
        data[:,i] ~ MvNormal(vec(predict[:,i]), σ)
    end
end

model = fitlv(odedata)
chain = sample(model, HMC(0.05, 4), 500)

Where odedata is a 2*50 array with my training time series.

I run into the following error:

MethodError: Cannot `convert` an object of type Nothing to an object of type Array{Array{Float64,1},1}
Closest candidates are:
  convert(::Type{Array{T,N}}, !Matched::StaticArrays.SizedArray{S,T,N,M} where M) where {T, S, N} at /home/aslan_garcia/.julia/packages/StaticArrays/l7lu2/src/SizedArray.jl:72
  convert(::Type{Array{T,N}}, !Matched::AxisArrays.AxisArray{T,N,D,Ax} where Ax where D) where {T, N} at /home/aslan_garcia/.julia/packages/AxisArrays/IFpjG/src/core.jl:304
  convert(::Type{Array{T,N}}, !Matched::FillArrays.Zeros{V,N,Axes} where Axes) where {T, V, N} at /home/aslan_garcia/.julia/packages/FillArrays/NjFh2/src/FillArrays.jl:419

Any pointers will be very much appreciated, thanks!

Join the Slack. You might want to talk with Raj who is working through some of these examples right now.

1 Like