Debian Codegraph @

test/test_stochastic.py

raw symbols SCIP
1 """
2 ==========================
3 Stochastic test
4 ==========================
5
6 This example is designed to test the stochatic optimization algorithms module
7 for descrete and semicontinous measures from the POT library.
8
9 """
10
11 # Authors: Kilian Fatras <[email protected]>
12 # Rémi Flamary <[email protected]>
13 #
14 # License: MIT License
15
16 import numpy as np
17 import ot
18
19
20 #############################################################################
21 # COMPUTE TEST FOR SEMI-DUAL PROBLEM
22 #############################################################################
23
24 #############################################################################
25 #
26 # TEST SAG algorithm
27 # ---------------------------------------------
28 # 2 identical discrete measures u defined on the same space with a
29 # regularization term, a learning rate and a number of iteration
30
31
32 def test_stochastic_sag():
33 # test sag
34 n = 10
35 reg = 1
36 numItermax = 30000
37 rng = np.random.RandomState(0)
38
39 x = rng.randn(n, 2)
40 u = ot.utils.unif(n)
41
42 M = ot.dist(x, x)
43
44 G = ot.stochastic.solve_semi_dual_entropic(
45 u, u, M, reg, "sag", numItermax=numItermax
46 )
47
48 # check constraints
49 np.testing.assert_allclose(u, G.sum(1), atol=1e-03) # cf convergence sag
50 np.testing.assert_allclose(u, G.sum(0), atol=1e-03) # cf convergence sag
51
52
53 #############################################################################
54 #
55 # TEST ASGD algorithm
56 # ---------------------------------------------
57 # 2 identical discrete measures u defined on the same space with a
58 # regularization term, a learning rate and a number of iteration
59
60
61 def test_stochastic_asgd():
62 # test asgd
63 n = 10
64 reg = 1
65 numItermax = 10000
66 rng = np.random.RandomState(0)
67
68 x = rng.randn(n, 2)
69 u = ot.utils.unif(n)
70
71 M = ot.dist(x, x)
72
73 G, log = ot.stochastic.solve_semi_dual_entropic(
74 u, u, M, reg, "asgd", numItermax=numItermax, log=True
75 )
76
77 # check constraints
78 np.testing.assert_allclose(u, G.sum(1), atol=1e-02) # cf convergence asgd
79 np.testing.assert_allclose(u, G.sum(0), atol=1e-02) # cf convergence asgd
80
81
82 #############################################################################
83 #
84 # TEST Convergence SAG and ASGD toward Sinkhorn's solution
85 # --------------------------------------------------------
86 # 2 identical discrete measures u defined on the same space with a
87 # regularization term, a learning rate and a number of iteration
88
89
90 def test_sag_asgd_sinkhorn():
91 # test all algorithms
92 n = 10
93 reg = 1
94 nb_iter = 10000
95 rng = np.random.RandomState(0)
96
97 x = rng.randn(n, 2)
98 u = ot.utils.unif(n)
99 M = ot.dist(x, x)
100
101 G_asgd = ot.stochastic.solve_semi_dual_entropic(
102 u, u, M, reg, "asgd", numItermax=nb_iter
103 )
104 G_sag = ot.stochastic.solve_semi_dual_entropic(
105 u, u, M, reg, "sag", numItermax=nb_iter
106 )
107 G_sinkhorn = ot.sinkhorn(u, u, M, reg)
108
109 # check constraints
110 np.testing.assert_allclose(G_sag.sum(1), G_sinkhorn.sum(1), atol=1e-02)
111 np.testing.assert_allclose(G_sag.sum(0), G_sinkhorn.sum(0), atol=1e-02)
112 np.testing.assert_allclose(G_asgd.sum(1), G_sinkhorn.sum(1), atol=1e-02)
113 np.testing.assert_allclose(G_asgd.sum(0), G_sinkhorn.sum(0), atol=1e-02)
114 np.testing.assert_allclose(G_sag, G_sinkhorn, atol=1e-02) # cf convergence sag
115 np.testing.assert_allclose(G_asgd, G_sinkhorn, atol=1e-02) # cf convergence asgd
116
117
118 #############################################################################
119 # COMPUTE TEST FOR DUAL PROBLEM
120 #############################################################################
121
122 #############################################################################
123 #
124 # TEST SGD algorithm
125 # ---------------------------------------------
126 # 2 identical discrete measures u defined on the same space with a
127 # regularization term, a batch_size and a number of iteration
128
129
130 def test_stochastic_dual_sgd():
131 # test sgd
132 n = 10
133 reg = 1
134 numItermax = 5000
135 batch_size = 10
136 rng = np.random.RandomState(0)
137
138 x = rng.randn(n, 2)
139 u = ot.utils.unif(n)
140
141 M = ot.dist(x, x)
142
143 G, log = ot.stochastic.solve_dual_entropic(
144 u, u, M, reg, batch_size, numItermax=numItermax, log=True
145 )
146
147 # check constraints
148 np.testing.assert_allclose(u, G.sum(1), atol=1e-03) # cf convergence sgd
149 np.testing.assert_allclose(u, G.sum(0), atol=1e-03) # cf convergence sgd
150
151
152 #############################################################################
153 #
154 # TEST Convergence SGD toward Sinkhorn's solution
155 # --------------------------------------------------------
156 # 2 identical discrete measures u defined on the same space with a
157 # regularization term, a batch_size and a number of iteration
158
159
160 def test_dual_sgd_sinkhorn():
161 # test all dual algorithms
162 n = 10
163 reg = 1
164 nb_iter = 5000
165 batch_size = 10
166 rng = np.random.RandomState(0)
167
168 # Test uniform
169 x = rng.randn(n, 2)
170 u = ot.utils.unif(n)
171 M = ot.dist(x, x)
172
173 G_sgd = ot.stochastic.solve_dual_entropic(
174 u, u, M, reg, batch_size, numItermax=nb_iter
175 )
176
177 G_sinkhorn = ot.sinkhorn(u, u, M, reg)
178
179 # check constraints
180 np.testing.assert_allclose(G_sgd.sum(1), G_sinkhorn.sum(1), atol=1e-02)
181 np.testing.assert_allclose(G_sgd.sum(0), G_sinkhorn.sum(0), atol=1e-02)
182 np.testing.assert_allclose(G_sgd, G_sinkhorn, atol=1e-02) # cf convergence sgd
183
184 # Test gaussian
185 n = 30
186 reg = 1
187 batch_size = 30
188
189 a = ot.datasets.make_1D_gauss(n, 15, 5) # m= mean, s= std
190 b = ot.datasets.make_1D_gauss(n, 15, 5)
191 X_source = np.arange(n, dtype=np.float64)
192 Y_target = np.arange(n, dtype=np.float64)
193 M = ot.dist(X_source.reshape((n, 1)), Y_target.reshape((n, 1)))
194 M /= M.max()
195
196 G_sgd = ot.stochastic.solve_dual_entropic(
197 a, b, M, reg, batch_size, numItermax=nb_iter
198 )
199
200 G_sinkhorn = ot.sinkhorn(a, b, M, reg)
201
202 # check constraints
203 np.testing.assert_allclose(G_sgd.sum(1), G_sinkhorn.sum(1), atol=1e-03)
204 np.testing.assert_allclose(G_sgd.sum(0), G_sinkhorn.sum(0), atol=1e-03)
205 np.testing.assert_allclose(G_sgd, G_sinkhorn, atol=1e-03) # cf convergence sgd
206
207
208 def test_loss_dual_entropic(nx):
209 nx.seed(0)
210
211 xs = nx.randn(50, 2)
212 xt = nx.randn(40, 2) + 2
213
214 ws = nx.rand(50)
215 ws = ws / nx.sum(ws)
216
217 wt = nx.rand(40)
218 wt = wt / nx.sum(wt)
219
220 u = nx.randn(50)
221 v = nx.randn(40)
222
223 def metric(x, y):
224 return -nx.dot(x, y.T)
225
226 ot.stochastic.loss_dual_entropic(u, v, xs, xt)
227
228 ot.stochastic.loss_dual_entropic(u, v, xs, xt, ws=ws, wt=wt, metric=metric)
229
230
231 def test_plan_dual_entropic(nx):
232 nx.seed(0)
233
234 xs = nx.randn(50, 2)
235 xt = nx.randn(40, 2) + 2
236
237 ws = nx.rand(50)
238 ws = ws / nx.sum(ws)
239
240 wt = nx.rand(40)
241 wt = wt / nx.sum(wt)
242
243 u = nx.randn(50)
244 v = nx.randn(40)
245
246 def metric(x, y):
247 return -nx.dot(x, y.T)
248
249 G1 = ot.stochastic.plan_dual_entropic(u, v, xs, xt)
250
251 assert np.all(nx.to_numpy(G1) >= 0)
252 assert G1.shape[0] == 50
253 assert G1.shape[1] == 40
254
255 G2 = ot.stochastic.plan_dual_entropic(u, v, xs, xt, ws=ws, wt=wt, metric=metric)
256
257 assert np.all(nx.to_numpy(G2) >= 0)
258 assert G2.shape[0] == 50
259 assert G2.shape[1] == 40
260
261
262 def test_loss_dual_quadratic(nx):
263 nx.seed(0)
264
265 xs = nx.randn(50, 2)
266 xt = nx.randn(40, 2) + 2
267
268 ws = nx.rand(50)
269 ws = ws / nx.sum(ws)
270
271 wt = nx.rand(40)
272 wt = wt / nx.sum(wt)
273
274 u = nx.randn(50)
275 v = nx.randn(40)
276
277 def metric(x, y):
278 return -nx.dot(x, y.T)
279
280 ot.stochastic.loss_dual_quadratic(u, v, xs, xt)
281
282 ot.stochastic.loss_dual_quadratic(u, v, xs, xt, ws=ws, wt=wt, metric=metric)
283
284
285 def test_plan_dual_quadratic(nx):
286 nx.seed(0)
287
288 xs = nx.randn(50, 2)
289 xt = nx.randn(40, 2) + 2
290
291 ws = nx.rand(50)
292 ws = ws / nx.sum(ws)
293
294 wt = nx.rand(40)
295 wt = wt / nx.sum(wt)
296
297 u = nx.randn(50)
298 v = nx.randn(40)
299
300 def metric(x, y):
301 return -nx.dot(x, y.T)
302
303 G1 = ot.stochastic.plan_dual_quadratic(u, v, xs, xt)
304
305 assert np.all(nx.to_numpy(G1) >= 0)
306 assert G1.shape[0] == 50
307 assert G1.shape[1] == 40
308
309 G2 = ot.stochastic.plan_dual_quadratic(u, v, xs, xt, ws=ws, wt=wt, metric=metric)
310
311 assert np.all(nx.to_numpy(G2) >= 0)
312 assert G2.shape[0] == 50
313 assert G2.shape[1] == 40