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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
"""
==========================
Stochastic test
==========================
This example is designed to test the stochatic optimization algorithms module
for descrete and semicontinous measures from the POT library.
"""
# Authors: Kilian Fatras <kilian.fatras@gmail.com>
# Rémi Flamary <remi.flamary@polytechnique.edu>
#
# License: MIT License
import numpy as np
import ot
#############################################################################
# COMPUTE TEST FOR SEMI-DUAL PROBLEM
#############################################################################
#############################################################################
#
# 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 = 10
reg = 1
numItermax = 30000
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G = ot.stochastic.solve_semi_dual_entropic(
u, u, M, reg, "sag", numItermax=numItermax
)
# check constraints
np.testing.assert_allclose(u, G.sum(1), atol=1e-03) # cf convergence sag
np.testing.assert_allclose(u, G.sum(0), atol=1e-03) # 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 = 10
reg = 1
numItermax = 10000
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G, log = ot.stochastic.solve_semi_dual_entropic(
u, u, M, reg, "asgd", numItermax=numItermax, log=True
)
# check constraints
np.testing.assert_allclose(u, G.sum(1), atol=1e-02) # cf convergence asgd
np.testing.assert_allclose(u, G.sum(0), atol=1e-02) # 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 = 10
reg = 1
nb_iter = 10000
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G_asgd = ot.stochastic.solve_semi_dual_entropic(
u, u, M, reg, "asgd", numItermax=nb_iter
)
G_sag = ot.stochastic.solve_semi_dual_entropic(
u, u, M, reg, "sag", numItermax=nb_iter
)
G_sinkhorn = ot.sinkhorn(u, u, M, reg)
# check constraints
np.testing.assert_allclose(G_sag.sum(1), G_sinkhorn.sum(1), atol=1e-02)
np.testing.assert_allclose(G_sag.sum(0), G_sinkhorn.sum(0), atol=1e-02)
np.testing.assert_allclose(G_asgd.sum(1), G_sinkhorn.sum(1), atol=1e-02)
np.testing.assert_allclose(G_asgd.sum(0), G_sinkhorn.sum(0), atol=1e-02)
np.testing.assert_allclose(G_sag, G_sinkhorn, atol=1e-02) # cf convergence sag
np.testing.assert_allclose(G_asgd, G_sinkhorn, atol=1e-02) # cf convergence asgd
#############################################################################
# COMPUTE TEST FOR DUAL PROBLEM
#############################################################################
#############################################################################
#
# TEST SGD algorithm
# ---------------------------------------------
# 2 identical discrete measures u defined on the same space with a
# regularization term, a batch_size and a number of iteration
def test_stochastic_dual_sgd():
# test sgd
n = 10
reg = 1
numItermax = 5000
batch_size = 10
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G, log = ot.stochastic.solve_dual_entropic(
u, u, M, reg, batch_size, numItermax=numItermax, log=True
)
# check constraints
np.testing.assert_allclose(u, G.sum(1), atol=1e-03) # cf convergence sgd
np.testing.assert_allclose(u, G.sum(0), atol=1e-03) # cf convergence sgd
#############################################################################
#
# TEST Convergence SGD toward Sinkhorn's solution
# --------------------------------------------------------
# 2 identical discrete measures u defined on the same space with a
# regularization term, a batch_size and a number of iteration
def test_dual_sgd_sinkhorn():
# test all dual algorithms
n = 10
reg = 1
nb_iter = 5000
batch_size = 10
rng = np.random.RandomState(0)
# Test uniform
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G_sgd = ot.stochastic.solve_dual_entropic(
u, u, M, reg, batch_size, numItermax=nb_iter
)
G_sinkhorn = ot.sinkhorn(u, u, M, reg)
# check constraints
np.testing.assert_allclose(G_sgd.sum(1), G_sinkhorn.sum(1), atol=1e-02)
np.testing.assert_allclose(G_sgd.sum(0), G_sinkhorn.sum(0), atol=1e-02)
np.testing.assert_allclose(G_sgd, G_sinkhorn, atol=1e-02) # cf convergence sgd
# Test gaussian
n = 30
reg = 1
batch_size = 30
a = ot.datasets.make_1D_gauss(n, 15, 5) # m= mean, s= std
b = ot.datasets.make_1D_gauss(n, 15, 5)
X_source = np.arange(n, dtype=np.float64)
Y_target = np.arange(n, dtype=np.float64)
M = ot.dist(X_source.reshape((n, 1)), Y_target.reshape((n, 1)))
M /= M.max()
G_sgd = ot.stochastic.solve_dual_entropic(
a, b, M, reg, batch_size, numItermax=nb_iter
)
G_sinkhorn = ot.sinkhorn(a, b, M, reg)
# check constraints
np.testing.assert_allclose(G_sgd.sum(1), G_sinkhorn.sum(1), atol=1e-03)
np.testing.assert_allclose(G_sgd.sum(0), G_sinkhorn.sum(0), atol=1e-03)
np.testing.assert_allclose(G_sgd, G_sinkhorn, atol=1e-03) # cf convergence sgd
def test_loss_dual_entropic(nx):
nx.seed(0)
xs = nx.randn(50, 2)
xt = nx.randn(40, 2) + 2
ws = nx.rand(50)
ws = ws / nx.sum(ws)
wt = nx.rand(40)
wt = wt / nx.sum(wt)
u = nx.randn(50)
v = nx.randn(40)
def metric(x, y):
return -nx.dot(x, y.T)
ot.stochastic.loss_dual_entropic(u, v, xs, xt)
ot.stochastic.loss_dual_entropic(u, v, xs, xt, ws=ws, wt=wt, metric=metric)
def test_plan_dual_entropic(nx):
nx.seed(0)
xs = nx.randn(50, 2)
xt = nx.randn(40, 2) + 2
ws = nx.rand(50)
ws = ws / nx.sum(ws)
wt = nx.rand(40)
wt = wt / nx.sum(wt)
u = nx.randn(50)
v = nx.randn(40)
def metric(x, y):
return -nx.dot(x, y.T)
G1 = ot.stochastic.plan_dual_entropic(u, v, xs, xt)
assert np.all(nx.to_numpy(G1) >= 0)
assert G1.shape[0] == 50
assert G1.shape[1] == 40
G2 = ot.stochastic.plan_dual_entropic(u, v, xs, xt, ws=ws, wt=wt, metric=metric)
assert np.all(nx.to_numpy(G2) >= 0)
assert G2.shape[0] == 50
assert G2.shape[1] == 40
def test_loss_dual_quadratic(nx):
nx.seed(0)
xs = nx.randn(50, 2)
xt = nx.randn(40, 2) + 2
ws = nx.rand(50)
ws = ws / nx.sum(ws)
wt = nx.rand(40)
wt = wt / nx.sum(wt)
u = nx.randn(50)
v = nx.randn(40)
def metric(x, y):
return -nx.dot(x, y.T)
ot.stochastic.loss_dual_quadratic(u, v, xs, xt)
ot.stochastic.loss_dual_quadratic(u, v, xs, xt, ws=ws, wt=wt, metric=metric)
def test_plan_dual_quadratic(nx):
nx.seed(0)
xs = nx.randn(50, 2)
xt = nx.randn(40, 2) + 2
ws = nx.rand(50)
ws = ws / nx.sum(ws)
wt = nx.rand(40)
wt = wt / nx.sum(wt)
u = nx.randn(50)
v = nx.randn(40)
def metric(x, y):
return -nx.dot(x, y.T)
G1 = ot.stochastic.plan_dual_quadratic(u, v, xs, xt)
assert np.all(nx.to_numpy(G1) >= 0)
assert G1.shape[0] == 50
assert G1.shape[1] == 40
G2 = ot.stochastic.plan_dual_quadratic(u, v, xs, xt, ws=ws, wt=wt, metric=metric)
assert np.all(nx.to_numpy(G2) >= 0)
assert G2.shape[0] == 50
assert G2.shape[1] == 40
|