Debian Codegraph @

test/gromov/test_estimators.py

raw symbols SCIP
1 """Tests for gromov._estimators.py"""
2
3 # Author: Rémi Flamary <[email protected]>
4 # Tanguy Kerdoncuff <[email protected]>
5 # Cédric Vincent-Cuaz <[email protected]>
6 #
7 # License: MIT License
8
9 import numpy as np
10 import pytest
11
12 import ot
13 from ot.backend import NumpyBackend
14
15
16 def test_pointwise_gromov(nx):
17 n_samples = 5 # nb samples
18
19 mu_s = np.array([0, 0])
20 cov_s = np.array([[1, 0], [0, 1]])
21
22 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=42)
23
24 xt = xs[::-1].copy()
25
26 p = ot.unif(n_samples)
27 q = ot.unif(n_samples)
28
29 C1 = ot.dist(xs, xs)
30 C2 = ot.dist(xt, xt)
31
32 C1 /= C1.max()
33 C2 /= C2.max()
34
35 C1b, C2b, pb, qb = nx.from_numpy(C1, C2, p, q)
36
37 def loss(x, y):
38 return np.abs(x - y)
39
40 def lossb(x, y):
41 return nx.abs(x - y)
42
43 G, log = ot.gromov.pointwise_gromov_wasserstein(
44 C1, C2, p, q, loss, max_iter=100, log=True, verbose=True, random_state=42
45 )
46 G = NumpyBackend().todense(G)
47 Gb, logb = ot.gromov.pointwise_gromov_wasserstein(
48 C1b, C2b, pb, qb, lossb, max_iter=100, log=True, verbose=True, random_state=42
49 )
50 Gb = nx.to_numpy(nx.todense(Gb))
51
52 # check constraints
53 np.testing.assert_allclose(G, Gb, atol=1e-06)
54 np.testing.assert_allclose(p, Gb.sum(1), atol=1e-04) # cf convergence gromov
55 np.testing.assert_allclose(q, Gb.sum(0), atol=1e-04) # cf convergence gromov
56
57 np.testing.assert_allclose(float(logb["gw_dist_estimated"]), 0.0, atol=1e-08)
58 np.testing.assert_allclose(float(logb["gw_dist_std"]), 0.0, atol=1e-08)
59
60 G, log = ot.gromov.pointwise_gromov_wasserstein(
61 C1,
62 C2,
63 p,
64 q,
65 loss,
66 max_iter=100,
67 alpha=0.1,
68 log=True,
69 verbose=True,
70 random_state=42,
71 )
72 G = NumpyBackend().todense(G)
73 Gb, logb = ot.gromov.pointwise_gromov_wasserstein(
74 C1b,
75 C2b,
76 pb,
77 qb,
78 lossb,
79 max_iter=100,
80 alpha=0.1,
81 log=True,
82 verbose=True,
83 random_state=42,
84 )
85 Gb = nx.to_numpy(nx.todense(Gb))
86
87 np.testing.assert_allclose(G, Gb, atol=1e-06)
88
89
90 @pytest.skip_backend("tf", reason="test very slow with tf backend")
91 @pytest.skip_backend("jax", reason="test very slow with jax backend")
92 def test_sampled_gromov(nx):
93 n_samples = 5 # nb samples
94
95 mu_s = np.array([0, 0], dtype=np.float64)
96 cov_s = np.array([[1, 0], [0, 1]], dtype=np.float64)
97
98 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=42)
99
100 xt = xs[::-1].copy()
101
102 p = ot.unif(n_samples)
103 q = ot.unif(n_samples)
104
105 C1 = ot.dist(xs, xs)
106 C2 = ot.dist(xt, xt)
107
108 C1 /= C1.max()
109 C2 /= C2.max()
110
111 C1b, C2b, pb, qb = nx.from_numpy(C1, C2, p, q)
112
113 def loss(x, y):
114 return np.abs(x - y)
115
116 def lossb(x, y):
117 return nx.abs(x - y)
118
119 G, log = ot.gromov.sampled_gromov_wasserstein(
120 C1,
121 C2,
122 p,
123 q,
124 loss,
125 max_iter=20,
126 nb_samples_grad=2,
127 epsilon=1,
128 log=True,
129 verbose=True,
130 random_state=42,
131 )
132 Gb, logb = ot.gromov.sampled_gromov_wasserstein(
133 C1b,
134 C2b,
135 pb,
136 qb,
137 lossb,
138 max_iter=20,
139 nb_samples_grad=2,
140 epsilon=1,
141 log=True,
142 verbose=True,
143 random_state=42,
144 )
145 Gb = nx.to_numpy(Gb)
146
147 # check constraints
148 np.testing.assert_allclose(G, Gb, atol=1e-06)
149 np.testing.assert_allclose(p, Gb.sum(1), atol=1e-04) # cf convergence gromov
150 np.testing.assert_allclose(q, Gb.sum(0), atol=1e-04) # cf convergence gromov