vindy.layers package

Submodules

vindy.layers.sindy_layer module

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

Bases: Layer

Sparse Identification of Nonlinear Dynamics (SINDy) layer.

This layer evaluates a library of candidate functions on latent inputs and performs sparse regression to recover governing coefficients.

Parameters:
  • state_dim (int) – Number of latent variables (dimension of the latent state z).

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

  • feature_libraries (list of BaseLibrary, optional) – Feature libraries applied to the latent variables (e.g. PolynomialLibrary).

  • param_feature_libraries (list of BaseLibrary, optional) – Feature libraries applied to parameters (mu).

  • second_order (bool, optional) – If True, enforce second-order structure for dynamics (include z_dot features).

  • kernel_regularizer (tf.keras.regularizers.Regularizer, optional) – Regularizer applied to the learned coefficient kernel.

  • x_mu_interaction (bool, optional) – If True, include interaction features between state and parameters.

  • mask (array-like, optional) – Mask to fix or remove certain coefficients from training.

  • fixed_coeffs (array-like, optional) – Values for coefficients that are fixed (applied after masking).

  • dtype (str, optional) – Data type used by the layer (e.g. ‘float32’).

  • **kwargs – Additional keyword arguments passed to tf.keras.layers.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 to obtain the governing coefficients.

Notes

This initializer validates arguments, constructs default feature libraries when none are provided and prepares internal bookkeeping variables used by the layer (masks, coefficient shapes, etc.).

__init__(state_dim, param_dim=0, feature_libraries=None, param_feature_libraries=None, second_order=True, kernel_regularizer=tensorflow.keras.regularizers.L1L2, x_mu_interaction=True, mask=None, fixed_coeffs=None, dtype='float32', **kwargs)[source]

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 to obtain the governing coefficients.

Notes

This initializer validates arguments, constructs default feature libraries when none are provided and prepares internal bookkeeping variables used by the layer (masks, coefficient shapes, etc.).

assert_arguments(arguments)[source]

Validate initializer arguments and raise informative assertions.

Parameters:

arguments (dict) – Mapping from argument name to value (typically locals() from __init__).

call(inputs, training=False)

Forward pass of the SINDy layer: evaluate features and compute prediction.

Parameters:
  • inputs (tf.Tensor) – Latent variables, shape (batch_size, latent_dim).

  • training (bool, optional) – Whether the call is in training mode.

Returns:

Predicted derivatives with shape (batch_size, output_dim).

Return type:

tf.Tensor

property coefficient_matrix_shape
concat_features(z, libraries)[source]

Concatenate outputs of several feature libraries.

Parameters:
  • z (tf.Tensor) – Input to the feature libraries.

  • libraries (list) – Iterable of library objects that are callable on z.

Returns:

Concatenated feature outputs along the last axis.

Return type:

tf.Tensor

features(inputs)[source]

Compute concatenated features from configured libraries.

Parameters:

inputs (tf.Tensor) – Input tensor that contains state and (optionally) parameter values.

Returns:

Concatenated feature matrix for the SINDy regression.

Return type:

tf.Tensor

fill_coefficient_matrix(trainable_coeffs)[source]

Insert the trainable coefficients into the full coefficient matrix.

Parameters:

trainable_coeffs (array-like) – Trainable coefficients arranged to match the active DOFs.

Returns:

Full coefficient matrix with fixed coefficients applied.

Return type:

tf.Tensor

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

Construct human-readable feature names for states and parameters.

Parameters:
  • z (list of str, optional) – Names for the state variables. If None, default names are generated.

  • mu (list of str, optional) – Names for parameter variables. If None, default names are generated.

Returns:

Feature names in the order produced by features.

Return type:

list of sympy.Symbol or str

get_prunable_weights()[source]
get_sindy_coeffs()[source]
init_weigths(kernel_regularizer)[source]
integrate(z0, t, mu=None, method='RK45', sindy_fcn=None)[source]

Integrate the SINDy model forward in time using scipy.integrate.solve_ivp.

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

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

  • mu (array-like or callable, optional) – Parameter trajectory (or callable) to pass to the model.

  • method (str, optional) – Integration method for solve_ivp (e.g. ‘RK45’).

  • sindy_fcn (callable, optional) – Callable implementing the right-hand side. If None, uses self.rhs_.

Returns:

The object returned by scipy.integrate.solve_ivp.

Return type:

OdeResult

property kernel_shape

