1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
"""
==========================
Stochastic test
==========================
This example is designed to test the stochatic optimization algorithms module
for descrete and semicontinous measures from the POT library.
"""
# Author: Kilian Fatras <kilian.fatras@gmail.com>
#
# License: MIT License
import numpy as np
import ot
#############################################################################
#
# TEST SAG algorithm
# ---------------------------------------------
# 2 identical discrete measures u defined on the same space with a
# regularization term, a learning rate and a number of iteration
def test_stochastic_sag():
# test sag
n = 15
reg = 1
numItermax = 300000
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G = ot.stochastic.transportation_matrix_entropic(u, u, M, reg, "sag",
numItermax=numItermax)
# check constratints
np.testing.assert_allclose(
u, G.sum(1), atol=1e-04) # cf convergence sag
np.testing.assert_allclose(
u, G.sum(0), atol=1e-04) # cf convergence sag
#############################################################################
#
# TEST ASGD algorithm
# ---------------------------------------------
# 2 identical discrete measures u defined on the same space with a
# regularization term, a learning rate and a number of iteration
def test_stochastic_asgd():
# test asgd
n = 15
reg = 1
numItermax = 300000
lr = 1
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G = ot.stochastic.transportation_matrix_entropic(u, u, M, reg, "asgd",
numItermax=numItermax,
lr=lr)
# check constratints
np.testing.assert_allclose(
u, G.sum(1), atol=1e-03) # cf convergence asgd
np.testing.assert_allclose(
u, G.sum(0), atol=1e-03) # cf convergence asgd
#############################################################################
#
# TEST Convergence SAG and ASGD toward Sinkhorn's solution
# --------------------------------------------------------
# 2 identical discrete measures u defined on the same space with a
# regularization term, a learning rate and a number of iteration
def test_sag_asgd_sinkhorn():
# test all algorithms
n = 15
reg = 1
nb_iter = 300000
lr = 1
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
zero = np.zeros(n)
M = ot.dist(x, x)
G_asgd = ot.stochastic.transportation_matrix_entropic(u, u, M, reg, "asgd",
numItermax=nb_iter,
lr=1)
G_sag = ot.stochastic.transportation_matrix_entropic(u, u, M, reg, "sag",
numItermax=nb_iter)
G_sinkhorn = ot.sinkhorn(u, u, M, reg)
# check constratints
np.testing.assert_allclose(
zero, (G_sag - G_sinkhorn).sum(1), atol=1e-03) # cf convergence sag
np.testing.assert_allclose(
zero, (G_sag - G_sinkhorn).sum(0), atol=1e-03) # cf convergence sag
np.testing.assert_allclose(
zero, (G_asgd - G_sinkhorn).sum(1), atol=1e-03) # cf convergence asgd
np.testing.assert_allclose(
zero, (G_asgd - G_sinkhorn).sum(0), atol=1e-03) # cf convergence asgd
|