# Been testing example of Neural ODE code with Julia 1.85. i get error ComponentArray

**URL:** <https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276>\
**Category:** New to Julia\
**Created:** [June 13, 2023, 11:02am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276 "2023-06-13T11:02:44Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)\
**Post date:** [June 13, 2023, 11:02am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/1 "2023-06-13T11:02:44Z")

</div>

''Lux, DiffEqFlux, DifferentialEquations, Optimization, OptimizationOptimJL, Random, Plots,ComponentArrays

rng = Random.default\_rng()  
u0 = Float32[2.0; 0.0]  
datasize = 30  
tspan = (0.0f0, 1.5f0)  
tsteps = range(tspan[1], tspan[2], length = datasize)

function trueODEfunc(du, u, p, t)  
true\_A = [-0.1 2.0; -2.0 -0.1]  
du .= ((u.^3)‘true\_A)’  
end

prob\_trueode = ODEProblem(trueODEfunc, u0, tspan)  
ode\_data = Array(solve(prob\_trueode, Tsit5(), saveat = tsteps))

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 = 300)

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 13, 2023, 11:27pm UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/2 "2023-06-13T23:27:15Z")

</div>

What error do you get? Can you share the whole stack trace?

---

<div class="post-metadata">

**Author:** ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)\
**Post date:** [June 14, 2023, 4:01am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/3 "2023-06-14T04:01:43Z")

</div>

UndefVarError: ComponentArray not defined

Stacktrace:  
[1] getproperty(x::Module, f::Symbol)  
@ Base .\Base.jl:31  
[2] top-level scope  
@ In[59]:1

---

<div class="post-metadata">

**Author:** ![Wen-Wei\_Tseng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wen-wei_tseng/32/28839_2.png) [@Wen-Wei\_Tseng](https://discourse.julialang.org/u/Wen-Wei_Tseng)\
**Post date:** [June 14, 2023, 4:57am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/4 "2023-06-14T04:57:09Z")

</div>

You could add and use the `ComponentArrays` package explicitly.

Like here: [First Neural ODE example — PDEs](https://sosiristseng.github.io/jl-pde/ude-01-firstexample.html)

Edit: I should have referred the original source [Neural Ordinary Differential Equations · DiffEqFlux.jl](https://docs.sciml.ai/DiffEqFlux/dev/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 14, 2023, 11:37am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/5 "2023-06-14T11:37:15Z")

</div>

> [@ak47](#):
>
> Lux, DiffEqFlux, DifferentialEquations, Optimization, OptimizationOptimJL, Random, Plots,ComponentArrays

If I’m reading your quote literally, this is incorrect because you dropped the `using` statement:

```julia
using Lux, DiffEqFlux, DifferentialEquations, Optimization, OptimizationOptimJL, Random, Plots,ComponentArrays

```

and you need to make sure these packages are installed.

---

<div class="post-metadata">

**Author:** ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)\
**Post date:** [June 15, 2023, 2:35am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/6 "2023-06-15T02:35:44Z")

</div>

Thank you. Your example is running smoothly with Julia 1.9.1.

---

<div class="post-metadata">

**Author:** ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)\
**Post date:** [June 15, 2023, 2:38am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/7 "2023-06-15T02:38:56Z")

</div>

Thank you Chris. But, there seems to be a complain at line ‘pinit = Lux.ComponentArray(p)’ (with Julia 1.9.1).

---

<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 15, 2023, 3:01am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/8 "2023-06-15T03:01:39Z")

</div>

> [@ak47](#):
>
> But, there seems to be a complain at line ‘pinit = Lux.ComponentArray(p)’ (with Julia 1.9.1).

What does it say? Please post full stack traces.

---

<div class="post-metadata">

**Author:** ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)\
**Post date:** [June 15, 2023, 7:03am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/9 "2023-06-15T07:03:48Z")

</div>

UndefVarError: `ComponentArray` not defined

Stacktrace:  
[1] getproperty(x::Module, f::Symbol)  
@ Base .\Base.jl:31  
[2] top-level scope  
@ In[2]:46

---

<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 16, 2023, 12:23pm UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/10 "2023-06-16T12:23:25Z")

