Skip to content
Edit this page View source of this page

optora.divergences.entropic_transport

Entropic optimal transport cost relative to a kernel-smoothed reference.

EntropicTransportDivergence

Bases: Divergence

Excess entropic transport cost of a candidate over its smoothed reference.

For a reference distribution q and a candidate p on a shared finite support with ground cost \(C\), a regularization strength \(\epsilon > 0\), and a reference measure \(\nu\) on the support, the Sinkhorn distance of Wang, Gao and Xie (2025) is

\[ \mathcal{W}_\epsilon(q, p) = \min_{\gamma \in \Gamma(q, p)} \mathbb{E}_\gamma[C] + \epsilon\, H(\gamma \mid q \otimes \nu), \]

with \(H\) the relative entropy. Its minimum over p is attained at the kernel-smoothed reference \(P^0 = q^\top Q\), where \(Q_{ij} \propto \nu_j e^{-C_{ij}/\epsilon}\) is the row-stochastic Gibbs kernel, and subtracting that minimum leaves

\[ D(p \,\|\, q) = \epsilon \min_{\gamma \in \Gamma(q, p)} \mathrm{KL}\big(\gamma \,\big\|\, \mathrm{diag}(q)\, Q\big) = \epsilon \max_g \; \sum_j p_j g_j - \sum_i q_i \log \sum_j Q_{ij} e^{g_j}, \]

the second form being the concave semi-dual over column potentials \(g\). It is nonnegative and vanishes exactly at its centre \(P^0\), not at p == q, because entropic smoothing moves the centre of the ball off the reference. optora.dro.SinkhornAmbiguitySet bounds this quantity by its radius.

The semi-dual is maximized in two phases. Both stop once the L1 column-marginal violation, which is the norm of the semi-dual's gradient, falls below tol.

  1. Sinkhorn scaling in the log domain, \(O(n^2)\) per iteration. It converges linearly at a rate that degrades as \(\max C/\epsilon\) grows, and at a sharp kernel it stalls far from the maximum.
  2. If Sinkhorn stops short, a Levenberg-Marquardt damped Newton method on the semi-dual with its exact Hessian (Brauer et al. 2017), \(O(n^3)\) per iteration, warm-started from the Sinkhorn potentials. A step is kept unless it lowers the semi-dual by more than the rounding error of its evaluation. The damping falls after every accepted step, so the method becomes a pure Newton method near the maximum, where it converges quadratically however sharp the kernel is.

The value returned is \(\epsilon\) times the semi-dual at the final potentials. Every dual point bounds the divergence from below, so an inexact solve can only understate it. Near the maximum the understatement is second order in the marginal violation, whereas the primal objective of the transport plan would be off to first order. The potentials are held fixed under autograd, so by the envelope theorem the gradient is exact once the solve has converged.

Zero-mass entries of either marginal are excluded from the solve exactly rather than clamped. A row with \(q_i = 0\) transports nothing, and a column with \(p_j = 0\) takes the potential \(-\infty\), the semi-dual's maximizer in that coordinate, which removes it from every row's normalizer. The value is then that of the problem restricted to the two supports, with no accuracy floor. The derivative with respect to the mass of an empty column is \(-\infty\), the slope of \(t \log t\) at zero. It is not representable, and autograd reports zero for it.

Attributes:

Name Type Description
cost Tensor

Square ground cost matrix of shape (n, n).

reference Tensor

Nonnegative reference measure \(\nu\) of shape (n,). It matters only up to a positive scalar factor, because \(Q\) normalizes each row.

epsilon

Positive entropic regularization strength.

max_iter

Maximum number of Sinkhorn scaling iterations.

newton_max_iter

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

tol

Convergence tolerance on the L1 column-marginal violation, floored at \(n\) times the machine epsilon of the working dtype, the rounding floor of the violation itself.

eps

Deprecated and unused. It is None unless passed explicitly.

check_interval

Sinkhorn iterations between host reads of the convergence flag. The potentials freeze once converged. The Newton phase, whose iterations are expensive, reads the flag after every iteration.

__init__(cost, epsilon, reference=None, max_iter=1000, newton_max_iter=50, tol=1e-09, eps=None, check_interval=DEFAULT_CHECK_INTERVAL, validate=False)

Initialize the entropic transport divergence.

Parameters:

Name Type Description Default
cost Tensor

Square ground cost matrix of shape (n, n).

required
epsilon float

Positive entropic regularization strength.

required
reference Tensor | None

Nonnegative reference measure of shape (n,). Defaults to the counting measure, ones(n) in cost's dtype.

None
max_iter int

Maximum number of Sinkhorn scaling iterations.

1000
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 batch element. Zero disables the phase.

50
tol float

Convergence tolerance on the L1 column-marginal violation, 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-09
eps float | None

Deprecated and ignored. Zero-mass marginal entries 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

Sinkhorn iterations between host reads of the convergence flag.

DEFAULT_CHECK_INTERVAL
validate bool

Whether to check that cost is finite and reference is nonnegative with positive mass. The check reads reductions on the host, so it is opt-in.

False

Raises:

Type Description
ValueError

If cost is not a square 2D tensor, reference has the wrong shape, a hyperparameter is out of range, or validate is set and cost or reference is invalid.

log_kernel(dtype)

Return the row-stochastic Gibbs kernel \(\log Q\) in dtype.

The kernel is \(\log Q_{ij} = \log \nu_j - C_{ij}/\epsilon - \log \sum_k \nu_k e^{-C_{ik}/\epsilon}\), normalized by logsumexp so that a large cost over a small \(\epsilon\) never underflows a whole row. Columns with \(\nu_j = 0\) are exactly -inf.

Parameters:

Name Type Description Default
dtype dtype

Floating-point dtype to compute in.

required

Returns:

Type Description
Tensor

A tensor of shape (n, n) whose rows exponentiate to

Tensor

distributions.

forward(p, q)

Compute the excess entropic transport cost of p over q.

Parameters:

Name Type Description Default
p Tensor

Candidate distribution of shape (..., n).

required
q Tensor

Reference distribution of shape (..., n), broadcasting against p.

required

Returns:

Type Description
Tensor

\(D(p \,\|\, q)\) over the broadcast batch, clamped to be

Tensor

nonnegative, computed in optora.core.working_dtype(p, q, cost)

Tensor

and returned in the promoted dtype of p and q. A candidate

Tensor

with mass where \(\nu\) has none admits no coupling and returns

Tensor

inf.

Raises:

Type Description
ValueError

If the last dimension of p or q does not match cost.