| 1 |
"""Tests for gromov._semirelaxed.py""" |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import numpy as np |
| 8 |
import pytest |
| 9 |
|
| 10 |
import ot |
| 11 |
from ot.backend import torch |
| 12 |
|
| 13 |
from ot.gromov._utils import networkx_import, sklearn_import |
| 14 |
|
| 15 |
|
| 16 |
def test_semirelaxed_gromov(nx): |
| 17 |
rng = np.random.RandomState(0) |
| 18 |
|
| 19 |
list_n = [30, 15] |
| 20 |
nt = 2 |
| 21 |
ns = np.sum(list_n) |
| 22 |
|
| 23 |
C1 = np.zeros((ns, ns), dtype=np.float64) |
| 24 |
C2 = np.array([[0.8, 0.1], [0.1, 1.0]], dtype=np.float64) |
| 25 |
|
| 26 |
pos = [0, 30, 45] |
| 27 |
|
| 28 |
for i in range(nt): |
| 29 |
for j in range(nt): |
| 30 |
ni, nj = list_n[i], list_n[j] |
| 31 |
xij = rng.binomial(size=(ni, nj), n=1, p=C2[i, j]) |
| 32 |
pos_i_min, pos_i_max = pos[i], pos[i + 1] |
| 33 |
pos_j_min, pos_j_max = pos[j], pos[j + 1] |
| 34 |
C1[pos_i_min:pos_i_max, pos_j_min:pos_j_max] = xij |
| 35 |
|
| 36 |
p = ot.unif(ns, type_as=C1) |
| 37 |
q0 = ot.unif(C2.shape[0], type_as=C1) |
| 38 |
G0 = p[:, None] * q0[None, :] |
| 39 |
|
| 40 |
C1b, C2b, pb, q0b, G0b = nx.from_numpy(C1, C2, p, q0, G0) |
| 41 |
|
| 42 |
for loss_fun in ["square_loss", "kl_loss"]: |
| 43 |
G, log = ot.gromov.semirelaxed_gromov_wasserstein( |
| 44 |
C1, C2, p, loss_fun="square_loss", symmetric=None, log=True, G0=G0 |
| 45 |
) |
| 46 |
Gb, logb = ot.gromov.semirelaxed_gromov_wasserstein( |
| 47 |
C1b, |
| 48 |
C2b, |
| 49 |
None, |
| 50 |
loss_fun="square_loss", |
| 51 |
symmetric=False, |
| 52 |
log=True, |
| 53 |
G0=None, |
| 54 |
alpha_min=0.0, |
| 55 |
alpha_max=1.0, |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 60 |
np.testing.assert_allclose(p, nx.sum(Gb, axis=1), atol=1e-04) |
| 61 |
np.testing.assert_allclose(list_n / ns, np.sum(G, axis=0), atol=1e-01) |
| 62 |
np.testing.assert_allclose(list_n / ns, nx.sum(Gb, axis=0), atol=1e-01) |
| 63 |
|
| 64 |
srgw, log2 = ot.gromov.semirelaxed_gromov_wasserstein2( |
| 65 |
C1, C2, None, loss_fun="square_loss", symmetric=False, log=True, G0=G0 |
| 66 |
) |
| 67 |
srgwb, logb2 = ot.gromov.semirelaxed_gromov_wasserstein2( |
| 68 |
C1b, C2b, pb, loss_fun="square_loss", symmetric=None, log=True, G0=None |
| 69 |
) |
| 70 |
|
| 71 |
G = log2["T"] |
| 72 |
Gb = nx.to_numpy(logb2["T"]) |
| 73 |
|
| 74 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 75 |
np.testing.assert_allclose(p, Gb.sum(1), atol=1e-04) |
| 76 |
np.testing.assert_allclose( |
| 77 |
list_n / ns, Gb.sum(0), atol=1e-04 |
| 78 |
) |
| 79 |
|
| 80 |
np.testing.assert_allclose(log2["srgw_dist"], logb["srgw_dist"], atol=1e-07) |
| 81 |
np.testing.assert_allclose(logb2["srgw_dist"], log["srgw_dist"], atol=1e-07) |
| 82 |
|
| 83 |
|
| 84 |
C1 = 0.5 * (C1 + C1.T) |
| 85 |
|
| 86 |
C1b, C2b, pb, q0b, G0b = nx.from_numpy(C1, C2, p, q0, G0) |
| 87 |
|
| 88 |
init_plan_list = [ |
| 89 |
(None, G0b), |
| 90 |
("product", None), |
| 91 |
("random_product", "random_product"), |
| 92 |
] |
| 93 |
|
| 94 |
if networkx_import: |
| 95 |
init_plan_list += [("fluid", "fluid"), ("fluid_soft", "fluid_soft")] |
| 96 |
|
| 97 |
if sklearn_import: |
| 98 |
init_plan_list += [ |
| 99 |
("spectral", "spectral"), |
| 100 |
("spectral_soft", "spectral_soft"), |
| 101 |
("kmeans", "kmeans"), |
| 102 |
("kmeans_soft", "kmeans_soft"), |
| 103 |
] |
| 104 |
|
| 105 |
for init, init_b in init_plan_list: |
| 106 |
G, log = ot.gromov.semirelaxed_gromov_wasserstein( |
| 107 |
C1, C2, p, loss_fun="square_loss", symmetric=None, log=True, G0=init |
| 108 |
) |
| 109 |
Gb = ot.gromov.semirelaxed_gromov_wasserstein( |
| 110 |
C1b, C2b, pb, loss_fun="square_loss", symmetric=True, log=False, G0=init_b |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 115 |
np.testing.assert_allclose( |
| 116 |
p, nx.sum(Gb, axis=1), atol=1e-04 |
| 117 |
) |
| 118 |
|
| 119 |
if not isinstance(init, str): |
| 120 |
np.testing.assert_allclose( |
| 121 |
list_n / ns, nx.sum(Gb, axis=0), atol=1e-02 |
| 122 |
) |
| 123 |
else: |
| 124 |
if ( |
| 125 |
"spectral" not in init |
| 126 |
): |
| 127 |
np.testing.assert_allclose(list_n / ns, nx.sum(Gb, axis=0), atol=1e-02) |
| 128 |
|
| 129 |
srgw, log2 = ot.gromov.semirelaxed_gromov_wasserstein2( |
| 130 |
C1, C2, p, loss_fun="square_loss", symmetric=True, log=True, G0=G0 |
| 131 |
) |
| 132 |
srgwb, logb2 = ot.gromov.semirelaxed_gromov_wasserstein2( |
| 133 |
C1b, C2b, pb, loss_fun="square_loss", symmetric=None, log=True, G0=None |
| 134 |
) |
| 135 |
|
| 136 |
srgw_ = ot.gromov.semirelaxed_gromov_wasserstein2( |
| 137 |
C1, C2, p, loss_fun="square_loss", symmetric=True, log=False, G0=G0 |
| 138 |
) |
| 139 |
|
| 140 |
G = log2["T"] |
| 141 |
|
| 142 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 143 |
np.testing.assert_allclose(p, nx.sum(Gb, 1), atol=1e-04) |
| 144 |
np.testing.assert_allclose(list_n / ns, np.sum(G, axis=0), atol=1e-01) |
| 145 |
np.testing.assert_allclose(list_n / ns, nx.sum(Gb, axis=0), atol=1e-01) |
| 146 |
|
| 147 |
np.testing.assert_allclose(log2["srgw_dist"], log["srgw_dist"], atol=1e-07) |
| 148 |
np.testing.assert_allclose(logb2["srgw_dist"], log["srgw_dist"], atol=1e-07) |
| 149 |
np.testing.assert_allclose(srgw, srgw_, atol=1e-07) |
| 150 |
|
| 151 |
|
| 152 |
def test_semirelaxed_gromov2_gradients(): |
| 153 |
n_samples = 50 |
| 154 |
|
| 155 |
mu_s = np.array([0, 0]) |
| 156 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 157 |
|
| 158 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 159 |
|
| 160 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=5) |
| 161 |
|
| 162 |
p = ot.unif(n_samples) |
| 163 |
|
| 164 |
C1 = ot.dist(xs, xs) |
| 165 |
C2 = ot.dist(xt, xt) |
| 166 |
|
| 167 |
C1 /= C1.max() |
| 168 |
C2 /= C2.max() |
| 169 |
|
| 170 |
if torch: |
| 171 |
devices = [torch.device("cpu")] |
| 172 |
if torch.cuda.is_available(): |
| 173 |
devices.append(torch.device("cuda")) |
| 174 |
for device in devices: |
| 175 |
for loss_fun in ["square_loss", "kl_loss"]: |
| 176 |
|
| 177 |
p1 = torch.tensor(p, requires_grad=False, device=device) |
| 178 |
C11 = torch.tensor(C1, requires_grad=True, device=device) |
| 179 |
C12 = torch.tensor(C2, requires_grad=True, device=device) |
| 180 |
|
| 181 |
val = ot.gromov.semirelaxed_gromov_wasserstein2( |
| 182 |
C11, C12, p1, loss_fun=loss_fun |
| 183 |
) |
| 184 |
|
| 185 |
val.backward() |
| 186 |
|
| 187 |
assert val.device == p1.device |
| 188 |
assert p1.grad is None |
| 189 |
assert C11.shape == C11.grad.shape |
| 190 |
assert C12.shape == C12.grad.shape |
| 191 |
|
| 192 |
|
| 193 |
def test_srgw_helper_backend(nx): |
| 194 |
n_samples = 20 |
| 195 |
|
| 196 |
mu = np.array([0, 0]) |
| 197 |
cov = np.array([[1, 0], [0, 1]]) |
| 198 |
|
| 199 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=0) |
| 200 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=1) |
| 201 |
|
| 202 |
p = ot.unif(n_samples) |
| 203 |
q = ot.unif(n_samples) |
| 204 |
C1 = ot.dist(xs, xs) |
| 205 |
C2 = ot.dist(xt, xt) |
| 206 |
|
| 207 |
C1 /= C1.max() |
| 208 |
C2 /= C2.max() |
| 209 |
|
| 210 |
for loss_fun in ["square_loss", "kl_loss"]: |
| 211 |
C1b, C2b, pb, qb = nx.from_numpy(C1, C2, p, q) |
| 212 |
Gb, logb = ot.gromov.semirelaxed_gromov_wasserstein( |
| 213 |
C1b, C2b, pb, loss_fun, armijo=False, symmetric=True, G0=None, log=True |
| 214 |
) |
| 215 |
|
| 216 |
|
| 217 |
constCb, hC1b, hC2b, fC2tb = ot.gromov.init_matrix_semirelaxed( |
| 218 |
C1b, C2b, pb, loss_fun |
| 219 |
) |
| 220 |
ones_pb = nx.ones(pb.shape[0], type_as=pb) |
| 221 |
|
| 222 |
def f(G): |
| 223 |
qG = nx.sum(G, 0) |
| 224 |
marginal_product = nx.outer(ones_pb, nx.dot(qG, fC2tb)) |
| 225 |
return ot.gromov.gwloss(constCb + marginal_product, hC1b, hC2b, G, nx=None) |
| 226 |
|
| 227 |
def df(G): |
| 228 |
qG = nx.sum(G, 0) |
| 229 |
marginal_product = nx.outer(ones_pb, nx.dot(qG, fC2tb)) |
| 230 |
return ot.gromov.gwggrad(constCb + marginal_product, hC1b, hC2b, G, nx=None) |
| 231 |
|
| 232 |
def line_search(cost, G, deltaG, Mi, cost_G, df_G): |
| 233 |
return ot.gromov.solve_semirelaxed_gromov_linesearch( |
| 234 |
G, deltaG, cost_G, hC1b, hC2b, ones_pb, 0.0, 1.0, fC2t=fC2tb, nx=None |
| 235 |
) |
| 236 |
|
| 237 |
|
| 238 |
res, log = ot.optim.semirelaxed_cg( |
| 239 |
pb, |
| 240 |
qb, |
| 241 |
0.0, |
| 242 |
1.0, |
| 243 |
f, |
| 244 |
df, |
| 245 |
Gb, |
| 246 |
line_search, |
| 247 |
log=True, |
| 248 |
numItermax=1e4, |
| 249 |
stopThr=1e-9, |
| 250 |
stopThr2=1e-9, |
| 251 |
) |
| 252 |
|
| 253 |
np.testing.assert_allclose(res, Gb, atol=1e-06) |
| 254 |
|
| 255 |
|
| 256 |
@pytest.mark.parametrize( |
| 257 |
"loss_fun", |
| 258 |
[ |
| 259 |
"square_loss", |
| 260 |
"kl_loss", |
| 261 |
pytest.param("unknown_loss", marks=pytest.mark.xfail(raises=ValueError)), |
| 262 |
], |
| 263 |
) |
| 264 |
def test_gw_semirelaxed_helper_validation(loss_fun): |
| 265 |
n_samples = 20 |
| 266 |
mu = np.array([0, 0]) |
| 267 |
cov = np.array([[1, 0], [0, 1]]) |
| 268 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=0) |
| 269 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=1) |
| 270 |
p = ot.unif(n_samples) |
| 271 |
C1 = ot.dist(xs, xs) |
| 272 |
C2 = ot.dist(xt, xt) |
| 273 |
ot.gromov.init_matrix_semirelaxed(C1, C2, p, loss_fun=loss_fun) |
| 274 |
|
| 275 |
|
| 276 |
def test_semirelaxed_fgw(nx): |
| 277 |
rng = np.random.RandomState(0) |
| 278 |
list_n = [16, 8] |
| 279 |
nt = 2 |
| 280 |
ns = 24 |
| 281 |
|
| 282 |
C1 = np.zeros((ns, ns)) |
| 283 |
C2 = np.array([[0.7, 0.05], [0.05, 0.9]]) |
| 284 |
|
| 285 |
pos = [0, 16, 24] |
| 286 |
|
| 287 |
for i in range(nt): |
| 288 |
for j in range(nt): |
| 289 |
ni, nj = list_n[i], list_n[j] |
| 290 |
xij = rng.binomial(size=(ni, nj), n=1, p=C2[i, j]) |
| 291 |
pos_i_min, pos_i_max = pos[i], pos[i + 1] |
| 292 |
pos_j_min, pos_j_max = pos[j], pos[j + 1] |
| 293 |
C1[pos_i_min:pos_i_max, pos_j_min:pos_j_max] = xij |
| 294 |
|
| 295 |
F1 = np.zeros((ns, 1)) |
| 296 |
F1[:16] = rng.normal(loc=0.0, scale=0.01, size=(16, 1)) |
| 297 |
F1[16:] = rng.normal(loc=1.0, scale=0.01, size=(8, 1)) |
| 298 |
F2 = np.zeros((2, 1)) |
| 299 |
F2[1, :] = 1.0 |
| 300 |
M = ( |
| 301 |
(F1**2).dot(np.ones((1, nt))) |
| 302 |
+ np.ones((ns, 1)).dot((F2**2).T) |
| 303 |
- 2 * F1.dot(F2.T) |
| 304 |
) |
| 305 |
|
| 306 |
p = ot.unif(ns) |
| 307 |
q0 = ot.unif(C2.shape[0]) |
| 308 |
G0 = p[:, None] * q0[None, :] |
| 309 |
|
| 310 |
|
| 311 |
Mb, C1b, C2b, pb, q0b, G0b = nx.from_numpy(M, C1, C2, p, q0, G0) |
| 312 |
G, log = ot.gromov.semirelaxed_fused_gromov_wasserstein( |
| 313 |
M, |
| 314 |
C1, |
| 315 |
C2, |
| 316 |
None, |
| 317 |
loss_fun="square_loss", |
| 318 |
alpha=0.5, |
| 319 |
symmetric=None, |
| 320 |
log=True, |
| 321 |
G0=None, |
| 322 |
) |
| 323 |
Gb, logb = ot.gromov.semirelaxed_fused_gromov_wasserstein( |
| 324 |
Mb, |
| 325 |
C1b, |
| 326 |
C2b, |
| 327 |
pb, |
| 328 |
loss_fun="square_loss", |
| 329 |
alpha=0.5, |
| 330 |
symmetric=False, |
| 331 |
log=True, |
| 332 |
G0=G0b, |
| 333 |
) |
| 334 |
|
| 335 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 336 |
np.testing.assert_allclose( |
| 337 |
p, nx.sum(Gb, axis=1), atol=1e-04 |
| 338 |
) |
| 339 |
np.testing.assert_allclose( |
| 340 |
[2 / 3, 1 / 3], nx.sum(Gb, axis=0), atol=1e-02 |
| 341 |
) |
| 342 |
|
| 343 |
|
| 344 |
srgw, log2 = ot.gromov.semirelaxed_fused_gromov_wasserstein2( |
| 345 |
M, |
| 346 |
C1, |
| 347 |
C2, |
| 348 |
p, |
| 349 |
loss_fun="square_loss", |
| 350 |
alpha=0.5, |
| 351 |
symmetric=False, |
| 352 |
log=True, |
| 353 |
G0=G0, |
| 354 |
) |
| 355 |
srgwb, logb2 = ot.gromov.semirelaxed_fused_gromov_wasserstein2( |
| 356 |
Mb, |
| 357 |
C1b, |
| 358 |
C2b, |
| 359 |
None, |
| 360 |
loss_fun="square_loss", |
| 361 |
alpha=0.5, |
| 362 |
symmetric=None, |
| 363 |
log=True, |
| 364 |
G0=None, |
| 365 |
) |
| 366 |
|
| 367 |
G = log2["T"] |
| 368 |
Gb = nx.to_numpy(logb2["T"]) |
| 369 |
|
| 370 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 371 |
np.testing.assert_allclose(p, Gb.sum(1), atol=1e-04) |
| 372 |
np.testing.assert_allclose( |
| 373 |
[2 / 3, 1 / 3], G.sum(0), atol=1e-04 |
| 374 |
) |
| 375 |
|
| 376 |
np.testing.assert_allclose(log2["srfgw_dist"], logb["srfgw_dist"], atol=1e-07) |
| 377 |
np.testing.assert_allclose(logb2["srfgw_dist"], log["srfgw_dist"], atol=1e-07) |
| 378 |
|
| 379 |
|
| 380 |
C1 = 0.5 * (C1 + C1.T) |
| 381 |
Mb, C1b, C2b, pb, q0b, G0b = nx.from_numpy(M, C1, C2, p, q0, G0) |
| 382 |
|
| 383 |
init_plan_list = [ |
| 384 |
(None, G0b), |
| 385 |
("product", None), |
| 386 |
("random_product", "random_product"), |
| 387 |
] |
| 388 |
|
| 389 |
if networkx_import: |
| 390 |
init_plan_list += [("fluid", "fluid")] |
| 391 |
|
| 392 |
if sklearn_import: |
| 393 |
init_plan_list += [("kmeans", "kmeans")] |
| 394 |
|
| 395 |
for loss_fun in ["square_loss", "kl_loss"]: |
| 396 |
for init, init_b in init_plan_list: |
| 397 |
G, log = ot.gromov.semirelaxed_fused_gromov_wasserstein( |
| 398 |
M, |
| 399 |
C1, |
| 400 |
C2, |
| 401 |
p, |
| 402 |
loss_fun=loss_fun, |
| 403 |
alpha=0.5, |
| 404 |
symmetric=None, |
| 405 |
log=True, |
| 406 |
G0=init, |
| 407 |
) |
| 408 |
Gb = ot.gromov.semirelaxed_fused_gromov_wasserstein( |
| 409 |
Mb, |
| 410 |
C1b, |
| 411 |
C2b, |
| 412 |
pb, |
| 413 |
loss_fun=loss_fun, |
| 414 |
alpha=0.5, |
| 415 |
symmetric=True, |
| 416 |
log=False, |
| 417 |
G0=init_b, |
| 418 |
) |
| 419 |
|
| 420 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 421 |
np.testing.assert_allclose( |
| 422 |
p, nx.sum(Gb, axis=1), atol=1e-04 |
| 423 |
) |
| 424 |
np.testing.assert_allclose( |
| 425 |
[2 / 3, 1 / 3], nx.sum(Gb, axis=0), atol=1e-02 |
| 426 |
) |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
srgw, log2 = ot.gromov.semirelaxed_fused_gromov_wasserstein2( |
| 431 |
M, C1, C2, p, loss_fun=loss_fun, alpha=0.5, symmetric=True, log=True, G0=G0 |
| 432 |
) |
| 433 |
srgwb, logb2 = ot.gromov.semirelaxed_fused_gromov_wasserstein2( |
| 434 |
Mb, |
| 435 |
C1b, |
| 436 |
C2b, |
| 437 |
pb, |
| 438 |
loss_fun=loss_fun, |
| 439 |
alpha=0.5, |
| 440 |
symmetric=None, |
| 441 |
log=True, |
| 442 |
G0=None, |
| 443 |
) |
| 444 |
|
| 445 |
G2 = log2["T"] |
| 446 |
Gb2 = nx.to_numpy(logb2["T"]) |
| 447 |
|
| 448 |
np.testing.assert_allclose(G2, Gb2, atol=1e-06) |
| 449 |
np.testing.assert_allclose(G2, G, atol=1e-06) |
| 450 |
|
| 451 |
np.testing.assert_allclose(log2["srfgw_dist"], log["srfgw_dist"], atol=1e-07) |
| 452 |
np.testing.assert_allclose(logb2["srfgw_dist"], log["srfgw_dist"], atol=1e-07) |
| 453 |
np.testing.assert_allclose(srgw, srgwb, atol=1e-07) |
| 454 |
|
| 455 |
|
| 456 |
def test_semirelaxed_fgw2_gradients(): |
| 457 |
n_samples = 20 |
| 458 |
|
| 459 |
mu_s = np.array([0, 0]) |
| 460 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 461 |
|
| 462 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 463 |
|
| 464 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=5) |
| 465 |
|
| 466 |
p = ot.unif(n_samples) |
| 467 |
|
| 468 |
C1 = ot.dist(xs, xs) |
| 469 |
C2 = ot.dist(xt, xt) |
| 470 |
M = ot.dist(xs, xt) |
| 471 |
|
| 472 |
C1 /= C1.max() |
| 473 |
C2 /= C2.max() |
| 474 |
|
| 475 |
if torch: |
| 476 |
devices = [torch.device("cpu")] |
| 477 |
if torch.cuda.is_available(): |
| 478 |
devices.append(torch.device("cuda")) |
| 479 |
for device in devices: |
| 480 |
|
| 481 |
for loss_fun in ["square_loss", "kl_loss"]: |
| 482 |
p1 = torch.tensor(p, requires_grad=False, device=device) |
| 483 |
C11 = torch.tensor(C1, requires_grad=True, device=device) |
| 484 |
C12 = torch.tensor(C2, requires_grad=True, device=device) |
| 485 |
M1 = torch.tensor(M, requires_grad=True, device=device) |
| 486 |
|
| 487 |
val = ot.gromov.semirelaxed_fused_gromov_wasserstein2( |
| 488 |
M1, C11, C12, p1, loss_fun=loss_fun |
| 489 |
) |
| 490 |
|
| 491 |
val.backward() |
| 492 |
|
| 493 |
assert val.device == p1.device |
| 494 |
assert p1.grad is None |
| 495 |
assert C11.shape == C11.grad.shape |
| 496 |
assert C12.shape == C12.grad.shape |
| 497 |
assert M1.shape == M1.grad.shape |
| 498 |
|
| 499 |
|
| 500 |
p1 = torch.tensor(p, requires_grad=False, device=device) |
| 501 |
C11 = torch.tensor(C1, requires_grad=True, device=device) |
| 502 |
C12 = torch.tensor(C2, requires_grad=True, device=device) |
| 503 |
M1 = torch.tensor(M, requires_grad=True, device=device) |
| 504 |
alpha = torch.tensor(0.5, requires_grad=True, device=device) |
| 505 |
|
| 506 |
val = ot.gromov.semirelaxed_fused_gromov_wasserstein2( |
| 507 |
M1, C11, C12, p1, loss_fun=loss_fun, alpha=alpha |
| 508 |
) |
| 509 |
|
| 510 |
val.backward() |
| 511 |
|
| 512 |
assert val.device == p1.device |
| 513 |
assert p1.grad is None |
| 514 |
assert C11.shape == C11.grad.shape |
| 515 |
assert C12.shape == C12.grad.shape |
| 516 |
assert alpha.shape == alpha.grad.shape |
| 517 |
|
| 518 |
|
| 519 |
def test_srfgw_helper_backend(nx): |
| 520 |
n_samples = 20 |
| 521 |
|
| 522 |
mu = np.array([0, 0]) |
| 523 |
cov = np.array([[1, 0], [0, 1]]) |
| 524 |
|
| 525 |
rng = np.random.RandomState(42) |
| 526 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=0) |
| 527 |
ys = rng.randn(xs.shape[0], 2) |
| 528 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=1) |
| 529 |
yt = rng.randn(xt.shape[0], 2) |
| 530 |
|
| 531 |
p = ot.unif(n_samples) |
| 532 |
q = ot.unif(n_samples) |
| 533 |
G0 = p[:, None] * q[None, :] |
| 534 |
|
| 535 |
C1 = ot.dist(xs, xs) |
| 536 |
C2 = ot.dist(xt, xt) |
| 537 |
|
| 538 |
C1 /= C1.max() |
| 539 |
C2 /= C2.max() |
| 540 |
|
| 541 |
M = ot.dist(ys, yt) |
| 542 |
M /= M.max() |
| 543 |
|
| 544 |
Mb, C1b, C2b, pb, qb, G0b = nx.from_numpy(M, C1, C2, p, q, G0) |
| 545 |
alpha = 0.5 |
| 546 |
Gb, logb = ot.gromov.semirelaxed_fused_gromov_wasserstein( |
| 547 |
Mb, |
| 548 |
C1b, |
| 549 |
C2b, |
| 550 |
pb, |
| 551 |
"square_loss", |
| 552 |
alpha=0.5, |
| 553 |
armijo=False, |
| 554 |
symmetric=True, |
| 555 |
G0=G0b, |
| 556 |
log=True, |
| 557 |
) |
| 558 |
|
| 559 |
|
| 560 |
constCb, hC1b, hC2b, fC2tb = ot.gromov.init_matrix_semirelaxed( |
| 561 |
C1b, C2b, pb, loss_fun="square_loss" |
| 562 |
) |
| 563 |
ones_pb = nx.ones(pb.shape[0], type_as=pb) |
| 564 |
|
| 565 |
def f(G): |
| 566 |
qG = nx.sum(G, 0) |
| 567 |
marginal_product = nx.outer(ones_pb, nx.dot(qG, fC2tb)) |
| 568 |
return ot.gromov.gwloss(constCb + marginal_product, hC1b, hC2b, G, nx=None) |
| 569 |
|
| 570 |
def df(G): |
| 571 |
qG = nx.sum(G, 0) |
| 572 |
marginal_product = nx.outer(ones_pb, nx.dot(qG, fC2tb)) |
| 573 |
return ot.gromov.gwggrad(constCb + marginal_product, hC1b, hC2b, G, nx=None) |
| 574 |
|
| 575 |
def line_search(cost, G, deltaG, Mi, cost_G, df_G): |
| 576 |
return ot.gromov.solve_semirelaxed_gromov_linesearch( |
| 577 |
G, deltaG, cost_G, C1b, C2b, ones_pb, M=(1 - alpha) * Mb, reg=alpha, nx=None |
| 578 |
) |
| 579 |
|
| 580 |
|
| 581 |
res, log = ot.optim.semirelaxed_cg( |
| 582 |
pb, |
| 583 |
qb, |
| 584 |
(1 - alpha) * Mb, |
| 585 |
alpha, |
| 586 |
f, |
| 587 |
df, |
| 588 |
Gb, |
| 589 |
line_search, |
| 590 |
log=True, |
| 591 |
numItermax=1e4, |
| 592 |
stopThr=1e-9, |
| 593 |
stopThr2=1e-9, |
| 594 |
) |
| 595 |
|
| 596 |
np.testing.assert_allclose(res, Gb, atol=1e-06) |
| 597 |
|
| 598 |
|
| 599 |
def test_entropic_semirelaxed_gromov(nx): |
| 600 |
|
| 601 |
list_n = [30, 15] |
| 602 |
nt = 2 |
| 603 |
ns = np.sum(list_n) |
| 604 |
|
| 605 |
C1 = np.zeros((ns, ns), dtype=np.float64) |
| 606 |
C2 = np.array([[0.8, 0.1], [0.1, 0.9]], dtype=np.float64) |
| 607 |
|
| 608 |
rng = np.random.RandomState(0) |
| 609 |
|
| 610 |
pos = [0, 30, 45] |
| 611 |
|
| 612 |
for i in range(nt): |
| 613 |
for j in range(nt): |
| 614 |
ni, nj = list_n[i], list_n[j] |
| 615 |
xij = rng.binomial(size=(ni, nj), n=1, p=C2[i, j]) |
| 616 |
pos_i_min, pos_i_max = pos[i], pos[i + 1] |
| 617 |
pos_j_min, pos_j_max = pos[j], pos[j + 1] |
| 618 |
C1[pos_i_min:pos_i_max, pos_j_min:pos_j_max] = xij |
| 619 |
|
| 620 |
p = ot.unif(ns, type_as=C1) |
| 621 |
q0 = ot.unif(C2.shape[0], type_as=C1) |
| 622 |
G0 = p[:, None] * q0[None, :] |
| 623 |
|
| 624 |
C1b, C2b, pb, q0b, G0b = nx.from_numpy(C1, C2, p, q0, G0) |
| 625 |
epsilon = 0.1 |
| 626 |
for loss_fun in ["square_loss", "kl_loss"]: |
| 627 |
G, log = ot.gromov.entropic_semirelaxed_gromov_wasserstein( |
| 628 |
C1, |
| 629 |
C2, |
| 630 |
p, |
| 631 |
loss_fun=loss_fun, |
| 632 |
epsilon=epsilon, |
| 633 |
symmetric=None, |
| 634 |
log=True, |
| 635 |
G0=G0, |
| 636 |
) |
| 637 |
Gb, logb = ot.gromov.entropic_semirelaxed_gromov_wasserstein( |
| 638 |
C1b, |
| 639 |
C2b, |
| 640 |
None, |
| 641 |
loss_fun=loss_fun, |
| 642 |
epsilon=epsilon, |
| 643 |
symmetric=False, |
| 644 |
log=True, |
| 645 |
G0=None, |
| 646 |
) |
| 647 |
|
| 648 |
|
| 649 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 650 |
np.testing.assert_allclose(p, nx.sum(Gb, axis=1), atol=1e-04) |
| 651 |
np.testing.assert_allclose(list_n / ns, np.sum(G, axis=0), atol=1e-01) |
| 652 |
np.testing.assert_allclose(list_n / ns, nx.sum(Gb, axis=0), atol=1e-01) |
| 653 |
|
| 654 |
srgw, log2 = ot.gromov.entropic_semirelaxed_gromov_wasserstein2( |
| 655 |
C1, |
| 656 |
C2, |
| 657 |
None, |
| 658 |
loss_fun=loss_fun, |
| 659 |
epsilon=epsilon, |
| 660 |
symmetric=False, |
| 661 |
log=True, |
| 662 |
G0=G0, |
| 663 |
) |
| 664 |
srgwb, logb2 = ot.gromov.entropic_semirelaxed_gromov_wasserstein2( |
| 665 |
C1b, |
| 666 |
C2b, |
| 667 |
pb, |
| 668 |
loss_fun=loss_fun, |
| 669 |
epsilon=epsilon, |
| 670 |
symmetric=None, |
| 671 |
log=True, |
| 672 |
G0=None, |
| 673 |
) |
| 674 |
|
| 675 |
G = log2["T"] |
| 676 |
Gb = nx.to_numpy(logb2["T"]) |
| 677 |
|
| 678 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 679 |
np.testing.assert_allclose(p, Gb.sum(1), atol=1e-04) |
| 680 |
np.testing.assert_allclose( |
| 681 |
list_n / ns, Gb.sum(0), atol=1e-04 |
| 682 |
) |
| 683 |
|
| 684 |
np.testing.assert_allclose(log2["srgw_dist"], logb["srgw_dist"], atol=1e-07) |
| 685 |
np.testing.assert_allclose(logb2["srgw_dist"], log["srgw_dist"], atol=1e-07) |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
C1 = 0.5 * (C1 + C1.T) |
| 690 |
C1b, C2b, pb, q0b, G0b = nx.from_numpy(C1, C2, p, q0, G0) |
| 691 |
|
| 692 |
init_plan_list = [] |
| 693 |
|
| 694 |
if networkx_import: |
| 695 |
init_plan_list += [("fluid", "fluid")] |
| 696 |
|
| 697 |
if sklearn_import: |
| 698 |
init_plan_list += [("kmeans", "kmeans")] |
| 699 |
|
| 700 |
init_plan_list += [("product", None), (None, G0b)] |
| 701 |
|
| 702 |
for init, init_b in init_plan_list: |
| 703 |
print(f"---- init : {init} / init_b : {init_b}") |
| 704 |
G, log = ot.gromov.entropic_semirelaxed_gromov_wasserstein( |
| 705 |
C1, |
| 706 |
C2, |
| 707 |
p, |
| 708 |
loss_fun="square_loss", |
| 709 |
epsilon=epsilon, |
| 710 |
symmetric=None, |
| 711 |
log=True, |
| 712 |
G0=init, |
| 713 |
) |
| 714 |
Gb, logb = ot.gromov.entropic_semirelaxed_gromov_wasserstein( |
| 715 |
C1b, |
| 716 |
C2b, |
| 717 |
pb, |
| 718 |
loss_fun="square_loss", |
| 719 |
epsilon=epsilon, |
| 720 |
symmetric=True, |
| 721 |
log=True, |
| 722 |
G0=init_b, |
| 723 |
) |
| 724 |
|
| 725 |
|
| 726 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 727 |
np.testing.assert_allclose( |
| 728 |
p, nx.sum(Gb, axis=1), atol=1e-04 |
| 729 |
) |
| 730 |
|
| 731 |
if not isinstance(init, str): |
| 732 |
np.testing.assert_allclose( |
| 733 |
list_n / ns, nx.sum(Gb, axis=0), atol=1e-02 |
| 734 |
) |
| 735 |
else: |
| 736 |
np.testing.assert_allclose(list_n / ns, nx.sum(Gb, axis=0), atol=1e-02) |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
srgw, log2 = ot.gromov.entropic_semirelaxed_gromov_wasserstein2( |
| 741 |
C1, |
| 742 |
C2, |
| 743 |
p, |
| 744 |
loss_fun="square_loss", |
| 745 |
epsilon=epsilon, |
| 746 |
symmetric=True, |
| 747 |
log=True, |
| 748 |
G0=init, |
| 749 |
) |
| 750 |
srgwb, logb2 = ot.gromov.entropic_semirelaxed_gromov_wasserstein2( |
| 751 |
C1b, |
| 752 |
C2b, |
| 753 |
pb, |
| 754 |
loss_fun="square_loss", |
| 755 |
epsilon=epsilon, |
| 756 |
symmetric=None, |
| 757 |
log=True, |
| 758 |
G0=init_b, |
| 759 |
) |
| 760 |
|
| 761 |
srgw_ = ot.gromov.entropic_semirelaxed_gromov_wasserstein2( |
| 762 |
C1, |
| 763 |
C2, |
| 764 |
p, |
| 765 |
loss_fun="square_loss", |
| 766 |
epsilon=epsilon, |
| 767 |
symmetric=True, |
| 768 |
log=False, |
| 769 |
G0=G0, |
| 770 |
) |
| 771 |
|
| 772 |
G2 = log2["T"] |
| 773 |
G2b = logb2["T"] |
| 774 |
|
| 775 |
np.testing.assert_allclose(G2, G2b, atol=1e-06) |
| 776 |
np.testing.assert_allclose(G2, G, atol=1e-06) |
| 777 |
|
| 778 |
np.testing.assert_allclose(log2["srgw_dist"], log["srgw_dist"], atol=1e-07) |
| 779 |
np.testing.assert_allclose(logb2["srgw_dist"], log["srgw_dist"], atol=1e-07) |
| 780 |
np.testing.assert_allclose(srgw, srgw_, atol=1e-07) |
| 781 |
|
| 782 |
|
| 783 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 784 |
@pytest.skip_backend("tf", reason="test very slow with tf backend") |
| 785 |
def test_entropic_semirelaxed_gromov_dtype_device(nx): |
| 786 |
|
| 787 |
n_samples = 5 |
| 788 |
|
| 789 |
mu_s = np.array([0, 0]) |
| 790 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 791 |
|
| 792 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=42) |
| 793 |
|
| 794 |
xt = xs[::-1].copy() |
| 795 |
|
| 796 |
p = ot.unif(n_samples) |
| 797 |
|
| 798 |
C1 = ot.dist(xs, xs) |
| 799 |
C2 = ot.dist(xt, xt) |
| 800 |
|
| 801 |
C1 /= C1.max() |
| 802 |
C2 /= C2.max() |
| 803 |
|
| 804 |
for tp in nx.__type_list__: |
| 805 |
print(nx.dtype_device(tp)) |
| 806 |
for loss_fun in ["square_loss", "kl_loss"]: |
| 807 |
C1b, C2b, pb = nx.from_numpy(C1, C2, p, type_as=tp) |
| 808 |
|
| 809 |
Gb = ot.gromov.entropic_semirelaxed_gromov_wasserstein( |
| 810 |
C1b, C2b, pb, loss_fun, epsilon=0.1, verbose=True |
| 811 |
) |
| 812 |
gw_valb = ot.gromov.entropic_semirelaxed_gromov_wasserstein2( |
| 813 |
C1b, C2b, pb, loss_fun, epsilon=0.1, verbose=True |
| 814 |
) |
| 815 |
|
| 816 |
nx.assert_same_dtype_device(C1b, Gb) |
| 817 |
nx.assert_same_dtype_device(C1b, gw_valb) |
| 818 |
|
| 819 |
|
| 820 |
def test_entropic_semirelaxed_fgw(nx): |
| 821 |
rng = np.random.RandomState(0) |
| 822 |
list_n = [16, 8] |
| 823 |
nt = 2 |
| 824 |
ns = 24 |
| 825 |
|
| 826 |
C1 = np.zeros((ns, ns)) |
| 827 |
C2 = np.array([[0.7, 0.05], [0.05, 0.9]]) |
| 828 |
|
| 829 |
pos = [0, 16, 24] |
| 830 |
|
| 831 |
for i in range(nt): |
| 832 |
for j in range(nt): |
| 833 |
ni, nj = list_n[i], list_n[j] |
| 834 |
xij = rng.binomial(size=(ni, nj), n=1, p=C2[i, j]) |
| 835 |
pos_i_min, pos_i_max = pos[i], pos[i + 1] |
| 836 |
pos_j_min, pos_j_max = pos[j], pos[j + 1] |
| 837 |
C1[pos_i_min:pos_i_max, pos_j_min:pos_j_max] = xij |
| 838 |
|
| 839 |
F1 = np.zeros((ns, 1)) |
| 840 |
F1[:16] = rng.normal(loc=0.0, scale=0.01, size=(16, 1)) |
| 841 |
F1[16:] = rng.normal(loc=1.0, scale=0.01, size=(8, 1)) |
| 842 |
F2 = np.zeros((2, 1)) |
| 843 |
F2[1, :] = 1.0 |
| 844 |
M = ( |
| 845 |
(F1**2).dot(np.ones((1, nt))) |
| 846 |
+ np.ones((ns, 1)).dot((F2**2).T) |
| 847 |
- 2 * F1.dot(F2.T) |
| 848 |
) |
| 849 |
|
| 850 |
p = ot.unif(ns) |
| 851 |
q0 = ot.unif(C2.shape[0]) |
| 852 |
G0 = p[:, None] * q0[None, :] |
| 853 |
|
| 854 |
|
| 855 |
Mb, C1b, C2b, pb, q0b, G0b = nx.from_numpy(M, C1, C2, p, q0, G0) |
| 856 |
|
| 857 |
G, log = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein( |
| 858 |
M, |
| 859 |
C1, |
| 860 |
C2, |
| 861 |
None, |
| 862 |
loss_fun="square_loss", |
| 863 |
epsilon=0.1, |
| 864 |
alpha=0.5, |
| 865 |
symmetric=None, |
| 866 |
log=True, |
| 867 |
G0=None, |
| 868 |
) |
| 869 |
Gb, logb = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein( |
| 870 |
Mb, |
| 871 |
C1b, |
| 872 |
C2b, |
| 873 |
pb, |
| 874 |
loss_fun="square_loss", |
| 875 |
epsilon=0.1, |
| 876 |
alpha=0.5, |
| 877 |
symmetric=False, |
| 878 |
log=True, |
| 879 |
G0=G0b, |
| 880 |
) |
| 881 |
|
| 882 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 883 |
np.testing.assert_allclose( |
| 884 |
p, nx.sum(Gb, axis=1), atol=1e-04 |
| 885 |
) |
| 886 |
np.testing.assert_allclose( |
| 887 |
[2 / 3, 1 / 3], nx.sum(Gb, axis=0), atol=1e-02 |
| 888 |
) |
| 889 |
|
| 890 |
srgw, log2 = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein2( |
| 891 |
M, |
| 892 |
C1, |
| 893 |
C2, |
| 894 |
p, |
| 895 |
loss_fun="square_loss", |
| 896 |
epsilon=0.1, |
| 897 |
alpha=0.5, |
| 898 |
symmetric=False, |
| 899 |
log=True, |
| 900 |
G0=G0, |
| 901 |
) |
| 902 |
srgwb, logb2 = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein2( |
| 903 |
Mb, |
| 904 |
C1b, |
| 905 |
C2b, |
| 906 |
None, |
| 907 |
loss_fun="square_loss", |
| 908 |
epsilon=0.1, |
| 909 |
alpha=0.5, |
| 910 |
symmetric=None, |
| 911 |
log=True, |
| 912 |
G0=None, |
| 913 |
) |
| 914 |
|
| 915 |
G = log2["T"] |
| 916 |
Gb = nx.to_numpy(logb2["T"]) |
| 917 |
|
| 918 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 919 |
np.testing.assert_allclose(p, Gb.sum(1), atol=1e-04) |
| 920 |
np.testing.assert_allclose( |
| 921 |
[2 / 3, 1 / 3], Gb.sum(0), atol=1e-04 |
| 922 |
) |
| 923 |
|
| 924 |
np.testing.assert_allclose(log2["srfgw_dist"], logb["srfgw_dist"], atol=1e-07) |
| 925 |
np.testing.assert_allclose(logb2["srfgw_dist"], log["srfgw_dist"], atol=1e-07) |
| 926 |
|
| 927 |
|
| 928 |
C1 = 0.5 * (C1 + C1.T) |
| 929 |
Mb, C1b, C2b, pb, q0b, G0b = nx.from_numpy(M, C1, C2, p, q0, G0) |
| 930 |
|
| 931 |
init_plan_list = [ |
| 932 |
(None, G0b), |
| 933 |
("product", None), |
| 934 |
("random_product", "random_product"), |
| 935 |
] |
| 936 |
|
| 937 |
if networkx_import: |
| 938 |
init_plan_list += [("fluid", "fluid")] |
| 939 |
|
| 940 |
if sklearn_import: |
| 941 |
init_plan_list += [("kmeans", "kmeans")] |
| 942 |
|
| 943 |
for loss_fun in ["square_loss", "kl_loss"]: |
| 944 |
for init, init_b in init_plan_list: |
| 945 |
G, log = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein( |
| 946 |
M, |
| 947 |
C1, |
| 948 |
C2, |
| 949 |
p, |
| 950 |
loss_fun=loss_fun, |
| 951 |
epsilon=0.1, |
| 952 |
alpha=0.5, |
| 953 |
symmetric=None, |
| 954 |
log=True, |
| 955 |
G0=init, |
| 956 |
) |
| 957 |
Gb = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein( |
| 958 |
Mb, |
| 959 |
C1b, |
| 960 |
C2b, |
| 961 |
pb, |
| 962 |
loss_fun=loss_fun, |
| 963 |
epsilon=0.1, |
| 964 |
alpha=0.5, |
| 965 |
symmetric=True, |
| 966 |
log=False, |
| 967 |
G0=init_b, |
| 968 |
) |
| 969 |
|
| 970 |
np.testing.assert_allclose(G, Gb, atol=1e-06) |
| 971 |
np.testing.assert_allclose( |
| 972 |
p, nx.sum(Gb, axis=1), atol=1e-04 |
| 973 |
) |
| 974 |
np.testing.assert_allclose( |
| 975 |
[2 / 3, 1 / 3], nx.sum(Gb, axis=0), atol=1e-02 |
| 976 |
) |
| 977 |
|
| 978 |
|
| 979 |
srgw, log2 = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein2( |
| 980 |
M, |
| 981 |
C1, |
| 982 |
C2, |
| 983 |
p, |
| 984 |
loss_fun=loss_fun, |
| 985 |
epsilon=0.1, |
| 986 |
alpha=0.5, |
| 987 |
symmetric=True, |
| 988 |
log=True, |
| 989 |
G0=init, |
| 990 |
) |
| 991 |
srgwb, logb2 = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein2( |
| 992 |
Mb, |
| 993 |
C1b, |
| 994 |
C2b, |
| 995 |
pb, |
| 996 |
loss_fun=loss_fun, |
| 997 |
epsilon=0.1, |
| 998 |
alpha=0.5, |
| 999 |
symmetric=None, |
| 1000 |
log=True, |
| 1001 |
G0=init_b, |
| 1002 |
) |
| 1003 |
|
| 1004 |
G2 = log2["T"] |
| 1005 |
Gb2 = nx.to_numpy(logb2["T"]) |
| 1006 |
np.testing.assert_allclose(G2, Gb2, atol=1e-06) |
| 1007 |
np.testing.assert_allclose(G2, G, atol=1e-06) |
| 1008 |
np.testing.assert_allclose(p, Gb2.sum(1), atol=1e-04) |
| 1009 |
np.testing.assert_allclose( |
| 1010 |
[2 / 3, 1 / 3], Gb2.sum(0), atol=1e-04 |
| 1011 |
) |
| 1012 |
|
| 1013 |
np.testing.assert_allclose(log2["srfgw_dist"], log["srfgw_dist"], atol=1e-07) |
| 1014 |
np.testing.assert_allclose(logb2["srfgw_dist"], log["srfgw_dist"], atol=1e-07) |
| 1015 |
np.testing.assert_allclose(srgw, srgwb, atol=1e-07) |
| 1016 |
|
| 1017 |
|
| 1018 |
@pytest.skip_backend("tf", reason="test very slow with tf backend") |
| 1019 |
def test_entropic_semirelaxed_fgw_dtype_device(nx): |
| 1020 |
|
| 1021 |
n_samples = 5 |
| 1022 |
|
| 1023 |
mu_s = np.array([0, 0]) |
| 1024 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 1025 |
|
| 1026 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=42) |
| 1027 |
|
| 1028 |
xt = xs[::-1].copy() |
| 1029 |
|
| 1030 |
rng = np.random.RandomState(42) |
| 1031 |
ys = rng.randn(xs.shape[0], 2) |
| 1032 |
yt = ys[::-1].copy() |
| 1033 |
|
| 1034 |
p = ot.unif(n_samples) |
| 1035 |
|
| 1036 |
C1 = ot.dist(xs, xs) |
| 1037 |
C2 = ot.dist(xt, xt) |
| 1038 |
|
| 1039 |
C1 /= C1.max() |
| 1040 |
C2 /= C2.max() |
| 1041 |
|
| 1042 |
M = ot.dist(ys, yt) |
| 1043 |
for tp in nx.__type_list__: |
| 1044 |
print(nx.dtype_device(tp)) |
| 1045 |
|
| 1046 |
Mb, C1b, C2b, pb = nx.from_numpy(M, C1, C2, p, type_as=tp) |
| 1047 |
|
| 1048 |
for loss_fun in ["square_loss", "kl_loss"]: |
| 1049 |
Gb = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein( |
| 1050 |
Mb, C1b, C2b, pb, loss_fun, epsilon=0.1, verbose=True |
| 1051 |
) |
| 1052 |
fgw_valb = ot.gromov.entropic_semirelaxed_fused_gromov_wasserstein2( |
| 1053 |
Mb, C1b, C2b, pb, loss_fun, epsilon=0.1, verbose=True |
| 1054 |
) |
| 1055 |
|
| 1056 |
nx.assert_same_dtype_device(C1b, Gb) |
| 1057 |
nx.assert_same_dtype_device(C1b, fgw_valb) |
| 1058 |
|
| 1059 |
|
| 1060 |
@pytest.skip_backend("tf", reason="test very slow with tf backend") |
| 1061 |
@pytest.skip_backend("jax", reason="test very slow with tf backend") |
| 1062 |
def test_semirelaxed_gromov_barycenter(nx): |
| 1063 |
ns = 5 |
| 1064 |
nt = 8 |
| 1065 |
|
| 1066 |
Xs, ys = ot.datasets.make_data_classif("3gauss", ns, random_state=42) |
| 1067 |
Xt, yt = ot.datasets.make_data_classif("3gauss2", nt, random_state=42) |
| 1068 |
|
| 1069 |
C1 = ot.dist(Xs) |
| 1070 |
C2 = ot.dist(Xt) |
| 1071 |
p1 = ot.unif(ns) |
| 1072 |
p2 = ot.unif(nt) |
| 1073 |
n_samples = 3 |
| 1074 |
|
| 1075 |
C1b, C2b, p1b, p2b = nx.from_numpy(C1, C2, p1, p2) |
| 1076 |
|
| 1077 |
|
| 1078 |
with pytest.raises(ValueError): |
| 1079 |
stop_criterion = "unknown stop criterion" |
| 1080 |
_ = ot.gromov.semirelaxed_gromov_barycenters( |
| 1081 |
n_samples, |
| 1082 |
[C1, C2], |
| 1083 |
None, |
| 1084 |
[0.5, 0.5], |
| 1085 |
"square_loss", |
| 1086 |
max_iter=10, |
| 1087 |
tol=1e-3, |
| 1088 |
stop_criterion=stop_criterion, |
| 1089 |
verbose=False, |
| 1090 |
random_state=42, |
| 1091 |
) |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
init_plan_list = [("fluid", "fluid"), ("kmeans", "kmeans"), ("random", "random")] |
| 1097 |
|
| 1098 |
for init, init_b in init_plan_list: |
| 1099 |
for stop_criterion in ["barycenter", "loss"]: |
| 1100 |
print("--- stop_criterion:", stop_criterion) |
| 1101 |
|
| 1102 |
if (init == "fluid") and (not networkx_import): |
| 1103 |
with pytest.raises(ValueError): |
| 1104 |
Cb = ot.gromov.semirelaxed_gromov_barycenters( |
| 1105 |
n_samples, |
| 1106 |
[C1, C2], |
| 1107 |
None, |
| 1108 |
[0.5, 0.5], |
| 1109 |
"square_loss", |
| 1110 |
max_iter=5, |
| 1111 |
tol=1e-3, |
| 1112 |
stop_criterion=stop_criterion, |
| 1113 |
verbose=False, |
| 1114 |
random_state=42, |
| 1115 |
G0=init, |
| 1116 |
) |
| 1117 |
|
| 1118 |
elif (init == "kmeans") and (not sklearn_import): |
| 1119 |
with pytest.raises(ValueError): |
| 1120 |
Cb = ot.gromov.semirelaxed_gromov_barycenters( |
| 1121 |
n_samples, |
| 1122 |
[C1, C2], |
| 1123 |
None, |
| 1124 |
[0.5, 0.5], |
| 1125 |
"square_loss", |
| 1126 |
max_iter=5, |
| 1127 |
tol=1e-3, |
| 1128 |
stop_criterion=stop_criterion, |
| 1129 |
verbose=False, |
| 1130 |
random_state=42, |
| 1131 |
G0=init, |
| 1132 |
) |
| 1133 |
else: |
| 1134 |
Cb = ot.gromov.semirelaxed_gromov_barycenters( |
| 1135 |
n_samples, |
| 1136 |
[C1, C2], |
| 1137 |
None, |
| 1138 |
[0.5, 0.5], |
| 1139 |
"square_loss", |
| 1140 |
max_iter=5, |
| 1141 |
tol=1e-3, |
| 1142 |
stop_criterion=stop_criterion, |
| 1143 |
verbose=False, |
| 1144 |
random_state=42, |
| 1145 |
G0=init, |
| 1146 |
) |
| 1147 |
|
| 1148 |
Cbb = nx.to_numpy( |
| 1149 |
ot.gromov.semirelaxed_gromov_barycenters( |
| 1150 |
n_samples, |
| 1151 |
[C1b, C2b], |
| 1152 |
[p1b, p2b], |
| 1153 |
[0.5, 0.5], |
| 1154 |
"square_loss", |
| 1155 |
max_iter=5, |
| 1156 |
tol=1e-3, |
| 1157 |
stop_criterion=stop_criterion, |
| 1158 |
verbose=False, |
| 1159 |
random_state=42, |
| 1160 |
G0=init_b, |
| 1161 |
) |
| 1162 |
) |
| 1163 |
np.testing.assert_allclose(Cb, Cbb, atol=1e-06) |
| 1164 |
np.testing.assert_allclose(Cbb.shape, (n_samples, n_samples)) |
| 1165 |
|
| 1166 |
|
| 1167 |
Cb_, err_ = ot.gromov.semirelaxed_gromov_barycenters( |
| 1168 |
n_samples, |
| 1169 |
[C1, C2], |
| 1170 |
[p1, p2], |
| 1171 |
None, |
| 1172 |
"square_loss", |
| 1173 |
max_iter=5, |
| 1174 |
tol=1e-3, |
| 1175 |
stop_criterion=stop_criterion, |
| 1176 |
verbose=False, |
| 1177 |
warmstartT=True, |
| 1178 |
random_state=42, |
| 1179 |
log=True, |
| 1180 |
G0=init, |
| 1181 |
) |
| 1182 |
Cbb_, errb_ = ot.gromov.semirelaxed_gromov_barycenters( |
| 1183 |
n_samples, |
| 1184 |
[C1b, C2b], |
| 1185 |
[p1b, p2b], |
| 1186 |
[0.5, 0.5], |
| 1187 |
"square_loss", |
| 1188 |
max_iter=5, |
| 1189 |
tol=1e-3, |
| 1190 |
stop_criterion=stop_criterion, |
| 1191 |
verbose=False, |
| 1192 |
warmstartT=True, |
| 1193 |
random_state=42, |
| 1194 |
log=True, |
| 1195 |
G0=init_b, |
| 1196 |
) |
| 1197 |
|
| 1198 |
Cbb_ = nx.to_numpy(Cbb_) |
| 1199 |
|
| 1200 |
np.testing.assert_allclose(Cb_, Cbb_, atol=1e-06) |
| 1201 |
np.testing.assert_array_almost_equal( |
| 1202 |
err_["err"], nx.to_numpy(*errb_["err"]) |
| 1203 |
) |
| 1204 |
np.testing.assert_allclose(Cbb_.shape, (n_samples, n_samples)) |
| 1205 |
|
| 1206 |
|
| 1207 |
if sklearn_import: |
| 1208 |
C = ot.gromov.semirelaxed_gromov_barycenters( |
| 1209 |
ns, |
| 1210 |
[C1, C2], |
| 1211 |
None, |
| 1212 |
[0.5, 0.5], |
| 1213 |
"square_loss", |
| 1214 |
max_iter=5, |
| 1215 |
tol=1e-3, |
| 1216 |
stop_criterion="loss", |
| 1217 |
verbose=False, |
| 1218 |
random_state=42, |
| 1219 |
G0="kmeans", |
| 1220 |
) |
| 1221 |
Cb = ot.gromov.semirelaxed_gromov_barycenters( |
| 1222 |
ns, |
| 1223 |
[C1b, C2b], |
| 1224 |
[p1b, p2b], |
| 1225 |
[0.5, 0.5], |
| 1226 |
"square_loss", |
| 1227 |
max_iter=5, |
| 1228 |
tol=1e-3, |
| 1229 |
stop_criterion=stop_criterion, |
| 1230 |
verbose=False, |
| 1231 |
random_state=42, |
| 1232 |
G0="kmeans", |
| 1233 |
) |
| 1234 |
|
| 1235 |
np.testing.assert_allclose(C, nx.to_numpy(Cb), atol=1e-06) |
| 1236 |
|
| 1237 |
|
| 1238 |
C_ = ot.gromov.semirelaxed_gromov_barycenters( |
| 1239 |
ns, |
| 1240 |
[C1, C2], |
| 1241 |
None, |
| 1242 |
[0.5, 0.5], |
| 1243 |
"square_loss", |
| 1244 |
max_iter=5, |
| 1245 |
tol=1e-3, |
| 1246 |
stop_criterion="loss", |
| 1247 |
verbose=False, |
| 1248 |
random_state=42, |
| 1249 |
G0=init, |
| 1250 |
init_C=C1, |
| 1251 |
) |
| 1252 |
|
| 1253 |
Cb_ = ot.gromov.semirelaxed_gromov_barycenters( |
| 1254 |
ns, |
| 1255 |
[C1b, C2b], |
| 1256 |
[p1b, p2b], |
| 1257 |
[0.5, 0.5], |
| 1258 |
"square_loss", |
| 1259 |
max_iter=5, |
| 1260 |
tol=1e-3, |
| 1261 |
stop_criterion=stop_criterion, |
| 1262 |
verbose=False, |
| 1263 |
random_state=42, |
| 1264 |
G0=init_b, |
| 1265 |
init_C=C1b, |
| 1266 |
) |
| 1267 |
|
| 1268 |
np.testing.assert_allclose(C_, Cb_, atol=1e-06) |
| 1269 |
|
| 1270 |
|
| 1271 |
Cb2, err = ot.gromov.semirelaxed_gromov_barycenters( |
| 1272 |
n_samples, |
| 1273 |
[C1, C2], |
| 1274 |
[p1, p2], |
| 1275 |
[0.5, 0.5], |
| 1276 |
"kl_loss", |
| 1277 |
max_iter=5, |
| 1278 |
tol=1e-3, |
| 1279 |
warmstartT=False, |
| 1280 |
stop_criterion="loss", |
| 1281 |
log=True, |
| 1282 |
G0=init_b, |
| 1283 |
random_state=42, |
| 1284 |
) |
| 1285 |
Cb2b, errb = ot.gromov.semirelaxed_gromov_barycenters( |
| 1286 |
n_samples, |
| 1287 |
[C1b, C2b], |
| 1288 |
[p1b, p2b], |
| 1289 |
[0.5, 0.5], |
| 1290 |
"kl_loss", |
| 1291 |
max_iter=5, |
| 1292 |
tol=1e-3, |
| 1293 |
warmstartT=False, |
| 1294 |
stop_criterion="loss", |
| 1295 |
log=True, |
| 1296 |
G0=init_b, |
| 1297 |
random_state=42, |
| 1298 |
) |
| 1299 |
Cb2b = nx.to_numpy(Cb2b) |
| 1300 |
|
| 1301 |
try: |
| 1302 |
np.testing.assert_allclose(Cb2, Cb2b, atol=1e-06) |
| 1303 |
except AssertionError: |
| 1304 |
np.testing.assert_allclose(err["loss"][-1], errb["loss"][-1], atol=1e-06) |
| 1305 |
|
| 1306 |
np.testing.assert_allclose(Cb2b.shape, (n_samples, n_samples)) |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
rng = ot.utils.check_random_state(42) |
| 1311 |
xalea = rng.randn(n_samples, 2) |
| 1312 |
init_C = ot.utils.dist(xalea, xalea) |
| 1313 |
init_C /= init_C.max() |
| 1314 |
init_Cb = nx.from_numpy(init_C) |
| 1315 |
|
| 1316 |
Cb2_, err2_ = ot.gromov.semirelaxed_gromov_barycenters( |
| 1317 |
n_samples, |
| 1318 |
[C1, C2], |
| 1319 |
[p1, p2], |
| 1320 |
[0.5, 0.5], |
| 1321 |
"square_loss", |
| 1322 |
max_iter=10, |
| 1323 |
tol=1e-3, |
| 1324 |
verbose=False, |
| 1325 |
random_state=42, |
| 1326 |
log=True, |
| 1327 |
init_C=init_C, |
| 1328 |
) |
| 1329 |
Cb2b_, err2b_ = ot.gromov.semirelaxed_gromov_barycenters( |
| 1330 |
n_samples, |
| 1331 |
[C1b, C2b], |
| 1332 |
[p1b, p2b], |
| 1333 |
[0.5, 0.5], |
| 1334 |
"square_loss", |
| 1335 |
max_iter=10, |
| 1336 |
tol=1e-3, |
| 1337 |
verbose=True, |
| 1338 |
random_state=42, |
| 1339 |
init_C=init_Cb, |
| 1340 |
log=True, |
| 1341 |
) |
| 1342 |
Cb2b_ = nx.to_numpy(Cb2b_) |
| 1343 |
np.testing.assert_allclose(Cb2_, Cb2b_, atol=1e-06) |
| 1344 |
np.testing.assert_array_almost_equal(err2_["err"], nx.to_numpy(*err2b_["err"])) |
| 1345 |
np.testing.assert_allclose(Cb2b_.shape, (n_samples, n_samples)) |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
Cb = ot.gromov.semirelaxed_gromov_barycenters( |
| 1350 |
n_samples, |
| 1351 |
[C1], |
| 1352 |
None, |
| 1353 |
None, |
| 1354 |
"square_loss", |
| 1355 |
max_iter=1, |
| 1356 |
tol=1e-3, |
| 1357 |
stop_criterion=stop_criterion, |
| 1358 |
verbose=False, |
| 1359 |
random_state=42, |
| 1360 |
) |
| 1361 |
Cbb = nx.to_numpy( |
| 1362 |
ot.gromov.semirelaxed_gromov_barycenters( |
| 1363 |
n_samples, |
| 1364 |
[C1b], |
| 1365 |
None, |
| 1366 |
[1.0], |
| 1367 |
"square_loss", |
| 1368 |
max_iter=1, |
| 1369 |
tol=1e-3, |
| 1370 |
stop_criterion=stop_criterion, |
| 1371 |
verbose=False, |
| 1372 |
random_state=42, |
| 1373 |
) |
| 1374 |
) |
| 1375 |
np.testing.assert_allclose(Cb, Cbb, atol=1e-06) |
| 1376 |
np.testing.assert_allclose(Cbb.shape, (n_samples, n_samples)) |
| 1377 |
|
| 1378 |
|
| 1379 |
def test_semirelaxed_fgw_barycenter(nx): |
| 1380 |
ns = 10 |
| 1381 |
nt = 20 |
| 1382 |
|
| 1383 |
Xs, ys = ot.datasets.make_data_classif("3gauss", ns, random_state=42) |
| 1384 |
Xt, yt = ot.datasets.make_data_classif("3gauss2", nt, random_state=42) |
| 1385 |
|
| 1386 |
rng = np.random.RandomState(42) |
| 1387 |
ys = rng.randn(Xs.shape[0], 2) |
| 1388 |
yt = rng.randn(Xt.shape[0], 2) |
| 1389 |
|
| 1390 |
C1 = ot.dist(Xs) |
| 1391 |
C2 = ot.dist(Xt) |
| 1392 |
C1 /= C1.max() |
| 1393 |
C2 /= C2.max() |
| 1394 |
|
| 1395 |
p1, p2 = ot.unif(ns), ot.unif(nt) |
| 1396 |
n_samples = 3 |
| 1397 |
|
| 1398 |
ysb, ytb, C1b, C2b, p1b, p2b = nx.from_numpy(ys, yt, C1, C2, p1, p2) |
| 1399 |
|
| 1400 |
lambdas = [0.5, 0.5] |
| 1401 |
Csb = [C1b, C2b] |
| 1402 |
Ysb = [ysb, ytb] |
| 1403 |
Xb, Cb, logb = ot.gromov.semirelaxed_fgw_barycenters( |
| 1404 |
n_samples, |
| 1405 |
Ysb, |
| 1406 |
Csb, |
| 1407 |
None, |
| 1408 |
lambdas, |
| 1409 |
0.5, |
| 1410 |
fixed_structure=False, |
| 1411 |
fixed_features=False, |
| 1412 |
loss_fun="square_loss", |
| 1413 |
max_iter=10, |
| 1414 |
tol=1e-3, |
| 1415 |
random_state=12345, |
| 1416 |
log=True, |
| 1417 |
) |
| 1418 |
|
| 1419 |
recovered_Cb = ot.gromov.update_barycenter_structure(logb["T"], Csb, lambdas) |
| 1420 |
recovered_Xb = ot.gromov.update_barycenter_feature(logb["T"], Ysb, lambdas) |
| 1421 |
|
| 1422 |
np.testing.assert_allclose(Cb, recovered_Cb) |
| 1423 |
np.testing.assert_allclose(Xb, recovered_Xb) |
| 1424 |
|
| 1425 |
xalea = rng.randn(n_samples, 2) |
| 1426 |
init_C = ot.dist(xalea, xalea) |
| 1427 |
init_C /= init_C.max() |
| 1428 |
init_Cb = nx.from_numpy(init_C) |
| 1429 |
|
| 1430 |
with pytest.raises( |
| 1431 |
ot.utils.UndefinedParameter |
| 1432 |
): |
| 1433 |
Xb, Cb, logb = ot.gromov.semirelaxed_fgw_barycenters( |
| 1434 |
n_samples, |
| 1435 |
Ysb, |
| 1436 |
Csb, |
| 1437 |
ps=[p1b, p2b], |
| 1438 |
lambdas=None, |
| 1439 |
alpha=0.5, |
| 1440 |
fixed_structure=True, |
| 1441 |
init_C=None, |
| 1442 |
fixed_features=False, |
| 1443 |
loss_fun="square_loss", |
| 1444 |
max_iter=10, |
| 1445 |
tol=1e-3, |
| 1446 |
) |
| 1447 |
|
| 1448 |
Xb, Cb = ot.gromov.semirelaxed_fgw_barycenters( |
| 1449 |
n_samples, |
| 1450 |
[ysb, ytb], |
| 1451 |
[C1b, C2b], |
| 1452 |
ps=[p1b, p2b], |
| 1453 |
lambdas=None, |
| 1454 |
alpha=0.5, |
| 1455 |
fixed_structure=True, |
| 1456 |
init_C=init_Cb, |
| 1457 |
fixed_features=False, |
| 1458 |
loss_fun="square_loss", |
| 1459 |
max_iter=10, |
| 1460 |
tol=1e-3, |
| 1461 |
) |
| 1462 |
Xb, Cb = nx.to_numpy(Xb), nx.to_numpy(Cb) |
| 1463 |
np.testing.assert_allclose(Cb.shape, (n_samples, n_samples)) |
| 1464 |
np.testing.assert_allclose(Xb.shape, (n_samples, ys.shape[1])) |
| 1465 |
|
| 1466 |
init_X = rng.randn(n_samples, ys.shape[1]) |
| 1467 |
init_Xb = nx.from_numpy(init_X) |
| 1468 |
|
| 1469 |
|
| 1470 |
with pytest.raises( |
| 1471 |
ot.utils.UndefinedParameter |
| 1472 |
): |
| 1473 |
Xb, Cb, logb = ot.gromov.semirelaxed_fgw_barycenters( |
| 1474 |
n_samples, |
| 1475 |
[ysb, ytb], |
| 1476 |
[C1b, C2b], |
| 1477 |
[p1b, p2b], |
| 1478 |
[0.5, 0.5], |
| 1479 |
0.5, |
| 1480 |
fixed_structure=False, |
| 1481 |
fixed_features=True, |
| 1482 |
init_X=None, |
| 1483 |
loss_fun="square_loss", |
| 1484 |
max_iter=10, |
| 1485 |
tol=1e-3, |
| 1486 |
warmstartT=True, |
| 1487 |
log=True, |
| 1488 |
random_state=98765, |
| 1489 |
verbose=True, |
| 1490 |
) |
| 1491 |
Xb, Cb, logb = ot.gromov.semirelaxed_fgw_barycenters( |
| 1492 |
n_samples, |
| 1493 |
[ysb, ytb], |
| 1494 |
[C1b, C2b], |
| 1495 |
[p1b, p2b], |
| 1496 |
[0.5, 0.5], |
| 1497 |
0.5, |
| 1498 |
fixed_structure=False, |
| 1499 |
fixed_features=True, |
| 1500 |
init_X=init_Xb, |
| 1501 |
loss_fun="square_loss", |
| 1502 |
max_iter=10, |
| 1503 |
tol=1e-3, |
| 1504 |
warmstartT=True, |
| 1505 |
log=True, |
| 1506 |
random_state=98765, |
| 1507 |
verbose=True, |
| 1508 |
) |
| 1509 |
|
| 1510 |
X, C = nx.to_numpy(Xb), nx.to_numpy(Cb) |
| 1511 |
np.testing.assert_allclose(C.shape, (n_samples, n_samples)) |
| 1512 |
np.testing.assert_allclose(X.shape, (n_samples, ys.shape[1])) |
| 1513 |
|
| 1514 |
|
| 1515 |
with pytest.raises(ValueError): |
| 1516 |
stop_criterion = "unknown stop criterion" |
| 1517 |
X, C, log = ot.gromov.semirelaxed_fgw_barycenters( |
| 1518 |
n_samples, |
| 1519 |
[ys, yt], |
| 1520 |
[C1, C2], |
| 1521 |
[p1, p2], |
| 1522 |
[0.5, 0.5], |
| 1523 |
0.5, |
| 1524 |
fixed_structure=False, |
| 1525 |
fixed_features=False, |
| 1526 |
loss_fun="kl_loss", |
| 1527 |
max_iter=10, |
| 1528 |
tol=1e-3, |
| 1529 |
stop_criterion=stop_criterion, |
| 1530 |
init_C=C, |
| 1531 |
init_X=X, |
| 1532 |
warmstartT=True, |
| 1533 |
random_state=12345, |
| 1534 |
log=True, |
| 1535 |
) |
| 1536 |
|
| 1537 |
for stop_criterion in ["barycenter", "loss"]: |
| 1538 |
X, C, log = ot.gromov.semirelaxed_fgw_barycenters( |
| 1539 |
n_samples, |
| 1540 |
[ys, yt], |
| 1541 |
[C1, C2], |
| 1542 |
[p1, p2], |
| 1543 |
[0.5, 0.5], |
| 1544 |
0.5, |
| 1545 |
fixed_structure=False, |
| 1546 |
fixed_features=False, |
| 1547 |
loss_fun="kl_loss", |
| 1548 |
max_iter=10, |
| 1549 |
tol=1e-3, |
| 1550 |
stop_criterion=stop_criterion, |
| 1551 |
init_C=C, |
| 1552 |
init_X=X, |
| 1553 |
warmstartT=True, |
| 1554 |
random_state=12345, |
| 1555 |
log=True, |
| 1556 |
verbose=True, |
| 1557 |
) |
| 1558 |
np.testing.assert_allclose(C.shape, (n_samples, n_samples)) |
| 1559 |
np.testing.assert_allclose(X.shape, (n_samples, ys.shape[1])) |
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
recovered_C = ot.gromov.update_barycenter_structure( |
| 1564 |
log["T"], [C1, C2], lambdas, None, "kl_loss", True |
| 1565 |
) |
| 1566 |
|
| 1567 |
np.testing.assert_allclose(C, recovered_C) |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
init_plan_list = [ |
| 1572 |
("fluid", "fluid"), |
| 1573 |
("kmeans", "kmeans"), |
| 1574 |
("product", "product"), |
| 1575 |
("random", "random"), |
| 1576 |
] |
| 1577 |
|
| 1578 |
for init, init_b in init_plan_list: |
| 1579 |
print(f"---- init : {init} / init_b : {init_b}") |
| 1580 |
|
| 1581 |
if (init == "fluid") and (not networkx_import): |
| 1582 |
with pytest.raises(ValueError): |
| 1583 |
X, C, log = ot.gromov.semirelaxed_fgw_barycenters( |
| 1584 |
n_samples, |
| 1585 |
[ys, yt], |
| 1586 |
[C1, C2], |
| 1587 |
[p1, p2], |
| 1588 |
[0.5, 0.5], |
| 1589 |
0.5, |
| 1590 |
fixed_structure=False, |
| 1591 |
fixed_features=False, |
| 1592 |
loss_fun="square_loss", |
| 1593 |
max_iter=10, |
| 1594 |
tol=1e-3, |
| 1595 |
stop_criterion="loss", |
| 1596 |
G0=init, |
| 1597 |
warmstartT=True, |
| 1598 |
random_state=12345, |
| 1599 |
log=True, |
| 1600 |
verbose=True, |
| 1601 |
) |
| 1602 |
elif (init == "kmeans") and (not sklearn_import): |
| 1603 |
with pytest.raises(ValueError): |
| 1604 |
X, C, log = ot.gromov.semirelaxed_fgw_barycenters( |
| 1605 |
n_samples, |
| 1606 |
[ys, yt], |
| 1607 |
[C1, C2], |
| 1608 |
[p1, p2], |
| 1609 |
[0.5, 0.5], |
| 1610 |
0.5, |
| 1611 |
fixed_structure=False, |
| 1612 |
fixed_features=False, |
| 1613 |
loss_fun="square_loss", |
| 1614 |
max_iter=10, |
| 1615 |
tol=1e-3, |
| 1616 |
stop_criterion="loss", |
| 1617 |
G0=init, |
| 1618 |
warmstartT=True, |
| 1619 |
random_state=12345, |
| 1620 |
log=True, |
| 1621 |
verbose=True, |
| 1622 |
) |
| 1623 |
else: |
| 1624 |
X, C, log = ot.gromov.semirelaxed_fgw_barycenters( |
| 1625 |
n_samples, |
| 1626 |
[ys, yt], |
| 1627 |
[C1, C2], |
| 1628 |
[p1, p2], |
| 1629 |
[0.5, 0.5], |
| 1630 |
0.5, |
| 1631 |
fixed_structure=False, |
| 1632 |
fixed_features=False, |
| 1633 |
loss_fun="square_loss", |
| 1634 |
max_iter=10, |
| 1635 |
tol=1e-3, |
| 1636 |
stop_criterion="loss", |
| 1637 |
G0=init, |
| 1638 |
warmstartT=True, |
| 1639 |
random_state=12345, |
| 1640 |
log=True, |
| 1641 |
verbose=True, |
| 1642 |
) |
| 1643 |
Xb, Cb, logb = ot.gromov.semirelaxed_fgw_barycenters( |
| 1644 |
n_samples, |
| 1645 |
[ysb, ytb], |
| 1646 |
[C1b, C2b], |
| 1647 |
[p1b, p2b], |
| 1648 |
[0.5, 0.5], |
| 1649 |
0.5, |
| 1650 |
fixed_structure=False, |
| 1651 |
fixed_features=False, |
| 1652 |
loss_fun="square_loss", |
| 1653 |
max_iter=10, |
| 1654 |
tol=1e-3, |
| 1655 |
stop_criterion="loss", |
| 1656 |
G0=init_b, |
| 1657 |
warmstartT=True, |
| 1658 |
random_state=12345, |
| 1659 |
log=True, |
| 1660 |
verbose=True, |
| 1661 |
) |
| 1662 |
np.testing.assert_allclose(X, nx.to_numpy(Xb)) |
| 1663 |
np.testing.assert_allclose(C, nx.to_numpy(Cb)) |
| 1664 |
|
| 1665 |
|
| 1666 |
Xb_, Cb_, logb_ = ot.gromov.semirelaxed_fgw_barycenters( |
| 1667 |
n_samples, |
| 1668 |
[ysb, ytb], |
| 1669 |
[C1b, C2b], |
| 1670 |
[p1b, p2b], |
| 1671 |
[0.5, 0.5], |
| 1672 |
0.5, |
| 1673 |
fixed_structure=False, |
| 1674 |
fixed_features=False, |
| 1675 |
loss_fun="square_loss", |
| 1676 |
max_iter=10, |
| 1677 |
tol=1e-3, |
| 1678 |
stop_criterion="loss", |
| 1679 |
G0="random", |
| 1680 |
warmstartT=True, |
| 1681 |
random_state=12345, |
| 1682 |
log=True, |
| 1683 |
verbose=True, |
| 1684 |
init_C=Cb, |
| 1685 |
init_X=Xb, |
| 1686 |
) |
| 1687 |
np.testing.assert_allclose(Xb, Xb_) |
| 1688 |
np.testing.assert_allclose(Cb, Cb_) |
| 1689 |
|
| 1690 |
|
| 1691 |
if sklearn_import: |
| 1692 |
Xb_, Cb_, logb_ = ot.gromov.semirelaxed_fgw_barycenters( |
| 1693 |
n_samples, |
| 1694 |
[ysb, ytb], |
| 1695 |
[C1b, C2b], |
| 1696 |
[p1b, p2b], |
| 1697 |
[0.5, 0.5], |
| 1698 |
0.5, |
| 1699 |
fixed_structure=False, |
| 1700 |
fixed_features=False, |
| 1701 |
loss_fun="square_loss", |
| 1702 |
max_iter=10, |
| 1703 |
tol=1e-3, |
| 1704 |
stop_criterion="loss", |
| 1705 |
G0="kmeans", |
| 1706 |
warmstartT=True, |
| 1707 |
random_state=12345, |
| 1708 |
log=True, |
| 1709 |
verbose=True, |
| 1710 |
init_C=Cb, |
| 1711 |
init_X=Xb, |
| 1712 |
) |
| 1713 |
|
| 1714 |
X, C, log = ot.gromov.semirelaxed_fgw_barycenters( |
| 1715 |
ns, |
| 1716 |
[ys, yt], |
| 1717 |
[C1, C2], |
| 1718 |
[p1, p2], |
| 1719 |
[0.5, 0.5], |
| 1720 |
0.5, |
| 1721 |
fixed_structure=False, |
| 1722 |
fixed_features=False, |
| 1723 |
loss_fun="square_loss", |
| 1724 |
max_iter=10, |
| 1725 |
tol=1e-3, |
| 1726 |
stop_criterion="loss", |
| 1727 |
G0="kmeans", |
| 1728 |
warmstartT=True, |
| 1729 |
random_state=12345, |
| 1730 |
log=True, |
| 1731 |
verbose=True, |
| 1732 |
) |
| 1733 |
Xb, Cb, logb = ot.gromov.semirelaxed_fgw_barycenters( |
| 1734 |
ns, |
| 1735 |
[ysb, ytb], |
| 1736 |
[C1b, C2b], |
| 1737 |
[p1b, p2b], |
| 1738 |
[0.5, 0.5], |
| 1739 |
0.5, |
| 1740 |
fixed_structure=False, |
| 1741 |
fixed_features=False, |
| 1742 |
loss_fun="square_loss", |
| 1743 |
max_iter=10, |
| 1744 |
tol=1e-3, |
| 1745 |
stop_criterion="loss", |
| 1746 |
G0="kmeans", |
| 1747 |
warmstartT=True, |
| 1748 |
random_state=12345, |
| 1749 |
log=True, |
| 1750 |
verbose=True, |
| 1751 |
) |
| 1752 |
np.testing.assert_allclose(X, nx.to_numpy(Xb)) |
| 1753 |
np.testing.assert_allclose(C, nx.to_numpy(Cb)) |
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
X, C = ot.gromov.semirelaxed_fgw_barycenters( |
| 1758 |
n_samples, |
| 1759 |
[ys], |
| 1760 |
[C1], |
| 1761 |
[p1], |
| 1762 |
None, |
| 1763 |
0.5, |
| 1764 |
fixed_structure=False, |
| 1765 |
fixed_features=False, |
| 1766 |
loss_fun="square_loss", |
| 1767 |
max_iter=2, |
| 1768 |
tol=1e-3, |
| 1769 |
stop_criterion=stop_criterion, |
| 1770 |
warmstartT=True, |
| 1771 |
random_state=12345, |
| 1772 |
log=False, |
| 1773 |
verbose=False, |
| 1774 |
) |
| 1775 |
Xb, Cb = ot.gromov.semirelaxed_fgw_barycenters( |
| 1776 |
n_samples, |
| 1777 |
[ysb], |
| 1778 |
[C1b], |
| 1779 |
[p1b], |
| 1780 |
[1.0], |
| 1781 |
0.5, |
| 1782 |
fixed_structure=False, |
| 1783 |
fixed_features=False, |
| 1784 |
loss_fun="square_loss", |
| 1785 |
max_iter=2, |
| 1786 |
tol=1e-3, |
| 1787 |
stop_criterion=stop_criterion, |
| 1788 |
warmstartT=True, |
| 1789 |
random_state=12345, |
| 1790 |
log=False, |
| 1791 |
verbose=False, |
| 1792 |
) |
| 1793 |
|
| 1794 |
np.testing.assert_allclose(C, Cb, atol=1e-06) |
| 1795 |
np.testing.assert_allclose(X, Xb, atol=1e-06) |