# Failure to calculate the gradient of ODE with the RHS as an argument: "LoadError: TypeError: in TrackedReal, in V, expected V\<:Real, got Type{Any}"

**URL:** https://discourse.julialang.org/t/failure-to-calculate-the-gradient-of-ode-with-the-rhs-as-an-argument-loaderror-typeerror-in-trackedreal-in-v-expected-v-real-got-type-any/41480
**Category:** General Usage
**Tags:** diffeq, flux
**Created:** [June 15, 2020, 11:19pm UTC](https://discourse.julialang.org/t/failure-to-calculate-the-gradient-of-ode-with-the-rhs-as-an-argument-loaderror-typeerror-in-trackedreal-in-v-expected-v-real-got-type-any/41480 "2020-06-15T23:19:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yanz4](https://avatars.discourse-cdn.com/v4/letter/y/b5e925/32.png) [@yanz4](https://discourse.julialang.org/u/yanz4)
#### Post date: [June 15, 2020, 11:19pm UTC](https://discourse.julialang.org/t/failure-to-calculate-the-gradient-of-ode-with-the-rhs-as-an-argument-loaderror-typeerror-in-trackedreal-in-v-expected-v-real-got-type-any/41480/1 "2020-06-15T23:19:58Z")

</div>

Dear Forum,

I am trying to add ODE into machine learning pipeline and calculate the gradient of an ode layer that takes the RHS as an argument. We tried to feed the RHS, in terms of _inputFunction(t)_, into ODE (i.e. fiip function). In fiip, _inputFunction(t)_ is extracted from p, which is fed in function _model(inputdata)_.  
The code is as below.

```julia
using Flux
using DifferentialEquations, DiffEqSensitivity

parameter = [0.5]

function fiip(du, u, p, t)
    p1, inputFunction = p
    du[1] = dx = p1 * u[1] + inputFunction(t)
    du[2] = dy = -p1 * u[2]
end

function model(inputdata)
    inputFunction(t) = inputdata * t
    
    p = [parameter[1], inputFunction]
    
    u0 = [1.0; 1.0];
    tspan = (0.0, 10.0)
    
    concrete_solve(
        ODEProblem(fiip, u0, tspan, p),
        Tsit5(),
        u0,
        p,
        saveat = 0:0.1:10,
        sensealg = QuadratureAdjoint(),
    )[1,:]
end

loss(x, y) = Flux.mse(model(x), y) # Loss function

x = 10
y = 100 * sin.(0:0.1:10)

# Gradient calculation
gs = Flux.gradient(() -> loss(x, y), params(parameter)) 

```

When the code executes to the last line, i.e. calculate the gradient of ODE with respect to the designated parameter, such error pops up:

> LoadError: TypeError: in TrackedReal, in V, expected V\<:Real, got Type{Any}

**We’d be highly grateful for any insight into the possible origin and solution to this problem.**  
Also, will it help if we use callbacks on the ODE solver instead?

---

<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, 2020, 11:30pm UTC](https://discourse.julialang.org/t/failure-to-calculate-the-gradient-of-ode-with-the-rhs-as-an-argument-loaderror-typeerror-in-trackedreal-in-v-expected-v-real-got-type-any/41480/2 "2020-06-15T23:30:07Z")

</div>

Are you using Julia v1.4?

---

<div class="post-metadata">

### Author: ![yanz4](https://avatars.discourse-cdn.com/v4/letter/y/b5e925/32.png) [@yanz4](https://discourse.julialang.org/u/yanz4)
#### Post date: [June 15, 2020, 11:31pm UTC](https://discourse.julialang.org/t/failure-to-calculate-the-gradient-of-ode-with-the-rhs-as-an-argument-loaderror-typeerror-in-trackedreal-in-v-expected-v-real-got-type-any/41480/3 "2020-06-15T23:31:45Z")

</div>

Version 1.4.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, 2020, 11:34pm UTC](https://discourse.julialang.org/t/failure-to-calculate-the-gradient-of-ode-with-the-rhs-as-an-argument-loaderror-typeerror-in-trackedreal-in-v-expected-v-real-got-type-any/41480/4 "2020-06-15T23:34:59Z")

</div>

Oh, the issue is

> [@yanz4](#):
>
> `p = [parameter[1], inputFunction]`

is not an array of numbers. The adjoint methods need that the parameter vector is an array of values you can compute on. You can just use a closure for the anonymous function

---

<div class="post-metadata">

### Author: ![yanz4](https://avatars.discourse-cdn.com/v4/letter/y/b5e925/32.png) [@yanz4](https://discourse.julialang.org/u/yanz4)
#### Post date: [June 16, 2020, 6:16pm UTC](https://discourse.julialang.org/t/failure-to-calculate-the-gradient-of-ode-with-the-rhs-as-an-argument-loaderror-typeerror-in-trackedreal-in-v-expected-v-real-got-type-any/41480/6 "2020-06-16T18:16:55Z")

</div>

Thanks so much for your informative comments. It worked for us!
