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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
# Author: Kilian Fatras <kilian.fatras@gmail.com>
#
# License: MIT License
import numpy as np
def coordinate_gradient(b, M, reg, v, i):
'''
Compute the coordinate gradient update for regularized discrete
distributions for (i, :)
The function computes the gradient of the semi dual problem:
.. math::
\W_varepsilon(a, b) = \max_\v \sum_i (\sum_j v_j * b_j
- \reg log(\sum_j exp((v_j - M_{i,j})/reg) * b_j)) * a_i
where :
- M is the (ns,nt) metric cost matrix
- v is a dual variable in R^J
- reg is the regularization term
- a and b are source and target weights (sum to 1)
The algorithm used for solving the problem is the ASGD & SAG algorithms
as proposed in [18]_ [alg.1 & alg.2]
Parameters
----------
b : np.ndarray(nt,),
target measure
M : np.ndarray(ns, nt),
cost matrix
reg : float nu,
Regularization term > 0
v : np.ndarray(nt,),
optimization vector
i : number int,
picked number i
Returns
-------
coordinate gradient : np.ndarray(nt,)
Examples
--------
>>> n_source = 7
>>> n_target = 4
>>> reg = 1
>>> numItermax = 300000
>>> lr = 1
>>> a = ot.utils.unif(n_source)
>>> b = ot.utils.unif(n_target)
>>> rng = np.random.RandomState(0)
>>> X_source = rng.randn(n_source, 2)
>>> Y_target = rng.randn(n_target, 2)
>>> M = ot.dist(X_source, Y_target)
>>> method = "ASGD"
>>> asgd_pi = stochastic.transportation_matrix_entropic(a, b, M, reg,
method, numItermax,
lr)
>>> print(asgd_pi)
References
----------
[Genevay et al., 2016] :
Stochastic Optimization for Large-scale Optimal Transport,
Advances in Neural Information Processing Systems (2016),
arXiv preprint arxiv:1605.08527.
'''
r = M[i, :] - v
exp_v = np.exp(-r / reg) * b
khi = exp_v / (np.sum(exp_v))
return b - khi
def sag_entropic_transport(a, b, M, reg, numItermax=10000, lr=0.1):
'''
Compute the SAG algorithm to solve the regularized discrete measures
optimal transport max problem
The function solves the following optimization problem:
.. math::
\gamma = arg\min_\gamma <\gamma,M>_F + reg\cdot\Omega(\gamma)
s.t. \gamma 1 = a
\gamma^T 1= b
\gamma \geq 0
where :
- M is the (ns,nt) metric cost matrix
- :math:`\Omega` is the entropic regularization term
:math:`\Omega(\gamma)=\sum_{i,j} \gamma_{i,j}\log(\gamma_{i,j})`
- a and b are source and target weights (sum to 1)
The algorithm used for solving the problem is the SAG algorithm
as proposed in [18]_ [alg.1]
Parameters
----------
a : np.ndarray(ns,),
source measure
b : np.ndarray(nt,),
target measure
M : np.ndarray(ns, nt),
cost matrix
reg : float number,
Regularization term > 0
numItermax : int number
number of iteration
lr : float number
learning rate
Returns
-------
v : np.ndarray(nt,)
dual variable
Examples
--------
>>> n_source = 7
>>> n_target = 4
>>> reg = 1
>>> numItermax = 300000
>>> lr = 1
>>> a = ot.utils.unif(n_source)
>>> b = ot.utils.unif(n_target)
>>> rng = np.random.RandomState(0)
>>> X_source = rng.randn(n_source, 2)
>>> Y_target = rng.randn(n_target, 2)
>>> M = ot.dist(X_source, Y_target)
>>> method = "SAG"
>>> sag_pi = stochastic.transportation_matrix_entropic(a, b, M, reg,
method, numItermax,
lr)
>>> print(asgd_pi)
References
----------
[Genevay et al., 2016] :
Stochastic Optimization for Large-scale Optimal Transport,
Advances in Neural Information Processing Systems (2016),
arXiv preprint arxiv:1605.08527.
'''
n_source = np.shape(M)[0]
n_target = np.shape(M)[1]
v = np.zeros(n_target)
stored_gradient = np.zeros((n_source, n_target))
sum_stored_gradient = np.zeros(n_target)
for _ in range(numItermax):
i = np.random.randint(n_source)
cur_coord_grad = a[i] * coordinate_gradient(b, M, reg, v, i)
sum_stored_gradient += (cur_coord_grad - stored_gradient[i])
stored_gradient[i] = cur_coord_grad
v += lr * (1. / n_source) * sum_stored_gradient
return v
def averaged_sgd_entropic_transport(b, M, reg, numItermax=300000, lr=1):
'''
Compute the ASGD algorithm to solve the regularized semi contibous measures
optimal transport max problem
The function solves the following optimization problem:
.. math::
\gamma = arg\min_\gamma <\gamma,M>_F + reg\cdot\Omega(\gamma)
s.t. \gamma 1 = a
\gamma^T 1= b
\gamma \geq 0
where :
- M is the (ns,nt) metric cost matrix
- :math:`\Omega` is the entropic regularization term
:math:`\Omega(\gamma)=\sum_{i,j} \gamma_{i,j}\log(\gamma_{i,j})`
- a and b are source and target weights (sum to 1)
The algorithm used for solving the problem is the ASGD algorithm
as proposed in [18]_ [alg.2]
Parameters
----------
b : np.ndarray(nt,),
target measure
M : np.ndarray(ns, nt),
cost matrix
reg : float number,
Regularization term > 0
numItermax : int number
number of iteration
lr : float number
learning rate
Returns
-------
ave_v : np.ndarray(nt,)
optimization vector
Examples
--------
>>> n_source = 7
>>> n_target = 4
>>> reg = 1
>>> numItermax = 300000
>>> lr = 1
>>> a = ot.utils.unif(n_source)
>>> b = ot.utils.unif(n_target)
>>> rng = np.random.RandomState(0)
>>> X_source = rng.randn(n_source, 2)
>>> Y_target = rng.randn(n_target, 2)
>>> M = ot.dist(X_source, Y_target)
>>> method = "ASGD"
>>> asgd_pi = stochastic.transportation_matrix_entropic(a, b, M, reg,
method, numItermax,
lr)
>>> print(asgd_pi)
References
----------
[Genevay et al., 2016] :
Stochastic Optimization for Large-scale Optimal Transport,
Advances in Neural Information Processing Systems (2016),
arXiv preprint arxiv:1605.08527.
'''
n_source = np.shape(M)[0]
n_target = np.shape(M)[1]
cur_v = np.zeros(n_target)
ave_v = np.zeros(n_target)
for cur_iter in range(numItermax):
k = cur_iter + 1
i = np.random.randint(n_source)
cur_coord_grad = coordinate_gradient(b, M, reg, cur_v, i)
cur_v += (lr / np.sqrt(k)) * cur_coord_grad
ave_v = (1. / k) * cur_v + (1 - 1. / k) * ave_v
return ave_v
def c_transform_entropic(b, M, reg, v):
'''
The goal is to recover u from the c-transform.
The function computes the c_transform of a dual variable from the other
dual variable:
.. math::
u = v^{c,reg} = -reg \sum_j exp((v - M)/reg) b_j
where :
- M is the (ns,nt) metric cost matrix
- u, v are dual variables in R^IxR^J
- reg is the regularization term
It is used to recover an optimal u from optimal v solving the semi dual
problem, see Proposition 2.1 of [18]_
Parameters
----------
b : np.ndarray(nt,)
target measure
M : np.ndarray(ns, nt)
cost matrix
reg : float
regularization term > 0
v : np.ndarray(nt,)
dual variable
Returns
-------
u : np.ndarray(ns,)
Examples
--------
>>> n_source = 7
>>> n_target = 4
>>> reg = 1
>>> numItermax = 300000
>>> lr = 1
>>> a = ot.utils.unif(n_source)
>>> b = ot.utils.unif(n_target)
>>> rng = np.random.RandomState(0)
>>> X_source = rng.randn(n_source, 2)
>>> Y_target = rng.randn(n_target, 2)
>>> M = ot.dist(X_source, Y_target)
>>> method = "ASGD"
>>> asgd_pi = stochastic.transportation_matrix_entropic(a, b, M, reg,
method, numItermax,
lr)
>>> print(asgd_pi)
References
----------
[Genevay et al., 2016] :
Stochastic Optimization for Large-scale Optimal Transport,
Advances in Neural Information Processing Systems (2016),
arXiv preprint arxiv:1605.08527.
'''
n_source = np.shape(M)[0]
u = np.zeros(n_source)
for i in range(n_source):
r = M[i, :] - v
exp_v = np.exp(-r / reg) * b
u[i] = - reg * np.log(np.sum(exp_v))
return u
def transportation_matrix_entropic(a, b, M, reg, method, numItermax=10000,
lr=0.1):
'''
Compute the transportation matrix to solve the regularized discrete
measures optimal transport max problem
The function solves the following optimization problem:
.. math::
\gamma = arg\min_\gamma <\gamma,M>_F + reg\cdot\Omega(\gamma)
s.t. \gamma 1 = a
\gamma^T 1= b
\gamma \geq 0
where :
- M is the (ns,nt) metric cost matrix
- :math:`\Omega` is the entropic regularization term
:math:`\Omega(\gamma)=\sum_{i,j} \gamma_{i,j}\log(\gamma_{i,j})`
- a and b are source and target weights (sum to 1)
The algorithm used for solving the problem is the SAG or ASGD algorithms
as proposed in [18]_
Parameters
----------
a : np.ndarray(ns,),
source measure
b : np.ndarray(nt,),
target measure
M : np.ndarray(ns, nt),
cost matrix
reg : float number,
Regularization term > 0
methode : str,
used method (SAG or ASGD)
numItermax : int number
number of iteration
lr : float number
learning rate
n_source : int number
size of the source measure
n_target : int number
size of the target measure
Returns
-------
pi : np.ndarray(ns, nt)
transportation matrix
Examples
--------
>>> n_source = 7
>>> n_target = 4
>>> reg = 1
>>> numItermax = 300000
>>> lr = 1
>>> a = ot.utils.unif(n_source)
>>> b = ot.utils.unif(n_target)
>>> rng = np.random.RandomState(0)
>>> X_source = rng.randn(n_source, 2)
>>> Y_target = rng.randn(n_target, 2)
>>> M = ot.dist(X_source, Y_target)
>>> method = "ASGD"
>>> asgd_pi = stochastic.transportation_matrix_entropic(a, b, M, reg,
method, numItermax,
lr)
>>> print(asgd_pi)
References
----------
[Genevay et al., 2016] :
Stochastic Optimization for Large-scale Optimal Transport,
Advances in Neural Information Processing Systems (2016),
arXiv preprint arxiv:1605.08527.
'''
if method.lower() == "sag":
opt_v = sag_entropic_transport(a, b, M, reg, numItermax, lr)
elif method.lower() == "asgd":
opt_v = averaged_sgd_entropic_transport(b, M, reg, numItermax, lr)
else:
print("Please, select your method between SAG and ASGD")
return None
opt_u = c_transform_entropic(b, M, reg, opt_v)
pi = (np.exp((opt_u[:, None] + opt_v[None, :] - M[:, :]) / reg) *
a[:, None] * b[None, :])
return pi
|