</div>

What did you get when you did `using ComponentArrays`?

---

<div class="post-metadata">

**Author:** ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)\
**Post date:** [June 16, 2023, 12:35pm UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/11 "2023-06-16T12:35:57Z")

</div>

Same error as before.

---

<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 16, 2023, 12:37pm UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/12 "2023-06-16T12:37:00Z")

</div>

So you opened a new REPL, did `using ComponentArrays`, got no warnings or error, and then did `ComponetArray(x = rand(4,4))` or something of the sort and it said it wasn’t defined?

---

<div class="post-metadata">

**Author:** ![jslin](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jslin](https://discourse.julialang.org/u/jslin)\
**Post date:** [July 2, 2023, 8:39am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/13 "2023-07-02T08:39:39Z")

</div>

I am using Julia 1.8.5. Being new to Julia, I also have problems. I copied the code from “[Neural Ordinary Differential Equations · DiffEqFlux.jl](https://docs.sciml.ai/DiffEqFlux/stable/examples/neural_ode)”

1. I have the same error with pinit = Lux.ComponentArray(p), and it works after I add using ComponentArrays and remove Lux as:  
Using ComponentArrays  
pinit = ComponentArray(p)
2. But I am still stuck at optimization.solve using ADAM, and I changed them to the following, but it still not working with the error " Adam not defined"  
using OptimizationOptimisers  
result\_neuralode = Optimization.solve(optprob,  
Optim.Adam(0.05),  
callback = callback,  
maxiters = 300)  
Error message changed to “optimization algorithm not found” if I used only Adm instead of Optim.Adam as below  
esult\_neuralode = Optimization.solve(optprob,  
Adam(0.05),  
callback = callback,  
maxiters = 300)  
Any suggestion how I can proceed? Thanks.

---

<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:** [July 2, 2023, 9:20am UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/14 "2023-07-02T09:20:40Z")

</div>

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

I thought I linked it to you the other day. I’m trying to figure out why the docs build is behind.

---

<div class="post-metadata">

**Author:** ![jslin](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jslin](https://discourse.julialang.org/u/jslin)\
**Post date:** [July 2, 2023, 1:02pm UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/15 "2023-07-02T13:02:42Z")

</div>

## Sorry, I am a different person but encountered the same problem. I did download the linked file you mentioned. Still have the error with ADAM line result\_neuralode = Optimization.solve(optprob, ADAM(0.05), callback = callback, maxiters = 300) The error message is as follows:

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: [Optimization.jl: A Unified Optimization Package · Optimization.jl](https://docs.sciml.ai/Optimization/stable/).

---

<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:** [July 2, 2023, 4:55pm UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/16 "2023-07-02T16:55:33Z")

</div>

Did you `using OptimizationOptimisers`?

---

<div class="post-metadata">

**Author:** ![David\_A\_Ch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_a_ch/32/51207_2.png) [@David\_A\_Ch](https://discourse.julialang.org/u/David_A_Ch)\
**Post date:** [July 2, 2023, 10:02pm UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/17 "2023-07-02T22:02:56Z")

</div>

Hello, I am facing the same error and I already imported and use the package OptimizationOptimisers.

---

<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:** [July 2, 2023, 10:26pm UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/18 "2023-07-02T22:26:25Z")

</div>

Can you share an MWE?

---

<div class="post-metadata">

**Author:** ![jslin](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jslin](https://discourse.julialang.org/u/jslin)\
**Post date:** [July 2, 2023, 11:53pm UTC](https://discourse.julialang.org/t/been-testing-example-of-neural-ode-code-with-julia-1-85-i-get-error-componentarray/100276/19 "2023-07-02T23:53:00Z")

</div>

After I restart the computer, and ran the code as posted, The original source original source works. [Neural Ordinary Differential Equations · DiffEqFlux.jl](https://docs.sciml.ai/DiffEqFlux/dev/examples/neural_ode/)

I am not sure what happened. Even though it gave me error before but now it works. Is there some thing one should do to clear existing catch or precompiled stuff.

Much appreciated.
