vindy.networks package

Submodules

vindy.networks.autoencoder_sindy module

class AutoencoderSindy(*args, **kwargs)[source]

Bases: BaseModel

assert_arguments(arguments)[source]

Asserts that the arguments passed to the model are valid :param arguments: all arguments passed to the model :return:

build_decoder(z)[source]

Build a fully connected decoder with layers of reversed sizes in layer_sizes.

Parameters:

z (array-like) – Latent variable.

Returns:

Reconstructed full state.

Return type:

tf.Tensor

build_encoder(x)[source]

Build a fully connected encoder with layers of size layer_sizes.

Parameters:

x (array-like) – Input to the autoencoder.

Returns:

  • x_input (tf.keras.Input) – Input tensor.

  • z (tf.Tensor) – Latent variable.

build_loss(inputs)[source]

Split input into state, its derivative, and the parameters, perform the forward pass, calculate the loss, and update the weights.

Parameters:

inputs (list) – List of array-like objects.

Returns:

Dictionary of losses.

Return type:

dict

build_model(x, mu)[source]

Build the model.

Parameters:
  • x (array-like) – Full state.

  • mu (array-like) – Parameters.

calc_latent_time_derivatives(x, dx_dt, dx_ddt=None)[source]

Calculate time derivatives of latent variables given the time derivatives of the input variables (used for comparison with SINDy)

Parameters:
  • x (array-like) – Full state.

  • dx_dt (array-like) – Time derivative of state.

  • dx_ddt (array-like, optional) – Second time derivative of state (default is None).

Returns:

Latent variables and their time derivatives.

Return type:

tuple

compile(optimizer=<keras.src.optimizers.adam.Adam object>, loss=<keras.src.losses.losses.BinaryCrossentropy object>, sindy_optimizer=None, **kwargs)[source]

Wrapper for the compile function of the keras model to enable the use of different optimizers for different parts of the model.

Parameters:
  • optimizer (tf.keras.optimizers.Optimizer, optional) – Optimizer for the autoencoder (default is Adam with learning rate 1e-3).

  • loss (tf.keras.losses.Loss, optional) – Loss function for the autoencoder (default is BinaryCrossentropy).

  • sindy_optimizer (tf.keras.optimizers.Optimizer, optional) – Optimizer for the SINDy part of the model (default is None).

  • kwargs (dict) – Additional arguments for the compile function.

create_loss_trackers()[source]

Create loss trackers for the model.

decode(z)[source]

Decode latent variable.

Parameters:

z (array-like of shape (n_samples, reduced_order)) – Latent variable.

Returns:

Full state.

Return type:

array-like of shape (n_samples, n_features, n_dof_per_feature)

define_scaling(x)[source]

Define the scaling factor for given training data.

Parameters:

x (array-like) – Training data.

encode(x)[source]

Encode full state.

Parameters:

x (array-like of shape (n_samples, n_features, n_dof_per_feature)) – Full state.

Returns:

Latent variable.

Return type:

array-like of shape (n_samples, reduced_order)

get_loss(x, dx_dt, mu, x_int=None, mu_int=None)[source]

Calculate loss for first order system.

Parameters:
  • x (array-like) – Full state.

  • dx_dt (array-like) – Time derivative of state.

  • mu (array-like) – Control input.

  • x_int (array-like, optional) – Full state at future time steps (default is None).

  • mu_int (array-like, optional) – Control input at future time steps (default is None).

Returns:

Dictionary of losses.

Return type:

dict

get_loss_2nd(x, dx_dt, dx_ddt, mu, x_int=None, dx_dt_int=None, mu_int=None)[source]

Calculate loss for second order system.

Parameters:
  • x (array-like) – Full state.

  • dx_dt (array-like) – Time derivative of state.

  • dx_ddt (array-like) – Second time derivative of state.

  • mu (array-like) – Control input.

  • x_int (array-like, optional) – Full state at future time steps (default is None).

  • dx_dt_int (array-like, optional) – Time derivative of state at future time steps (default is None).

  • mu_int (array-like, optional) – Control input at future time steps (default is None).

Returns:

Dictionary of losses.

Return type:

dict

get_loss_rec(x)[source]

Calculate the reconstruction loss.

Parameters:

x (array-like) – Full state.

Returns:

Reconstruction loss.

Return type:

tf.Tensor

get_trainable_weights()[source]

Return the trainable weights of the model.

Returns:

List of trainable weights.

Return type:

list

reconstruct(x, _=None)[source]

Reconstruct full state.

Parameters:

