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


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