Repository navigation
[MRG] Fix sinkhorn_unbalanced_translation_invariant with asymmetric reg_m - #908
flyingdoubleG wants to merge 2 commits into
Conversation
iammanoj807
left a comment
There was a problem hiding this comment.
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.
d96079d to
a91cc3a
Compare
Types of changes
Bug fix:
ot.unbalanced.sinkhorn_unbalanced_translation_invariantconverges to a wrong plan when the two marginal penalties differ (reg_m = (reg_m1, reg_m2)withreg_m1 != reg_m2). It is also reached throughmethod="sinkhorn_translation_invariant"inot.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) leavesH(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
uis split intosum(b * v**reg_ratio2) ** k_rho2(insideu_hat) andsum(a * u_hat ** (-reg_ratio1)) ** (-xi_rho1). For the result to match the formula above, the cross exponent must bereg_m1 * reg_m2 / ((reg + reg_m1) * (reg_m1 + reg_m2)), which isk1 * reg_m2 / reg. The code usesk2 * reg_m2 / reg, and symmetricallyk1 * reg_m1 / reginstead ofk2 * reg_m1 / regforv. The two pairings agree only whenreg_m1 == reg_m2, which is the case every existing test uses.Examples (mass 1.7 vs 1, reg_m = (0.5, 2)):
sinkhorn_knopp_unbalancedby 2.1% (relative max).sinkhorn_knopp_unbalancedreaches 1e-14. Over 40 random problems with mixedreg_m, the worst relative error is 1.41. With a 2-db, the per-histogram costs are off by up to 1.5%.Changes
k_rho1 = k2 * reg_m1 / regandk_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)
reg_m = infon one or both sides is unchanged: both cross exponents are 0 there, before and after. Error vssinkhorn_knopp_unbalancedis at most 3e-14.band reg_m = (0.5, 2), each column's cost now equals the 1-dreg_type="entropy"solve.test_translation_invariant_asymmetric_reg_m:sinkhorn_knopp_unbalanced, and the 2-dbcosts against per-column solves;test/unbalanced/test_sinkhorn.py: 192 passed (numpy + torch backends).PR checklist