# UndefVarError: ComponentArray not defined

**URL:** <https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884>\
**Category:** New to Julia\
**Tags:** sciml\
**Created:** [June 27, 2023, 10:18am UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884 "2023-06-27T10:18:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Nesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_nesta/32/50318_2.png) [@Marco\_Nesta](https://discourse.julialang.org/u/Marco_Nesta)\
**Post date:** [June 27, 2023, 10:18am UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/1 "2023-06-27T10:18:41Z")

</div>

using Lux, DiffEqFlux, DifferentialEquations, Optimization, OptimizationOptimJL, Random, Plots

rng = Random.default\_rng()  
u0 = Float32[1.0, 1.0]  
datasize = 80  
tspan = (0.0f0, 10f0)  
tsteps = range(tspan[1], tspan[2], length = datasize)  
p = Float32[1.5, 1.0, 3.0, 1.0]

function lotka\_volterra(du,u,p,t)  
x, y = u  
α, β, δ, γ = p  
du[1] = dx = α_x - β_x_y  
du[2] = dy = -δ_y + γ_x_y  
end  
prob\_trueode = ODEProblem(lotka\_volterra,u0,tspan,p)

ode\_data = solve(prob\_trueode, Tsit5(), saveat = tsteps)  
plot(tsteps, ode\_data[1,:], label = [“x” “y”], xlabel = “Time”, ylabel = “Population”, title = “Lotka-Volterra”, lw = 3)  
dudt2 = Lux.Chain(x → x.^3,  
Lux.Dense(2, 50, tanh),  
Lux.Dense(50, 2))  
p, st = Lux.setup(rng, dudt2)  
prob\_neuralode = NeuralODE(dudt2, tspan, Tsit5(), saveat = tsteps)

function predict\_neuralode(p)  
Array(prob\_neuralode(u0, p, st)[1])  
end

function loss\_neuralode(p)  
pred = predict\_neuralode(p)  
loss = sum(abs2, ode\_data .- pred)  
return loss, pred  
end

# Do not plot by default for the documentation

# Users should change doplot=true to see the plots callbacks

callback = function (p, l, pred; doplot = false)  
println(l)

# plot current prediction against data

if doplot  
plt = scatter(tsteps, ode\_data[1,:], label = “data”)  
scatter!(plt, tsteps, pred[1,:], label = “prediction”)  
display(plot(plt))  
end  
return false  
end

pinit = Lux.ComponentArray(p)  
callback(pinit, loss\_neuralode(pinit)…; doplot=true)

# use Optimization.jl to solve the problem

adtype = Optimization.AutoZygote()

optf = Optimization.OptimizationFunction((x, p) → loss\_neuralode(x), adtype)  
optprob = Optimization.OptimizationProblem(optf, pinit)

result\_neuralode = Optimization.solve(optprob,  
ADAM(0.05),  
callback = callback,  
maxiters = 500)

optprob2 = remake(optprob,u0 = result\_neuralode.u)

result\_neuralode2 = Optimization.solve(optprob2,  
Optim.BFGS(initial\_stepnorm=0.01),  
callback=callback,  
allow\_f\_increases = false)

callback(result\_neuralode2.u, loss\_neuralode(result\_neuralode2.u)…; doplot=true)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 27, 2023, 12:51pm UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/2 "2023-06-27T12:51:44Z")

</div>

Did you install ComponentArrays?

---

<div class="post-metadata">

**Author:** ![Marco\_Nesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_nesta/32/50318_2.png) [@Marco\_Nesta](https://discourse.julialang.org/u/Marco_Nesta)\
**Post date:** [June 28, 2023, 10:52am UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/3 "2023-06-28T10:52:19Z")

</div>

Hello Chris, yes, I have installed ComponentArrays.  
[Manifest.toml](https://discourse.julialang.org/uploads/short-url/6KvDWHeEIizUFMAtdZ8eq5eUgTq.toml) (94.8 KB)  
[Project.toml](https://discourse.julialang.org/uploads/short-url/8EVZkdd0DOpe9c6uQeOWDd5S2kZ.toml) (1.4 KB)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 28, 2023, 12:28pm UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/4 "2023-06-28T12:28:08Z")

</div>

oh it looks like you’re missing the `using ComponantArrays` here. Where was this example pulled from?

---

<div class="post-metadata">

**Author:** ![Marco\_Nesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_nesta/32/50318_2.png) [@Marco\_Nesta](https://discourse.julialang.org/u/Marco_Nesta)\
**Post date:** [June 28, 2023, 12:35pm UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/5 "2023-06-28T12:35:26Z")

</div>

[Neural Ordinary Differential Equations · DiffEqFlux.jl (sciml.ai)](https://docs.sciml.ai/DiffEqFlux/stable/examples/neural_ode/)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 28, 2023, 1:01pm UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/6 "2023-06-28T13:01:50Z")

</div>

Oh interesting, the dev version is updated: [Neural Ordinary Differential Equations · DiffEqFlux.jl](https://docs.sciml.ai/DiffEqFlux/dev/examples/neural_ode/). I’ll make sure to trigger the stable version to update.

---

<div class="post-metadata">

**Author:** ![Marco\_Nesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_nesta/32/50318_2.png) [@Marco\_Nesta](https://discourse.julialang.org/u/Marco_Nesta)\
**Post date:** [June 28, 2023, 1:06pm UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/7 "2023-06-28T13:06:39Z")

</div>

Thank you, Chris. For my thesis project, the idea is to use this approach to simulate an RC circuit using NODEs and see if it can help in predicting the internal temperatures of a house. What do you think? I would be glad to know.

---

<div class="post-metadata">

**Author:** ![Marco\_Nesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_nesta/32/50318_2.png) [@Marco\_Nesta](https://discourse.julialang.org/u/Marco_Nesta)\
**Post date:** [June 28, 2023, 1:46pm UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/8 "2023-06-28T13:46:22Z")

</div>

I tried changing pinit=Lux.Componentsarray(p) with pinit = deepcopy(p) but Know i have this error

Optimization algorithm not found. Either the chosen algorithm is not a valid solver  
choice for the `OptimizationProblem`, or the Optimization solver library is not loaded.  
Make sure that you have loaded an appropriate Optimization.jl solver library, for example,  
`solve(prob,Optim.BFGS())` requires `using OptimizationOptimJL` and  
`solve(prob,Adam())` requires `using OptimizationOptimisers`.

For more information, see the Optimization.jl documentation: [https://docs.sciml.ai/Optimization/stable/](https://docs.sciml.ai/Optimization/stable/).

Chosen Optimizer: Stacktrace: [1] \_\_solve(::OptimizationProblem{true, OptimizationFunction{true, Optimization.AutoZygote, var"#26#27", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT\_OBSERVED\_NO\_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, NamedTuple{(:layer\_1, :layer\_2, :layer\_3), Tuple{NamedTuple{(), Tuple{}}, NamedTuple{(:weight, :bias), Tuple{Matrix{Float32}, Matrix{Float32}}}, NamedTuple{(:weight, :bias), Tuple{Matrix{Float32}, Matrix{Float32}}}}}, SciMLBase.NullParameters, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, ::Adam; kwargs::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:callback, :maxiters), Tuple{var"#23#25", Int64}}}) @ SciMLBase [C:\Users\marco.julia\packages\SciMLBase\QqtZA\src\solve.jl:173](file:///C:/Users/marco/.julia/packages/SciMLBase/QqtZA/src/solve.jl:173) [2] solve(::OptimizationProblem{true, OptimizationFunction{true, Optimization.AutoZygote, var"#26#27", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT\_OBSERVED\_NO\_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, NamedTuple{(:layer\_1, :layer\_2, :layer\_3), Tuple{NamedTuple{(), Tuple{}}, NamedTuple{(:weight, :bias), Tuple{Matrix{Float32}, Matrix{Float32}}}, NamedTuple{(:weight, :bias), Tuple{Matrix{Float32}, Matrix{Float32}}}}}, SciMLBase.NullParameters, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, ::Adam; kwargs::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:callback, :maxiters), Tuple{var"#23#25", Int64}}}) @ SciMLBase [C:\Users\marco.julia\packages\SciMLBase\QqtZA\src\solve.jl:84](file:///C:/Users/marco/.julia/packages/SciMLBase/QqtZA/src/solve.jl:84) [3] top-level scope

---

<div class="post-metadata">

**Author:** ![Marco\_Nesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_nesta/32/50318_2.png) [@Marco\_Nesta](https://discourse.julialang.org/u/Marco_Nesta)\
**Post date:** [June 28, 2023, 1:47pm UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/9 "2023-06-28T13:47:13Z")

</div>

Adam(0.05

, (0.9, 0.999), 1.0e-8

, IdDict{Any, Any}())

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 28, 2023, 3:20pm UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/10 "2023-06-28T15:20:20Z")

</div>

Did you change your code entirely to the updated tutorial from the dev version? I just set the doc rebuild so that stable is fixed, but you should make sure you’re using the Lux updated form.

---

<div class="post-metadata">

**Author:** ![Marco\_Nesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_nesta/32/50318_2.png) [@Marco\_Nesta](https://discourse.julialang.org/u/Marco_Nesta)\
**Post date:** [June 28, 2023, 3:54pm UTC](https://discourse.julialang.org/t/undefvarerror-componentarray-not-defined/100884/11 "2023-06-28T15:54:02Z")

</div>

I solved it using the same code in Julia on VS Code instead of using Jupyter Notebook, I don’t know why but now it works. thanks Chris
