Skip to content

Commit 2c7b980

Browse files
Vivien SeguyVivien Seguy
authored andcommitted
add free support barycenter algorithm
1 parent e39f04a commit 2c7b980

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ It provides the following solvers:
1717
* Entropic regularization OT solver with Sinkhorn Knopp Algorithm [2] and stabilized version [9][10] with optional GPU implementation (requires cudamat).
1818
* Smooth optimal transport solvers (dual and semi-dual) for KL and squared L2 regularizations [17].
1919
* Non regularized Wasserstein barycenters [16] with LP solver (only small scale).
20+
* Non regularized free support Wasserstein barycenters [20].
2021
* Bregman projections for Wasserstein barycenter [3] and unmixing [4].
2122
* Optimal transport for domain adaptation with group lasso regularization [5]
2223
* Conditional gradient [6] and Generalized conditional gradient for regularized OT [7].
@@ -225,3 +226,5 @@ You can also post bug reports and feature requests in Github issues. Make sure t
225226
[18] Genevay, A., Cuturi, M., Peyré, G. & Bach, F. (2016) [Stochastic Optimization for Large-scale Optimal Transport](arXiv preprint arxiv:1605.08527). Advances in Neural Information Processing Systems (2016).
226227

227228
[19] Seguy, V., Bhushan Damodaran, B., Flamary, R., Courty, N., Rolet, A.& Blondel, M. [Large-scale Optimal Transport and Mapping Estimation](https://arxiv.org/pdf/1711.02283.pdf). International Conference on Learning Representation (2018)
229+
230+
[20] Cuturi, M. and Doucet, A. (2014) [Fast Computation of Wasserstein Barycenters](http://proceedings.mlr.press/v32/cuturi14.html). International Conference in Machine Learning

examples/plot_free_support_barycenter.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Generate data
2222
# -------------
2323
#%% parameters and data generation
24-
N = 6
24+
N = 3
2525
d = 2
2626
measures_locations = []
2727
measures_weights = []
@@ -33,24 +33,25 @@
3333
mu = np.random.normal(0., 4., (d,))
3434

3535
A = np.random.rand(d, d)
36-
cov = np.dot(A,A.transpose())
36+
cov = np.dot(A, A.transpose())
3737

38-
xs = ot.datasets.make_2D_samples_gauss(n, mu, cov)
39-
b = np.random.uniform(0., 1., (n,))
40-
b = b/np.sum(b)
38+
x_i = ot.datasets.make_2D_samples_gauss(n, mu, cov)
39+
b_i = np.random.uniform(0., 1., (n,))
40+
b_i = b_i / np.sum(b_i)
4141

42-
measures_locations.append(xs)
43-
measures_weights.append(b)
44-
45-
k = 10
46-
X_init = np.random.normal(0., 1., (k,d))
47-
b_init = np.ones((k,)) / k
42+
measures_locations.append(x_i)
43+
measures_weights.append(b_i)
4844

4945

5046
##############################################################################
5147
# Compute free support barycenter
5248
# -------------
53-
X = ot.lp.cvx.free_support_barycenter(measures_locations, measures_weights, X_init, b_init)
49+
50+
k = 10
51+
X_init = np.random.normal(0., 1., (k, d))
52+
b = np.ones((k,)) / k
53+
54+
X = ot.lp.cvx.free_support_barycenter(measures_locations, measures_weights, X_init, b)
5455

5556

5657
##############################################################################
@@ -60,10 +61,10 @@
6061
#%% plot samples
6162

6263
pl.figure(1)
63-
for (xs, b) in zip(measures_locations, measures_weights):
64-
color = np.random.randint(low=1, high=10*N)
65-
pl.scatter(xs[:, 0], xs[:, 1], s=b*1000, label='input measure')
66-
pl.scatter(X[:, 0], X[:, 1], s=b_init*1000, c='black' , marker='^', label='2-Wasserstein barycenter')
64+
for (x_i, b_i) in zip(measures_locations, measures_weights):
65+
color = np.random.randint(low=1, high=10 * N)
66+
pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure')
67+
pl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter')
6768
pl.title('Data measures and their barycenter')
6869
pl.legend(loc=0)
69-
pl.show()
70+
pl.show()

ot/lp/cvx.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ def barycenter(A, M, weights=None, verbose=False, log=False, solver='interior-po
147147
return b
148148

149149

150-
151-
152-
def free_support_barycenter(measures_locations, measures_weights, X_init, b_init, weights=None, numItermax=100, stopThr=1e-6, verbose=False):
153-
150+
def free_support_barycenter(measures_locations, measures_weights, X_init, b, weights=None, numItermax=100, stopThr=1e-6, verbose=False):
154151
"""
155152
Solves the free support (locations of the barycenters are optimized, not the weights) Wasserstein barycenter problem (i.e. the weighted Frechet mean for the 2-Wasserstein distance)
156153
@@ -168,7 +165,7 @@ def free_support_barycenter(measures_locations, measures_weights, X_init, b_init
168165
169166
X_init : (k,d) np.ndarray
170167
Initialization of the support locations (on k atoms) of the barycenter
171-
b_init : (k,) np.ndarray
168+
b : (k,) np.ndarray
172169
Initialization of the weights of the barycenter (non-negatives, sum to 1)
173170
weights : (k,) np.ndarray
174171
Initialization of the coefficients of the barycenter (non-negatives, sum to 1)
@@ -199,27 +196,27 @@ def free_support_barycenter(measures_locations, measures_weights, X_init, b_init
199196
iter_count = 0
200197

201198
d = X_init.shape[1]
202-
k = b_init.size
199+
k = b.size
203200
N = len(measures_locations)
204201

205202
if not weights:
206-
weights = np.ones((N,))/N
203+
weights = np.ones((N,)) / N
207204

208205
X = X_init
209206

210-
displacement_square_norm = stopThr+1.
207+
displacement_square_norm = stopThr + 1.
211208

212-
while ( displacement_square_norm > stopThr and iter_count < numItermax ):
209+
while (displacement_square_norm > stopThr and iter_count < numItermax):
213210

214211
T_sum = np.zeros((k, d))
215212

216213
for (measure_locations_i, measure_weights_i, weight_i) in zip(measures_locations, measures_weights, weights.tolist()):
217214

218215
M_i = ot.dist(X, measure_locations_i)
219-
T_i = ot.emd(b_init, measure_weights_i, M_i)
220-
T_sum = T_sum + weight_i*np.reshape(1. / b_init, (-1, 1)) * np.matmul(T_i, measure_locations_i)
216+
T_i = ot.emd(b, measure_weights_i, M_i)
217+
T_sum = T_sum + weight_i * np.reshape(1. / b, (-1, 1)) * np.matmul(T_i, measure_locations_i)
221218

222-
displacement_square_norm = np.sum(np.square(X-T_sum))
219+
displacement_square_norm = np.sum(np.square(X - T_sum))
223220
X = T_sum
224221

225222
if verbose:
@@ -228,4 +225,3 @@ def free_support_barycenter(measures_locations, measures_weights, X_init, b_init
228225
iter_count += 1
229226

230227
return X
231-

0 commit comments

Comments
 (0)