Examples

Explore CVXPYlayers through interactive notebooks and scripts.

Quick Start

PyTorch

Most popular for deep learning. Full torch.nn.Module integration with autograd support.

https://github.com/cvxpy/cvxpylayers/blob/master/examples/torch/torch_example.py
JAX

Functional style with jax.grad and jax.vmap. Support for jax.jit coming soon.

https://github.com/cvxpy/cvxpylayers/blob/master/examples/jax/jax_example.py
MLX

Optimized for Apple Silicon. Unified memory for M1/M2/M3 chips.

https://github.com/cvxpy/cvxpylayers/blob/master/examples/mlx/mlx_example.py

Tutorials

Step-by-step introductions to CVXPYlayers:

PyTorch Tutorial

Complete walkthrough of defining problems, creating layers, and training.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/tutorial.ipynb

Control Systems

Linear Quadratic Regulator (LQR)

Learn optimal value function parameters for LQR control.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/lqr.ipynb
Vehicle Path Planning

Autonomous vehicle trajectory optimization.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/vehicle.ipynb
Approximate Dynamic Programming

Convex approximations for dynamic programming problems.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/convex_approximate_dynamic_programming.ipynb

Finance & Portfolio Optimization

Markowitz Portfolio

Classic mean-variance optimization with dynamic rebalancing.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/markowitz_tuning.ipynb

Machine Learning


Resource Allocation


Engineering

GP Circuit Sizing

Geometric program for transistor sizing with gp=True. Sensitivity analysis of delay vs. area/power budgets.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/gp_circuit_sizing.ipynb

Optimization Techniques

Batched Portfolio with Duals

Batched solving, parameter broadcasting, and dual variable extraction for portfolio optimization.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/batched_portfolio.ipynb
Robust Optimization

Learn the right amount of robustness from data. Worst-case design under uncertainty.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/robust_optimization.ipynb
Predict-then-Optimize

Neural net + CvxpyLayer end-to-end pipeline. Decision-focused learning vs. two-stage training.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/predict_then_optimize.ipynb
Convex Regression

Fit a convex piecewise-linear function (max-of-affines) to data via SGD.

https://colab.research.google.com/github/cvxpy/cvxpylayers/blob/master/examples/torch/convex_regression.ipynb

Running Locally

Clone and run any example:

git clone https://github.com/cvxpy/cvxpylayers.git
cd cvxpylayers
pip install -e ".[torch]"
pip install matplotlib jupyter

# Run a notebook
jupyter notebook examples/torch/lqr.ipynb