Hi everyone.
I’m working on developing a NeuralODE trained on ad ODE which describes a building’s thermal behaviour through an RC equation.
I tried both with the definition of a NeuralODE from a multilayer NN and by training a single layer NN with Flux.train!. In both cases I get a dimensional mismatch problem.
I followed some examples solved by @ChrisRackauckas such as How to create a multilayer perceptron using DiffEqFlux.jl · Issue #554 · SciML/DiffEqFlux.jl · GitHub, but still I couldn’t solve/understand this issues.
I would really appreciate some help!
Thanks in advace.
using DifferentialEquations, Plots, Flux,Optim, DiffEqFlux, DataInterpolations,Random, ComponentArrays, Lux
using Optimization, OptimizationOptimisers, OptimizationOptimJL
function RC!(du,u,p,t)
Rv, Ci = p
Text = ext_disturbance(t)
phih= ext_disturbance2(t)
du[1] = 1/(Rv*Ci) .* (Text .- u[1]) .+ phih/Ci
end
u0= [5.0]
tspan= (0.0f0, 5.0f00)
t= range(tspan[1], tspan[2], length=10)
p= [0.05, 10.0]
Testerna = [1,2,3,4,5,6,7,8,9,10]
phih = [10, 20, 30, 40, 50, 60 ,70 ,80 ,90 ,100];
disturbance = LinearInterpolation(Testerna,t);
disturbance2 = LinearInterpolation(phih,t);
function ext_disturbance(t)
return disturbance(t)
end
function ext_disturbance2(t)
return disturbance2(t)
end
prob=ODEProblem(RC!, u0, tspan, p)
sol= solve(prob, Tsit5(), saveat=t)
plot(t,sol[1,:], label = “reale”,xlabel = “tempo [s]”, ylabel = “Temperatura [°C]”);
prob3 = ODEProblem(RC!, u0, tspan, p_random)
dudt= Lux.Chain( p-> solve(prob3, Tsit5(), saveat=t, p=p)[1,:],
vec,
Lux.Dense(10,50, tanh),
Lux.Dense(50,1))
n_ode= NeuralODE(dudt, tspan, Tsit5(), saveat=t)
p = [0.05, 10]
rng= Random.default_rng()
p, st = Lux.setup(rng,dudt)
function predict_neuralode(p)
Array(n_ode(u0,p,st)[1])
end
function loss_neuralode(p)
pred= predict_neuralode(p)
loss = sum(abs2, sol .- pred)
return loss, pred
end
callback = function ()
display(loss())
end
pinit = ComponentArray(p)
adtype = Optimization.AutoZygote()
optf = Optimization.OptimizationFunction((x, p) → loss_neuralode(x), adtype)
optprob = Optimization.OptimizationProblem(optf, pinit)
result_neuralode = Optimization.solve(optprob,
Optimisers.Adam(0.05),
callback = callback,
maxiters = 400)
This is the beginning of the error that I got ( it is too long to be copied):
BoundsError: attempt to access 1-element Vector{Float64} at index [2]
Stacktrace:
[1] getindex
@ .\essentials.jl:13 [inlined]
[2] indexed_iterate
@ .\tuple.jl:89 [inlined]
[3] RC!(du::Vector{Float64}, u::Vector{Float64}, p::Vector{Float64}, t::Float32)
@ Main .\In[78]:5
[4] Void
@ C:\Users\Michele.julia\packages\SciMLBase\s9wrq\src\utils.jl:473 [inlined]
[5] (::FunctionWrappers.CallWrapper{Nothing})(f::SciMLBase.Void{typeof(RC!)}, arg1::Vector{Float64}, arg2::Vector{Float64}, arg3::Vector{Float64}, arg4::Float32)
@ FunctionWrappers C:\Users\Michele.julia\packages\FunctionWrappers\Q5cBx\src\FunctionWrappers.jl:65
[6] macro expansion
@ C:\Users\Michele.julia\packages\FunctionWrappers\Q5cBx\src\FunctionWrappers.jl:137 [inlined]
[7] do_ccall
@ C:\Users\Michele.julia\packages\FunctionWrappers\Q5cBx\src\FunctionWrappers.jl:125 [inlined]
[8] FunctionWrapper
@ C:\Users\Michele.julia\packages\FunctionWrappers\Q5cBx\src\FunctionWrappers.jl:144 [inlined]
[9] _call
@ C:\Users\Michele.julia\packages\FunctionWrappersWrappers\9XR0m\src\FunctionWrappersWrappers.jl:12 [inlined]
[10] FunctionWrappersWrapper
@ C:\Users\Michele.julia\packages\FunctionWrappersWrappers\9XR0m\src\FunctionWrappersWrappers.jl:10 [inlined]
[11] ODEFunction
@ C:\Users\Michele.julia\packages\SciMLBase\s9wrq\src\scimlfunctions.jl:2126 [inlined] …