Constraints
This module contains concrete implementations of the constraint component.
GradParams
__init__(self, n_params, n_eqs)
special
Constrains the neural network by optimizing over the coefficients together with the network. Coefficient vectors are randomly initialized from a standard Gaussian.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_params |
int |
number of features in feature matrix. |
required |
n_eqs |
int |
number of outputs / equations to be discovered. |
required |
Source code in deepymod/model/constraint.py
def __init__(self, n_params: int, n_eqs: int) -> None:
"""Constrains the neural network by optimizing over the coefficients together with the network.
Coefficient vectors are randomly initialized from a standard Gaussian.
Args:
n_params (int): number of features in feature matrix.
n_eqs (int): number of outputs / equations to be discovered.
"""
super().__init__()
self.coeff_vectors = torch.nn.ParameterList([torch.nn.Parameter(torch.randn(n_params, 1)) for _ in torch.arange(n_eqs)])
calculate_coeffs(self, sparse_thetas, time_derivs)
Returns the coefficients of the constraint, since we're optimizing them by gradient descent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sparse_thetas |
List[torch.Tensor] |
List containing the sparse feature tensors of size (n_samples, n_active_features). |
required |
time_derivs |
List[torch.Tensor] |
List containing the time derivatives of size (n_samples, n_outputs). |
required |
Returns:
Type | Description |
---|---|
(TensorList) |
Calculated coefficients of size (n_features, n_outputs). |
Source code in deepymod/model/constraint.py
def calculate_coeffs(self, sparse_thetas: TensorList, time_derivs: TensorList):
"""Returns the coefficients of the constraint, since we're optimizing them by
gradient descent.
Args:
sparse_thetas (TensorList): List containing the sparse feature tensors of size (n_samples, n_active_features).
time_derivs (TensorList): List containing the time derivatives of size (n_samples, n_outputs).
Returns:
(TensorList): Calculated coefficients of size (n_features, n_outputs).
"""
return self.coeff_vectors
LeastSquares
__init__(self)
special
Least Squares Constraint solved by QR decomposition
Source code in deepymod/model/constraint.py
def __init__(self) -> None:
""" Least Squares Constraint solved by QR decomposition"""
super().__init__()
calculate_coeffs(self, sparse_thetas, time_derivs)
Calculates the coefficients of the constraint using the QR decomposition for every pair of sparse feature matrix and time derivative.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sparse_thetas |
List[torch.Tensor] |
List containing the sparse feature tensors of size (n_samples, n_active_features). |
required |
time_derivs |
List[torch.Tensor] |
List containing the time derivatives of size (n_samples, n_outputs). |
required |
Returns:
Type | Description |
---|---|
List[torch.Tensor] |
(TensorList): Calculated coefficients of size (n_features, n_outputs). |
Source code in deepymod/model/constraint.py
def calculate_coeffs(self, sparse_thetas: TensorList, time_derivs: TensorList) -> TensorList:
"""Calculates the coefficients of the constraint using the QR decomposition for every pair
of sparse feature matrix and time derivative.
Args:
sparse_thetas (TensorList): List containing the sparse feature tensors of size (n_samples, n_active_features).
time_derivs (TensorList): List containing the time derivatives of size (n_samples, n_outputs).
Returns:
(TensorList): Calculated coefficients of size (n_features, n_outputs).
"""
opt_coeff = []
for theta, dt in zip(sparse_thetas, time_derivs):
Q, R = torch.qr(theta) # solution of lst. sq. by QR decomp.
opt_coeff.append(torch.inverse(R) @ Q.T @ dt)
# Putting them in the right spot
coeff_vectors = [torch.zeros((mask.shape[0], 1)).to(coeff_vector.device).masked_scatter_(mask[:, None], coeff_vector)
for mask, coeff_vector
in zip(self.sparsity_masks, opt_coeff)]
return coeff_vectors