vindy.layers package

Submodules

vindy.layers.sindy_layer module

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

Bases: Layer

Layer for SINDy approximation of the time derivative of the latent variable. Feature libraries are applied to the latent variable and its time derivative, and a sparse regression is performed.

Parameters:
  • state_dim (int) – Number of latent variables.

  • param_dim (int, optional) – Number of parameters (default is 0).

  • feature_libraries (list, optional) – List of feature libraries for the latent variables (default is [PolynomialLibrary(degree=3)]).

  • param_feature_libraries (list, optional) – List of feature libraries for the parameters (default is []).

  • second_order (bool, optional) – If True, enforce 2nd order structure (default is True).

  • kernel_regularizer (tf.keras.regularizers.Regularizer, optional) – Regularizer for the kernel (default is tf.keras.regularizers.L1L2(l1=1e-3, l2=0)).

  • x_mu_interaction (bool, optional) – If True, interaction between latent variables and parameters (default is True).

  • mask (array-like, optional) – If required, certain coefficients of the latent governing equations can be fixed and are consequently masked out for training (default is None).

  • fixed_coeffs (array-like, optional) – Values for the coefficients that are masked out during training (default is None).

  • dtype (str, optional) – Data type of the layer (default is “float32”).

  • kwargs (dict) – Additional arguments for the TensorFlow layer class.

assert_arguments(arguments)[source]

Assert that the arguments passed to the layer are valid.

Parameters:

arguments (dict) – Arguments passed to the layer.

call(inputs, training=False)[source]

Perform the forward pass of the SINDy layer.

Parameters:
  • inputs (tf.Tensor) – Input tensor.

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

Returns:

Output tensor after applying the SINDy layer.

Return type:

tf.Tensor

property coefficient_matrix_shape

Returns the shape of the coefficient matrix.

Returns:

Shape of the coefficient matrix.

Return type:

tuple

concat_features(z, libraries)[source]

Concatenate features from different libraries.

Parameters:
  • z (tf.Tensor) – Input tensor.

  • libraries (list) – List of feature libraries.

Returns:

Concatenated features.

Return type:

tf.Tensor

fill_coefficient_matrix(trainable_coeffs)[source]

Fill the coefficient matrix with the trainable coefficients.

Parameters:

trainable_coeffs (array-like) – Trainable coefficients.

Returns:

Coefficient matrix filled with the trainable coefficients.

Return type:

tf.Tensor

get_feature_names(z=None, mu=None)[source]

Construct feature names for states and parameters.

Parameters:
  • z (list of str, optional) – Names of the states, e.g., [‘z1’, ‘z2’, …] (default is None).

  • mu (list of str, optional) – Names of the parameters, e.g., [‘mu1’, ‘mu2’, …] (default is None).

Returns:

List of feature names.

Return type:

list of str

get_prunable_weights()[source]

Get the prunable weights of the SINDy layer.

Returns:

List of prunable weights.

Return type:

list

get_sindy_coeffs()[source]

Get the SINDy coefficients as a numpy array.

Returns:

SINDy coefficients.

Return type:

np.ndarray

init_weigths(kernel_regularizer)[source]

Initialize the weights of the SINDy layer.

Parameters:

kernel_regularizer (tf.keras.regularizers.Regularizer) – Regularizer for the kernel.

property kernel_shape

Returns the dimension of the kernel (weights) of the SINDy layer.

Returns:

Shape of the kernel.

Return type:

tuple

property loss_trackers

Returns the loss trackers of the layer if any. The standard sindy layer has no loss trackers.

Returns:

Loss trackers.

Return type:

dict

model_equation_to_str(z=None, mu=None, precision: int = 3)[source]

Convert coefficients and feature names into a readable equation.

Parameters:
  • z (list of str, optional) – Names of the states, e.g., [‘z1’, ‘z2’, …] (default is None).

  • mu (list of str, optional) – Names of the parameters, e.g., [‘mu1’, ‘mu2’, …] (default is None).

  • precision (int, optional) – Number of decimal places (default is 3).

Returns:

Model equation as a string.

Return type:

str

print(z=None, mu=None, precision: int = 3)[source]

Print the model equation.

Parameters:
  • z (list of str, optional) – Names of the states, e.g., [‘z1’, ‘z2’, …] (default is None).

  • mu (list of str, optional) – Names of the parameters, e.g., [‘mu1’, ‘mu2’, …] (default is None).

  • precision (int, optional) – Number of decimal places (default is 3).

