Main Content

deep.ode.options.ODE45

Neural ODE solver options for nonstiff differential equations

Since R2025a

    Description

    A deep.ode.options.ODE45 object specifies options for the "ode45" solver of a neural ordinary differential equation (ODE) layer.

    The solver is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair. Specify the options object using the SolverOptions property of a neuralODELayer object. For options to use with ode objects, see matlab.ode.options.ODE45 Properties.

    Creation

    Create a neuralODELayer object and set the Solver property to "ode45". To set the solver options of the layer, use dot notation. For example, to set the initial step size to 1e-3, use layer.SolverOptions.InitialStep = 1e-3, where layer is an instance of the neural ODE layer.

    In most cases, you do not need to create the deep.ode.options.ODE45 directly.

    Properties

    expand all

    Suggested initial step size, stored as positive scalar or [].

    The initial step size sets an upper bound on the size of the first step that the solver tries.

    If you do not specify an initial step size, then the solver bases the initial step size on the slope of the solution at the initial time point of the integration. If the slope of all solution components is zero, then the solver might try a step size that is too large. If you know that the initial step size is too large, or if you want to be sure that the solver resolves important behavior at the beginning of the integration, then provide a suitable initial step size.

    Data Types: single | double

    Maximum step, stored as positive scalar or [].

    The maximum step size sets an upper bound on the size of any step that the solver takes. If the equation has periodic behavior, for example, then you can set the maximum step size to a fraction of the period so that the solver does not step over an area of interest.

    Do not use the maximum step size to increase the accuracy of the solution. If the solution is not accurate enough, then reduce the value of RelativeTolerance and use the solution to determine a suitable value for AbsoluteTolerance.

    Data Types: single | double

    Relative error tolerance, specified as a positive scalar. This tolerance measures the error relative to the magnitude of each solution component. Roughly speaking, it controls the number of correct digits in all solution components, except those smaller than the absolute tolerance AbsoluteTolerance.

    At each step, the ODE solver estimates the local error e in the ith component of the solution. To be successful, the step must have acceptable error, as determined by both the relative and absolute error tolerances:

    |e(i)| <= max(RelativeTolerance*abs(y(i)),AbsoluteTolerance(i))

    Data Types: single | double

    Absolute error tolerance, specified as a positive scalar or vector. This tolerance is a threshold below which the value of the solution becomes unimportant. If the solution |y| is smaller than AbsoluteTolerance, then the solver does not need to obtain any correct digits in |y|. For this reason, the value of AbsoluteTolerance should take into account the scale of the solution components.

    If AbsoluteTolerance is a vector, then it must be the same length as the solution. If AbsoluteTolerance is a scalar, then the value applies to all solution components.

    At each step, the ODE solver estimates the local error e in the ith component of the solution. To be successful, the step must have acceptable error, as determined by both the relative and absolute error tolerances:

    |e(i)| <= max(RelativeTolerance*abs(y(i)),AbsoluteTolerance(i))

    Data Types: single | double

    Method to compute gradients with respect to the initial conditions and parameters, specified as one of these values:

    • "direct" — Compute gradients by backpropagating through the operations undertaken by the numerical solver.

    • "adjoint" — Compute gradients by solving the associated adjoint ODE system.

    • "adjoint-seminorm" — Compute gradients using adjoint seminorm computations [3]. This option typically uses fewer terms in the approximation error computations and can speed up some neural ODE models.

    The dlaccelerate function does not support accelerating forward passes of networks with neural ODE layers with the GradientMode property set to "direct". To accelerate forward passes of networks with neural ODE layers, set the GradientMode property to "adjoint" or "adjoint-seminorm", or accelerate parts of your code that do not perform forward passes of the network.

    Warning

    When GradientMode is "adjoint", all layers in the Network property of the neural ODE layer must support acceleration. Otherwise, the software can return unexpected results.

    When GradientMode is "adjoint", the software traces the forward pass of the Network property of the neural ODE layer to determine the computation graph used for automatic differentiation. This tracing process can take some time and can end up recomputing the same trace. By optimizing, caching, and reusing the traces, the software can speed up the gradient computation.

    For more information on deep learning function acceleration, see Deep Learning Function Acceleration for Custom Training Loops.

    The deep.ode.options.ODE45 object stores this property as a character vector.

    Examples

    collapse all

    Create a neural ODE layer. Specify an ODE network containing a convolution layer followed by a tanh layer. Specify a time interval of [0, 1].

    inputSize = [14 14 8];
    
    layersODE = [
        imageInputLayer(inputSize)
        convolution2dLayer(3,8,Padding="same")
        tanhLayer];
    
    netODE = dlnetwork(layersODE);
    
    tspan = [0 1];
    layer = neuralODELayer(netODE,tspan)
    layer = 
      NeuralODELayer with properties:
    
                     Name: ''
             TimeInterval: [0 1]
    
       Learnable Parameters
                  Network: [1×1 dlnetwork]
    
       Solver properties
             GradientMode: 'direct'
        RelativeTolerance: 1.0000e-03
        AbsoluteTolerance: 1.0000e-06
                   Solver: ode45
    
      Show all properties
    
    

    Specify an initial step size of 1e-3 and a maximum step size of 1e-2.

    layer.SolverOptions.InitialStep = 1e-3;
    layer.SolverOptions.MaxStep = 1e-2;

    View the solver options.

    layer.SolverOptions
    ans = 
      ODE45 with properties:
    
              InitialStep: 1.0000e-03
                  MaxStep: 0.0100
        RelativeTolerance: 1.0000e-03
        AbsoluteTolerance: 1.0000e-06
             GradientMode: 'direct'
    
    

    Tips

    • To apply the neural ode45 operation in deep learning models defined as functions or in custom layer functions, use dlode45.

    Algorithms

    The ode45 algorithm is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair. It is a single-step solver – in computing y(tn), it needs only the solution at the immediately preceding time point, y(tn-1). [1] [2]

    References

    [1] Dormand, J. R. and P. J. Prince, “A family of embedded Runge-Kutta formulae,” J. Comp. Appl. Math., Vol. 6, 1980, pp. 19–26.

    [2] Shampine, L. F. and M. W. Reichelt, “The MATLAB ODE Suite,” SIAM Journal on Scientific Computing, Vol. 18, 1997, pp. 1–22.

    [3] Kidger, Patrick, Ricky T. Q. Chen, and Terry Lyons. “‘Hey, That’s Not an ODE’: Faster ODE Adjoints via Seminorms.” arXiv, May 10, 2021. https://doi.org/10.48550/arXiv.2009.09457.

    Version History

    Introduced in R2025a