dymad.models.model_base

Module Attributes

Encoder

encoder(net, w) -> x

Features

features(z, w) -> s

Composer

composer(net, s, z, w) -> r

Decoder

decoder(net, z, w) -> x

Classes

ComposedDynamics([encoder, dynamics, ...])

Base class for dynamic models.

Predictor(*args, **kwargs)

SupportsSetWeights(*args, **kwargs)

class dymad.models.model_base.ComposedDynamics(encoder=None, dynamics=None, decoder=None, predict=None, model_config=None, dims=None)

Bases: Module

Base class for dynamic models.

Notation:

  • x: Physical state/observation space; can be time-delayed

  • u: Control input; can be time-delayed

  • z: Embedded space, where dynamics is learned.

  • s: Features for dynamics, composing z and u as needed

  • r: Output of processor, might be lower dimensional than z

  • z’: next step of z (discrete-time) or z_dot (continuous-time)

Full model:

  • z = encoder(x, u)

  • z’ = dynamics(z, u)

  • x = decoder(z)

Details in dynamics:

  • s = features(z, u); e.g., concatenation of linear or bilinear terms

  • r = processor(s, u); e.g., NN or linear transform

  • z’ = composition(s, r); e.g., Direct or Skip-Connection

In the above, encoder, features, composer, and decoder are functions that should be hooked to the model instance, while processor is a nn.Module.

Linear training assumes:

  • processor is linear

  • linear_targets = r = W @ features(z, u)

  • and fits W only

Signature for predict:

  • predict(x0: torch.Tensor, w: ComponentInputPayload, ts: Union[np.ndarray, torch.Tensor], **kwargs) -> Tuple[torch.Tensor, torch.Tensor]

  • Usually comes from dymad/models/prediction

For mathematical formulation, see theory/architecture in the documentation.

The class can be used in two ways:

  • Through predefined models and build_model() function. User defines resolve_spec() class method, as needed by build_model().

  • By directly instantiating the class and hooking the functions and networks. User needs to define all components manually with an initializer like:

    def __init__(self, model_config: Dict, data_meta: Dict, dtype=None, device=None)

Parameters:
  • encoder (Encoder) – Encoder function

  • dynamics (Tuple[Features, Composer]) – Features function and composer function for dynamics

  • decoder (Decoder) – Decoder function

  • predict (Tuple[Predictor, str]) – Prediction function and input order

  • model_config (Dict, optional) – Model configuration dictionary

  • dims (Dict, optional) – Dimensions dictionary, usually generated from get_dims()

CONT = None
GRAPH = None
decoder(z, w)

Decode the latent states into outputs.

Return type:

Tensor

diagnostic_info()

Return diagnostic information about the model.

Returns:

String with model details

Return type:

str

dynamics(z, w)

Compute the dynamics output given latent states and inputs.

Note this uses three components: features, processor, and composer.

Return type:

Tensor

encoder(w)

Encode the inputs into latent states.

Return type:

Tensor

forward(t=None, x=None, u=None, p=None, ei=None, ew=None, ea=None)

Forward pass through the full model: encode, dynamics, decode.

Unified across most of the models, but this can be overridden if needed.

Note

The forward pass should not be used directly. This interface is provided for model inspection and analysis.

Return type:

tuple[Tensor, Tensor, Tensor]

linear_eval(w)

Compute linear evaluation, dz, and states, z, for the model.

dz = Af(z)

z is the encoded state, which will be used to compute the expected output.

Return type:

tuple[Tensor, Tensor]

linear_features(w)

Compute linear features, f, and outputs, dz, for the model.

dz = Af(z)

dz is the output of the dynamics, z_dot for cont-time, z_next for disc-time.

Return type:

tuple[Tensor, Tensor]

linear_solve(inp, out, **kwargs)

Solve for linear dynamics weights given input-output pairs.

The solution depends on the specific model and processor used.

Return type:

tuple[Tensor, Tensor]

predict(x0, w, ts, method='dopri5', **kwargs)

Predict trajectory using specified method.

This function essentially determines whether the model is continuous-time or discrete-time.

Return type:

Tensor

classmethod resolve_spec(model_spec, model_config, data_meta, dtype, device)

Resolve a typed model spec into concrete build-time components.

set_linear_weights(W=None, b=None, U=None, V=None)

Set the weights of the linear dynamics module.

Return type:

tuple[Tensor, Tensor]

dymad.models.model_base.Composer

composer(net, s, z, w) -> r

alias of Callable[[Module, Tensor, Tensor, ComponentInputPayload], Tensor]

dymad.models.model_base.Decoder

decoder(net, z, w) -> x

alias of Callable[[Module, Tensor, ComponentInputPayload], Tensor]

dymad.models.model_base.Encoder

encoder(net, w) -> x

alias of Callable[[Module, ComponentInputPayload], Tensor]

dymad.models.model_base.Features

features(z, w) -> s

alias of Callable[[Tensor, ComponentInputPayload], Tensor]

class dymad.models.model_base.Predictor(*args, **kwargs)

Bases: Protocol

class dymad.models.model_base.SupportsSetWeights(*args, **kwargs)

Bases: Protocol

set_weights(W=None, b=None, U=None, V=None)
Return type:

tuple[Tensor, ...]