torchdyn.numerics

Numerical methods (DE solvers and root finding methods) required for inference and training of torchdyn models. Exposes a functional API that can be also used to simulate various systems and generate data.

torchdyn.numerics.odeint module

Functional API of ODE integration routines, with specialized functions for different options odeint and odeint_mshooting prepare and redirect to more specialized routines, detected automatically.

torchdyn.numerics.odeint._adaptive_odeint(f, k1, x, dt, t_span, solver, atol=0.0001, rtol=0.0001, args=None, interpolator=None, return_all_eval=False, seminorm=(False, None))[source]

Adaptive ODE solve routine, called by odeint.

Parameters
  • f ([type]) –

  • k1 ([type]) –

  • x ([type]) –

  • dt ([type]) –

  • t_span ([type]) –

  • solver ([type]) –

  • atol ([type], optional) – Defaults to 1e-4.

  • rtol ([type], optional) – Defaults to 1e-4.

  • args (Dict) –

  • use_interp (bool, optional) –

  • return_all_eval (bool, optional) – Defaults to False.

Notes

(1) We check if the user wants all evaluated solution points, not only those corresponding to times in t_span. This is automatically set to True when odeint is called for interpolated adjoints

torchdyn.numerics.odeint._fixed_odeint(f, x, t_span, solver, save_at=(), args={})[source]

Solves IVPs with same t_span, using fixed-step methods

torchdyn.numerics.odeint._jagged_fixed_odeint(f, x, t_span, solver)[source]

Solves ``n_segments’’ jagged IVPs in parallel with fixed-step methods. Each sub-IVP can vary in number

of solution steps and step sizes

Returns:

