# Error with training decoupled NODEs

**URL:** <https://discourse.julialang.org/t/error-with-training-decoupled-nodes/58311>\
**Category:** Machine Learning\
**Tags:** question\
**Created:** [March 31, 2021, 4:16pm UTC](https://discourse.julialang.org/t/error-with-training-decoupled-nodes/58311 "2021-03-31T16:16:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvkoch](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@jvkoch](https://discourse.julialang.org/u/jvkoch)\
**Post date:** [March 31, 2021, 4:16pm UTC](https://discourse.julialang.org/t/error-with-training-decoupled-nodes/58311/1 "2021-03-31T16:16:56Z")

</div>

I’m attempting to train two decoupled neural ODEs simultaneously. Although decoupled, these ODEs interact through a coupled loss function (not shown). I have constructed this in two ways: 1) by writing two separate ODEProblem() definitions and evaluating the dependent loss function and 2) as included in this post; that is, by writing a single ODEProblem() definition as a decoupled pair of ODEs. Both methods fail with strange errors.

A minimum working example (I’ve removed all of the training data and custom loss function) is as follows (I’m running Julia 1.6):

```julia
using DiffEqFlux, DifferentialEquations, Flux

tSpan = (0.0,3.0)
tSteps = range(tSpan[1], tSpan[2], length=4)

L = FastChain(FastDense(1,3,sigmoid),FastDense(3,1))
p = [initial_params(L);-0.1] # appended w/ initial param for linear ODE to be trained

function dudt(u,p,t)
	z = L(u[1],p[1:end-1])
	return [p[end]*u[1],z[1]]
end

evolveODE = ODEProblem(dudt, [1.0,1.0], tSpan, p)

function lossFun(p)

	u = Array(solve(evolveODE, Tsit5(), saveat = tSteps))
	loss = sum(abs2, u[1,:])

	return loss, u[1,:]
end

callback = function (p, l, pred)
  display(l)
  return false
end

result_neuralode = DiffEqFlux.sciml_train(lossFun,
                                           p,
                                           ADAM(0.05),
                                           cb = callback,
                                           maxiters = 5)

```

As written, the ODEProblem() can be solved straight away in the REPL, However, in this simple script, it fails with the error:

> MethodError: Cannot `convert` an object of type Bool to an object of type Matrix{Float64}

This confuses me - what Boolean object exists in this example? As mentioned, I tried this two different ways - the other method also gives me a different error, but this example is simpler to post and work through.

Any thoughts?

---

<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:** [April 1, 2021, 12:35pm UTC](https://discourse.julialang.org/t/error-with-training-decoupled-nodes/58311/2 "2021-04-01T12:35:12Z")

</div>

The neural network needs an array input. Working code:

```julia
using DiffEqFlux, DifferentialEquations, Flux

tSpan = (0.0,3.0)
tSteps = range(tSpan[1], tSpan[2], length=4)

L = FastChain(FastDense(1,3,sigmoid),FastDense(3,1))
p = [initial_params(L);-0.1] # appended w/ initial param for linear ODE to be trained

function dudt(u,p,t)
	z = L([u[1]],p[1:end-1])
	return [p[end]*u[1],z[1]]
end

evolveODE = ODEProblem(dudt, [1.0,1.0], tSpan, p)

function lossFun(p)

	u = Array(solve(evolveODE, Tsit5(), p=p, saveat = tSteps))
	loss = sum(abs2, u[1,:])

	return loss, u[1,:]
end

callback = function (p, l, pred)
  display(l)
  return false
end

result_neuralode = DiffEqFlux.sciml_train(lossFun,
                                           p,
                                           ADAM(0.05),
                                           cb = callback,
                                           maxiters = 500)

```