x (array-like of shape (n_samples, n_features, n_dof_per_feature)) – Full state.

Returns:

Reconstructed full state.

Return type:

array-like of shape (n_samples, n_features, n_dof_per_feature)

static reconstruction_loss(x, x_pred)[source]

Calculate the reconstruction loss.

Parameters:
  • x (array-like of shape (n_samples, n_features)) – Original input data.

  • x_pred (array-like of shape (n_samples, n_features)) – Reconstructed input data.

Returns:

Reconstruction loss.

Return type:

tf.Tensor

rescale(x)[source]

Rescale the data.

Parameters:

x (array-like) – Scaled data.

Returns:

Rescaled data.

Return type:

array-like

scale(x)[source]

Scale the data.

Parameters:

x (array-like) – Input data.

Returns:

Scaled data.

Return type:

array-like

vindy.networks.base_model module

class BaseModel(*args, **kwargs)[source]

Bases: Model, ABC

Abstract base class for autoencoder SINDy models.

assert_arguments(arguments)[source]

Assert that the arguments passed to the model are valid.

Parameters:

arguments (dict) – All arguments passed to the model.

Raises:

AssertionError – If any of the arguments are invalid.

build_sindy(z, mu)[source]

Build the model for the forward pass of the SINDy layer.

Parameters:
  • z (array-like) – Latent state.

  • mu (array-like) – Parameters.

Returns:

Concatenated input for the SINDy layer and the output of the SINDy layer.

Return type:

tuple

concatenate_sindy_input(z, dzdt=None, mu=None)[source]

Concatenate the state, its derivative, and the parameters to the input of the SINDy layer.

Parameters:
  • z (array-like) – State.

  • dzdt (array-like, optional) – Derivative of the state (default is None).

  • mu (array-like, optional) – Parameters (default is None).

Returns:

Concatenated input for the SINDy layer.

Return type:

array-like

evaluate_sindy_layer(z, dz_dt, mu)[source]

Evaluate the SINDy layer.

Parameters:
  • z (array-like) – Latent variable.

  • dz_dt (array-like) – Time derivative of the latent variable (only required for second order models).

  • mu (array-like) – Parameters.

Returns:

SINDy prediction, mean, and log variance.

Return type:

tuple

fit(x, y=None, validation_data=None, **kwargs)[source]

Wrapper for the fit function of the keras model to flatten the data if necessary.

Parameters:
  • x (array-like) – Training data.

  • y (array-like, optional) – Target data (default is None).

  • validation_data (tuple or array-like, optional) – Data on which to evaluate the loss and any model metrics at the end of each epoch (default is None).

  • kwargs (dict) – Additional arguments for the fit function.

Returns:

A History object. Its History.history attribute is a record of training loss values and metrics values at successive epochs.

Return type:

History

flatten3d(x)[source]
static flatten_dummy(x)[source]
get_int_loss(inputs)[source]

Integrate the identified dynamical system and compare the result to the true dynamics.

Parameters:

inputs (list) – List of inputs.

Returns:

Integration loss.

Return type:

float

integrate(z0, t, mu=None, method='RK45')[source]

Integrate the model using scipy.integrate.solve_ivp.

Parameters:
  • z0 (array-like) – Initial state.

  • t (array-like) – Time points to evaluate the solution at.

  • mu (array-like or callable, optional) – Parameters to use in the model (default is None).

  • method (str, optional) – Integration method to use (default is “RK45”).

Returns:

Solution of the integration.

Return type:

scipy.integrate.OdeResult

static load(aesindy, x=None, mu=None, mask=None, fixed_coeffs=None, path: str = None, kwargs_overwrite: dict = {})[source]

Load the model from the given path.

Parameters:
  • aesindy (class) – The class of the model to be loaded.

  • x (array-like, optional) – Data needed to initialize the model (default is None).

  • mu (array-like, optional) – Parameters used to create the model the first time (default is None).

  • mask (array-like, optional) – Mask for the model (default is None).

  • fixed_coeffs (array-like, optional) – Fixed coefficients for the model (default is None).

  • path (str, optional) – Path to the model (default is None).

  • kwargs_overwrite (dict, optional) – Additional kwargs to overwrite the config (default is {}).

Returns:

Loaded model.

Return type:

BaseModel

print(z=None, mu=None, precision=3)[source]
rhs_(t, z)[source]
save(path: str = None)[source]

Save the model weights and configuration to a given path.

Parameters:

path (str, optional) – Path to the folder where the model should be saved (default is None).