A list of `len(t_span)’ containing solutions of each IVP computed in parallel.

torchdyn.numerics.odeint._shifted_fixed_odeint(f, x, t_span)[source]

Solves ``n_segments’’ jagged IVPs in parallel with fixed-step methods. All subproblems have equal step sizes and number of solution points

Notes

Assumes dt fixed. TODO: update in each loop evaluation.

torchdyn.numerics.odeint.odeint(f, x, t_span, solver, atol=0.001, rtol=0.001, t_stops=None, verbose=False, interpolator=None, return_all_eval=False, save_at=(), args={}, seminorm=(False, None))[source]

Solve an initial value problem (IVP) determined by function f and initial condition x.

Functional odeint API of the torchdyn package.

Parameters
  • f (Callable) –

  • x (Tensor) –

  • t_span (Union[List, Tensor]) –

  • solver (Union[str, nn.Module]) –

  • atol (float, optional) – Defaults to 1e-3.

  • rtol (float, optional) – Defaults to 1e-3.

  • t_stops (Union[List, Tensor, None], optional) – Defaults to None.

  • verbose (bool, optional) – Defaults to False.

  • interpolator (bool, optional) – Defaults to False.

  • return_all_eval (bool, optional) – Defaults to False.

  • save_at (Union[List, Tensor], optional) – Defaults to t_span

  • args (Dict) – Arbitrary parameters used in step

  • seminorm (Tuple[bool, Union[int, None]], optional) – Whether to use seminorms in local error computation.

Returns

returns a Tuple (t_eval, solution).

Return type

Tuple[Tensor, Tensor]

Return type

Tuple[Tensor, Tensor]

torchdyn.numerics.odeint.odeint_hybrid(f, x, t_span, j_span, solver, callbacks, atol=0.001, rtol=0.001, event_tol=0.0001, priority='jump', seminorm=(False, None))[source]
Solve an initial value problem (IVP) determined by function f and initial condition x, with jump events defined

by a callbacks.

Parameters
  • f ([type]) –

  • x ([type]) –

  • t_span ([type]) –

  • j_span ([type]) –

  • solver ([type]) –

  • callbacks ([type]) –

  • t_eval (list, optional) – Defaults to [].

  • atol ([type], optional) – Defaults to 1e-3.

  • rtol ([type], optional) – Defaults to 1e-3.

  • event_tol ([type], optional) – Defaults to 1e-4.

  • priority (str, optional) – Defaults to ‘jump’.

torchdyn.numerics.odeint.odeint_mshooting(f, x, t_span, solver, B0=None, fine_steps=2, maxiter=4)[source]

Solve an initial value problem (IVP) determined by function f and initial condition x using parallel-in-time solvers.

Parameters
  • f (Callable) – vector field

  • x (Tensor) – batch of initial conditions

  • t_span (Tensor) – integration interval

  • solver (Union[str, nn.Module]) – parallel-in-time solver.

  • B0 ([type], optional) – Initialized shooting parameters. If left to None, will compute automatically using the coarse method of solver. Defaults to None.

  • fine_steps (int, optional) – Defaults to 2.

  • maxiter (int, optional) – Defaults to 4.

Notes

TODO: At the moment assumes the ODE to NOT be time-varying. An extension is possible by adaptive the step function of a parallel-in-time solvers.

torchdyn.numerics.odeint.odeint_symplectic(f, x, t_span, solver, atol=0.001, rtol=0.001, verbose=False, return_all_eval=False, save_at=())[source]

Solve an initial value problem (IVP) determined by function f and initial condition x using symplectic methods.

Designed to be a subroutine of odeint (i.e. will eventually automatically be dispatched to here, much like _adaptive_odeint)

Parameters
  • f (Callable) –

  • x (Tensor) –

  • t_span (Union[List, Tensor]) –

  • solver (Union[str, nn.Module]) –

  • atol (float, optional) – Defaults to 1e-3.

  • rtol (float, optional) – Defaults to 1e-3.

  • verbose (bool, optional) – Defaults to False.

  • return_all_eval (bool, optional) – Defaults to False.

  • save_at (Union[List, Tensor], optional) – Defaults to t_span

torchdyn.numerics.sensitivity module

torchdyn.numerics.sensitivity._gather_odefunc_adjoint(vf, vf_params, solver, atol, rtol, interpolator, solver_adjoint, atol_adjoint, rtol_adjoint, integral_loss, problem_type, maxiter=4, fine_steps=4)[source]

Prepares definition of autograd.Function for adjoint sensitivity analysis of the above ODEProblem

torchdyn.numerics.sensitivity._gather_odefunc_interp_adjoint(vf, vf_params, solver, atol, rtol, interpolator, solver_adjoint, atol_adjoint, rtol_adjoint, integral_loss, problem_type, maxiter=4, fine_steps=4)[source]

Prepares definition of autograd.Function for interpolated adjoint sensitivity analysis of the above ODEProblem

torchdyn.numerics.sensitivity.generic_odeint(problem_type, vf, x, t_span, solver, atol, rtol, interpolator, B0=None, return_all_eval=False, maxiter=4, fine_steps=4, save_at=())[source]

Dispatches to appropriate odeint function depending on Problem class (ODEProblem, MultipleShootingProblem)

torchdyn.numerics.solvers module

torchdyn.numerics.hypersolvers module

torchdyn.numerics.interpolators module

Contains several Interpolator classes

class torchdyn.numerics.interpolators.FourthOrder(dtype)[source]

4th order interpolation scheme.

fit(dt, f0, f1, x0, x1, x_mid, **kwargs)[source]
class torchdyn.numerics.interpolators.Interpolator(order)[source]
evaluate(coefs, t0, t1, t)[source]

Evaluates a generic interpolant given coefs between [t0, t1].

fit(f0, f1, x0, x1, t, dt, **kwargs)[source]
sync_device_dtype(x, t_span)[source]

Ensures x, t_span, tableau and other interpolator tensors are on the same device with compatible dtypes

class torchdyn.numerics.interpolators.Linear[source]
class torchdyn.numerics.interpolators.ThirdHermite[source]
torchdyn.numerics.interpolators.str_to_interp(solver_name, dtype=torch.float32)[source]

Transforms string specifying desired interpolation scheme into an instance of the Interpolator class.

torchdyn.numerics.root module

torchdyn.numerics.systems module

class torchdyn.numerics.systems.CoupledFourierSystem(dim=2, A_dist=Uniform(low: -10.0, high: 10.0), phi_dist=Uniform(low: -1.0, high: 1.0), w_dist=Uniform(low: -20.0, high: 20.0), randomizable=True)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

randomize_parameters()[source]
training: bool
class torchdyn.numerics.systems.FourierSystem(dim=2, A_dist=Uniform(low: -10.0, high: 10.0), phi_dist=Uniform(low: -1.0, high: 1.0), w_dist=Uniform(low: -20.0, high: 20.0), randomizable=True)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

randomize_parameters()[source]
training: bool
class torchdyn.numerics.systems.LTISystem(dim=2, randomizable=True)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

randomize_parameters()[source]
training: bool
class torchdyn.numerics.systems.Lorenz[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(t, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class torchdyn.numerics.systems.MatMulBoundedSystem(dim=2, activation=Tanh(), layers=5, hdim=32, randomizable=True)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

randomize_parameters()[source]
training: bool
class torchdyn.numerics.systems.MatMulSystem(dim=2, activation=Tanh(), layers=5, hdim=32, randomizable=True)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

randomize_parameters()[source]
training: bool
class torchdyn.numerics.systems.ODEProblem2[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, z)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class torchdyn.numerics.systems.ODEProblem3[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, z)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class torchdyn.numerics.systems.ODEProblem4[source]

Rabinovich-Fabrikant

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, z)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class torchdyn.numerics.systems.SineSystem[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, z)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class torchdyn.numerics.systems.StiffFourierSystem(dim=2, A_dist=Uniform(low: -10.0, high: 10.0), phi_dist=Uniform(low: -1.0, high: 1.0), w_dist=Uniform(low: -20.0, high: 20.0), randomizable=True)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(s, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

randomize_parameters()[source]
training: bool
class torchdyn.numerics.systems.VanDerPol(alpha=10)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(t, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool

torchdyn.numerics.utils module

Contains various utilities for odeint and numerical methods. Various norms, step size initialization, event callbacks for hybrid systems, vmapped matrix-Jacobian products and some additional goodies.

class torchdyn.numerics.utils.EventCallback[source]

Basic callback for hybrid differential equations. Must define an event condition and a state-jump

Method generated by attrs for class EventCallback.

check_event(t, x)[source]
jump_map(t, x)[source]
training: bool
class torchdyn.numerics.utils.EventState(evid)[source]
class torchdyn.numerics.utils.RootLogger[source]
log(logged_data)[source]
permanent_log(logged_data)[source]
class torchdyn.numerics.utils.StochasticEventCallback[source]

Method generated by attrs for class StochasticEventCallback.

check_event(t, x)[source]
initialize(x0)[source]
jump_map(t, x)[source]
training: bool
class torchdyn.numerics.utils.WrapFunc(f)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(t, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
torchdyn.numerics.utils.adapt_step(dt, error_ratio, safety, min_factor, max_factor, order)[source]
torchdyn.numerics.utils.dense_output(sol, t_sol, t_eval, return_spline=False)[source]
torchdyn.numerics.utils.hairer_norm(tensor)[source]
torchdyn.numerics.utils.init_step(f, f0, x0, t0, order, atol, rtol)[source]
torchdyn.numerics.utils.make_norm(state)[source]