Return the shape of the internal kernel (trainable coefficients).

Returns:

Kernel shape as (n_dofs, 1).

Return type:

tuple

property loss_trackers

Return loss trackers used by the layer.

Returns:

Mapping of loss names to Keras Metric objects. By default the SINDy layer does not add separate loss trackers and returns an empty dict.

Return type:

dict

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

Convert coefficients and feature names into a human-readable equation string.

Parameters:
  • z (list of str, optional) – Names of the state variables.

  • mu (list of str, optional) – Names of the parameter variables.

  • precision (int, optional) – Decimal precision for printing coefficients.

Returns:

Multi-line string with one equation per latent state.

Return type:

str

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

Print the discovered SINDy equations to stdout.

Parameters:
  • z (list of str, optional) – Variable names for states.

  • mu (list of str, optional) – Variable names for parameters.

  • precision (int, optional) – Number of decimal places when formatting coefficients.

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

Evaluate the right-hand side z’(t) = f(z, mu) for provided inputs.

Parameters:
  • t (float) – Current time (unused by default but present for compatibility).

  • inputs (array-like) – Flattened inputs (state or state+param) for the RHS function.

Returns:

Time derivative evaluated at the given inputs.

Return type:

tf.Tensor or ndarray

set_mask(mask, fixed_coeffs=None)[source]

Normalize and pad mask and fixed coefficient arrays to the expected shape.

Parameters:
  • mask (array-like or None) – Mask specifying which coefficients are trainable (1) or disabled (0).

  • fixed_coeffs (array-like or None) – Fixed coefficient values to be applied for masked entries.

Returns:

(mask, fixed_coeffs) both cast to the layer dtype and padded to the proper coefficient matrix shape.

Return type:

tuple

vindy.layers.vindy_layer module

class LogVarL1L2Regularizer(*args: Any, **kwargs: Any)[source]

Bases: Regularizer

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

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

Bases: SindyLayer

Layer for variational identification of nonlinear dynamics (VINDy).

Approximates 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 or int, default=1) – Scaling factor for the KL divergence.

  • priors (BaseDistribution or list of BaseDistribution, default=Gaussian(0.0, 1.0)) – Prior distribution(s) for the coefficients.

  • **kwargs – Additional keyword arguments, see SindyLayer.

__init__(beta=1, priors=tensorflow.keras.layers.Layer, **kwargs)[source]

Layer for variational identification of nonlinear dynamics (VINDy).

Approximates 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 or int, default=1) – Scaling factor for the KL divergence.

  • priors (BaseDistribution or list of BaseDistribution, default=Gaussian(0.0, 1.0)) – Prior distribution(s) for the coefficients.

  • **kwargs – Additional keyword arguments, see SindyLayer.

assert_additional_args(beta, priors)[source]

Validate that the additional arguments are correct.

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

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

call(inputs, training=False)

Apply the VINDy layer to the inputs.

Applies the feature libraries to the inputs, samples the coefficients from a normal distribution parametrized by the layer’s kernel (weights), and computes the dot product of the features and the coefficients.

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

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

Returns:

If training: [z_dot, coeffs_mean, coeffs_log_var] If not training: z_dot

Return type:

tf.Tensor or list of tf.Tensor

call_uq(inputs, coeffs)

Apply the VINDy layer with given coefficients for uncertainty quantification.

Applies the VINDy layer for given coefficients so that not the mean coefficients of the distribution are taken.

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

  • coeffs (tf.Tensor) – Coefficients to use for the computation.

Returns:

Time derivative z_dot.

Return type:

tf.Tensor

get_sindy_coeffs()[source]
init_weigths(kernel_regularizer)[source]
integrate_uq(z0, t, mu=None, method='RK45')[source]
kl_loss(mean, scale)[source]

Compute the KL divergence between the priors and coefficient distributions.

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

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

Returns:

Scaled KL divergence loss.

Return type:

tf.Tensor

property loss_trackers

Return loss trackers used by the layer.

Returns:

Mapping of loss names to Keras Metric objects. By default the SINDy layer does not add separate loss trackers and returns an empty dict.

Return type:

dict

pdf_thresholding(threshold: float = 1.0)[source]

Cancel coefficients based on their probability density function at zero.

Cancels 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, default=1.0) – Threshold value for cancelling coefficients.

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 for x-axis.

  • z (array-like, optional) – Latent state variable names.

  • mu (array-like, optional) – Parameter variable names.

Module contents