- The paper introduces a modular open-source library that leverages Lagrangian-based first-order methods to address constrained optimization in deep learning models.
- It integrates seamlessly with PyTorch by utilizing automatic differentiation and mini-batch processing to update both primal parameters and dual Lagrange multipliers.
- Its design enables practical applications in fairness, robustness, and physics-informed neural networks, broadening the toolkit for structured deep learning tasks.
Cooper is an open-source Python library designed to facilitate the solution of constrained optimization problems within the PyTorch deep learning framework (2504.01212). It specifically targets scenarios where deep neural networks form part of the objective function or the constraints. The library implements several Lagrangian-based first-order optimization algorithms, aiming to seamlessly integrate constrained optimization techniques with standard deep learning workflows, including automatic differentiation, mini-batch processing, and the use of various network architectures and optimizers.
Cooper addresses optimization problems typically formulated as:
w∈Wminf(w) subject togi(w)≤0,i=1,…,m hj(w)=0,j=1,…,k
Here, w represents the parameters of a deep learning model (or other variables), f(w) is the objective function, gi(w) are inequality constraints, and hj(w) are equality constraints. In deep learning contexts, f, gi, and hj are often non-convex functions involving neural network computations, and gradients are typically estimated using mini-batches of data.
Cooper leverages the Lagrangian formulation to handle these constraints. The associated Lagrangian function is:
L(w,λ,μ)=f(w)+i=1∑mλigi(w)+j=1∑kμjhj(w)
where λ=(λ1,…,λm) are the Lagrange multipliers (dual variables) for the inequality constraints (λi≥0), and μ=(μ1,…,μk) are the multipliers for the equality constraints.
The original constrained problem is related to the unconstrained saddle-point (min-max) problem:
wminλ≥0,μmaxL(w,λ,μ)
Cooper implements first-order methods to find approximate solutions to this saddle-point problem. These methods typically involve alternating updates to the primal variables (w) and the dual variables (λ,μ). For instance, a common approach is gradient descent on w and projected gradient ascent on λ,μ:
wt+1=wt−ηp∇wL(wt,λt,μt)
λt+1=P≥0(λt+ηd∇λL(wt,λt,μt))=P≥0(λt+ηdg(wt))
μt+1=μt+ηd∇μL(wt,λt,μt)=μt+ηdh(wt)
where ηp and ηd are primal and dual learning rates, P≥0 denotes projection onto the non-negative orthant, g(w)=(g1(w),…,gm(w)), and h(w)=(h1(w),…,hk(w)). Cooper implements variations and potentially more sophisticated schemes (e.g., involving augmented Lagrangians or different update rules) compatible with stochastic gradients derived from mini-batches.
Integration with PyTorch and Implementation Structure
A key design principle of Cooper is its tight integration with PyTorch. It allows users to define the objective f(w) and constraints gi(w),hj(w) using standard PyTorch modules and operations. Cooper then leverages PyTorch's automatic differentiation (autograd) engine to compute the necessary gradients (∇wL, ∇λL, ∇μL) efficiently.
The typical workflow involves these steps:
- Define the Problem: Specify the objective function and the constraint functions (both inequality and equality). These are typically functions that take the model parameters (or model output) as input and return scalar values representing the loss or constraint violation levels.
- Instantiate
ConstrainedMinimizationProblem: Create an instance of Cooper's ConstrainedMinimizationProblem class. This object encapsulates the objective and constraint definitions.
- Define Primal and Dual Optimizers: Select standard PyTorch optimizers (e.g.,
torch.optim.Adam, torch.optim.SGD) for updating the primal parameters (w) and Cooper's internal Lagrange multipliers (λ,μ). Cooper manages the dual variables and their updates.
- Instantiate
ConstrainedOptimizer: Wrap the primal optimizer and the ConstrainedMinimizationProblem object within Cooper's ConstrainedOptimizer. This optimizer orchestrates the alternating primal-dual updates.
- Optimization Loop: Implement the training loop. In each iteration:
- Compute the objective and constraint values based on a mini-batch.
- Perform a backward pass to compute gradients via
ConstrainedOptimizer.zero_grad() and lagrangian.backward(). Cooper computes the Lagrangian internally based on current multiplier values.
- Call
ConstrainedOptimizer.step() to update both the primal model parameters and the dual Lagrange multipliers according to the chosen Lagrangian-based scheme.
Below is a conceptual code structure illustrating this workflow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import torch
import cooper
model = ... # Your torch.nn.Module
objective_fn = ... # Computes f(w) based on model output and batch data
constraint_fns = [...] # List of functions computing g_i(w) or h_j(w)
cmp = cooper.ConstrainedMinimizationProblem(is_constrained=True)
primal_optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
cooper_optimizer = cooper.ConstrainedOptimizer(
primal_optimizer=primal_optimizer,
dual_restarts=False, # Option for handling dual variables
problem=cmp
)
for inputs, targets in dataloader:
# Compute objective and constraints
lagrangian = cmp.compute_lagrangian(
closure=lambda: objective_fn(model(inputs), targets),
constraints=constraint_fns # Cooper evaluates these internally
)
# Perform optimization step (updates primal and dual variables)
cooper_optimizer.zero_grad()
lagrangian.backward()
cooper_optimizer.step()
|
This structure allows users to leverage familiar PyTorch components while Cooper handles the complexities of the constrained optimization updates, including managing the Lagrange multipliers and ensuring non-negativity for inequality constraints. Its design explicitly supports mini-batch gradient estimates, making it suitable for large-scale deep learning tasks.
Applicability and Use Cases
Cooper's primary domain is deep learning, but its underlying framework is applicable to general non-convex continuous constrained optimization problems where first-order methods are appropriate. Within deep learning, potential applications include:
- Fairness Constraints: Enforcing fairness criteria (e.g., demographic parity, equalized odds) by formulating them as constraints on the model's predictions across different demographic groups.
- Robustness: Improving model robustness, for example, by constraining the output variation under adversarial perturbations or ensuring stability properties.
- Physics-Informed Neural Networks (PINNs): Incorporating physical laws (often expressed as differential equations) as equality or inequality constraints that the neural network output must satisfy.
- Structured Regularization: Imposing structural constraints on model parameters or activations, such as sparsity, low-rankness, or specific norms, beyond standard weight decay.
- Resource Constraints: Optimizing models under budget constraints related to computational cost, latency, or memory footprint during inference.
- Safe Reinforcement Learning: Enforcing safety constraints during policy optimization in reinforcement learning settings.
The library's focus on Lagrangian methods provides a principled way to handle these constraints, offering a trade-off between objective minimization and constraint satisfaction controlled by the Lagrange multipliers, which are learned during optimization.
Conclusion
Cooper provides a valuable tool for practitioners seeking to incorporate complex constraints into deep learning models using PyTorch (2504.01212). By implementing Lagrangian-based first-order methods and integrating tightly with PyTorch's ecosystem, it simplifies the application of constrained optimization techniques to challenging non-convex problems prevalent in the field. Its suitability for mini-batch settings and general applicability make it a potentially useful component in developing fair, robust, physically consistent, or otherwise structured machine learning systems. The source code's availability facilitates its adoption and extension by the research community (github.com).