Turing.jl
Turing.jl copied to clipboard
Batch size reduced to zero when doing inference on ODEs
I'm trying to do inference on a 2D ODE model but I'm running into an error during inference where the batch size is reduced to 0 resulting in the following error:
┌ Warning: batch size was reduced to 0
└ @ MCMCDiagnosticTools ~/.julia/packages/MCMCDiagnosticTools/kKCOf/src/mcse.jl:30
ERROR: ArgumentError: step cannot be zero
Full traceback here.
MWE:
using Turing
using Distributions
using DifferentialEquations
using Random
Random.seed!(14)
Turing.setadbackend(:forwarddiff)
function model_step!(du, u, p, t)
du .*= 0
return nothing
end
p = 0.0
u0 = [1.0 1.0; 1.0 1.0]
timespan = (0.0, 10.0)
prob1 = ODEProblem(model_step!, u0, timespan, p)
sol1 = solve(prob1, Tsit5(), saveat = 0.1)
odedata = Array(sol1) + 0.8 * randn(size(Array(sol1)))
@model function fit_model(data, prob1)
σ ~ InverseGamma(2, 3) # ~ is the tilde character
α ~ truncated(Distributions.Normal(1.5, 0.5), 0.5, 2.5)
p = α
prob = remake(prob1, p = p, u0 = convert(Matrix{typeof(α)}, prob1.u0))
predicted = solve(prob, Tsit5(), saveat = 0.1)
for i = 1:length(predicted)
for j = 1:size(predicted[i])[1], k = 1:size(predicted[i])[2]
data[j, k, i] ~ Distributions.Normal(predicted[i][j, k], σ)
end
end
end
model = fit_model(odedata, prob1)
chain = sample(model, NUTS(), 1)
Here are all of the packages I have:
[6e4b80f9] BenchmarkTools v1.2.2
[a077e3f3] DiffEqProblemLibrary v4.14.0
[0c46a032] DifferentialEquations v7.0.0
[31c24e10] Distributions v0.25.37
[6a86dc24] FiniteDiff v2.8.1
[26cc04aa] FiniteDifferences v0.12.20
[f6369f11] ForwardDiff v0.10.24
[5c1252a2] GeometryBasics v0.4.1
[c7f686f2] MCMCChains v5.0.3
[6fe1bfb0] OffsetArrays v1.10.8
[1dea7af3] OrdinaryDiffEq v6.1.0
[91a5bcdd] Plots v1.25.4
[d236fae5] PreallocationTools v0.2.1
[1277b4bf] ShiftedArrays v1.0.0
[90137ffa] StaticArrays v1.3.1
[4c63d2b9] StatsFuns v0.9.14
[f3b207a7] StatsPlots v0.14.30
[fce5fe82] Turing v0.19.3
[b8865327] UnicodePlots v2.5.1
[e3e34ffb] VoronoiCells v0.3.0
[37e2e46d] LinearAlgebra
[9a3f8284] Random
Here is my versioninfo()
Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin19.5.0)
CPU: Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, haswell)
Environment:
JULIA_EDITOR = code
JULIA_NUM_THREADS =
A quick initial comment without having studied your code in detail: It seems the display of the chain errors but not sampling itself, so you can work around the problem by suppressing the output of the last line, e.g., by adding a semicolon.
I can confirm that this did indeed solve/mask the issue.
Another comment: Turing is not compatible with Julia 1.7 yet (did you install Libtask_jll manually to work around the dependency issues?).
The main problem though is that you only create a chain of a single sample but the Monte Carlo standard error (MCSE) estimate computed and shown when displaying a chain requires at least two samples: Otherwise in https://github.com/TuringLang/MCMCDiagnosticTools.jl/blob/ae8dfb276c26da74b1a0c1dc96f0c31d229ff600/src/mcse.jl#L29 is set to 0. This is also shown in your trace:
┌ Warning: batch size was reduced to 0
└ @ MCMCDiagnosticTools ~/.julia/packages/MCMCDiagnosticTools/kKCOf/src/mcse.jl:30
So sampling more than a single sample should fix the problem as well.
Seems fixed.