Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 20 additions & 7 deletions ot/partial/partial_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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)

Expand Down
25 changes: 25 additions & 0 deletions test/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading