# Neural Hybrid DE

**URL:** <https://discourse.julialang.org/t/neural-hybrid-de/102599>\
**Category:** Machine Learning\
**Tags:** differentialequation\
**Created:** [August 8, 2023, 12:10pm UTC](https://discourse.julialang.org/t/neural-hybrid-de/102599 "2023-08-08T12:10:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mieszko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mieszko/32/202770_2.png) [@Mieszko](https://discourse.julialang.org/u/Mieszko)\
**Post date:** [August 8, 2023, 12:10pm UTC](https://discourse.julialang.org/t/neural-hybrid-de/102599/1 "2023-08-08T12:10:55Z")

</div>

Hey, I am following this article [Neural Hybrid Differential Equations | juliabloggers.com](https://www.juliabloggers.com/neural-hybrid-differential-equations/) concerning Neural Hybrid DE.  
Currently, I am stuck at this moment with my code.

```julia

using DiffEqFlux, DifferentialEquations, Flux, Optimization
using Plots, DiffEqSensitivity, Zygote, Random, OptimizationFlux
using SciMLSensitivity, OrdinaryDiffEq

u0= Float32[1.;0.]

tspan = (0.0f0, 50.0f0)

dtsave = 0.5f0

t = tspan[1]:dtsave:tspan[2]

function oscilator!(du, u, p, t)
    du[1] = u[2]
    du[2] = -u[1] - 1//10*u[2]
end

prob_data = ODEProblem(oscilator!, u0, tspan)

pl = plot(solve(prob_data, Tsit5(), saveat = t), label =["x(t)" "v(t)"])

kicktimes = tspan[1]:1:tspan[2]

function kick!(integrator)
    integrator.u[end] += one(eltype(integrator.u))
end

cb_= PresetTimeCallback(kicktimes, kick!, save_positions=(true, true))

sol_data = solve(prob_data, Tsit5(), callback=cb_, saveat=t)
t_data = sol_data.t
ode_data = Array(sol_data)

pl1 = plot(t_data, ode_data[1, :], label = "data x(t)")
plot!(pl1, t_data, ode_data[2, :], label = "data v(t)")

pl2 = plot(t_data[1:20], ode_data[1,1:20], label="data x(t)")
plot!(pl2,t_data[1:20], ode_data[2,1:20], label="data v(t)")
pl = plot(pl2, pl1, layout=(1,2), xlabel="t")

nn1 = Chain(Dense(2, 64, tanh), Dense(64, 1))

p_nn1 = Flux.params(nn1)

function f1!(du,u,p,t)
    du[1] = u[2]
    du[2] = - u[1] - 1//10*u[2]
    du[3] = nn1(u[1:2], p)[1]
    return nothing
  end
  
  affect!(integrator) = integrator.u[2] += integrator.u[3]
  cb = PresetTimeCallback(kicktimes,affect!,save_positions=(false,false))
  z0 = Float32[u0;one(u0[1])]
  prob1 = ODEProblem(f1!,z0,tspan,p_nn1)
  
  function loss(p; prob=prob1, sensealg = ReverseDiffAdjoint())
    pred = Array(solve(prob,Tsit5(),callback=cb,
                 saveat=dtsave,sensealg=sensealg))[1:2,:]
    sum(abs2, ode_data .- pred)
  end
  
  function loss(p, prob=prob1, sensealg = ReverseDiffAdjoint())
    _prob = remake(prob,p=p)
    pred = Array(solve(_prob,Tsit5(),callback=cb,
                 saveat=dtsave,sensealg=sensealg))[1:2,:]
    sum(abs2,ode_data .- pred)
  end
  
  loss(p_nn1)

```

The error I am getting is: MethodError: no method matching (::Chain{Tuple{Dense{typeof(tanh),  
Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}})(::Vector{Float32}, ::Params{Zygote.Buffer{Any, Vector{Any}}}).  
Any help would be appreciated 🙂

---

<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:** [August 8, 2023, 1:30pm UTC](https://discourse.julialang.org/t/neural-hybrid-de/102599/2 "2023-08-08T13:30:38Z")

</div>

You may want to instead look at the tutorials in the library which are tested and maintained:

[https://docs.sciml.ai/SciMLSensitivity/dev/examples/hybrid\_jump/hybrid\_diffeq/](https://docs.sciml.ai/SciMLSensitivity/dev/examples/hybrid_jump/hybrid_diffeq/)

[https://docs.sciml.ai/SciMLSensitivity/dev/examples/hybrid\_jump/bouncing\_ball/](https://docs.sciml.ai/SciMLSensitivity/dev/examples/hybrid_jump/bouncing_ball/)

@frankschae maybe link to them from the blog post?

---

<div class="post-metadata">

**Author:** ![frankschae](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankschae/32/8382_2.png) [@frankschae](https://discourse.julialang.org/u/frankschae)\
**Post date:** [August 8, 2023, 6:14pm UTC](https://discourse.julialang.org/t/neural-hybrid-de/102599/3 "2023-08-08T18:14:17Z")

</div>

FWIW, I think you want to do:

```julia
p_nn1, re = Flux.destructure(nn1)
function f1!(du, u, p, t)
    du[1] = u[2]
    du[2] = -u[1] - 1 // 10 * u[2]
    du[3] = re(p)(u[1:2])[1]
    return nothing
end

```

> @frankschae maybe link to them from the blog post?

Good idea. I’ll update the post.