sindy_coeffs()[source]

Return the coefficients of the SINDy model.

Returns:

Coefficients of the SINDy model.

Return type:

array-like

split_inputs(inputs)[source]

Split the inputs into the state, its derivative, and the parameters (if present).

Parameters:

inputs (list) – List of inputs.

Returns:

Split inputs.

Return type:

tuple

test_step(inputs)[source]

Perform one test step.

Parameters:

inputs (list) – List of inputs.

Returns:

Dictionary of losses.

Return type:

dict

train_step(inputs)[source]

Perform one training step.

Parameters:

inputs (list) – List of inputs.

Returns:

Dictionary of losses.

Return type:

dict

unflatten3d(x)[source]
vis_modes(x, n_modes=3)[source]

Visualize the reconstruction of the reduced coefficients of the PCA modes.

Parameters:
  • x (array-like) – Input data.

  • n_modes (int, optional) – Number of modes to visualize (default is 3).

vindy.networks.sindy_network module

class SindyNetwork(*args, **kwargs)[source]

Bases: BaseModel

Model to discover dynamics of a system using SINDy or VINDy.

build_loss(inputs)[source]

Split input into state, its derivative, and the parameters, perform the forward pass, calculate the loss, and update the weights.

Parameters:

inputs (list) – List of array-like objects.

Returns:

Dictionary of losses.

Return type:

dict

build_model(z, mu)[source]

Build the model.

Parameters:
  • z (array-like) – Latent state.

  • mu (array-like) – Parameters.

create_loss_trackers()[source]

Create loss trackers for the model.

get_loss(z, dz_dt, mu, z_int=None, mu_int=None)[source]

Calculate loss for first order system.

Parameters:
  • z (array-like) – Full state.

  • dz_dt (array-like) – Time derivative of state.

  • mu (array-like) – Control input.

  • z_int (array-like, optional) – Full state at future time steps (default is None).

  • mu_int (array-like, optional) – Control input at future time steps (default is None).

Returns:

Dictionary of losses.

Return type:

dict

get_loss_2nd(z, dz_dt, dz_ddt, mu, z_int=None, dz_dt_int=None, mu_int=None)[source]

Calculate loss for second order system.

Parameters:
  • z (array-like) – Full state.

  • dz_dt (array-like) – Time derivative of state.

  • dz_ddt (array-like) – Second time derivative of state.

  • mu (array-like) – Control input.

  • z_int (array-like, optional) – Full state at future time steps (default is None).

  • dz_dt_int (array-like, optional) – Time derivative of state at future time steps (default is None).

  • mu_int (array-like, optional) – Control input at future time steps (default is None).

Returns:

Dictionary of losses.

Return type:

dict

get_trainable_weights()[source]

Return the trainable weights of the model.

Returns:

List of trainable weights.

Return type:

list

vindy.networks.variational_autoencoder_sindy module

class VAESindy(*args, **kwargs)[source]

Bases: AutoencoderSindy

Model to discover low-dimensional dynamics of a high-dimensional system using variational autoencoders and SINDy/VINDy.

build_encoder(x)[source]

Build the variational encoder part of the model.

Parameters:

x (array-like) – Input data.

Returns:

Input tensor and latent variable tensor.

Return type:

tuple

call(inputs, _=None)[source]

Perform a forward pass through the model.

Parameters:

inputs (array-like) – Input data.

Returns:

Reconstructed data.

Return type:

array-like

create_loss_trackers()[source]

Create loss trackers for the model.

encode(x, training=False)[source]

Encode full state to latent distribution and return its mean.

Parameters:
  • x (array-like of shape (n_samples, n_features, n_dof_per_feature)) – Full state.

  • training (bool, optional) – Whether the model is in training mode (default is False).

Returns:

Latent variable.

Return type:

array-like of shape (n_samples, reduced_order)

kl_loss(mean, log_var)[source]

Calculate the KL divergence loss for Gaussian distributions.

Parameters:
  • mean (tf.Tensor) – Mean of the latent variable distribution.

  • log_var (tf.Tensor) – Log variance of the latent variable distribution.

Returns:

KL divergence loss.

Return type:

tf.Tensor

static reconstruction_loss(x, x_reconstruction)[source]

Compute the reconstruction loss of the autoencoder as log(mse) as stated in https://arxiv.org/pdf/2006.10273.pdf

Parameters:
  • x (array-like) – Input data.

  • x_reconstruction (array-like) – Reconstructed data.

Returns:

Reconstruction loss.

Return type:

tf.Tensor

Module contents