Python Optimal Transport (POT)

repository·master·Indexed 25 days ago

https://github.com/pythonot/pot

A comprehensive library providing differentiable solvers for optimal transport problems used in signal processing, image processing, and machine learning. POT supports multiple backends including Numpy, Pytorch, Jax, Cupy, and Tensorflow. It features a wide range of solvers for linear/exact OT, regularized OT (Sinkhorn), Wasserstein and Gromov-Wasserstein barycenters, unbalanced and partial OT, and specialized machine learning applications such as domain adaptation and mapping estimation.

Tokens
5.8K
Snippets
14
Records
38
Agent score
84%

What's inside POT

  1. Overview of POT features and solvers

    master

    POT provides a wide range of Optimal Transport (OT) solvers and machine learning applications:

    Generic OT Solvers

    • Linear/Exact OT: Network Simplex solver for Earth Mover's Distance.
    • Regularized OT: Conditional gradient, Generalized conditional gradient, and various Sinkhorn Knopp algorithms (including stabilized, greedy, and screening versions).
    • Barycenters: Wasserstein, Gromov-Wasserstein (GW), convolutional, and debiased Sinkhorn barycenters.
    • Gromov-Wasserstein: Exact and regularized GW distances, Fused-GW (FGW) distances, and GW barycenters.
    • Unbalanced & Partial OT: Unbalanced OT (UOT) with KL relaxation, partial Wasserstein, and partial Gromov-Wasserstein solvers.
    • Specialized OT: Sliced Wasserstein, Wasserstein distance on the circle, Spherical Sliced Wasserstein, and Gaussian Mixture Model (GMM) OT.
    • Large-scale OT: Stochastic solvers and differentiable losses for large-scale problems.

    Machine Learning Solvers

    • Domain Adaptation: OT for domain adaptation with group lasso, Laplacian, or semi-supervised regularization, and the JCPOT algorithm.
    • Mapping Estimation: Linear OT mapping and Joint OT mapping estimation.
    • Graph Neural Networks: TFGW and TW (OT-GNN) layers.
    • Other: Wasserstein Discriminant Analysis (requires autograd and pymanopt).
  2. Install POT (Python Optimal Transport)

    master
    POT is an open-source Python library providing solvers for optimization problems related to Optimal Transport for signal processing, image processing, and machine learning. It supports multiple backends including Pytorch, Jax, Numpy, Cupy, and Tensorflow.
  3. Understand the core use cases of POT

    master

    POT (Python Optimal Transport) is designed for solving Optimal Transport (OT) problems in the context of machine learning. You should use POT when you need to:

    1. Measure similarity between distributions: Use the Wasserstein distance (the optimal value of the OT problem) to compare datasets, word embeddings, signals, or spectra. It is particularly useful when distributions have disjoint supports.
    2. Find correspondences (Mapping Estimation): Use the OT matrix (the optimal mapping) to find non-supervised correspondences between samples in discrete distributions. This is useful for tasks like color transfer between images or domain adaptation.
    3. Perform research with custom regularization: Use generic solvers like ot.optim.cg to solve OT problems with any smooth/continuous regularization term (e.g., graph Laplacian regularization or Gromov-Wasserstein).

    Note on Scale: POT is optimized for efficiency but is not designed for extremely large-scale problems due to the $\mathcal{O}(n^2)$ memory requirement for the cost matrix. For very large sample sizes, consider using 'lazy' Sinkhorn implementations or neural network-based dual approximations.

  4. Explore the POT API modules

    master

    The ot module is the primary entry point for the Python Optimal Transport (POT) library. It provides access to various specialized modules for different optimal transport tasks. Key modules include:

    • ot.backend: Backend support.
    • ot.batch: Batch processing capabilities.
    • ot.bregman: Bregman divergences.
    • ot.coot: C++ accelerated optimal transport.
    • ot.datasets: Access to optimal transport datasets.
    • ot.gromov: Gromov-Wasserstein distances.
    • ot.lp: Linear programming solvers.
    • ot.partial: Partial optimal transport.
    • ot.plot: Visualization tools.
    • ot.unbalanced: Unbalanced optimal transport.
    • ot.weak: Weak optimal transport.

    Refer to the ot module for the main high-level functions.

  5. Explore POT example galleries by topic

    master

    POT provides a wide range of examples covering various Optimal Transport (OT) tasks. You can find specialized implementation examples in the following categories:

    • OT and regularized OT: Standard OT problems and entropic regularization.
    • Differentiable OT with PyTorch: Using OT within deep learning workflows and backends.
    • Gromov-Wasserstein (GW) and Fused GW: Comparing distributions in different metric spaces.
    • Unbalanced and Partial OT: Handling distributions with different total masses.
    • OT in 1D and Sliced Wasserstein: Efficient OT computations for high-dimensional data using projections.
    • OT on Gaussian and Gaussian Mixture Models: Analytical or specialized OT for parametric models.
    • Factored and Low-Rank OT: Scalable OT for large datasets.
    • Wasserstein and (F)GW barycenters: Computing the mean/centroid of multiple distributions.
    • Domain adaptation with OT: Using OT for aligning different data distributions.
  6. Use multiple backends (PyTorch, JAX, TensorFlow, CuPy) for OT solvers

    master

    Since version 0.8, POT supports multiple backends, allowing you to write solvers that are independent of the input array type. Functions like ot.emd and ot.emd2 will automatically accept numpy.array, torch.tensor, or jax.numpy.array. The output will match the input type and device (e.g., returning a PyTorch tensor if the input was a PyTorch tensor). When possible, computations are performed on the same device and outputs are differentiable with respect to the inputs.

    # a and b are 1D histograms (sum to 1 and positive)
    # M is the ground cost matrix
    T = ot.emd(a, b, M)  # exact linear program
    w = ot.emd2(a, b, M)  # Wasserstein computation
  7. Install dependencies for ot.dr (Wasserstein dimensionality reduction)

    master

    The ot.dr submodule requires autograd and pymanopt. You can install them via pip:

    pip install pymanopt autograd
    pip install pymanopt autograd
  8. GPU acceleration and backend compatibility

    master

    POT provides automatic GPU compatibility for most functions. However, solvers relying on the exact C++ solver (such as ot.emd, ot.emd2, ot.gromov_wasserstein, ot.gromov_wasserstein2, and ot.optim.cg) will execute on the CPU. If you pass GPU arrays to these functions, POT will automatically handle the memory copy to CPU, though this may incur overhead.

    Supported Backends:

    • Numpy: All functions and solvers.
    • Pytorch: All outputs are differentiable w.r.t. inputs.
    • Jax: Some functions are differentiable; others require a wrapper.
    • Tensorflow: All outputs are differentiable w.r.t. inputs.
    • Cupy: GPU only; no differentiation support.
  9. Solve Entropic Regularized Optimal Transport

    master

    Entropic regularization is the most common method for smoothing the Optimal Transport problem, making it strictly convex and solvable via the Sinkhorn algorithm. You can solve this using the unified ot.solve API or the classical ot.sinkhorn / ot.sinkhorn2 APIs.

    Unified API:

    • ot.solve(M, a, b, reg=...): Returns an object containing both the transport plan (.plan) and the loss value (.value).

    Classical API:

    • ot.sinkhorn(a, b, M, reg=...): Returns the OT Sinkhorn matrix (transport plan).
    • ot.sinkhorn2(a, b, M, reg=...): Returns the OT Sinkhorn value (loss).
    # unified API
    P = ot.solve(M, a, b, reg=1).plan  # OT Sinkhorn matrix
    loss = ot.solve(M, a, b, reg=1).value # OT Sinkhorn value
    
    # classical API
    P = ot.sinkhorn(a, b, M, reg=1)  # OT Sinkhorn matrix
    loss = ot.sinkhorn2(a, b, M, reg=1)  # OT Sinkhorn value
  10. Perform Domain Adaptation using OTDA classes

    master

    POT provides several Domain Adaptation (OTDA) classes that inherit from ot.da.BaseTransport. These classes follow a scikit-learn-like interface:

    1. Initialize: Set parameters like regularization.
    2. Fit: Call .fit(Xs=Xs, Xt=Xt) to estimate the mapping between source samples Xs and target samples Xt.
    3. Transform: Use .transform(Xs=Xs) to map source samples to the target distribution, or .inverse_transform(Xt=Xt) to map target samples back to the source.

    Available implementations:

    • ot.da.EMDTransport: Barycentric mapping with EMD transport.
    • ot.da.SinkhornTransport: Barycentric mapping with Sinkhorn transport.
    • ot.da.SinkhornL1l2Transport: Barycentric mapping with Sinkhorn + group Lasso regularization.
    • ot.da.SinkhornLpl1Transport: Barycentric mapping with Sinkhorn + non-convex group Lasso regularization.
    • ot.da.LinearTransport: Linear mapping estimation between Gaussians.
    • ot.da.MappingTransport: Nonlinear mapping estimation.
    ot_emd = ot.da.EMDTransport()
    ot_emd.fit(Xs=Xs, Xt=Xt)
    Xs_mapped = ot_emd.transform(Xs=Xs)
  11. Install POT via pip

    master

    You can install the POT (Python Optimal Transport) library using pip. The library has been tested on Linux, MacOSX, and Windows. It requires a C++ compiler for building/installing the EMD solver.

    To install the stable version:

    pip install POT

    To install the latest version from GitHub:

    pip install -U git+https://github.com/PythonOT/POT.git

    To install with all optional dependencies (including cvxopt, which is licensed under GPL 3.0):

    pip install POT[all]

    Available optional installation submodules:

    • backend-jax
    • backend-tf
    • backend-torch
    • cvxopt
    • dr
    • gnn
    • all
    pip install POT