| 1 |
"""Test for low rank sinkhorn solvers""" |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import ot |
| 8 |
import numpy as np |
| 9 |
import pytest |
| 10 |
from ot.lowrank import sklearn_import |
| 11 |
|
| 12 |
|
| 13 |
def test_compute_lr_sqeuclidean_matrix(): |
| 14 |
|
| 15 |
n = 100 |
| 16 |
X_s = np.reshape(1.0 * np.arange(2 * n), (n, 2)) |
| 17 |
X_t = np.reshape(1.0 * np.arange(2 * n), (n, 2)) |
| 18 |
|
| 19 |
M1, M2 = ot.lowrank.compute_lr_sqeuclidean_matrix(X_s, X_t, rescale_cost=False) |
| 20 |
M = ot.dist(X_s, X_t, metric="sqeuclidean") |
| 21 |
|
| 22 |
np.testing.assert_allclose(np.dot(M1, M2.T), M, atol=1e-05) |
| 23 |
|
| 24 |
|
| 25 |
def test_lowrank_sinkhorn(): |
| 26 |
|
| 27 |
n = 100 |
| 28 |
a = ot.unif(n) |
| 29 |
b = ot.unif(n) |
| 30 |
|
| 31 |
X_s = np.reshape(1.0 * np.arange(n), (n, 1)) |
| 32 |
X_t = np.reshape(1.0 * np.arange(n), (n, 1)) |
| 33 |
|
| 34 |
Q, R, g, log = ot.lowrank.lowrank_sinkhorn( |
| 35 |
X_s, X_t, a, b, reg=0.1, log=True, rescale_cost=False |
| 36 |
) |
| 37 |
P = log["lazy_plan"][:] |
| 38 |
value_linear = log["value_linear"] |
| 39 |
|
| 40 |
|
| 41 |
np.testing.assert_allclose(a, P.sum(1), atol=1e-05) |
| 42 |
np.testing.assert_allclose(b, P.sum(0), atol=1e-05) |
| 43 |
|
| 44 |
|
| 45 |
P_true = np.dot(Q, np.dot(np.diag(1 / g), R.T)) |
| 46 |
np.testing.assert_allclose(P, P_true, atol=1e-05) |
| 47 |
|
| 48 |
|
| 49 |
M = ot.dist(X_s, X_t, metric="sqeuclidean") |
| 50 |
value_linear_true = np.sum(M * P_true) |
| 51 |
np.testing.assert_allclose(value_linear, value_linear_true, atol=1e-05) |
| 52 |
|
| 53 |
|
| 54 |
with pytest.warns(UserWarning): |
| 55 |
ot.lowrank.lowrank_sinkhorn(X_s, X_t, a, b, reg=0.1, stopThr=0, numItermax=1) |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.parametrize(("init"), ("random", "deterministic", "kmeans")) |
| 59 |
def test_lowrank_sinkhorn_init(init): |
| 60 |
|
| 61 |
n = 100 |
| 62 |
a = ot.unif(n) |
| 63 |
b = ot.unif(n) |
| 64 |
|
| 65 |
X_s = np.reshape(1.0 * np.arange(n), (n, 1)) |
| 66 |
X_t = np.reshape(1.0 * np.arange(n), (n, 1)) |
| 67 |
|
| 68 |
|
| 69 |
if init in ["random", "deterministic"] or ( |
| 70 |
(init == "kmeans") and (sklearn_import is True) |
| 71 |
): |
| 72 |
Q, R, g, log = ot.lowrank.lowrank_sinkhorn( |
| 73 |
X_s, X_t, a, b, reg=0.1, init=init, log=True |
| 74 |
) |
| 75 |
P = log["lazy_plan"][:] |
| 76 |
|
| 77 |
|
| 78 |
np.testing.assert_allclose(a, P.sum(1), atol=1e-05) |
| 79 |
np.testing.assert_allclose(b, P.sum(0), atol=1e-05) |
| 80 |
|
| 81 |
else: |
| 82 |
with pytest.raises(ImportError): |
| 83 |
Q, R, g = ot.lowrank.lowrank_sinkhorn(X_s, X_t, a, b, reg=0.1, init=init) |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.parametrize(("alpha, rank"), ((0.8, 2), (0.5, 3), (0.2, 6))) |
| 87 |
def test_lowrank_sinkhorn_alpha_error(alpha, rank): |
| 88 |
|
| 89 |
n = 100 |
| 90 |
a = ot.unif(n) |
| 91 |
b = ot.unif(n) |
| 92 |
|
| 93 |
X_s = np.reshape(1.0 * np.arange(n), (n, 1)) |
| 94 |
X_t = np.reshape(1.0 * np.arange(0, n), (n, 1)) |
| 95 |
|
| 96 |
with pytest.raises(ValueError): |
| 97 |
ot.lowrank.lowrank_sinkhorn( |
| 98 |
X_s, X_t, a, b, reg=0.1, rank=rank, alpha=alpha, warn=False |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.parametrize(("gamma_init"), ("rescale", "theory")) |
| 103 |
def test_lowrank_sinkhorn_gamma_init(gamma_init): |
| 104 |
|
| 105 |
n = 100 |
| 106 |
a = ot.unif(n) |
| 107 |
b = ot.unif(n) |
| 108 |
|
| 109 |
X_s = np.reshape(1.0 * np.arange(n), (n, 1)) |
| 110 |
X_t = np.reshape(1.0 * np.arange(n), (n, 1)) |
| 111 |
|
| 112 |
Q, R, g, log = ot.lowrank.lowrank_sinkhorn( |
| 113 |
X_s, X_t, a, b, reg=0.1, gamma_init=gamma_init, log=True |
| 114 |
) |
| 115 |
P = log["lazy_plan"][:] |
| 116 |
|
| 117 |
|
| 118 |
np.testing.assert_allclose(a, P.sum(1), atol=1e-05) |
| 119 |
np.testing.assert_allclose(b, P.sum(0), atol=1e-05) |
| 120 |
|
| 121 |
|
| 122 |
@pytest.skip_backend("tf") |
| 123 |
def test_lowrank_sinkhorn_backends(nx): |
| 124 |
|
| 125 |
n = 100 |
| 126 |
a = ot.unif(n) |
| 127 |
b = ot.unif(n) |
| 128 |
|
| 129 |
X_s = np.reshape(1.0 * np.arange(n), (n, 1)) |
| 130 |
X_t = np.reshape(1.0 * np.arange(0, n), (n, 1)) |
| 131 |
|
| 132 |
ab, bb, X_sb, X_tb = nx.from_numpy(a, b, X_s, X_t) |
| 133 |
|
| 134 |
Q, R, g, log = ot.lowrank.lowrank_sinkhorn(X_sb, X_tb, ab, bb, reg=0.1, log=True) |
| 135 |
lazy_plan = log["lazy_plan"] |
| 136 |
P = lazy_plan[:] |
| 137 |
|
| 138 |
np.testing.assert_allclose(ab, P.sum(1), atol=1e-05) |
| 139 |
np.testing.assert_allclose(bb, P.sum(0), atol=1e-05) |