Hi all,
I get this type error which appears after I try to index with the output of indexin()
which is of type: Array{Union{Nothing, Int64},1}
. I have tried to fix this via all of the following methods:
match_idx = convert(Array{Int64}, indexin(0:24:144, sol.t))
match_idx = replace(indexin(0:24:144, sol.t), nothing => Int64)
match_idx = indexin(0:24:144, sol.t)
match_idxI = Array{Int64}(undef, length(match_data))
for i=1:length(match_data); match_idxI[i] = match_idx[i]; end
none of which work; each produces an error about converting from nothing to Int64. However, I noticed bizarrely though, any of these options work if I run the function where this issue exists line by line in the REPL?? Its only when I call the function (which is my loss function) from sciML_train that I get the error. Moreover, I can call the function on its own without any trouble and it actually works in the first case without any fixes.
Here is a relevant excerpt of the code:
function loss(p_curr)
sol = DifferentialEquations.solve(prob, Tsit5(), p=p_curr, saveat=tsteps, callback=cb)
match_idx = indexin(0:24:144, sol.t)
# alternatives:
# match_idx = convert(Array{Int64}, indexin(0:24:144, sol.t))
# match_idx = replace(indexin(0:24:144, sol.t), nothing => Int64)
# match_idxN = indexin(0:24:144, sol.t)
# match_idx = Array{Int64}(undef, length(match_data))
# for i=1:length(match_data); match_idx[i] = match_idxN[i]; end
pred = Array(sol)[:, match_idx]
loss = sum(abs2, pred - syndata)
return loss, sol, match_data
end
result_ode = DiffEqFlux.sciml_train(loss, p_init,
ADAM(0.1),
cb = callback,
maxiters = 300)
Note, I do this because the callback function in my ODE problem causes the solution to include time points beyond the ‘saveat’ interval that I don’t care about. Another work around would be totally fine.
Full error:
WARNING: both Flux and Plots export "scatter"; uses of it in module Main must be qualified
ERROR: ArgumentError: unable to check bounds for indices of type Nothing
Stacktrace:
[1] checkindex(::Type{Bool}, ::Base.OneTo{Int64}, ::Nothing) at ./abstractarray.jl:561
[2] checkindex at ./abstractarray.jl:576 [inlined]
[3] checkbounds_indices at ./abstractarray.jl:532 [inlined] (repeats 2 times)
[4] checkbounds at ./abstractarray.jl:485 [inlined]
[5] checkbounds at ./abstractarray.jl:506 [inlined]
[6] _getindex at ./multidimensional.jl:742 [inlined]
[7] getindex at ./abstractarray.jl:1060 [inlined]
[8] adjoint at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/lib/array.jl:31 [inlined]
[9] _pullback(::Zygote.Context, ::typeof(getindex), ::Array{Float64,2}, ::Function, ::Array{Union{Nothing, Int64},1}) at /Users/willsharpless/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:57
[10] loss at ./none:11 [inlined]
[11] _pullback(::Zygote.Context, ::typeof(loss), ::Array{Float64,1}) at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/compiler/interface2.jl:0
[12] #69 at /Users/willsharpless/.julia/packages/DiffEqFlux/pJFx8/src/train.jl:3 [inlined]
[13] _pullback(::Zygote.Context, ::DiffEqFlux.var"#69#70"{typeof(loss)}, ::Array{Float64,1}, ::SciMLBase.NullParameters) at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/compiler/interface2.jl:0
[14] adjoint at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/lib/lib.jl:191 [inlined]
[15] _pullback at /Users/willsharpless/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:57 [inlined]
[16] OptimizationFunction at /Users/willsharpless/.julia/packages/SciMLBase/fypD8/src/problems/basic_problems.jl:107 [inlined]
[17] _pullback(::Zygote.Context, ::OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::SciMLBase.NullParameters) at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/compiler/interface2.jl:0
[18] adjoint at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/lib/lib.jl:191 [inlined]
[19] adjoint(::Zygote.Context, ::typeof(Core._apply_iterate), ::typeof(iterate), ::OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Tuple{Array{Float64,1},SciMLBase.NullParameters}) at ./none:0
[20] _pullback at /Users/willsharpless/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:57 [inlined]
[21] OptimizationFunction at /Users/willsharpless/.julia/packages/SciMLBase/fypD8/src/problems/basic_problems.jl:107 [inlined]
[22] _pullback(::Zygote.Context, ::OptimizationFunction{false,GalacticOptim.AutoZygote,OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},GalacticOptim.var"#146#156"{GalacticOptim.var"#145#155"{OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Nothing}},GalacticOptim.var"#149#159"{GalacticOptim.var"#145#155"{OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Nothing}},GalacticOptim.var"#154#164",Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::SciMLBase.NullParameters) at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/compiler/interface2.jl:0
[23] adjoint at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/lib/lib.jl:191 [inlined]
[24] _pullback at /Users/willsharpless/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:57 [inlined]
[25] #8 at /Users/willsharpless/.julia/packages/GalacticOptim/JnLwV/src/solve.jl:94 [inlined]
[26] _pullback(::Zygote.Context, ::GalacticOptim.var"#8#13"{OptimizationProblem{false,OptimizationFunction{false,GalacticOptim.AutoZygote,OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},GalacticOptim.var"#146#156"{GalacticOptim.var"#145#155"{OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Nothing}},GalacticOptim.var"#149#159"{GalacticOptim.var"#145#155"{OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Nothing}},GalacticOptim.var"#154#164",Nothing,Nothing,Nothing},Array{Float64,1},SciMLBase.NullParameters,Nothing,Nothing,Nothing,Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol},NamedTuple{(:cb, :maxiters),Tuple{var"#45#46",Int64}}}},Array{Float64,1},GalacticOptim.NullData}) at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/compiler/interface2.jl:0
[27] pullback(::Function, ::Zygote.Params) at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/compiler/interface.jl:247
[28] gradient(::Function, ::Zygote.Params) at /Users/willsharpless/.julia/packages/Zygote/pM10l/src/compiler/interface.jl:58
[29] __solve(::OptimizationProblem{false,OptimizationFunction{false,GalacticOptim.AutoZygote,OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},GalacticOptim.var"#146#156"{GalacticOptim.var"#145#155"{OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Nothing}},GalacticOptim.var"#149#159"{GalacticOptim.var"#145#155"{OptimizationFunction{true,GalacticOptim.AutoZygote,DiffEqFlux.var"#69#70"{typeof(loss)},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Nothing}},GalacticOptim.var"#154#164",Nothing,Nothing,Nothing},Array{Float64,1},SciMLBase.NullParameters,Nothing,Nothing,Nothing,Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol},NamedTuple{(:cb, :maxiters),Tuple{var"#45#46",Int64}}}}, ::ADAM, ::Base.Iterators.Cycle{Tuple{GalacticOptim.NullData}}; maxiters::Int64, cb::Function, progress::Bool, save_best::Bool, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/willsharpless/.julia/packages/GalacticOptim/JnLwV/src/solve.jl:93
[30] #solve#450 at /Users/willsharpless/.julia/packages/SciMLBase/fypD8/src/solve.jl:3 [inlined]
[31] sciml_train(::typeof(loss), ::Array{Float64,1}, ::ADAM, ::GalacticOptim.AutoZygote; lower_bounds::Nothing, upper_bounds::Nothing, kwargs::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol},NamedTuple{(:cb, :maxiters),Tuple{var"#45#46",Int64}}}) at /Users/willsharpless/.julia/packages/DiffEqFlux/pJFx8/src/train.jl:6
[32] top-level scope at none:1