# Solve y'=y with neuralode

**URL:** https://discourse.julialang.org/t/solve-y-y-with-neuralode/109993
**Category:** Specific Domains
**Tags:** question, package, neural-network, differentialequation
**Created:** [February 9, 2024, 4:22pm UTC](https://discourse.julialang.org/t/solve-y-y-with-neuralode/109993 "2024-02-09T16:22:55Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ATR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atr/32/46739_2.png) [@ATR](https://discourse.julialang.org/u/ATR)
#### Post date: [February 9, 2024, 4:22pm UTC](https://discourse.julialang.org/t/solve-y-y-with-neuralode/109993/1 "2024-02-09T16:22:56Z")

</div>

Dear all, I am beginner in this topic and trying to solve simple y’=y (analytical solution: exp(t)) with y0=0 by modifying code on [Neural Ordinary Differential Equations · DiffEqFlux.jl (sciml.ai)](https://docs.sciml.ai/DiffEqFlux/stable/examples/neural_ode/), my code until now:  
begin  
# NeuralODE

```
u01 = Float32[2.; 0.]
n_samples = 16
tspan1 = (0.0f0, 1.0f0)

function trueODEfunc(du, u, p, t)
    true_A = [-0.1 2.0; -2.0 -0.1]
    du .= ((exp(t))'true_A)'
end
t = range(tspan1[1], tspan1[2], length=n_samples)
prob = DiffEqFlux.ODEProblem(trueODEfunc, u01, tspan1)
ode_data = Array(solve(prob, Tsit5(), saveat=t))

model = Chain(
    x -> exp(t),
    Dense(2, 50, tanh),
    Dense(50, 2),
)

n_ode = NeuralODE(model, tspan1, Tsit5(), saveat=t, reltol=1e-7, abstol=1e-9)
ps1 = Flux.params(n_ode)

loss_n_ode() = sum(abs2, ode_data .- n_ode(u01))

data = Iterators.repeated((), 25) # epochs

Flux.train!(loss_n_ode, ps1, data, ADAM(0.1))
pred = n_ode(u01)

traces = [
    scatter(x=t, y=ode_data[1, :], name="u_1", mode="lines+markers"),
    scatter(x=t, y=ode_data[2, :], name="u_2", mode="lines+markers"),
    scatter(x=t, y=pred[1, :], name="u_1 pred", mode="lines+markers"),
    scatter(x=t, y=pred[2, :], name="u_2 pred", mode="lines+markers"),
]

plt = plot(traces)

```

end

of course it throws error but am unable to understand the setup, does anybody has idea how to start to structure the code correctly? thank you in advance

---

<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: [February 9, 2024, 4:37pm UTC](https://discourse.julialang.org/t/solve-y-y-with-neuralode/109993/2 "2024-02-09T16:37:39Z")

</div>

Are you trying to learn it just from data? `x -> exp(t)` won’t work because it would have to be a function of `x`, and it’s a somewhat ill-advised to assume the derivative is an exponential function (since it’s the solution that would be).

Your true ODE function isn’t `y'=y` here, so I an quite get what you’re trying to do.

---

<div class="post-metadata">

### Author: ![ATR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atr/32/46739_2.png) [@ATR](https://discourse.julialang.org/u/ATR)
#### Post date: [February 9, 2024, 7:00pm UTC](https://discourse.julialang.org/t/solve-y-y-with-neuralode/109993/3 "2024-02-09T19:00:23Z")

</div>

thanks for your reply, the thing that i am trying to do is to solve simple ode y’=y for t =(0,1) with initial condition y0=1 by using PiNN and with Neural ODE:  
begin  
time\_min = 0.0  
time\_max = 1.0  
y0 = [1.0]  
end;  
function ode\_problem(dy, y, params, t)   
dy[1] = y[1]  
end;  
q\_analytical(t) = exp(t)  
numerical\_solution = let  
# Define the ODE problem  
prob = ODEProblem(ode\_problem, y0, (time\_min, time\_max))  
# Solve the ODE problem  
solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)  
end;  
this is my code prior to PiNN and Neural ODE, but I am quite confused how to put this to NN, should I simply generate data from exp() function and learn it? and if yes i am not sure how to implement it using available examples? thank you in advance

---

<div class="post-metadata">

### Author: ![ATR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atr/32/46739_2.png) [@ATR](https://discourse.julialang.org/u/ATR)
#### Post date: [February 9, 2024, 7:05pm UTC](https://discourse.julialang.org/t/solve-y-y-with-neuralode/109993/4 "2024-02-09T19:05:07Z")

</div>

the other thing i tried is:

# PiNN

begin  
linear(u, p, t) = u  
tspan = (0.0, 1.0)  
u0 = 0.0  
prob1 = ODEProblem(linear, u0, tspan)  
end  
begin  
rng = Random.default\_rng()  
Random.seed!(rng, 0)  
chain = Lux.Chain(Lux.Dense(1, 5, σ), Lux.Dense(5, 1))  
ps, st = Lux.setup(rng, chain) |\> Lux.f64  
opt = OptimizationOptimisers.Adam(0.1)  
alg = NeuralPDE.NNODE(chain, opt, init\_params = ps)  
end  
sol = solve(prob1, alg, verbose = true, maxiters = 2000, saveat = 0.01)

which after couple of executions learns the function but not sure if there is better solution?

---

<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: [February 10, 2024, 4:30pm UTC](https://discourse.julialang.org/t/solve-y-y-with-neuralode/109993/5 "2024-02-10T16:30:01Z")

</div>

> [@ATR](#):
>
> this is my code prior to PiNN and Neural ODE, but I am quite confused how to put this to NN, should I simply generate data from exp() function and learn it? and if yes i am not sure how to implement it using available examples? thank you in advance

What are you trying to do with it? If you know the ODE, you don’t need a neural ODE.

> [@ATR](#):
>
> which after couple of executions learns the function but not sure if there is better solution?

Using a standard ODE solver will be much faster than a NN here.

---

<div class="post-metadata">

### Author: ![ATR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atr/32/46739_2.png) [@ATR](https://discourse.julialang.org/u/ATR)
#### Post date: [February 10, 2024, 6:09pm UTC](https://discourse.julialang.org/t/solve-y-y-with-neuralode/109993/6 "2024-02-10T18:09:13Z")

</div>

Hi yes agree with you but it is simply the task i need to do 🙂 even if it is kind of a useless, and to me it is unclear how to push it thru NN so i was asking for help - that’s all.

---

<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: [February 10, 2024, 6:33pm UTC](https://discourse.julialang.org/t/solve-y-y-with-neuralode/109993/7 "2024-02-10T18:33:38Z")

</div>

I’m not 100% sure what you’re even trying to do.
