diff --git a/RELEASES.md b/RELEASES.md index 78f17f27a..ffbbcdf98 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -22,6 +22,7 @@ #### Closed issues +- Return a zero plan and zero transport cost when the requested mass is zero in both entropic partial Wasserstein solvers. - Fix dead links in the README (three example gallery pages and the reference [39]), the user guide and the contributing guide (PR #928) - Fix ordinary JAX gradients for dense `ot.emd2` and preserve the magnitude and higher-order dependence of gradients supplied to `JaxBackend.set_gradients`, including the envelope gradients in `ot.solve` (Issue #625) - Fix `ot.semidiscrete_wasserstein2_unif_circle` pairing sorted sample locations with unsorted weights, which broke permutation and rotation invariance for nonuniform weights (Issue #921) diff --git a/ot/partial/partial_solvers.py b/ot/partial/partial_solvers.py index 3d02425f8..df16ea6af 100755 --- a/ot/partial/partial_solvers.py +++ b/ot/partial/partial_solvers.py @@ -489,7 +489,7 @@ def entropic_partial_wasserstein( reg : float Regularization term > 0 m : float, optional - Amount of mass to be transported + Amount of mass to be transported. If zero, the plan is all zeros. method : str, optional Solver used, either ``'sinkhorn'`` (classical multiplicative-domain solver, default) or ``'sinkhorn_log'`` (numerically stable log-domain @@ -593,7 +593,7 @@ def _entropic_partial_wasserstein_sinkhorn( if m is None: m = nx.min(nx.stack((nx.sum(a), nx.sum(b)))) * 1.0 if m < 0: - raise ValueError("Problem infeasible. Parameter m should be greater than 0.") + raise ValueError("Problem infeasible. Parameter m should be nonnegative.") if m > nx.min(nx.stack((nx.sum(a), nx.sum(b)))): raise ValueError( "Problem infeasible. Parameter m should lower or" @@ -602,6 +602,13 @@ def _entropic_partial_wasserstein_sinkhorn( log_e = {"err": []} + if m == 0: + plan = nx.zeros(M.shape, type_as=M) + if log: + log_e["partial_w_dist"] = nx.sum(plan) + return plan, log_e + return plan + if nx.__name__ == "numpy": # Next 3 lines equivalent to K=nx.exp(-M/reg), but faster to compute K = np.empty(M.shape, dtype=M.dtype) @@ -699,7 +706,7 @@ def entropic_partial_wasserstein_logscale( reg : float Regularization term > 0 m : float, optional - Amount of mass to be transported + Amount of mass to be transported. If zero, the plan is all zeros. numItermax : int, optional Max number of iterations stopThr : float, optional @@ -756,13 +763,10 @@ def entropic_partial_wasserstein_logscale( if len(b) == 0: b = nx.ones(dim_b, type_as=b) / dim_b - La = nx.log(a) - Lb = nx.log(b) - if m is None: m = nx.min(nx.stack((nx.sum(a), nx.sum(b)))) * 1.0 if m < 0: - raise ValueError("Problem infeasible. Parameter m should be greater than 0.") + raise ValueError("Problem infeasible. Parameter m should be nonnegative.") if m > nx.min(nx.stack((nx.sum(a), nx.sum(b)))): raise ValueError( "Problem infeasible. Parameter m should lower or" @@ -771,6 +775,15 @@ def entropic_partial_wasserstein_logscale( log_e = {"err": []} + if m == 0: + plan = nx.zeros(M.shape, type_as=M) + if log: + log_e["partial_w_dist"] = nx.sum(plan) + return plan, log_e + return plan + + La = nx.log(a) + Lb = nx.log(b) LK = -M / reg LK = LK + nx.log(m) - nx.logsumexp(LK) diff --git a/test/test_partial.py b/test/test_partial.py index a20e18c68..3c56bdcdc 100755 --- a/test/test_partial.py +++ b/test/test_partial.py @@ -436,6 +436,31 @@ def test_entropic_partial_wasserstein_logscale_log_dict(): assert np.isfinite(G).all() +@pytest.mark.parametrize( + "solve", + [ + ot.partial.entropic_partial_wasserstein, + ot.partial.entropic_partial_wasserstein_logscale, + ], +) +@pytest.mark.parametrize("m", [0.0, None]) +@pytest.mark.filterwarnings("error") +def test_entropic_partial_wasserstein_zero_mass(solve, m): + a = np.array([0.25, 0.75], dtype=np.float32) + b = np.array([0.5, 0.25, 0.25], dtype=np.float32) + if m is None: + b[:] = 0 + M = np.array([[0, 1, 4], [1, 0, 1]], dtype=np.float32) + + plan, log = solve(a, b, M, reg=1.0, m=m, log=True) + + assert plan.shape == M.shape + assert plan.dtype == M.dtype + np.testing.assert_array_equal(plan, np.zeros_like(M)) + np.testing.assert_array_equal(solve(a, b, M, reg=1.0, m=m), plan) + assert log == {"err": [], "partial_w_dist": 0} + + def test_entropic_partial_wasserstein_logscale_input_validation(): """Out-of-range `m` should raise ValueError, matching the unstable solver.""" n = 10