Skip to contents

solve_gradient(loss, prob, alg, ..., sensealg) solves prob with solve(prob, alg, ...) and returns the gradient of loss(sol) with respect to the initial values and the parameters of the problem. It is the R counterpart of differentiating a solve in Julia, Zygote.gradient((u0, p) -> loss(solve(remake(prob; u0, p), alg; sensealg, kwargs...)), u0, p), the "differentiating ODE solutions" of SciMLSensitivity: sensealg selects the rule by which the solve is differentiated, as the sensealg keyword of Julia's solve.

Usage

solve_gradient(
  loss,
  prob,
  alg = NULL,
  ...,
  sensealg = NULL,
  allow_failure = FALSE,
  loss_grad = NULL,
  loss_rows = NULL
)

Arguments

loss

A function of the solution returning a number.

prob

An ODEProblem, DAEProblem, DDEProblem, NonlinearProblem, SteadyStateProblem, OptimizationProblem, SDEProblem or RODEProblem. A DAEProblem F(du, u, p, t) = 0 of index 1 is solved and differentiated as the mass matrix problem u_d' = w, 0 = F(w, u, p, t) (with FBDF() for DFBDF(), Rodas5P() for other DAE methods), so its events and every sensitivity method apply.

alg

The algorithm for solve().

...

Options for solve() (saveat, abstol, reltol, dt, seed, ...); the adjoint is solved with the same tolerances.

sensealg

A sensitivity algorithm; chosen automatically when NULL (or taken from the problem's sensealg option).

allow_failure

By default a solve that does not succeed (a retcode other than Success or Terminated, such as MaxIters) is an error; TRUE differentiates the trajectory that was computed.

loss_grad

Optional derivative of the loss: a function of the solution returning the derivative with respect to the saved values (a matrix shaped as as.matrix(sol), or a list with that matrix as u and the derivative with respect to the final parameters as p). It replaces the finite differences of the loss in the saved values. With save_idxs the matrix has the saved components only, as the loss sees them. Values the loss reads by interpolation, sol(t), are not saved values: they are still differenced, so loss_grad gives the derivative in the saved values alone.

loss_rows

Optional declaration of the saved rows the loss reads (indices of the rows of as.matrix(sol), or "last" for a terminal loss): the loss is differentiated in those rows only, and treated as independent of the others. Without it every saved value is differentiated.

Value

A list with the gradients u0 and p, the loss value value and the solution sol.

Details

The loss receives the solution and may use its saved values (sol$u, sol[x], sol[expr], as.matrix(sol)) and parameters (sol$ps[k]); give the times it needs as saveat. It may also interpolate values sol(t): the solve is repeated with those times saved (added to saveat, or as stops without saveat), hidden from the saved values the loss reads, so they are differentiated like the others; the returned sol includes them. Its derivatives with respect to the saved values and the parameters are taken by central differences, then pulled back by

A complex initial state is solved as the real system of its real and imaginary parts; the gradient in u0 is dL/dRe(u0) + i dL/dIm(u0).

Events are differentiated, including the dependence of event times on the parameters. When the problem was built from a System whose initial values depend on the parameters through initialization, the gradient with respect to p includes that dependence.

The value is the loss of the solution the gradient differentiates: the forward solve itself for the adjoint methods, and the solve of the state with its sensitivities for the forward methods, whose step sizes also control the error of the sensitivities (as in SciMLSensitivity), so it can differ from loss(solve(...)) within the tolerances. A failed numerical stage (the forward solve, the backward adjoint solve or a checkpoint replay) is an error naming the stage, its retcode and the time where it stopped, unless allow_failure = TRUE.

Examples

prob <- ODEProblem(function(u, p, t) -p[1] * u, 1, c(0, 1), 2)
loss <- function(sol) sum(as.matrix(sol))
g1 <- solve_gradient(loss, prob, Tsit5(), saveat = 0.25, reltol = 1e-10, abstol = 1e-12)
g2 <- solve_gradient(loss, prob, Tsit5(), saveat = 0.25, reltol = 1e-10, abstol = 1e-12,
                     sensealg = GaussAdjoint())
c(g1$p, g2$p)
#> [1] -0.6382553 -0.6382553