Debian Codegraph @

test/test_smooth.py

raw symbols SCIP
1 """Tests for ot.smooth model"""
2
3 # Author: Remi Flamary <[email protected]>
4 #
5 # License: MIT License
6
7 import numpy as np
8 import ot
9 import pytest
10 from scipy.optimize import check_grad
11
12
13 def test_smooth_ot_dual():
14 # get data
15 n = 100
16 rng = np.random.RandomState(0)
17
18 x = rng.randn(n, 2)
19 u = ot.utils.unif(n)
20
21 M = ot.dist(x, x)
22
23 with pytest.raises(NotImplementedError):
24 Gl2, log = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type="none")
25
26 # squared l2 regularisation
27 Gl2, log = ot.smooth.smooth_ot_dual(
28 u, u, M, 1, reg_type="l2", log=True, stopThr=1e-10
29 )
30
31 # check constraints
32 np.testing.assert_allclose(u, Gl2.sum(1), atol=1e-05) # cf convergence sinkhorn
33 np.testing.assert_allclose(u, Gl2.sum(0), atol=1e-05) # cf convergence sinkhorn
34
35 # kl regularisation
36 G = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type="kl", stopThr=1e-10)
37
38 # check constraints
39 np.testing.assert_allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn
40 np.testing.assert_allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn
41
42 G2 = ot.sinkhorn(u, u, M, 1, stopThr=1e-10)
43 np.testing.assert_allclose(G, G2, atol=1e-05)
44
45 # sparsity-constrained regularisation
46 max_nz = 2
47 Gsc, log = ot.smooth.smooth_ot_dual(
48 u,
49 u,
50 M,
51 1,
52 max_nz=max_nz,
53 log=True,
54 reg_type="sparsity_constrained",
55 stopThr=1e-10,
56 )
57
58 # check marginal constraints
59 np.testing.assert_allclose(u, Gsc.sum(1), atol=1e-03)
60 np.testing.assert_allclose(u, Gsc.sum(0), atol=1e-03)
61
62 # check sparsity constraints
63 np.testing.assert_array_less(np.sum(Gsc > 0, axis=0), np.ones(n) * max_nz + 1)
64
65
66 def test_smooth_ot_semi_dual():
67 # get data
68 n = 100
69 rng = np.random.RandomState(0)
70
71 x = rng.randn(n, 2)
72 u = ot.utils.unif(n)
73
74 M = ot.dist(x, x)
75
76 with pytest.raises(NotImplementedError):
77 Gl2, log = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type="none")
78
79 # squared l2 regularisation
80 Gl2, log = ot.smooth.smooth_ot_semi_dual(
81 u, u, M, 1, reg_type="l2", log=True, stopThr=1e-10
82 )
83
84 # check constraints
85 np.testing.assert_allclose(u, Gl2.sum(1), atol=1e-05) # cf convergence sinkhorn
86 np.testing.assert_allclose(u, Gl2.sum(0), atol=1e-05) # cf convergence sinkhorn
87
88 # kl regularisation
89 G = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type="kl", stopThr=1e-10)
90
91 # check constraints
92 np.testing.assert_allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn
93 np.testing.assert_allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn
94
95 G2 = ot.sinkhorn(u, u, M, 1, stopThr=1e-10)
96 np.testing.assert_allclose(G, G2, atol=1e-05)
97
98 # sparsity-constrained regularisation
99 max_nz = 2
100 Gsc = ot.smooth.smooth_ot_semi_dual(
101 u, u, M, 1, reg_type="sparsity_constrained", max_nz=max_nz, stopThr=1e-10
102 )
103
104 # check marginal constraints
105 np.testing.assert_allclose(u, Gsc.sum(1), atol=1e-03)
106 np.testing.assert_allclose(u, Gsc.sum(0), atol=1e-03)
107
108 # check sparsity constraints
109 np.testing.assert_array_less(np.sum(Gsc > 0, axis=0), np.ones(n) * max_nz + 1)
110
111
112 def test_sparsity_constrained_gradient():
113 max_nz = 5
114 regularizer = ot.smooth.SparsityConstrained(max_nz=max_nz)
115 rng = np.random.RandomState(0)
116 X = rng.randn(
117 10,
118 )
119 b = 0.5
120
121 def delta_omega_func(X):
122 return regularizer.delta_Omega(X)[0]
123
124 def delta_omega_grad(X):
125 return regularizer.delta_Omega(X)[1]
126
127 dual_grad_err = check_grad(delta_omega_func, delta_omega_grad, X)
128 np.testing.assert_allclose(dual_grad_err, 0.0, atol=1e-07)
129
130 def max_omega_func(X, b):
131 return regularizer.max_Omega(X, b)[0]
132
133 def max_omega_grad(X, b):
134 return regularizer.max_Omega(X, b)[1]
135
136 semi_dual_grad_err = check_grad(max_omega_func, max_omega_grad, X, b)
137 np.testing.assert_allclose(semi_dual_grad_err, 0.0, atol=1e-07)