Skip to content

[MRG] Fix sinkhorn_unbalanced_translation_invariant with asymmetric reg_m - #908

Open
flyingdoubleG wants to merge 2 commits into
PythonOT:masterfrom
flyingdoubleG:fix-translation-invariant-asymmetric
Open

flyingdoubleG wants to merge 2 commits into
PythonOT:masterfrom
flyingdoubleG:fix-translation-invariant-asymmetric

Conversation

@flyingdoubleG

Copy link
Copy Markdown

Types of changes

Bug fix: ot.unbalanced.sinkhorn_unbalanced_translation_invariant converges to a wrong plan when the two marginal penalties differ (reg_m = (reg_m1, reg_m2) with reg_m1 != reg_m2). It is also reached through method="sinkhorn_translation_invariant" in ot.unbalanced.sinkhorn_unbalanced / sinkhorn_unbalanced2.

Motivation and context / Related issue

Write ρ1 = reg_m1, ρ2 = reg_m2, ε = reg, and u = e^{f/ε}, v = e^{g/ε} for the scalings. Eliminating the optimal translation from the dual (Séjourné et al. 2022) leaves

H(f, g) = −(ρ1+ρ2) · A^{ρ1/(ρ1+ρ2)} · B^{ρ2/(ρ1+ρ2)} − ε ⟨K, u vᵀ⟩, with A = Σ a·u^{−ε/ρ1} and B = Σ b·v^{−ε/ρ2}.

Maximising H in f for fixed g gives

u = û · (B / Â)^{ρ2/(ρ1+ρ2+ε)}, where û = (a / Kv)^{ρ1/(ρ1+ε)} and  = Σ a·û^{−ε/ρ1}.

The update of v is the mirror image. In the code, the factor applied to u is split into sum(b * v**reg_ratio2) ** k_rho2 (inside u_hat) and sum(a * u_hat ** (-reg_ratio1)) ** (-xi_rho1). For the result to match the formula above, the cross exponent must be reg_m1 * reg_m2 / ((reg + reg_m1) * (reg_m1 + reg_m2)), which is k1 * reg_m2 / reg. The code uses k2 * reg_m2 / reg, and symmetrically k1 * reg_m1 / reg instead of k2 * reg_m1 / reg for v. The two pairings agree only when reg_m1 == reg_m2, which is the case every existing test uses.

Examples (mass 1.7 vs 1, reg_m = (0.5, 2)):

  • On a 6 x 5 problem with reg = 0.5, the plan differs from sinkhorn_knopp_unbalanced by 2.1% (relative max).
  • On a 5 x 9 problem with reg = 0.05, the optimality residual of the returned plan is 0.32, where sinkhorn_knopp_unbalanced reaches 1e-14. Over 40 random problems with mixed reg_m, the worst relative error is 1.41. With a 2-d b, the per-histogram costs are off by up to 1.5%.

Changes

k_rho1 = k2 * reg_m1 / reg and k_rho2 = k1 * reg_m2 / reg (the two were swapped), with a comment saying where each one is used.

How has this been tested (if it applies)

  • Over 40 random problems (n, m in [2, 9], reg in {0.05, 0.1, 0.5}, reg_m1, reg_m2 in {0.1, 0.5, 1, 5, 50}), against a float64 log-domain reference run to 1e-14:
    • this fix: worst relative error 2.5e-13 (master: 1.41);
    • an independent implementation of the update derived above: 3.5e-13.
  • reg_m = inf on one or both sides is unchanged: both cross exponents are 0 there, before and after. Error vs sinkhorn_knopp_unbalanced is at most 3e-14.
  • With a 2-d b and reg_m = (0.5, 2), each column's cost now equals the 1-d reg_type="entropy" solve.
  • New test test_translation_invariant_asymmetric_reg_m:
    • parametrized over reg_m in {(0.5, 2), (5, 0.1), (1, 50)}, for each backend;
    • checks the plan against sinkhorn_knopp_unbalanced, and the 2-d b costs against per-column solves;
    • the 6 new cases fail on master and pass with the fix.
  • test/unbalanced/test_sinkhorn.py: 192 passed (numpy + torch backends).

PR checklist

  • I have read the CONTRIBUTING document.
  • The documentation is up-to-date with the changes I made.
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR and Issue fix to the RELEASES.md file.

@iammanoj807 iammanoj807 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this against a direct implementation of entropic unbalanced Sinkhorn. It alternates u = (a / (K v)) ** (ρ1 / (ρ1 + ε)) and v = (b / (Kᵀ u)) ** (ρ2 / (ρ2 + ε)), with K = a bᵀ ⊙ exp(-M / ε) for reg_type="kl" and K = exp(-M / ε) for "entropy".

I used 30 random problems: 3 to 6 points per side, sum(a) between 0.5 and 2, sum(b) = 1, and reg of 0.1 or 0.5. The largest difference between the plans, over both reg_type values:

reg_m master this PR
(0.5, 2) 3.2e-02 1.7e-15
(5, 0.1) 2.4e+00 4.2e-15
(1, 1) 4.0e-15 4.0e-15
(0.7, inf) 4.6e-15 4.6e-15

So the swapped exponents fix the asymmetric case and leave the symmetric and (ρ, inf) cases as they were.

Separately, and not caused by this PR, since master gives the same result: with reg_m = (inf, ρ2), the row sums of the plan are a / a.sum() instead of a. The hard constraint therefore only holds when a sums to 1. (ρ1, inf) is fine for any mass of b.

import numpy as np
import ot

rng = np.random.default_rng(0)
a = rng.random(5) + 0.1
a *= 1.7 / a.sum()
b = rng.random(4) + 0.1
b /= b.sum()
M = 2 * rng.random((5, 4))

G = ot.unbalanced.sinkhorn_unbalanced_translation_invariant(a, b, M, 0.5, (np.inf, 0.7))
print(G.sum(1) / a)  # [0.58823497 0.58823535 ...], i.e. 1 / 1.7
G = ot.unbalanced.sinkhorn_knopp_unbalanced(a, b, M, 0.5, (np.inf, 0.7))
print(G.sum(1) / a)  # [0.99999949 0.99999949 ...]

I can open a separate issue for this if that is easier to track.

The exponents k_rho1 and k_rho2 applied to the sums over the other marginal in the updates of v and u were swapped. They coincide when reg_m1 == reg_m2, so only asymmetric marginal penalties converged to a wrong plan.
@flyingdoubleG
flyingdoubleG force-pushed the fix-translation-invariant-asymmetric branch from d96079d to a91cc3a Compare November 26, 2026 14:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants