| 1 |
"""Tests for main module ot.weak""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import ot |
| 8 |
import numpy as np |
| 9 |
|
| 10 |
|
| 11 |
def test_factored_ot(): |
| 12 |
|
| 13 |
n = 50 |
| 14 |
rng = np.random.RandomState(0) |
| 15 |
|
| 16 |
xs = rng.randn(n, 2) |
| 17 |
xt = rng.randn(n, 2) |
| 18 |
u = ot.utils.unif(n) |
| 19 |
|
| 20 |
Ga, Gb, X, log = ot.factored_optimal_transport(xs, xt, u, u, r=10, log=True) |
| 21 |
|
| 22 |
|
| 23 |
np.testing.assert_allclose(u, Ga.sum(1)) |
| 24 |
np.testing.assert_allclose(u, Gb.sum(0)) |
| 25 |
|
| 26 |
Ga, Gb, X, log = ot.factored_optimal_transport(xs, xt, u, u, reg=1, r=10, log=True) |
| 27 |
|
| 28 |
|
| 29 |
np.testing.assert_allclose(u, Ga.sum(1)) |
| 30 |
np.testing.assert_allclose(u, Gb.sum(0)) |
| 31 |
np.testing.assert_allclose(1, log["lazy_plan"][:].sum()) |
| 32 |
|
| 33 |
|
| 34 |
def test_factored_ot_backends(nx): |
| 35 |
|
| 36 |
n = 50 |
| 37 |
rng = np.random.RandomState(0) |
| 38 |
|
| 39 |
xs = rng.randn(n, 2) |
| 40 |
xt = rng.randn(n, 2) |
| 41 |
u = ot.utils.unif(n) |
| 42 |
|
| 43 |
xs2 = nx.from_numpy(xs) |
| 44 |
xt2 = nx.from_numpy(xt) |
| 45 |
u2 = nx.from_numpy(u) |
| 46 |
|
| 47 |
Ga2, Gb2, X2 = ot.factored_optimal_transport(xs2, xt2, u2, u2, r=10) |
| 48 |
|
| 49 |
|
| 50 |
np.testing.assert_allclose(u, nx.to_numpy(Ga2).sum(1)) |
| 51 |
np.testing.assert_allclose(u, nx.to_numpy(Gb2).sum(0)) |
| 52 |
|
| 53 |
Ga2, Gb2, X2 = ot.factored_optimal_transport(xs2, xt2, reg=1, r=10, X0=X2) |
| 54 |
|
| 55 |
|
| 56 |
np.testing.assert_allclose(u, nx.to_numpy(Ga2).sum(1)) |
| 57 |
np.testing.assert_allclose(u, nx.to_numpy(Gb2).sum(0)) |