Debian Codegraph @

test/test_regpath.py

raw symbols SCIP
1 """Tests for module regularization path"""
2
3 # Author: Haoran Wu <[email protected]>
4 #
5 # License: MIT License
6
7 import numpy as np
8 import ot
9
10
11 def test_fully_relaxed_path():
12 n_source = 50 # nb source samples (gaussian)
13 n_target = 40 # nb target samples (gaussian)
14
15 mu = np.array([0, 0])
16 cov = np.array([[1, 0], [0, 2]])
17
18 rng = np.random.RandomState(0)
19 xs = ot.datasets.make_2D_samples_gauss(n_source, mu, cov, random_state=rng)
20 xt = ot.datasets.make_2D_samples_gauss(n_target, mu, cov, random_state=rng)
21
22 # source and target distributions
23 a = ot.utils.unif(n_source)
24 b = ot.utils.unif(n_target)
25
26 # loss matrix
27 M = ot.dist(xs, xt)
28 M /= M.max()
29
30 t, _, _ = ot.regpath.regularization_path(a, b, M, reg=1e-8, semi_relaxed=False)
31
32 G = t.reshape((n_source, n_target))
33 np.testing.assert_allclose(a, G.sum(1), atol=1e-05)
34 np.testing.assert_allclose(b, G.sum(0), atol=1e-05)
35
36
37 def test_semi_relaxed_path():
38 n_source = 50 # nb source samples (gaussian)
39 n_target = 40 # nb target samples (gaussian)
40
41 mu = np.array([0, 0])
42 cov = np.array([[1, 0], [0, 2]])
43
44 rng = np.random.RandomState(0)
45 xs = ot.datasets.make_2D_samples_gauss(n_source, mu, cov, random_state=rng)
46 xt = ot.datasets.make_2D_samples_gauss(n_target, mu, cov, random_state=rng)
47
48 # source and target distributions
49 a = ot.utils.unif(n_source)
50 b = ot.utils.unif(n_target)
51
52 # loss matrix
53 M = ot.dist(xs, xt)
54 M /= M.max()
55
56 t, _, _ = ot.regpath.regularization_path(a, b, M, reg=1e-8, semi_relaxed=True)
57
58 G = t.reshape((n_source, n_target))
59 np.testing.assert_allclose(a, G.sum(1), atol=1e-05)
60 np.testing.assert_allclose(b, G.sum(0), atol=1e-10)