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

- Fix `ot.gaussian.bures_wasserstein_distance(..., paired=True)` returning incorrectly broadcast distances or raising an error for unequal batch sizes, instead of returning all crossed distances as documented. (PR #909)
- 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
2 changes: 1 addition & 1 deletion ot/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def bures_wasserstein_distance(ms, mt, Cs, Ct, paired=False, log=False):
assert (
len(ms.shape) == 2 and len(mt.shape) == 2
), "Both ms and mt should be batched"
if paired and len(ms.shape) == len(mt.shape):
if paired and len(ms) == len(mt):
# Return shape (n,)
squared_dist_m = nx.norm(ms - mt, axis=-1) ** 2
else:
Expand Down
39 changes: 39 additions & 0 deletions test/test_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,42 @@ def test_bures_barycenter_fixpoint_no_convergence_warning():
C = A @ A.transpose(0, 2, 1) + np.eye(4)[None]
with pytest.warns(UserWarning, match="did not converge"):
ot.gaussian.bures_barycenter_fixpoint(C, num_iter=1)


@pytest.mark.parametrize("batch_sizes", [(2, 1), (1, 3), (2, 3), (3, 2), (2, 2)])
@pytest.mark.parametrize("paired", [False, True])
@pytest.mark.parametrize("log", [False, True])
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_bures_wasserstein_distance_unequal_batches(
nx, batch_sizes, paired, log, dtype
):
ns, nt = batch_sizes
ms = np.array([[0, 0], [3, 4], [-2, 1]], dtype=dtype)[:ns]
mt = np.array([[1, -1], [2, 5], [-3, 2]], dtype=dtype)[:nt]
stds = np.array([[1, 2], [4, 5], [2, 3]], dtype=dtype)[:ns]
stdt = np.array([[2, 3], [1, 4], [3, 1]], dtype=dtype)[:nt]
Cs = np.array([np.diag(std**2) for std in stds])
Ct = np.array([np.diag(std**2) for std in stdt])

# For diagonal covariances, W2 is the Euclidean distance between the
# concatenated means and standard deviations.
reference = np.sqrt(
np.sum((ms[:, None] - mt[None]) ** 2, axis=-1)
+ np.sum((stds[:, None] - stdt[None]) ** 2, axis=-1)
)
if paired and ns == nt:
reference = reference.diagonal()

inputs = nx.from_numpy(ms, mt, Cs, Ct)
snapshots = [nx.to_numpy(value).copy() for value in inputs]
result = ot.gaussian.bures_wasserstein_distance(*inputs, paired=paired, log=log)
if log:
result, info = result
assert info["Cs12"].shape == Cs.shape

assert result.shape == reference.shape
np.testing.assert_allclose(nx.to_numpy(result), reference, rtol=1e-6, atol=1e-6)
nx.assert_same_dtype_device(inputs[0], result)
assert nx.to_numpy(result).dtype == dtype
for value, snapshot in zip(inputs, snapshots):
np.testing.assert_array_equal(nx.to_numpy(value), snapshot)
Loading