Return type:

None

prune_weights(threshold=0.01, training=False)[source]

Prune the weights of the SINDy layer by setting values below a threshold to zero.

Parameters:
  • threshold (float, optional) – Threshold for pruning (default is 0.01).

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

Return type:

None

set_mask(mask, fixed_coeffs=None)[source]

Set the mask and fixed coefficients for the SINDy layer to mask out certain coefficients and set their values.

Parameters:
  • mask (array-like or None) – Mask for the coefficients. If None, a mask of ones is used.

  • fixed_coeffs (array-like or None, optional) – Fixed coefficients for the masked values. If None, a matrix of zeros is used.

Returns:

A tuple containing the mask and fixed coefficients.

Return type:

tuple

tfFeat(inputs)[source]

Combine all features for the SINDy layer.

Parameters:

inputs (tf.Tensor) – Input tensor.

Returns:

Combined features.

Return type:

tf.Tensor

vindy.layers.vindy_layer module

class LogVarL1L2Regularizer(l1=0.0, l2=0.0)[source]

Bases: Regularizer

Regularizer for the log variance of the coefficients in the VINDy layer.

Parameters:
  • l1 (float, optional) – L1 regularization factor (default is 0.0).

  • l2 (float, optional) – L2 regularization factor (default is 0.0).

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

Bases: SindyLayer

Layer for variational identification of nonlinear dynamics (VINDy) approximation of the time derivative of the latent variable. Feature libraries are applied to the latent variables and a (sparse) variational inference is performed to obtain the coefficients.

Parameters:
  • beta (float) – Scaling factor for the KL divergence.

  • priors (BaseDistribution or list of BaseDistribution) – Prior distribution(s) for the coefficients.

  • kwargs (dict) – Additional keyword arguments.

priors

Prior distribution(s) for the coefficients.

Type:

BaseDistribution or list of BaseDistribution

beta

Scaling factor for the KL divergence.

Type:

float

kl_loss_tracker

Tracker for the KL divergence loss.

Type:

tf.keras.metrics.Mean

assert_additional_args(beta, priors)[source]

Assert that the additional arguments are valid.

Parameters:
  • beta (float or int) – Scaling factor for the KL divergence.

  • priors (BaseDistribution or list of BaseDistribution) – Prior distribution(s) for the coefficients.

Return type:

None

call(inputs, training=False)[source]

Apply the VINDy layer to the inputs.

Parameters:
  • inputs (tf.Tensor) – Input tensor.

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

Returns:

Output tensor after applying the VINDy layer. If training, returns a list containing the output tensor, mean, and log variance.

Return type:

tf.Tensor or list

get_sindy_coeffs()[source]

Get the SINDy coefficients as a numpy array.

Returns:

SINDy coefficients.

Return type:

np.ndarray

init_weigths(kernel_regularizer)[source]

Initialize the weights of the VINDy layer.

Parameters:

kernel_regularizer (tf.keras.regularizers.Regularizer) – Regularizer for the kernel weights.

Return type:

None

kl_loss(mean, scale)[source]

Compute the KL divergence between the priors and the coefficient distributions of the VINDy layer.

Parameters:
  • mean (tf.Tensor) – Mean of the coefficient distributions.

  • scale (tf.Tensor) – Scale of the coefficient distributions.

Returns:

KL divergence loss.

Return type:

tf.Tensor

property loss_trackers

Get the loss trackers.

Returns:

Dictionary of loss trackers.

Return type:

dict

pdf_thresholding(threshold: float = 1.0)[source]

Cancel the coefficients of the SINDy layer if their corresponding probability density function at zero is above the threshold, i.e., if pdf(0) > threshold.

Parameters:

threshold (float, optional) – Threshold for canceling coefficients (default is 1.0).

Return type:

None

visualize_coefficients(x_range=None, z=None, mu=None)[source]

Visualize the coefficients of the SINDy layer as distributions.

Parameters:
  • x_range (tuple, optional) – Range of x values for the plot (default is None).

  • z (list of str, optional) – Names of the states, e.g., [‘z1’, ‘z2’, …] (default is None).

  • mu (list of str, optional) – Names of the parameters, e.g., [‘mu1’, ‘mu2’, …] (default is None).

Return type:

None

Module contents