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
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),
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
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 |
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 |
|
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 |
__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 |
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 |
1e-06
|
eps
|
float | None
|
Deprecated and ignored. Zero-mass entries of |
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 |
False
|
newton_max_iter
|
int
|
Maximum number of damped Newton iterations run
after Sinkhorn stops short of |
50
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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
|
required |
q
|
Tensor
|
Reference distribution, a nonnegative tensor of shape
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor of shape |
Tensor
|
the broadcast batch, clamped to be nonnegative to absorb |
Tensor
|
floating-point error near zero, computed in |
Tensor
|
|
Tensor
|
promoted dtype of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the shape of |