Debian Codegraph @

test/gromov/test_bregman.py

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