CVX Solver¶
Note: In our experiments, we use the MOSEK solver which is not the default solver of CVXPY. It is powerful, but it requires a license and additional installation.
- class torchidl.sim.solvers.cvx.CVXSolver(num_row=1, batch_size=32, num_processes=32, lambda_y=1e-06, lambda_z=1e-06, l1_ratio=0.5, elastic_net=False, regularized=True, well_pose=True, regen_states=False, tol=1e-06, verbose=False)[source]¶
Train State-driven Implicit Model using CVXPY. This is highly recommended for small-scale problems due to its high precision and easy tuning.
- Parameters:
num_row (int, optional) – Number of rows to solve in each process each iteration. Defaults to 1.
batch_size (int, optional) – Number of columns to solve in each solving iteration. Defaults to 32.
num_processes (int, optional) – Number of processes available. Defaults to 32.
lambda_y (float, optional) – Lasso regularization parameter for Y. Defaults to 1e-6.
lambda_z (float, optional) – Lasso regularization parameter for Z. Defaults to 1e-6.
l1_ratio (float, optional) – Elastic Net regularization parameter. Defaults to 0.5.
elastic_net (bool, optional) – Whether to use Elastic Net regularization. Defaults to False.
regularized (bool, optional) – Whether to use L1 regularization. Defaults to True.
well_pose (bool, optional) – Whether to enforce well-posedness constraint. Defaults to True.
regen_states (bool, optional) – Whether to regenerate states. Defaults to False.
tol (float, optional) – Tolerance for zeroing out weights. Defaults to 1e-6.
- solve(X, U, Z, Y, model_config)[source]¶
Solve State-driven Implicit Model using CVXPY.
- Parameters:
X (np.ndarray) – Post-activation array.
U (np.ndarray) – Input array.
Z (np.ndarray) – Pre-activation array.
Y (np.ndarray) – Output array.
model_config (Dict[str, Any]) – Model configuration. - activation_fn (Callable): Activation function used by the implicit model. - device (str): Implicit model’s device. - atol (float): Equilibrium function’s tolerance. - kappa (float): Wellposedness condition parameter.
- Returns:
Implicit model’s parameters.
- Return type:
A, B, C, D (np.ndarray)
Example usage:
import torch
from torchidl import SIM
from torchidl import CVXSolver
# Load dataset
dataloader = ...
# Load a pretrained explicit model
explicit_model = ...
explicit_model.load_state_dict(torch.load("checkpoint.pt"))
# Define the SIM model
sim = SIM(activation_fn=torch.nn.functional.relu, device="cuda", dtype=torch.float32)
# Define the solver and solve the state-driven training problem
solver = CVXSolver()
sim.train(solver=solver, model=explicit_model, dataloader=dataloader)