Skip to content
Edit this page View source of this page

optora.divergences.wasserstein

Entropy-regularized Wasserstein (Sinkhorn) divergence between distributions.

SinkhornDivergence

Bases: Divergence

Debiased entropic Wasserstein divergence between discrete distributions.

For discrete distributions p and q (nonnegative tensors that sum to one along their last dimension) sharing a common support with pairwise ground cost cost, the entropic optimal transport cost is

\[ \mathrm{OT}_\epsilon(p, q) = \min_{\pi \in U(p, q)} \langle \mathrm{cost}, \pi \rangle + \epsilon \sum_{ij} \pi_{ij} \big(\log \pi_{ij} - 1\big) \]

where \(U(p, q)\) is the set of transport plans (joint distributions) with marginals p and q, and \(\epsilon\) is the entropic regularization strength. It is evaluated through its dual (Feydy et al. 2019),

\[ \mathrm{OT}_\epsilon(p, q) = \max_{f, g} \; \langle f, p \rangle + \langle g, q \rangle - \epsilon \big\langle p \otimes q,\, e^{(f \oplus g - \mathrm{cost}) / \epsilon} - 1 \big\rangle + c(p, q), \]

where \(c(p, q) = \epsilon \sum_i p_i \log p_i + \epsilon \sum_j q_j \log q_j - \epsilon\) collects terms that cancel in the debiased divergence below and are therefore dropped. The potentials stay finite even where p or q has no mass, and zero-mass entries are excluded from the solve exactly rather than clamped. The dual is maximized by log-domain Sinkhorn scaling and, if that stops short, a damped Newton polish (the solver optora.divergences.EntropicTransportDivergence uses), both stopping on the L1 marginal violation of the plan. Updates go through torch.logsumexp rather than the Gibbs kernel \(\exp(-\mathrm{cost} / \epsilon)\), which underflows to exact zero for small \(\epsilon\) or large cost.

The value is the dual objective at the final potentials, with \(f\) the exact \(c\)-transform of \(g\), rather than the transport cost \(\langle \mathrm{cost}, \pi \rangle\) of the final plan. Every dual point bounds \(\mathrm{OT}_\epsilon\) from below, and its error is second order in the marginal violation. The plan does not meet its marginals until the solve converges, so its cost would be off to first order. The potentials are held fixed under autograd, so by the envelope theorem the gradients, \(f\) in p and \(g\) in q, are exact once the solve has converged.

The plain entropic cost is biased, since \(\mathrm{OT}_\epsilon(p, p)\) is not zero for \(\epsilon > 0\). This class therefore computes the debiased Sinkhorn divergence

\[ S_\epsilon(p, q) = \mathrm{OT}_\epsilon(p, q) - \tfrac{1}{2}\mathrm{OT}_\epsilon(p, p) - \tfrac{1}{2}\mathrm{OT}_\epsilon(q, q), \]

which removes the self-transport bias so that \(S_\epsilon(p, p) = 0\) exactly, as the Divergence contract requires. As \(\epsilon \to 0\) it converges to the unregularized optimal transport cost under cost (Feydy et al. 2019). The three transport costs the debiasing needs share one ground cost, so they are solved as a single batched Sinkhorn problem rather than three sequential loops. optora.dro.wasserstein_dro uses this divergence to define Wasserstein-based ambiguity sets.

Attributes:

Name Type Description
cost Tensor

Square, nonnegative pairwise ground cost matrix between the shared support points of p and q, of shape (n, n), or a batch of such matrices of shape (..., n, n) whose leading dimensions broadcast against the batch of p and q.

epsilon

Positive entropic regularization strength. Smaller values approximate the unregularized transport cost more closely and need more Sinkhorn iterations to converge.

max_iter

Maximum number of Sinkhorn scaling iterations.

tol

Convergence tolerance on the L1 marginal violation of the transport plan, taken over the whole batched problem so it stops when its slowest element does, and floored at \(n\) times the machine epsilon of the working dtype.

eps

Deprecated and unused. It is None unless passed explicitly.

check_interval

Number of Sinkhorn iterations between host reads of the convergence flag. The potentials are frozen once converged, so the divergence does not depend on this interval.

newton_max_iter

Maximum number of damped Newton iterations run after Sinkhorn stops short of tol. Zero disables the phase.

__init__(cost, epsilon=0.1, max_iter=100, tol=1e-06, eps=None, check_interval=DEFAULT_CHECK_INTERVAL, validate=False, newton_max_iter=50)

Initialize the Sinkhorn divergence.

Parameters:

Name Type Description Default
cost Tensor

Square, nonnegative pairwise ground cost matrix between the shared support points of p and q, of shape (n, n), or a batch of such matrices of shape (..., n, n), one per transport problem, broadcast against the batch of p and q.

required
epsilon float

Positive entropic regularization strength.

0.1
max_iter int

Maximum number of Sinkhorn scaling iterations.

100
tol float

Convergence tolerance on the L1 marginal violation of the transport plan, over the whole batched problem. It is floored at n times the machine epsilon of the working dtype, below which the violation is rounding error.

1e-06
eps float | None

Deprecated and ignored. Zero-mass entries of p and q are excluded from the solve exactly, so there is nothing left to clamp. Passing it emits a DeprecationWarning, and it will be removed in a future minor release.

None
check_interval int

Number of Sinkhorn iterations between host reads of the convergence flag. Raise it to trade redundant frozen iterations for fewer device synchronizations.

DEFAULT_CHECK_INTERVAL
validate bool

Whether to check that cost is nonnegative. The check reads a reduction over cost on the host, which blocks until the device has produced it, so it is opt-in and off by default to keep construction asynchronous. The shape and hyperparameter checks are metadata-only and always run.

False
newton_max_iter int

Maximum number of damped Newton iterations run after Sinkhorn stops short of tol. Each solves one (n, n) linear system per transport problem. Zero disables the phase.

50

Raises:

Type Description
ValueError

If cost is not a square matrix or a batch of square matrices, if validate is set and cost contains negative entries, if epsilon, max_iter, tol, or check_interval are not positive, or if newton_max_iter is negative.

forward(p, q)

Compute the debiased Sinkhorn divergence of p from q.

The three entropic transport costs the debiasing needs are solved as one batched Sinkhorn problem rather than three sequential ones. The marginal pairs \((p, q)\), \((p, p)\) and \((q, q)\) are stacked along a new leading dimension against the shared ground cost, so the three problems share one loop and one convergence tracker, and the solve makes a third as many periodic host reads.

Parameters:

Name Type Description Default
p Tensor

Candidate distribution, a nonnegative tensor of shape (..., n) whose last dimension sums to one and indexes cost.

required
q Tensor

Reference distribution, a nonnegative tensor of shape (..., n) whose last dimension sums to one and indexes cost. Its batch shape must broadcast against p's and against the batch shape of cost.

required

Returns:

Type Description
Tensor

A tensor of shape batch_shape holding \(S_\epsilon(p, q)\) over

Tensor

the broadcast batch, clamped to be nonnegative to absorb

Tensor

floating-point error near zero, computed in

Tensor

optora.core.working_dtype(p, q, cost) and returned in the

Tensor

promoted dtype of p and q.

Raises:

Type Description
ValueError

If the shape of p or q does not match cost.