Debian Codegraph @

test/gromov/test_fugw.py

raw symbols SCIP
1 """Tests for module Fused Unbalanced Gromov-Wasserstein"""
2
3 # Author: Quang Huy Tran <[email protected]>
4 #
5 # License: MIT License
6
7 import itertools
8 import numpy as np
9 import ot
10 import pytest
11 from ot.gromov._unbalanced import (
12 fused_unbalanced_gromov_wasserstein,
13 fused_unbalanced_gromov_wasserstein2,
14 fused_unbalanced_across_spaces_divergence,
15 )
16
17
18 @pytest.skip_backend("jax", reason="test very slow with jax backend")
19 @pytest.skip_backend("tf", reason="test very slow with tensorflow backend")
20 @pytest.mark.parametrize(
21 "unbalanced_solver, divergence", itertools.product(["mm", "lbfgsb"], ["kl", "l2"])
22 )
23 def test_sanity(nx, unbalanced_solver, divergence):
24 n_samples = 5 # nb samples
25
26 mu_s = np.array([0, 0])
27 cov_s = np.array([[1, 0], [0, 1]])
28
29 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
30 xt = xs[::-1].copy()
31
32 p = ot.unif(n_samples)
33 q = ot.unif(n_samples)
34 G0 = p[:, None] * q[None, :]
35
36 # linear part
37 M_samp = np.ones((n_samples, n_samples))
38 np.fill_diagonal(np.fliplr(M_samp), 0)
39 M_samp_nx = nx.from_numpy(M_samp)
40
41 C1 = ot.dist(xs, xs)
42 C2 = ot.dist(xt, xt)
43 C1 /= C1.max()
44 C2 /= C2.max()
45
46 C1b, C2b, pb, qb, G0b = nx.from_numpy(C1, C2, p, q, G0)
47
48 reg_m = (100, 50)
49 eps = 0
50 alpha = 0.5
51 max_iter_ot = 100
52 max_iter = 100
53 tol = 1e-7
54 tol_ot = 1e-7
55
56 # test couplings
57 anti_id_sample = np.flipud(np.eye(n_samples, n_samples)) / n_samples
58
59 pi_sample, pi_feature = fused_unbalanced_gromov_wasserstein(
60 C1,
61 C2,
62 wx=p,
63 wy=q,
64 reg_marginals=reg_m,
65 epsilon=eps,
66 divergence=divergence,
67 unbalanced_solver=unbalanced_solver,
68 alpha=alpha,
69 M=M_samp,
70 init_duals=None,
71 init_pi=G0,
72 max_iter=max_iter,
73 tol=tol,
74 max_iter_ot=max_iter_ot,
75 tol_ot=tol_ot,
76 log=False,
77 verbose=False,
78 )
79
80 pi_sample_nx, pi_feature_nx = fused_unbalanced_gromov_wasserstein(
81 C1b,
82 C2b,
83 wx=pb,
84 wy=qb,
85 reg_marginals=reg_m,
86 epsilon=eps,
87 divergence=divergence,
88 unbalanced_solver=unbalanced_solver,
89 alpha=alpha,
90 M=M_samp_nx,
91 init_duals=None,
92 init_pi=G0b,
93 max_iter=max_iter,
94 tol=tol,
95 max_iter_ot=max_iter_ot,
96 tol_ot=tol_ot,
97 log=False,
98 verbose=False,
99 )
100 pi_sample_nx = nx.to_numpy(pi_sample_nx)
101 pi_feature_nx = nx.to_numpy(pi_feature_nx)
102
103 np.testing.assert_allclose(pi_sample, anti_id_sample, atol=1e-03)
104 np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-06)
105 np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06)
106
107 # test divergence
108
109 fugw = fused_unbalanced_gromov_wasserstein2(
110 C1,
111 C2,
112 wx=p,
113 wy=q,
114 reg_marginals=reg_m,
115 epsilon=eps,
116 divergence=divergence,
117 unbalanced_solver=unbalanced_solver,
118 alpha=alpha,
119 M=M_samp,
120 init_duals=None,
121 init_pi=G0,
122 max_iter=max_iter,
123 tol=tol,
124 max_iter_ot=max_iter_ot,
125 tol_ot=tol_ot,
126 log=False,
127 verbose=False,
128 )
129
130 fugw_nx = fused_unbalanced_gromov_wasserstein2(
131 C1b,
132 C2b,
133 wx=pb,
134 wy=qb,
135 reg_marginals=reg_m,
136 epsilon=eps,
137 divergence=divergence,
138 unbalanced_solver=unbalanced_solver,
139 alpha=alpha,
140 M=M_samp_nx,
141 init_duals=None,
142 init_pi=G0b,
143 max_iter=max_iter,
144 tol=tol,
145 max_iter_ot=max_iter_ot,
146 tol_ot=tol_ot,
147 log=False,
148 verbose=False,
149 )
150
151 fugw_nx = nx.to_numpy(fugw_nx)
152 np.testing.assert_allclose(fugw, fugw_nx, atol=1e-08)
153 np.testing.assert_allclose(fugw, 0, atol=1e-02)
154
155
156 @pytest.skip_backend("jax", reason="test very slow with jax backend")
157 @pytest.skip_backend("tf", reason="test very slow with tensorflow backend")
158 @pytest.mark.parametrize(
159 "unbalanced_solver, divergence, eps",
160 itertools.product(
161 ["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1e-2]
162 ),
163 )
164 def test_init_plans(nx, unbalanced_solver, divergence, eps):
165 n_samples = 5 # nb samples
166
167 mu_s = np.array([0, 0])
168 cov_s = np.array([[1, 0], [0, 1]])
169
170 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
171 xt = xs[::-1].copy()
172
173 p = ot.unif(n_samples)
174 q = ot.unif(n_samples)
175 G0 = p[:, None] * q[None, :]
176
177 # linear part
178 M_samp = np.ones((n_samples, n_samples))
179 np.fill_diagonal(np.fliplr(M_samp), 0)
180
181 C1 = ot.dist(xs, xs)
182 C2 = ot.dist(xt, xt)
183 C1 /= C1.max()
184 C2 /= C2.max()
185
186 reg_m = (100, 50)
187 alpha = 0.5
188 max_iter_ot = 5
189 max_iter = 5
190 tol = 1e-5
191 tol_ot = 1e-5
192
193 pi_sample, pi_feature = fused_unbalanced_gromov_wasserstein(
194 C1,
195 C2,
196 wx=p,
197 wy=q,
198 reg_marginals=reg_m,
199 epsilon=eps,
200 divergence=divergence,
201 unbalanced_solver=unbalanced_solver,
202 alpha=alpha,
203 M=M_samp,
204 init_duals=None,
205 init_pi=G0,
206 max_iter=max_iter,
207 tol=tol,
208 max_iter_ot=max_iter_ot,
209 tol_ot=tol_ot,
210 log=False,
211 verbose=False,
212 )
213
214 pi_sample_nx, pi_feature_nx = fused_unbalanced_gromov_wasserstein(
215 C1,
216 C2,
217 wx=p,
218 wy=q,
219 reg_marginals=reg_m,
220 epsilon=eps,
221 divergence=divergence,
222 unbalanced_solver=unbalanced_solver,
223 alpha=alpha,
224 M=M_samp,
225 init_duals=None,
226 init_pi=None,
227 max_iter=max_iter,
228 tol=tol,
229 max_iter_ot=max_iter_ot,
230 tol_ot=tol_ot,
231 log=False,
232 verbose=False,
233 )
234 pi_sample_nx = nx.to_numpy(pi_sample_nx)
235 pi_feature_nx = nx.to_numpy(pi_feature_nx)
236
237 np.testing.assert_allclose(pi_sample, pi_sample_nx, atol=1e-06)
238 np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06)
239
240 # test divergence
241
242 fugw = fused_unbalanced_gromov_wasserstein2(
243 C1,
244 C2,
245 wx=p,
246 wy=q,
247 reg_marginals=reg_m,
248 epsilon=eps,
249 divergence=divergence,
250 unbalanced_solver=unbalanced_solver,
251 alpha=alpha,
252 M=M_samp,
253 init_duals=None,
254 init_pi=G0,
255 max_iter=max_iter,
256 tol=tol,
257 max_iter_ot=max_iter_ot,
258 tol_ot=tol_ot,
259 log=False,
260 verbose=False,
261 )
262
263 fugw_nx = fused_unbalanced_gromov_wasserstein2(
264 C1,
265 C2,
266 wx=p,
267 wy=q,
268 reg_marginals=reg_m,
269 epsilon=eps,
270 divergence=divergence,
271 unbalanced_solver=unbalanced_solver,
272 alpha=alpha,
273 M=M_samp,
274 init_duals=None,
275 init_pi=None,
276 max_iter=max_iter,
277 tol=tol,
278 max_iter_ot=max_iter_ot,
279 tol_ot=tol_ot,
280 log=False,
281 verbose=False,
282 )
283
284 fugw_nx = nx.to_numpy(fugw_nx)
285 np.testing.assert_allclose(fugw, fugw_nx, atol=1e-08)
286
287
288 @pytest.skip_backend("jax", reason="test very slow with jax backend")
289 @pytest.skip_backend("tf", reason="test very slow with tensorflow backend")
290 @pytest.mark.parametrize(
291 "unbalanced_solver, divergence, eps",
292 itertools.product(
293 ["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1e-2]
294 ),
295 )
296 def test_init_duals(nx, unbalanced_solver, divergence, eps):
297 n_samples = 5 # nb samples
298
299 mu_s = np.array([0, 0])
300 cov_s = np.array([[1, 0], [0, 1]])
301
302 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
303 xt = xs[::-1].copy()
304
305 p = ot.unif(n_samples)
306 q = ot.unif(n_samples)
307 dual1, dual2 = nx.from_numpy(np.zeros_like(p), np.zeros_like(q))
308 init_duals = (dual1, dual2)
309
310 # linear part
311 M_samp = np.ones((n_samples, n_samples))
312 np.fill_diagonal(np.fliplr(M_samp), 0)
313
314 C1 = ot.dist(xs, xs)
315 C2 = ot.dist(xt, xt)
316 C1 /= C1.max()
317 C2 /= C2.max()
318
319 C1, C2, p, q, M_samp = nx.from_numpy(C1, C2, p, q, M_samp)
320
321 reg_m = (100, 50)
322 alpha = 0.5
323 max_iter_ot = 5
324 max_iter = 5
325 tol = 1e-5
326 tol_ot = 1e-5
327
328 pi_sample, pi_feature = fused_unbalanced_gromov_wasserstein(
329 C1,
330 C2,
331 wx=p,
332 wy=q,
333 reg_marginals=reg_m,
334 epsilon=eps,
335 divergence=divergence,
336 unbalanced_solver=unbalanced_solver,
337 alpha=alpha,
338 M=M_samp,
339 init_duals=None,
340 init_pi=None,
341 max_iter=max_iter,
342 tol=tol,
343 max_iter_ot=max_iter_ot,
344 tol_ot=tol_ot,
345 log=False,
346 verbose=False,
347 )
348
349 pi_sample_nx, pi_feature_nx = fused_unbalanced_gromov_wasserstein(
350 C1,
351 C2,
352 wx=p,
353 wy=q,
354 reg_marginals=reg_m,
355 epsilon=eps,
356 divergence=divergence,
357 unbalanced_solver=unbalanced_solver,
358 alpha=alpha,
359 M=M_samp,
360 init_duals=init_duals,
361 init_pi=None,
362 max_iter=max_iter,
363 tol=tol,
364 max_iter_ot=max_iter_ot,
365 tol_ot=tol_ot,
366 log=False,
367 verbose=False,
368 )
369 pi_sample_nx = nx.to_numpy(pi_sample_nx)
370 pi_feature_nx = nx.to_numpy(pi_feature_nx)
371
372 np.testing.assert_allclose(pi_sample, pi_sample_nx, atol=1e-06)
373 np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06)
374
375 # test divergence
376 fugw = fused_unbalanced_gromov_wasserstein2(
377 C1,
378 C2,
379 wx=p,
380 wy=q,
381 reg_marginals=reg_m,
382 epsilon=eps,
383 divergence=divergence,
384 unbalanced_solver=unbalanced_solver,
385 alpha=alpha,
386 M=M_samp,
387 init_duals=None,
388 init_pi=None,
389 max_iter=max_iter,
390 tol=tol,
391 max_iter_ot=max_iter_ot,
392 tol_ot=tol_ot,
393 log=False,
394 verbose=False,
395 )
396
397 fugw_nx = fused_unbalanced_gromov_wasserstein2(
398 C1,
399 C2,
400 wx=p,
401 wy=q,
402 reg_marginals=reg_m,
403 epsilon=eps,
404 divergence=divergence,
405 unbalanced_solver=unbalanced_solver,
406 alpha=alpha,
407 M=M_samp,
408 init_duals=init_duals,
409 init_pi=None,
410 max_iter=max_iter,
411 tol=tol,
412 max_iter_ot=max_iter_ot,
413 tol_ot=tol_ot,
414 log=False,
415 verbose=False,
416 )
417
418 fugw_nx = nx.to_numpy(fugw_nx)
419 np.testing.assert_allclose(fugw, fugw_nx, atol=1e-08)
420
421
422 @pytest.skip_backend("jax", reason="test very slow with jax backend")
423 @pytest.skip_backend("tf", reason="test very slow with tensorflow backend")
424 @pytest.mark.parametrize(
425 "unbalanced_solver, divergence, eps",
426 itertools.product(
427 ["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1e-2]
428 ),
429 )
430 def test_reg_marginals(nx, unbalanced_solver, divergence, eps):
431 n_samples = 5 # nb samples
432
433 mu_s = np.array([0, 0])
434 cov_s = np.array([[1, 0], [0, 1]])
435
436 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
437 xt = xs[::-1].copy()
438
439 p = ot.unif(n_samples)
440 q = ot.unif(n_samples)
441 G0 = p[:, None] * q[None, :]
442
443 # linear part
444 M_samp = np.ones((n_samples, n_samples))
445 np.fill_diagonal(np.fliplr(M_samp), 0)
446
447 C1 = ot.dist(xs, xs)
448 C2 = ot.dist(xt, xt)
449 C1 /= C1.max()
450 C2 /= C2.max()
451
452 alpha = 0.5
453 max_iter_ot = 5
454 max_iter = 5
455 tol = 1e-5
456 tol_ot = 1e-5
457
458 reg_m = 100
459 full_list_reg_m = [reg_m, reg_m]
460 full_tuple_reg_m = (reg_m, reg_m)
461 tuple_reg_m, list_reg_m = (reg_m), [reg_m]
462
463 list_options = [full_tuple_reg_m, tuple_reg_m, full_list_reg_m, list_reg_m]
464
465 pi_sample, pi_feature = fused_unbalanced_gromov_wasserstein(
466 C1,
467 C2,
468 wx=p,
469 wy=q,
470 reg_marginals=reg_m,
471 epsilon=eps,
472 divergence=divergence,
473 unbalanced_solver=unbalanced_solver,
474 alpha=alpha,
475 M=M_samp,
476 init_duals=None,
477 init_pi=G0,
478 max_iter=max_iter,
479 tol=tol,
480 max_iter_ot=max_iter_ot,
481 tol_ot=tol_ot,
482 log=False,
483 verbose=False,
484 )
485
486 fugw = fused_unbalanced_gromov_wasserstein2(
487 C1,
488 C2,
489 wx=p,
490 wy=q,
491 reg_marginals=reg_m,
492 epsilon=eps,
493 divergence=divergence,
494 unbalanced_solver=unbalanced_solver,
495 alpha=alpha,
496 M=M_samp,
497 init_duals=None,
498 init_pi=G0,
499 max_iter=max_iter,
500 tol=tol,
501 max_iter_ot=max_iter_ot,
502 tol_ot=tol_ot,
503 log=False,
504 verbose=False,
505 )
506
507 for opt in list_options:
508 pi_sample_nx, pi_feature_nx = fused_unbalanced_gromov_wasserstein(
509 C1,
510 C2,
511 wx=p,
512 wy=q,
513 reg_marginals=opt,
514 epsilon=eps,
515 divergence=divergence,
516 unbalanced_solver=unbalanced_solver,
517 alpha=alpha,
518 M=M_samp,
519 init_duals=None,
520 init_pi=None,
521 max_iter=max_iter,
522 tol=tol,
523 max_iter_ot=max_iter_ot,
524 tol_ot=tol_ot,
525 log=False,
526 verbose=False,
527 )
528 pi_sample_nx = nx.to_numpy(pi_sample_nx)
529 pi_feature_nx = nx.to_numpy(pi_feature_nx)
530
531 np.testing.assert_allclose(pi_sample, pi_sample_nx, atol=1e-06)
532 np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06)
533
534 # test divergence
535 fugw_nx = fused_unbalanced_gromov_wasserstein2(
536 C1,
537 C2,
538 wx=p,
539 wy=q,
540 reg_marginals=opt,
541 epsilon=eps,
542 divergence=divergence,
543 unbalanced_solver=unbalanced_solver,
544 alpha=alpha,
545 M=M_samp,
546 init_duals=None,
547 init_pi=None,
548 max_iter=max_iter,
549 tol=tol,
550 max_iter_ot=max_iter_ot,
551 tol_ot=tol_ot,
552 log=False,
553 verbose=False,
554 )
555
556 fugw_nx = nx.to_numpy(fugw_nx)
557 np.testing.assert_allclose(fugw, fugw_nx, atol=1e-08)
558
559
560 @pytest.skip_backend("jax", reason="test very slow with jax backend")
561 @pytest.skip_backend("tf", reason="test very slow with tensorflow backend")
562 @pytest.mark.parametrize(
563 "unbalanced_solver, divergence, eps",
564 itertools.product(
565 ["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1e-2]
566 ),
567 )
568 def test_log(nx, unbalanced_solver, divergence, eps):
569 n_samples = 5 # nb samples
570
571 mu_s = np.array([0, 0])
572 cov_s = np.array([[1, 0], [0, 1]])
573
574 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
575 xt = xs[::-1].copy()
576
577 p = ot.unif(n_samples)
578 q = ot.unif(n_samples)
579
580 # linear part
581 M_samp = np.ones((n_samples, n_samples))
582 np.fill_diagonal(np.fliplr(M_samp), 0)
583
584 C1 = ot.dist(xs, xs)
585 C2 = ot.dist(xt, xt)
586 C1 /= C1.max()
587 C2 /= C2.max()
588
589 reg_m = (100, 50)
590 alpha = 0.5
591 max_iter_ot = 5
592 max_iter = 5
593 tol = 1e-5
594 tol_ot = 1e-5
595
596 pi_sample, pi_feature = fused_unbalanced_gromov_wasserstein(
597 C1,
598 C2,
599 wx=p,
600 wy=q,
601 reg_marginals=reg_m,
602 epsilon=eps,
603 divergence=divergence,
604 unbalanced_solver=unbalanced_solver,
605 alpha=alpha,
606 M=M_samp,
607 init_duals=None,
608 init_pi=None,
609 max_iter=max_iter,
610 tol=tol,
611 max_iter_ot=max_iter_ot,
612 tol_ot=tol_ot,
613 log=False,
614 verbose=False,
615 )
616
617 pi_sample_nx, pi_feature_nx, log = fused_unbalanced_gromov_wasserstein(
618 C1,
619 C2,
620 wx=p,
621 wy=q,
622 reg_marginals=reg_m,
623 epsilon=eps,
624 divergence=divergence,
625 unbalanced_solver=unbalanced_solver,
626 alpha=alpha,
627 M=M_samp,
628 init_duals=None,
629 init_pi=None,
630 max_iter=max_iter,
631 tol=tol,
632 max_iter_ot=max_iter_ot,
633 tol_ot=tol_ot,
634 log=True,
635 verbose=False,
636 )
637 pi_sample_nx = nx.to_numpy(pi_sample_nx)
638 pi_feature_nx = nx.to_numpy(pi_feature_nx)
639
640 np.testing.assert_allclose(pi_sample, pi_sample_nx, atol=1e-06)
641 np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06)
642
643 # test divergence
644
645 fugw = fused_unbalanced_gromov_wasserstein2(
646 C1,
647 C2,
648 wx=p,
649 wy=q,
650 reg_marginals=reg_m,
651 epsilon=eps,
652 divergence=divergence,
653 unbalanced_solver=unbalanced_solver,
654 alpha=alpha,
655 M=M_samp,
656 init_duals=None,
657 init_pi=None,
658 max_iter=max_iter,
659 tol=tol,
660 max_iter_ot=max_iter_ot,
661 tol_ot=tol_ot,
662 log=False,
663 verbose=False,
664 )
665
666 fugw_nx, log = fused_unbalanced_gromov_wasserstein2(
667 C1,
668 C2,
669 wx=p,
670 wy=q,
671 reg_marginals=reg_m,
672 epsilon=eps,
673 divergence=divergence,
674 unbalanced_solver=unbalanced_solver,
675 alpha=alpha,
676 M=M_samp,
677 init_duals=None,
678 init_pi=None,
679 max_iter=max_iter,
680 tol=tol,
681 max_iter_ot=max_iter_ot,
682 tol_ot=tol_ot,
683 log=True,
684 verbose=False,
685 )
686
687 fugw_nx = nx.to_numpy(fugw_nx)
688 np.testing.assert_allclose(fugw, fugw_nx, atol=1e-08)
689
690
691 @pytest.skip_backend("jax", reason="test very slow with jax backend")
692 @pytest.skip_backend("tf", reason="test very slow with tensorflow backend")
693 @pytest.mark.parametrize(
694 "unbalanced_solver, divergence, eps",
695 itertools.product(
696 ["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1e-2]
697 ),
698 )
699 def test_marginals(nx, unbalanced_solver, divergence, eps):
700 n_samples = 5 # nb samples
701
702 mu_s = np.array([0, 0])
703 cov_s = np.array([[1, 0], [0, 1]])
704
705 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
706 xt = xs[::-1].copy()
707
708 p = ot.unif(n_samples)
709 q = ot.unif(n_samples)
710
711 # linear part
712 M_samp = np.ones((n_samples, n_samples))
713 np.fill_diagonal(np.fliplr(M_samp), 0)
714
715 C1 = ot.dist(xs, xs)
716 C2 = ot.dist(xt, xt)
717 C1 /= C1.max()
718 C2 /= C2.max()
719
720 reg_m = (100, 50)
721 alpha = 0.5
722 max_iter_ot = 5
723 max_iter = 5
724 tol = 1e-5
725 tol_ot = 1e-5
726
727 pi_sample, pi_feature = fused_unbalanced_gromov_wasserstein(
728 C1,
729 C2,
730 wx=p,
731 wy=q,
732 reg_marginals=reg_m,
733 epsilon=eps,
734 divergence=divergence,
735 unbalanced_solver=unbalanced_solver,
736 alpha=alpha,
737 M=M_samp,
738 init_duals=None,
739 init_pi=None,
740 max_iter=max_iter,
741 tol=tol,
742 max_iter_ot=max_iter_ot,
743 tol_ot=tol_ot,
744 log=False,
745 verbose=False,
746 )
747
748 pi_sample_nx, pi_feature_nx = fused_unbalanced_gromov_wasserstein(
749 C1,
750 C2,
751 wx=None,
752 wy=None,
753 reg_marginals=reg_m,
754 epsilon=eps,
755 divergence=divergence,
756 unbalanced_solver=unbalanced_solver,
757 alpha=alpha,
758 M=M_samp,
759 init_duals=None,
760 init_pi=None,
761 max_iter=max_iter,
762 tol=tol,
763 max_iter_ot=max_iter_ot,
764 tol_ot=tol_ot,
765 log=False,
766 verbose=False,
767 )
768 pi_sample_nx = nx.to_numpy(pi_sample_nx)
769 pi_feature_nx = nx.to_numpy(pi_feature_nx)
770
771 np.testing.assert_allclose(pi_sample, pi_sample_nx, atol=1e-06)
772 np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06)
773
774 # test divergence
775
776 fugw = fused_unbalanced_gromov_wasserstein2(
777 C1,
778 C2,
779 wx=p,
780 wy=q,
781 reg_marginals=reg_m,
782 epsilon=eps,
783 divergence=divergence,
784 unbalanced_solver=unbalanced_solver,
785 alpha=alpha,
786 M=M_samp,
787 init_duals=None,
788 init_pi=None,
789 max_iter=max_iter,
790 tol=tol,
791 max_iter_ot=max_iter_ot,
792 tol_ot=tol_ot,
793 log=False,
794 verbose=False,
795 )
796
797 fugw_nx = fused_unbalanced_gromov_wasserstein2(
798 C1,
799 C2,
800 wx=None,
801 wy=None,
802 reg_marginals=reg_m,
803 epsilon=eps,
804 divergence=divergence,
805 unbalanced_solver=unbalanced_solver,
806 alpha=alpha,
807 M=M_samp,
808 init_duals=None,
809 init_pi=None,
810 max_iter=max_iter,
811 tol=tol,
812 max_iter_ot=max_iter_ot,
813 tol_ot=tol_ot,
814 log=False,
815 verbose=False,
816 )
817
818 fugw_nx = nx.to_numpy(fugw_nx)
819 np.testing.assert_allclose(fugw, fugw_nx, atol=1e-08)
820
821
822 @pytest.skip_backend("jax", reason="test very slow with jax backend")
823 @pytest.skip_backend("tf", reason="test very slow with tensorflow backend")
824 def test_raise_value_error(nx):
825 n_samples = 5 # nb samples
826
827 mu_s = np.array([0, 0])
828 cov_s = np.array([[1, 0], [0, 1]])
829
830 p = ot.unif(n_samples)
831 q = ot.unif(n_samples)
832 G0 = p[:, None] * q[None, :]
833
834 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
835 xt = xs[::-1].copy()
836
837 C1 = ot.dist(xs, xs)
838 C2 = ot.dist(xt, xt)
839 C1 /= C1.max()
840 C2 /= C2.max()
841
842 C1b, C2b, pb, qb, G0b = nx.from_numpy(C1, C2, p, q, G0)
843
844 eps = 1e-2
845 reg_m = (10, 100)
846 max_iter_ot = 5
847 max_iter = 5
848 tol = 1e-6
849 tol_ot = 1e-6
850
851 # raise error of divergence
852 def fugw_div(divergence):
853 return fused_unbalanced_gromov_wasserstein(
854 C1,
855 C2,
856 wx=p,
857 wy=q,
858 reg_marginals=reg_m,
859 epsilon=eps,
860 divergence=divergence,
861 unbalanced_solver="mm",
862 alpha=0,
863 M=None,
864 init_duals=None,
865 init_pi=G0,
866 max_iter=max_iter,
867 tol=tol,
868 max_iter_ot=max_iter_ot,
869 tol_ot=tol_ot,
870 log=False,
871 verbose=False,
872 )
873
874 def fugw_div_nx(divergence):
875 return fused_unbalanced_gromov_wasserstein(
876 C1b,
877 C2b,
878 wx=pb,
879 wy=qb,
880 reg_marginals=reg_m,
881 epsilon=eps,
882 divergence=divergence,
883 unbalanced_solver="mm",
884 alpha=0,
885 M=None,
886 init_duals=None,
887 init_pi=G0b,
888 max_iter=max_iter,
889 tol=tol,
890 max_iter_ot=max_iter_ot,
891 tol_ot=tol_ot,
892 log=False,
893 verbose=False,
894 )
895
896 np.testing.assert_raises(NotImplementedError, fugw_div, "div_not_existed")
897 np.testing.assert_raises(NotImplementedError, fugw_div_nx, "div_not_existed")
898
899 # raise error of solver
900 def fugw_solver(unbalanced_solver):
901 return fused_unbalanced_gromov_wasserstein(
902 C1,
903 C2,
904 wx=p,
905 wy=q,
906 reg_marginals=reg_m,
907 epsilon=eps,
908 divergence="kl",
909 unbalanced_solver=unbalanced_solver,
910 alpha=0,
911 M=None,
912 init_duals=None,
913 init_pi=G0,
914 max_iter=max_iter,
915 tol=tol,
916 max_iter_ot=max_iter_ot,
917 tol_ot=tol_ot,
918 log=False,
919 verbose=False,
920 )
921
922 def fugw_solver_nx(unbalanced_solver):
923 return fused_unbalanced_gromov_wasserstein(
924 C1b,
925 C2b,
926 wx=pb,
927 wy=qb,
928 reg_marginals=reg_m,
929 epsilon=eps,
930 divergence="kl",
931 unbalanced_solver=unbalanced_solver,
932 alpha=0,
933 M=None,
934 init_duals=None,
935 init_pi=G0b,
936 max_iter=max_iter,
937 tol=tol,
938 max_iter_ot=max_iter_ot,
939 tol_ot=tol_ot,
940 log=False,
941 verbose=False,
942 )
943
944 np.testing.assert_raises(NotImplementedError, fugw_solver, "solver_not_existed")
945 np.testing.assert_raises(NotImplementedError, fugw_solver_nx, "solver_not_existed")
946
947
948 @pytest.mark.parametrize(
949 "unbalanced_solver, divergence, eps",
950 itertools.product(
951 ["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1e-2]
952 ),
953 )
954 def test_fused_unbalanced_across_spaces_divergence_wrong_reg_type(
955 nx, unbalanced_solver, divergence, eps
956 ):
957 n = 100
958 rng = np.random.RandomState(42)
959 x = rng.randn(n, 2)
960 rng = np.random.RandomState(75)
961 y = rng.randn(n, 2)
962 x, y = nx.from_numpy(x), nx.from_numpy(y)
963
964 reg_m = 100
965
966 def reg_type(reg_type):
967 return fused_unbalanced_across_spaces_divergence(
968 X=x,
969 Y=y,
970 reg_marginals=reg_m,
971 epsilon=eps,
972 reg_type=reg_type,
973 divergence=divergence,
974 unbalanced_solver=unbalanced_solver,
975 )
976
977 np.testing.assert_raises(NotImplementedError, reg_type, "reg_type_not_existed")
978
979
980 @pytest.skip_backend("jax", reason="test very slow with jax backend")
981 @pytest.skip_backend("tf", reason="test very slow with tensorflow backend")
982 @pytest.mark.parametrize(
983 "unbalanced_solver, divergence, eps, reg_type",
984 itertools.product(
985 ["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"],
986 ["kl", "l2"],
987 [0, 1e-2],
988 ["independent", "joint"],
989 ),
990 )
991 def test_fused_unbalanced_across_spaces_divergence_log(
992 nx, unbalanced_solver, divergence, eps, reg_type
993 ):
994 n_samples = 5 # nb samples
995
996 mu_s = np.array([0, 0])
997 cov_s = np.array([[1, 0], [0, 1]])
998
999 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
1000 xt = xs[::-1].copy()
1001
1002 px_s, px_f = ot.unif(n_samples), ot.unif(2)
1003 py_s, py_f = ot.unif(n_samples), ot.unif(2)
1004
1005 xs_nx, xt_nx = nx.from_numpy(xs, xt)
1006 px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f)
1007
1008 # linear part
1009 M_samp = np.ones((n_samples, n_samples))
1010 np.fill_diagonal(np.fliplr(M_samp), 0)
1011 M_feat = np.ones((2, 2))
1012 np.fill_diagonal(M_feat, 0)
1013 M_samp_nx, M_feat_nx = nx.from_numpy(M_samp, M_feat)
1014
1015 reg_m = (10, 5)
1016 alpha = (0.1, 0.2)
1017 max_iter_ot = 5
1018 max_iter = 5
1019 tol = 1e-7
1020 tol_ot = 1e-7
1021
1022 # test couplings
1023 pi_sample, pi_feature = fused_unbalanced_across_spaces_divergence(
1024 X=xs_nx,
1025 Y=xt_nx,
1026 wx_samp=px_s_nx,
1027 wx_feat=px_f_nx,
1028 wy_samp=py_s_nx,
1029 wy_feat=py_f_nx,
1030 reg_marginals=reg_m,
1031 epsilon=eps,
1032 reg_type=reg_type,
1033 divergence=divergence,
1034 unbalanced_solver=unbalanced_solver,
1035 alpha=alpha,
1036 M_samp=M_samp_nx,
1037 M_feat=M_feat_nx,
1038 init_pi=None,
1039 init_duals=None,
1040 max_iter=max_iter,
1041 tol=tol,
1042 max_iter_ot=max_iter_ot,
1043 tol_ot=tol_ot,
1044 log=False,
1045 verbose=False,
1046 )
1047
1048 pi_sample_nx, pi_feature_nx, log = fused_unbalanced_across_spaces_divergence(
1049 X=xs_nx,
1050 Y=xt_nx,
1051 wx_samp=px_s_nx,
1052 wx_feat=px_f_nx,
1053 wy_samp=py_s_nx,
1054 wy_feat=py_f_nx,
1055 reg_marginals=reg_m,
1056 epsilon=eps,
1057 reg_type=reg_type,
1058 divergence=divergence,
1059 unbalanced_solver=unbalanced_solver,
1060 alpha=alpha,
1061 M_samp=M_samp_nx,
1062 M_feat=M_feat_nx,
1063 init_pi=None,
1064 init_duals=None,
1065 max_iter=max_iter,
1066 tol=tol,
1067 max_iter_ot=max_iter_ot,
1068 tol_ot=tol_ot,
1069 log=True,
1070 verbose=False,
1071 )
1072
1073 np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-06)
1074 np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06)
1075
1076
1077 @pytest.skip_backend("jax", reason="test very slow with jax backend")
1078 @pytest.skip_backend("tf", reason="test very slow with tensorflow backend")
1079 @pytest.mark.parametrize("reg_type", ["independent", "joint"])
1080 def test_fused_unbalanced_across_spaces_divergence_warning(nx, reg_type):
1081 n_samples = 5 # nb samples
1082
1083 mu_s = np.array([0, 0])
1084 cov_s = np.array([[1, 0], [0, 1]])
1085
1086 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
1087 xt = xs[::-1].copy()
1088
1089 px_s, px_f = ot.unif(n_samples), ot.unif(2)
1090 py_s, py_f = ot.unif(n_samples), ot.unif(2)
1091
1092 xs_nx, xt_nx = nx.from_numpy(xs, xt)
1093 px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f)
1094
1095 unbalanced_solver = "mm"
1096 divergence = "kl"
1097
1098 # linear part
1099 M_samp = np.ones((n_samples, n_samples))
1100 np.fill_diagonal(np.fliplr(M_samp), 0)
1101 M_feat = np.ones((2, 2))
1102 np.fill_diagonal(M_feat, 0)
1103 M_samp_nx, M_feat_nx = nx.from_numpy(M_samp, M_feat)
1104
1105 reg_m = (1e6, 1e6)
1106 eps = 1e-2
1107 alpha = (0.1, 0.2)
1108 max_iter_ot = 5
1109 max_iter = 5
1110 tol = 1e-7
1111 tol_ot = 1e-7
1112
1113 def raise_warning():
1114 return fused_unbalanced_across_spaces_divergence(
1115 X=xs_nx,
1116 Y=xt_nx,
1117 wx_samp=px_s_nx,
1118 wx_feat=px_f_nx,
1119 wy_samp=py_s_nx,
1120 wy_feat=py_f_nx,
1121 reg_marginals=reg_m,
1122 epsilon=eps,
1123 reg_type=reg_type,
1124 divergence=divergence,
1125 unbalanced_solver=unbalanced_solver,
1126 alpha=alpha,
1127 M_samp=M_samp_nx,
1128 M_feat=M_feat_nx,
1129 init_pi=None,
1130 init_duals=None,
1131 max_iter=max_iter,
1132 tol=tol,
1133 max_iter_ot=max_iter_ot,
1134 tol_ot=tol_ot,
1135 log=False,
1136 verbose=False,
1137 )
1138
1139 np.testing.assert_raises(ValueError, raise_warning)