A library for differentiable nonlinear optimization.
Project description
A library for differentiable nonlinear optimization
[Paper] [Blog] [Webpage] [Tutorials] [Docs]
Theseus is an efficient application-agnostic library for building custom nonlinear optimization layers in PyTorch to support constructing various problems in robotics and vision as end-to-end differentiable architectures.
Differentiable nonlinear optimization provides a general scheme to encode inductive priors, as the objective function can be partly parameterized by neural models and partly with expert domain-specific differentiable models. The ability to compute gradients end-to-end is retained by differentiating through the optimizer which allows neural models to train on the final task loss, while also taking advantage of priors captured by the optimizer.
Current Features
Application agnostic interface
Our implementation provides an easy to use interface to build custom optimization layers and plug them into any neural architecture. Following differentiable features are currently available:
- Second-order nonlinear optimizers
- Gauss-Newton, Levenberg–Marquardt
- Linear solvers
- Dense: Cholesky, LU; Sparse: CHOLMOD, LU
- Commonly used costs, AutoDiffCostFunction
- Lie groups
- Robot kinematics
Efficiency based design
We support several features that improve computation times and memory consumption:
- Sparse linear solvers
- Batching and GPU acceleration
- Automatic vectorization
- Backward modes: Implicit, Truncated, Direct Loss Minimization (DLM), Sampling (LEO)
Getting Started
Prerequisites
- We strongly recommend you install Theseus in a venv or conda environment with Python 3.7-3.9.
- Theseus requires
torch
installation. To install for your particular CPU/CUDA configuration, follow the instructions in the PyTorch website. - For GPU support, Theseus requires nvcc to compile custom CUDA operations. Make sure it matches the version used to compile pytorch with
nvcc --version
. If not, install it and ensure its location is on your system's$PATH
variable. - Theseus also requires
suitesparse
, which you can install via:sudo apt-get install libsuitesparse-dev
(Ubuntu).conda install -c conda-forge suitesparse
(Mac).
Installing
git clone https://github.com/facebookresearch/theseus.git && cd theseus
pip install -e .
If you are interested in contributing to Theseus, instead install
pip install -e ".[dev]"
and follow the more detailed instructions in CONTRIBUTING.
Running unit tests
pytest theseus
By default, unit tests include tests for our CUDA extensions. You can add the option -m "not cudaext"
to skip them when installing without CUDA support.
Examples
Simple example. This example is fitting the curve $y$ to a dataset of $N$ observations $(x,y) \sim D$. This is modeled as an Objective
with a single CostFunction
that computes the residual $y - v e^x$. The Objective
and the GaussNewton
optimizer are encapsulated into a TheseusLayer
. With RMSprop
and MSE loss, $x$ is learned by differentiating through the TheseusLayer
.
import torch
import theseus as th
x_true, y_true, v_true = read_data() # shapes (1, N), (1, N), (1, 1)
x = th.Variable(torch.randn_like(x_true), name="x")
y = th.Variable(y_true, name="y")
v = th.Vector(1, name="v") # a manifold subclass of Variable for optim_vars
def error_fn(optim_vars, aux_vars): # returns y - v * exp(x)
x, y = aux_vars
return y.tensor - optim_vars[0].tensor * torch.exp(x.tensor)
objective = th.Objective()
cost_function = th.AutoDiffCostFunction(
[v], error_fn, y_true.shape[1], aux_vars=[x, y],
cost_weight=th.ScaleCostWeight(1.0))
objective.add(cost_function)
layer = th.TheseusLayer(th.GaussNewton(objective, max_iterations=10))
phi = torch.nn.Parameter(x_true + 0.1 * torch.ones_like(x_true))
outer_optimizer = torch.optim.RMSprop([phi], lr=0.001)
for epoch in range(10):
solution, info = layer.forward(
input_tensors={"x": phi.clone(), "v": torch.ones(1, 1)},
optimizer_kwargs={"backward_mode": th.BackwardMode.IMPLICIT})
outer_loss = torch.nn.functional.mse_loss(solution["v"], v_true)
outer_loss.backward()
outer_optimizer.step()
See tutorials, and robotics and vision examples to learn about the API and usage.
Additional Information
- Join the community on Github Discussions for questions and sugesstions.
- Use Github Issues for bugs and features.
- See CONTRIBUTING if interested in helping out.
Citing Theseus
If you use Theseus in your work, please cite the paper with the BibTeX below.
@article{pineda2022theseus,
title = {{Theseus: A Library for Differentiable Nonlinear Optimization}},
author = {Luis Pineda and Taosha Fan and Maurizio Monge and Shobha Venkataraman and Paloma Sodhi and Ricky Chen and Joseph Ortiz and Daniel DeTone and Austin Wang and Stuart Anderson and Jing Dong and Brandon Amos and Mustafa Mukadam},
journal = {arXiv preprint arXiv:2207.09442},
year = {2022}
}
License
Theseus is MIT licensed. See the LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file theseus-ai-0.1.0.tar.gz
.
File metadata
- Download URL: theseus-ai-0.1.0.tar.gz
- Upload date:
- Size: 136.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3aea6b738cf95b7fc2e86509309789b020c571f6085f149ed8280728b5e83773 |
|
MD5 | 1c1278ebcfb6280d5d26ad5fb4d5f993 |
|
BLAKE2b-256 | f54c4d94df9451f079fdcb590c9ed6a161cdd48d6a5e908937b388741685a41f |
File details
Details for the file theseus_ai-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: theseus_ai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 180.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76ff0114b7a2c1172710835a471e4063b6c8317d085cd78ffdbf617ef649b879 |
|
MD5 | fe0f211348c5a93d5919c10e8182c725 |
|
BLAKE2b-256 | 5a2006bbc6e67841e4e84cea81eb73348d3fff03c9f806659f9ce7cce3bff6c2 |
File details
Details for the file theseus_ai-0.1.0-cp39-cp39-manylinux_2_17_x86_64.whl
.
File metadata
- Download URL: theseus_ai-0.1.0-cp39-cp39-manylinux_2_17_x86_64.whl
- Upload date:
- Size: 578.5 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38973458cc1fa8c87f4809ea6c2b3f86729147d84672fd847af198cefaa4914b |
|
MD5 | 0321465c57557af658f4dc73e09d67d5 |
|
BLAKE2b-256 | 951353e3f82f0ddc883a5ceb7880e46fa824d232af2c62f70896570c37267fbf |