OTT: Optimal Transport Tools in Jax.
Project description
Optimal Transport Tools (OTT), A toolbox for everything Wasserstein.
See full documentation for detailed info.
OTT is a JAX toolbox that bundles a few utilities to solve optimal transport problems. These tools can help you compare and match two weighted point clouds (or histograms, measures, etc.), given a cost (e.g. a distance) between single points.
Most of OTT is, for now, supported by a sturdy, versatile and efficient implementation of the Sinkhorn algorithm that takes advantage of JAX features, such as JIT, auto-vectorization and implicit differentiation.
A typical OT problem has two ingredients: a pair of weight vectors a and b (one for each measure), with a ground cost matrix that is either directly given, or derived as the pairwise evaluation of a cost function on pairs of points taken from two measures. The main design choice in OTT comes from encapsulating the cost in a Geometry object, and bundle it with a few useful operations (notably kernel applications). The most common geometry is that of two clouds of vectors compared with the squared Euclidean distance, as illustrated in the example below:
Example
import jax
from ott.geometry import pointcloud
from ott.core import sinkhorn
# Samples two point clouds and their weights.
rngs = jax.random.split(jax.random.PRNGKey(0),4)
n, m, d = 12, 14, 2
x = jax.random.normal(rngs[0], (n,d)) + 1
y = jax.random.uniform(rngs[1], (m,d))
a = jax.random.uniform(rngs[2], (n,))
b = jax.random.uniform(rngs[3], (m,))
a, b = a / np.sum(a), b / np.sum(b)
# Computes the couplings via Sinkhorn algorithm.
geom = pointcloud.PointCloud(x,y)
out = sinkhorn.sinkhorn(geom, a, b)
P = geom.transport_from_potentials(out.f, out.g)
The call to sinkhorn above works out the optimal transport solution by storing its output. The transport matrix can be instantiated using those optimal solutions and the Geometry again. That transoprt matrix links each point from the first point cloud to one or more points from the second, as illustrated below.
To be more precise, the sinkhorn algorithm operates on the Geometry,
taking into account weights a and b, to solve the OT problem, produce a named tuple that contains two optimal dual potentials f and g (vectors of the same size as a and b), the objective reg_ot_cost and a log of the errors of the algorithm as it converges, and a converged flag.
Overall description of source code
Currently implements the following classes and functions:
-
In the geometry folder,
-
The
CostFnclass in costs.py and its descendants define cost functions between points. Two simple costs are currently provided,Euclideanbetween vectors, andBures, between a pair of mean vector and covariance (p.d.) matrix. -
The
Geometryclass in geometry.py and its descendants describe a cost structure between two measures. That cost structure is accessed through various member functions, either used when running the Sinkhorn algorithm (typically kernel multiplications, or log-sum-exp row/column-wise application) or after (to apply the OT matrix to a vector).-
In its generic
Geometryimplementation, as in geometry.py, an object can be initialized with either acost_matrixalong with anepsilonregularization parameter (or scheduler), or with akernel_matrix. -
If one wishes to compute OT between two weighted point clouds
and
endowed with a given cost function (e.g. Euclidean)
, the
PointCloudclass in pointcloud.py can be used to define the corresponding kernel. When the number of these points grows very large, this geometry can be instantiated with an
online=Trueparameter, to avoid storing the kernel matrix and choose instead to recompute the matrix on the fly at each application. -
Simlarly, if all measures to be considered are supported on a separable grid (e.g.
), and the cost is separable along all axis, i.e. the cost between two points on that grid is equal to the sum of (possibly
different) cost functions evaluated on each of the
pairs of coordinates, then the application of the kernel is much simplified, both in log space or on the histograms themselves. This particular case is exploited in the
Gridgeometry in grid.py which can be instantiated as a hypercube using agrid_sizeparameter, or directly through grid locations inx.
-
-
-
In the core folder,
-
The
sinkhornfunction in sinkhorn.py runs the Sinkhorn algorithm, with the aim of solving approximately one or various optimal transport problems in parallel. An OT problem is defined by aGeometryobject, and a pair(or batch thereof) of histograms. The function's outputs are stored in a
SinkhornOutputnamed t-uple, containing potentials, regularized OT cost, sequence of errors and a convergence flag. Such outputs (with the exception of errors and convergence flag) can be differentiated w.r.t. any of the three inputs(Geometry, a, b)either through backprop or implicit differentiation of the optimality conditions of the optimal potentialsfandg. -
In discrete_barycenter.py: implementation of discrete Wasserstein barycenters : given
histograms all supported on the same
Geometry, compute a barycenter of theses measures, using an algorithm by Janati et al. (2020)
-
-
In the tools folder,
-
In soft_sort.py: implementation of soft-sorting operators .
-
The
sinkhorn_divergencefunction in sinkhorn_divergence.py, implements the Sinkhorn divergence, a variant of the Wasserstein distance that uses regularization and is computed by centering the output ofsinkhornwhen comparing two measures. -
The
Transportclass in sinkhorn_divergence.py, provides a simple wrapper to thesinkhornfunction defined above when the user is primarily interested in computing and storing an OT matrix.
-
Disclaimer: this is not an official Google product.
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 Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ott-jax-0.1.2.tar.gz.
File metadata
- Download URL: ott-jax-0.1.2.tar.gz
- Upload date:
- Size: 37.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75850f393ed4d4eedbf112712e30e37eb74a6abe1abd85b7832f90102bbd2a69
|
|
| MD5 |
e0e6349d2659cf021d82a1b3a7a3bd4c
|
|
| BLAKE2b-256 |
29a32a8ae2a617ab9e7d6f4a57a6c52fbe104ad316bfed0b1e3c0d6efead12da
|
File details
Details for the file ott_jax-0.1.2-py3-none-any.whl.
File metadata
- Download URL: ott_jax-0.1.2-py3-none-any.whl
- Upload date:
- Size: 46.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d35548ee29a200dbbd2a5de5149e4b463fb87cc26952501fb5cd8e1137c36e7a
|
|
| MD5 |
c3024e2ba9f276b4167a64c86f0bec2f
|
|
| BLAKE2b-256 |
d46fff5b7aacdc7a19f4f63934a63e715f6897036a5ed261f02d0a639f86fc5f
|