Debian Codegraph @

test/unbalanced/test_sinkhorn.py

raw symbols SCIP
1 """Tests for module Unbalanced OT with entropy regularization"""
2
3 # Author: Hicham Janati <[email protected]>
4 # Laetitia Chapel <[email protected]>
5 # Quang Huy Tran <[email protected]>
6 #
7 # License: MIT License
8
9 import itertools
10 import numpy as np
11 import ot
12 import pytest
13 from ot.unbalanced import barycenter_unbalanced
14
15
16 @pytest.mark.parametrize(
17 "method,reg_type",
18 itertools.product(
19 [
20 "sinkhorn",
21 "sinkhorn_stabilized",
22 "sinkhorn_reg_scaling",
23 "sinkhorn_translation_invariant",
24 ],
25 ["kl", "entropy"],
26 ),
27 )
28 def test_unbalanced_convergence(nx, method, reg_type):
29 # test generalized sinkhorn for unbalanced OT
30 n = 100
31 rng = np.random.RandomState(42)
32
33 x = rng.randn(n, 2)
34 a = ot.utils.unif(n)
35
36 # make dists unbalanced
37 b = ot.utils.unif(n) * 1.5
38 M = ot.dist(x, x)
39 a, b, M = nx.from_numpy(a, b, M)
40
41 epsilon = 1.0
42 reg_m = 1.0
43
44 G, log = ot.unbalanced.sinkhorn_unbalanced(
45 a,
46 b,
47 M,
48 reg=epsilon,
49 reg_m=reg_m,
50 method=method,
51 reg_type=reg_type,
52 log=True,
53 verbose=True,
54 )
55 loss = nx.to_numpy(
56 ot.unbalanced.sinkhorn_unbalanced2(
57 a,
58 b,
59 M,
60 reg=epsilon,
61 reg_m=reg_m,
62 method=method,
63 reg_type=reg_type,
64 verbose=True,
65 )
66 )
67 # check fixed point equations
68 # in log-domain
69 fi = reg_m / (reg_m + epsilon)
70 logb = nx.log(b + 1e-16)
71 loga = nx.log(a + 1e-16)
72 if reg_type == "entropy":
73 logKtu = nx.logsumexp(log["logu"][None, :] - M.T / epsilon, axis=1)
74 logKv = nx.logsumexp(log["logv"][None, :] - M / epsilon, axis=1)
75 elif reg_type == "kl":
76 log_ab = loga[:, None] + logb[None, :]
77 logKtu = nx.logsumexp(log["logu"][None, :] - M.T / epsilon + log_ab.T, axis=1)
78 logKv = nx.logsumexp(log["logv"][None, :] - M / epsilon + log_ab, axis=1)
79 v_final = fi * (logb - logKtu)
80 u_final = fi * (loga - logKv)
81
82 np.testing.assert_allclose(
83 nx.to_numpy(u_final), nx.to_numpy(log["logu"]), atol=1e-05
84 )
85 np.testing.assert_allclose(
86 nx.to_numpy(v_final), nx.to_numpy(log["logv"]), atol=1e-05
87 )
88
89 # check if sinkhorn_unbalanced2 returns the correct loss
90 np.testing.assert_allclose(nx.to_numpy(nx.sum(G * M)), loss, atol=1e-5)
91
92
93 @pytest.mark.parametrize(
94 "method,reg_type",
95 itertools.product(
96 [
97 "sinkhorn",
98 "sinkhorn_stabilized",
99 "sinkhorn_reg_scaling",
100 "sinkhorn_translation_invariant",
101 ],
102 ["kl", "entropy"],
103 ),
104 )
105 def test_unbalanced_marginals(nx, method, reg_type):
106 # test generalized sinkhorn for unbalanced OT
107 n = 100
108 rng = np.random.RandomState(42)
109
110 x = rng.randn(n, 2)
111 a = ot.utils.unif(n)
112 b = ot.utils.unif(n)
113 M = ot.dist(x, x)
114 a, b, M = nx.from_numpy(a, b, M)
115
116 epsilon = 1.0
117 reg_m = 1.0
118
119 G0, log0 = ot.unbalanced.sinkhorn_unbalanced(
120 a, b, M, reg=epsilon, reg_m=reg_m, method=method, reg_type=reg_type, log=True
121 )
122 loss0 = ot.unbalanced.sinkhorn_unbalanced2(
123 a,
124 b,
125 M,
126 reg=epsilon,
127 reg_m=reg_m,
128 method=method,
129 reg_type=reg_type,
130 )
131
132 # check in case no histogram is provided or histogram is None
133 a_empty, b_empty = np.array([]), np.array([])
134 a_empty, b_empty = nx.from_numpy(a_empty, b_empty)
135
136 G_empty, log_empty = ot.unbalanced.sinkhorn_unbalanced(
137 a_empty,
138 b_empty,
139 M,
140 reg=epsilon,
141 reg_m=reg_m,
142 method=method,
143 reg_type=reg_type,
144 log=True,
145 )
146 loss_empty = ot.unbalanced.sinkhorn_unbalanced2(
147 a_empty, b_empty, M, reg=epsilon, reg_m=reg_m, method=method, reg_type=reg_type
148 )
149
150 np.testing.assert_allclose(
151 nx.to_numpy(log_empty["logu"]), nx.to_numpy(log0["logu"]), atol=1e-05
152 )
153 np.testing.assert_allclose(
154 nx.to_numpy(log_empty["logv"]), nx.to_numpy(log0["logv"]), atol=1e-05
155 )
156 np.testing.assert_allclose(nx.to_numpy(G_empty), nx.to_numpy(G0), atol=1e-05)
157 np.testing.assert_allclose(nx.to_numpy(loss_empty), nx.to_numpy(loss0), atol=1e-5)
158
159
160 @pytest.mark.parametrize(
161 "method,reg_type",
162 itertools.product(
163 [
164 "sinkhorn",
165 "sinkhorn_stabilized",
166 "sinkhorn_reg_scaling",
167 "sinkhorn_translation_invariant",
168 ],
169 ["kl", "entropy"],
170 ),
171 )
172 def test_unbalanced_warmstart(nx, method, reg_type):
173 # test generalized sinkhorn for unbalanced OT
174 n = 100
175 rng = np.random.RandomState(42)
176
177 x = rng.randn(n, 2)
178 a = ot.utils.unif(n)
179 b = ot.utils.unif(n)
180 M = ot.dist(x, x)
181 a, b, M = nx.from_numpy(a, b, M)
182
183 epsilon = 1.0
184 reg_m = 1.0
185
186 G0, log0 = ot.unbalanced.sinkhorn_unbalanced(
187 a,
188 b,
189 M,
190 reg=epsilon,
191 reg_m=reg_m,
192 method=method,
193 reg_type=reg_type,
194 warmstart=None,
195 log=True,
196 verbose=True,
197 )
198 loss0 = ot.unbalanced.sinkhorn_unbalanced2(
199 a,
200 b,
201 M,
202 reg=epsilon,
203 reg_m=reg_m,
204 method=method,
205 reg_type=reg_type,
206 warmstart=None,
207 verbose=True,
208 )
209
210 dim_a, dim_b = M.shape
211 warmstart = (nx.zeros(dim_a, type_as=M), nx.zeros(dim_b, type_as=M))
212 G, log = ot.unbalanced.sinkhorn_unbalanced(
213 a,
214 b,
215 M,
216 reg=epsilon,
217 reg_m=reg_m,
218 method=method,
219 reg_type=reg_type,
220 warmstart=warmstart,
221 log=True,
222 verbose=True,
223 )
224 loss = ot.unbalanced.sinkhorn_unbalanced2(
225 a,
226 b,
227 M,
228 reg=epsilon,
229 reg_m=reg_m,
230 method=method,
231 reg_type=reg_type,
232 warmstart=warmstart,
233 verbose=True,
234 )
235
236 _, log_emd = ot.lp.emd(a, b, M, log=True)
237 warmstart1 = (log_emd["u"], log_emd["v"])
238 G1, log1 = ot.unbalanced.sinkhorn_unbalanced(
239 a,
240 b,
241 M,
242 reg=epsilon,
243 reg_m=reg_m,
244 method=method,
245 reg_type=reg_type,
246 warmstart=warmstart1,
247 log=True,
248 verbose=True,
249 )
250 loss1 = ot.unbalanced.sinkhorn_unbalanced2(
251 a,
252 b,
253 M,
254 reg=epsilon,
255 reg_m=reg_m,
256 method=method,
257 reg_type=reg_type,
258 warmstart=warmstart1,
259 verbose=True,
260 )
261
262 np.testing.assert_allclose(
263 nx.to_numpy(log["logu"]), nx.to_numpy(log0["logu"]), atol=1e-05
264 )
265 np.testing.assert_allclose(
266 nx.to_numpy(log["logv"]), nx.to_numpy(log0["logv"]), atol=1e-05
267 )
268 np.testing.assert_allclose(
269 nx.to_numpy(log0["logu"]), nx.to_numpy(log1["logu"]), atol=1e-05
270 )
271 np.testing.assert_allclose(
272 nx.to_numpy(log0["logv"]), nx.to_numpy(log1["logv"]), atol=1e-05
273 )
274
275 np.testing.assert_allclose(nx.to_numpy(G), nx.to_numpy(G0), atol=1e-05)
276 np.testing.assert_allclose(nx.to_numpy(G0), nx.to_numpy(G1), atol=1e-05)
277
278 np.testing.assert_allclose(nx.to_numpy(loss), nx.to_numpy(loss0), atol=1e-5)
279 np.testing.assert_allclose(nx.to_numpy(loss0), nx.to_numpy(loss1), atol=1e-5)
280
281
282 @pytest.mark.parametrize(
283 "method,reg_type",
284 itertools.product(
285 [
286 "sinkhorn",
287 "sinkhorn_stabilized",
288 "sinkhorn_reg_scaling",
289 "sinkhorn_translation_invariant",
290 ],
291 ["kl", "entropy"],
292 ),
293 )
294 def test_unbalanced_reference_measure(nx, method, reg_type):
295 # test generalized sinkhorn for unbalanced OT
296 n = 100
297 rng = np.random.RandomState(42)
298
299 x = rng.randn(n, 2)
300 a = ot.utils.unif(n)
301 b = ot.utils.unif(n)
302 M = ot.dist(x, x)
303 a, b, M = nx.from_numpy(a, b, M)
304
305 epsilon = 1.0
306 reg_m = 1.0
307
308 G0, log0 = ot.unbalanced.sinkhorn_unbalanced(
309 a,
310 b,
311 M,
312 reg=epsilon,
313 reg_m=reg_m,
314 method=method,
315 reg_type=reg_type,
316 c=None,
317 log=True,
318 )
319 loss0 = ot.unbalanced.sinkhorn_unbalanced2(
320 a, b, M, reg=epsilon, reg_m=reg_m, method=method, reg_type=reg_type, c=None
321 )
322
323 if reg_type == "kl":
324 c = a[:, None] * b[None, :]
325 elif reg_type == "entropy":
326 c = nx.ones(M.shape, type_as=M)
327
328 G, log = ot.unbalanced.sinkhorn_unbalanced(
329 a,
330 b,
331 M,
332 reg=epsilon,
333 reg_m=reg_m,
334 method=method,
335 reg_type=reg_type,
336 c=c,
337 log=True,
338 )
339 loss = ot.unbalanced.sinkhorn_unbalanced2(
340 a, b, M, reg=epsilon, reg_m=reg_m, method=method, reg_type=reg_type, c=c
341 )
342
343 np.testing.assert_allclose(
344 nx.to_numpy(log["logu"]), nx.to_numpy(log0["logu"]), atol=1e-05
345 )
346 np.testing.assert_allclose(
347 nx.to_numpy(log["logv"]), nx.to_numpy(log0["logv"]), atol=1e-05
348 )
349 np.testing.assert_allclose(nx.to_numpy(G), nx.to_numpy(G0), atol=1e-05)
350 np.testing.assert_allclose(nx.to_numpy(loss), nx.to_numpy(loss0), atol=1e-5)
351
352
353 @pytest.mark.parametrize(
354 "method, log",
355 itertools.product(
356 [
357 "sinkhorn",
358 "sinkhorn_stabilized",
359 "sinkhorn_reg_scaling",
360 "sinkhorn_translation_invariant",
361 ],
362 [True, False],
363 ),
364 )
365 def test_sinkhorn_unbalanced2(nx, method, log):
366 n = 100
367 rng = np.random.RandomState(42)
368
369 x = rng.randn(n, 2)
370 a = ot.utils.unif(n)
371
372 # make dists unbalanced
373 b = ot.utils.unif(n) * 1.5
374 M = ot.dist(x, x)
375 a, b, M = nx.from_numpy(a, b, M)
376
377 epsilon = 1.0
378 reg_m = 1.0
379
380 loss = nx.to_numpy(
381 ot.unbalanced.sinkhorn_unbalanced2(
382 a,
383 b,
384 M,
385 reg=epsilon,
386 reg_m=reg_m,
387 method=method,
388 c=None,
389 log=False,
390 verbose=True,
391 )
392 )
393
394 res = ot.unbalanced.sinkhorn_unbalanced2(
395 a, b, M, reg=epsilon, reg_m=reg_m, method=method, c=None, log=log, verbose=True
396 )
397 loss0 = res[0] if log else res
398
399 np.testing.assert_allclose(nx.to_numpy(loss), nx.to_numpy(loss0), atol=1e-5)
400
401
402 @pytest.mark.parametrize(
403 "method,reg_m",
404 itertools.product(
405 [
406 "sinkhorn",
407 "sinkhorn_stabilized",
408 "sinkhorn_reg_scaling",
409 "sinkhorn_translation_invariant",
410 ],
411 [1, float("inf")],
412 ),
413 )
414 def test_unbalanced_relaxation_parameters(nx, method, reg_m):
415 # test generalized sinkhorn for unbalanced OT
416 n = 100
417 rng = np.random.RandomState(50)
418
419 x = rng.randn(n, 2)
420 a = ot.utils.unif(n)
421
422 # make dists unbalanced
423 b = rng.rand(n, 2)
424
425 M = ot.dist(x, x)
426 epsilon = 1.0
427
428 a, b, M = nx.from_numpy(a, b, M)
429
430 # options for reg_m
431 full_list_reg_m = [reg_m, reg_m]
432 full_tuple_reg_m = (reg_m, reg_m)
433 tuple_reg_m, list_reg_m = (reg_m), [reg_m]
434 nx_reg_m = reg_m * nx.ones(1)
435 list_options = [
436 nx_reg_m,
437 full_tuple_reg_m,
438 tuple_reg_m,
439 full_list_reg_m,
440 list_reg_m,
441 ]
442
443 loss, log = ot.unbalanced.sinkhorn_unbalanced(
444 a, b, M, reg=epsilon, reg_m=reg_m, method=method, log=True, verbose=True
445 )
446
447 for opt in list_options:
448 loss_opt, log_opt = ot.unbalanced.sinkhorn_unbalanced(
449 a, b, M, reg=epsilon, reg_m=opt, method=method, log=True, verbose=True
450 )
451
452 np.testing.assert_allclose(
453 nx.to_numpy(log["logu"]), nx.to_numpy(log_opt["logu"]), atol=1e-05
454 )
455 np.testing.assert_allclose(
456 nx.to_numpy(log["logv"]), nx.to_numpy(log_opt["logv"]), atol=1e-05
457 )
458 np.testing.assert_allclose(nx.to_numpy(loss), nx.to_numpy(loss_opt), atol=1e-05)
459
460
461 @pytest.mark.parametrize(
462 "method, reg_m1, reg_m2",
463 itertools.product(
464 [
465 "sinkhorn",
466 "sinkhorn_stabilized",
467 "sinkhorn_reg_scaling",
468 "sinkhorn_translation_invariant",
469 ],
470 [1, float("inf")],
471 [1, float("inf")],
472 ),
473 )
474 def test_unbalanced_relaxation_parameters_pair(nx, method, reg_m1, reg_m2):
475 # test generalized sinkhorn for unbalanced OT
476 n = 100
477 rng = np.random.RandomState(50)
478
479 x = rng.randn(n, 2)
480 a = ot.utils.unif(n)
481
482 # make dists unbalanced
483 b = rng.rand(n, 2)
484
485 M = ot.dist(x, x)
486 epsilon = 1.0
487
488 a, b, M = nx.from_numpy(a, b, M)
489
490 # options for reg_m
491 full_list_reg_m = [reg_m1, reg_m2]
492 full_tuple_reg_m = (reg_m1, reg_m2)
493 list_options = [full_tuple_reg_m, full_list_reg_m]
494
495 loss, log = ot.unbalanced.sinkhorn_unbalanced(
496 a,
497 b,
498 M,
499 reg=epsilon,
500 reg_m=(reg_m1, reg_m2),
501 method=method,
502 log=True,
503 verbose=True,
504 )
505
506 for opt in list_options:
507 loss_opt, log_opt = ot.unbalanced.sinkhorn_unbalanced(
508 a, b, M, reg=epsilon, reg_m=opt, method=method, log=True, verbose=True
509 )
510
511 np.testing.assert_allclose(
512 nx.to_numpy(log["logu"]), nx.to_numpy(log_opt["logu"]), atol=1e-05
513 )
514 np.testing.assert_allclose(
515 nx.to_numpy(log["logv"]), nx.to_numpy(log_opt["logv"]), atol=1e-05
516 )
517 np.testing.assert_allclose(nx.to_numpy(loss), nx.to_numpy(loss_opt), atol=1e-05)
518
519
520 @pytest.mark.parametrize(
521 "method",
522 [
523 "sinkhorn",
524 "sinkhorn_stabilized",
525 "sinkhorn_reg_scaling",
526 "sinkhorn_translation_invariant",
527 ],
528 )
529 def test_unbalanced_multiple_inputs(nx, method):
530 # test generalized sinkhorn for unbalanced OT
531 n = 100
532 rng = np.random.RandomState(42)
533
534 x = rng.randn(n, 2)
535 a = ot.utils.unif(n)
536
537 # make dists unbalanced
538 b = rng.rand(n, 2)
539
540 M = ot.dist(x, x)
541 epsilon = 1.0
542 reg_m = 1.0
543
544 a, b, M = nx.from_numpy(a, b, M)
545
546 G, log = ot.unbalanced.sinkhorn_unbalanced(
547 a, b, M, reg=epsilon, reg_m=reg_m, method=method, log=True, verbose=True
548 )
549
550 # check fixed point equations
551 # in log-domain
552 fi = reg_m / (reg_m + epsilon)
553 logb = nx.log(b + 1e-16)
554 loga = nx.log(a + 1e-16)[:, None]
555 logKtu = nx.logsumexp(log["logu"][:, None, :] - M[:, :, None] / epsilon, axis=0)
556 logKv = nx.logsumexp(log["logv"][None, :] - M[:, :, None] / epsilon, axis=1)
557 v_final = fi * (logb - logKtu)
558 u_final = fi * (loga - logKv)
559
560 print("u_final shape = {}".format(u_final.shape))
561 print("v_final shape = {}".format(v_final.shape))
562 print("logu shape = {}".format(log["logu"].shape))
563 print("logv shape = {}".format(log["logv"].shape))
564
565 np.testing.assert_allclose(
566 nx.to_numpy(u_final), nx.to_numpy(log["logu"]), atol=1e-05
567 )
568 np.testing.assert_allclose(
569 nx.to_numpy(v_final), nx.to_numpy(log["logv"]), atol=1e-05
570 )
571
572 # reg_type="entropy" as multiple inputs does not work for KL yet
573 losses = ot.unbalanced.sinkhorn_unbalanced2(
574 a, b, M, reg=epsilon, reg_m=reg_m, method=method, reg_type="entropy"
575 )
576
577 loss1 = ot.unbalanced.sinkhorn_unbalanced2(
578 a, b[:, 0], M, reg=epsilon, reg_m=reg_m, method=method, reg_type="entropy"
579 )
580 loss2 = ot.unbalanced.sinkhorn_unbalanced2(
581 a, b[:, 1], M, reg=epsilon, reg_m=reg_m, method=method, reg_type="entropy"
582 )
583
584 np.testing.assert_allclose(nx.to_numpy(losses), np.array([loss1, loss2]), atol=1e-4)
585
586
587 def test_stabilized_vs_sinkhorn(nx):
588 # test if stable version matches sinkhorn
589 n = 100
590
591 # Gaussian distributions
592 a = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std
593 b1 = ot.datasets.make_1D_gauss(n, m=60, s=8)
594 b2 = ot.datasets.make_1D_gauss(n, m=30, s=4)
595
596 # creating matrix A containing all distributions
597 b = np.vstack((b1, b2)).T
598
599 M = ot.utils.dist0(n)
600 M /= np.median(M)
601 epsilon = 1
602 reg_m = 1.0
603 stopThr = 1e-12
604
605 ab, bb, Mb = nx.from_numpy(a, b, M)
606
607 G, _ = ot.unbalanced.sinkhorn_unbalanced2(
608 ab,
609 bb,
610 Mb,
611 epsilon,
612 reg_m,
613 method="sinkhorn_stabilized",
614 log=True,
615 stopThr=stopThr,
616 )
617 G2, _ = ot.unbalanced.sinkhorn_unbalanced2(
618 ab, bb, Mb, epsilon, reg_m, method="sinkhorn", log=True, stopThr=stopThr
619 )
620 G2_np, _ = ot.unbalanced.sinkhorn_unbalanced2(
621 a, b, M, epsilon, reg_m, method="sinkhorn", log=True, stopThr=stopThr
622 )
623 G3, _ = ot.unbalanced.sinkhorn_unbalanced2(
624 ab,
625 bb,
626 Mb,
627 epsilon,
628 reg_m,
629 method="sinkhorn_translation_invariant",
630 log=True,
631 stopThr=stopThr,
632 )
633
634 G = nx.to_numpy(G)
635 G2 = nx.to_numpy(G2)
636 G3 = nx.to_numpy(G3)
637
638 np.testing.assert_allclose(G, G2, atol=1e-5)
639 np.testing.assert_allclose(G2, G2_np, atol=1e-5)
640 np.testing.assert_allclose(G3, G, atol=1e-5)
641
642
643 def test_sinkhorn_wrong_returnCost(nx):
644 n = 100
645 rng = np.random.RandomState(42)
646 x = rng.randn(n, 2)
647 rng = np.random.RandomState(75)
648 y = rng.randn(n, 2)
649 a_np = ot.utils.unif(n)
650 b_np = ot.utils.unif(n)
651
652 M = ot.dist(x, y)
653 M = M / M.max()
654 a, b, M = nx.from_numpy(a_np, b_np, M)
655 epsilon = 1
656 reg_m = 1.0
657
658 def sinkhorn2(returnCost):
659 return ot.unbalanced.sinkhorn_unbalanced2(
660 a, b, M, epsilon, reg_m, returnCost=returnCost
661 )
662
663 np.testing.assert_raises(ValueError, sinkhorn2, "invalid_returnCost")
664
665
666 @pytest.mark.parametrize(
667 "method", ["sinkhorn", "sinkhorn_stabilized", "sinkhorn_reg_scaling"]
668 )
669 def test_unbalanced_barycenter(nx, method):
670 # test generalized sinkhorn for unbalanced OT barycenter
671 n = 100
672 rng = np.random.RandomState(42)
673
674 x = rng.randn(n, 2)
675 A = rng.rand(n, 2)
676
677 # make dists unbalanced
678 A = A * np.array([1, 2])[None, :]
679 M = ot.dist(x, x)
680 epsilon = 1.0
681 reg_m = 1.0
682
683 A, M = nx.from_numpy(A, M)
684
685 q, log = barycenter_unbalanced(
686 A, M, reg=epsilon, reg_m=reg_m, method=method, log=True, verbose=True
687 )
688 # check fixed point equations
689 fi = reg_m / (reg_m + epsilon)
690 logA = nx.log(A + 1e-16)
691 logq = nx.log(q + 1e-16)[:, None]
692 logKtu = nx.logsumexp(log["logu"][:, None, :] - M[:, :, None] / epsilon, axis=0)
693 logKv = nx.logsumexp(log["logv"][None, :] - M[:, :, None] / epsilon, axis=1)
694 v_final = fi * (logq - logKtu)
695 u_final = fi * (logA - logKv)
696
697 np.testing.assert_allclose(
698 nx.to_numpy(u_final), nx.to_numpy(log["logu"]), atol=1e-05
699 )
700 np.testing.assert_allclose(
701 nx.to_numpy(v_final), nx.to_numpy(log["logv"]), atol=1e-05
702 )
703
704
705 def test_barycenter_stabilized_vs_sinkhorn(nx):
706 # test generalized sinkhorn for unbalanced OT barycenter
707 n = 100
708 rng = np.random.RandomState(42)
709
710 x = rng.randn(n, 2)
711 A = rng.rand(n, 2)
712
713 # make dists unbalanced
714 A = A * np.array([1, 4])[None, :]
715 M = ot.dist(x, x)
716 epsilon = 0.5
717 reg_m = 10
718
719 Ab, Mb = nx.from_numpy(A, M)
720
721 qstable, _ = barycenter_unbalanced(
722 Ab,
723 Mb,
724 reg=epsilon,
725 reg_m=reg_m,
726 log=True,
727 tau=100,
728 method="sinkhorn_stabilized",
729 verbose=True,
730 )
731 q, _ = barycenter_unbalanced(
732 Ab, Mb, reg=epsilon, reg_m=reg_m, method="sinkhorn", log=True
733 )
734 q_np, _ = barycenter_unbalanced(
735 A, M, reg=epsilon, reg_m=reg_m, method="sinkhorn", log=True
736 )
737 q, qstable = nx.to_numpy(q, qstable)
738 np.testing.assert_allclose(q, qstable, atol=1e-05)
739 np.testing.assert_allclose(q, q_np, atol=1e-05)
740
741
742 def test_wrong_method(nx):
743 n = 10
744 rng = np.random.RandomState(42)
745
746 x = rng.randn(n, 2)
747 a = ot.utils.unif(n)
748
749 # make dists unbalanced
750 b = ot.utils.unif(n) * 1.5
751
752 M = ot.dist(x, x)
753 epsilon = 1.0
754 reg_m = 1.0
755
756 a, b, M = nx.from_numpy(a, b, M)
757
758 with pytest.raises(ValueError):
759 ot.unbalanced.sinkhorn_unbalanced(
760 a,
761 b,
762 M,
763 reg=epsilon,
764 reg_m=reg_m,
765 method="badmethod",
766 log=True,
767 verbose=True,
768 )
769 with pytest.raises(ValueError):
770 ot.unbalanced.sinkhorn_unbalanced2(
771 a, b, M, epsilon, reg_m, method="badmethod", verbose=True
772 )
773
774
775 def test_implemented_methods(nx):
776 IMPLEMENTED_METHODS = ["sinkhorn", "sinkhorn_stabilized"]
777 TO_BE_IMPLEMENTED_METHODS = [
778 "sinkhorn_reg_scaling",
779 "sinkhorn_translation_invariant",
780 ]
781 NOT_VALID_TOKENS = ["foo"]
782 # test generalized sinkhorn for unbalanced OT barycenter
783 n = 3
784 rng = np.random.RandomState(42)
785
786 x = rng.randn(n, 2)
787 a = ot.utils.unif(n)
788
789 # make dists unbalanced
790 b = ot.utils.unif(n) * 1.5
791 A = rng.rand(n, 2)
792 M = ot.dist(x, x)
793 epsilon = 1.0
794 reg_m = 1.0
795
796 a, b, M, A = nx.from_numpy(a, b, M, A)
797
798 for method in IMPLEMENTED_METHODS:
799 ot.unbalanced.sinkhorn_unbalanced(a, b, M, epsilon, reg_m, method=method)
800 ot.unbalanced.sinkhorn_unbalanced2(a, b, M, epsilon, reg_m, method=method)
801 barycenter_unbalanced(A, M, reg=epsilon, reg_m=reg_m, method=method)
802 with pytest.warns(UserWarning, match="not implemented"):
803 for method in set(TO_BE_IMPLEMENTED_METHODS):
804 ot.unbalanced.sinkhorn_unbalanced(a, b, M, epsilon, reg_m, method=method)
805 ot.unbalanced.sinkhorn_unbalanced2(a, b, M, epsilon, reg_m, method=method)
806 barycenter_unbalanced(A, M, reg=epsilon, reg_m=reg_m, method=method)
807 with pytest.raises(ValueError):
808 for method in set(NOT_VALID_TOKENS):
809 ot.unbalanced.sinkhorn_unbalanced(a, b, M, epsilon, reg_m, method=method)
810 ot.unbalanced.sinkhorn_unbalanced2(a, b, M, epsilon, reg_m, method=method)
811 barycenter_unbalanced(A, M, reg=epsilon, reg_m=reg_m, method=method)