Taking gradients when propagating through an ODE solver

I need to differentiate parameters of my model (neural network) through an ODE solver. Apparently, DiffEqFlux allows me to do that but it seems that the gradients are computed only if the network parameters enter directly as the parameters of ODEprob(…). If these parameters enter indirectly (through some functional dependence) Flux returns ‘nothing’.

To make it clearer, I have the following example where I make one step of an ODE which is implemented in two slightly different ways.

using Flux, DiffEqFlux, DifferentialEquations

#weights and biases by hand

#ini state

function f1(u,p,t) #evaluates the coefficint in the body of f1
   return alpha*u

function f2(u,p,t) #p is directly the coefficient
    return p[1]*u

tspan = (0.0,dt)

prob1 = ODEProblem(f1,u0,tspan,p1) #model parameters directly in ODEproblem

prob2 = ODEProblem(f2,u0,tspan,p2) #model parameters indirectly in ODEproblem

function dynamics1()
   solve(prob1,Heun(), dt=dt,saveat=[dt],adaptive=false)[1,:]

function dynamics2()
   solve(prob2,Heun(), dt=dt,saveat=[dt],adaptive1=false)[1,:]

#some loss function
criterion1() = sum(abs.(dynamics1()-u0))
criterion2() = sum(abs.(dynamics2()-u0))

# analytic grad W
# analytic grad b
θ = Flux.params(p1)
gs1 = gradient(θ) do

gs2 = gradient(θ) do

println("grads f1:", gs1[p1] ) #Float32[0.020503124, 0.010251562]
println("grads f2:", gs2[p1] ) #nothing

In prob1 the parameters of the network p1 enter directly the ODEproblem(…,p1) and are further elaborated in the body of the function f1. Whereas in prob2, these parameters enter the ODEproblem through parameters p2 which depend on p1.

The results in the forward pass — dynamics1() and dynamics2() — are the same (and correct as can be checked analytically). However, if I take gradients of the network parameters p1, in the case of prob1 I get the correct results and in the case prob2 I get ‘nothing’.

Is there a way to make Flux compute the gradients also for the implementation with prob2?

1 Like

If you make your parameter vector p = [p1;p2] then grab the right parts like re(p[1:N]) it’ll work.

Thanks for your reply! However, I must be still missing something. I understand that I need to insert both
p1 and p2 as Flux parameters. When I do as you suggest I still get zero gradients for the network parameters p1.

These are the parts of the code that I have changed

function f2(u,p,t)
    return p[end]*u

p = [p1;p2]
prob2 = ODEProblem(f2,u0,tspan,p) 

θ = Flux.params(p)
gs2 = gradient(θ) do

println("grads:", gs2[p] ) #Float32[0.0, 0.0, 0.010251562]


criterion2 only uses part of the parameters, and correctly computes the gradient for that part. Did you mean to run both criterion1 and criterion2?

That’s the thing. I would like to use criterion2() and get the gradients of p1 as well. I somehow thought that it is possible as p2 is a function of p1.

Oh I missed that. You can’t differentiate that in isolation then. You have to differentiate the full process if you want the derivative of the full process.

θ = Flux.params([p1,p2])
gs2 = gradient(θ) do
1 Like

Great! Makes sense…Thanks for help!

1 Like