-
Notifications
You must be signed in to change notification settings - Fork 529
Free support barycenters #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6492e95
free support barycenter
vivienseguy 3f23fa1
free support barycenter
vivienseguy 98ce4cc
free support barycenter
vivienseguy e39f04a
add free support barycenter algorithm
2c7b980
add free support barycenter algorithm
67ddb92
add free support barycenter algorithm
4671279
add test free support barycenter algorithm + cleaning
08e5c0a
add test free support barycenter algorithm + cleaning
af57d90
return log dict in free support barycenter function
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
==================================================== | ||
2D free support Wasserstein barycenters of distributions | ||
==================================================== | ||
|
||
Illustration of 2D Wasserstein barycenters if discributions that are weighted | ||
sum of diracs. | ||
|
||
""" | ||
|
||
# Author: Vivien Seguy <vivien.seguy@iip.ist.i.kyoto-u.ac.jp> | ||
# | ||
# License: MIT License | ||
|
||
import numpy as np | ||
import matplotlib.pylab as pl | ||
import ot | ||
|
||
|
||
############################################################################## | ||
# Generate data | ||
# ------------- | ||
#%% parameters and data generation | ||
N = 3 | ||
d = 2 | ||
measures_locations = [] | ||
measures_weights = [] | ||
|
||
for i in range(N): | ||
|
||
n_i = np.random.randint(low=1, high=20) # nb samples | ||
|
||
mu_i = np.random.normal(0., 4., (d,)) # Gaussian mean | ||
|
||
A_i = np.random.rand(d, d) | ||
cov_i = np.dot(A_i, A_i.transpose()) # Gaussian covariance matrix | ||
|
||
x_i = ot.datasets.make_2D_samples_gauss(n_i, mu_i, cov_i) # Dirac locations | ||
b_i = np.random.uniform(0., 1., (n_i,)) | ||
b_i = b_i / np.sum(b_i) # Dirac weights | ||
|
||
measures_locations.append(x_i) | ||
measures_weights.append(b_i) | ||
|
||
|
||
############################################################################## | ||
# Compute free support barycenter | ||
# ------------- | ||
|
||
k = 10 # number of Diracs of the barycenter | ||
X_init = np.random.normal(0., 1., (k, d)) # initial Dirac locations | ||
b = np.ones((k,)) / k # weights of the barycenter (it will not be optimized, only the locations are optimized) | ||
|
||
X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b) | ||
|
||
|
||
############################################################################## | ||
# Plot data | ||
# --------- | ||
|
||
pl.figure(1) | ||
for (x_i, b_i) in zip(measures_locations, measures_weights): | ||
color = np.random.randint(low=1, high=10 * N) | ||
pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure') | ||
pl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter') | ||
pl.title('Data measures and their barycenter') | ||
pl.legend(loc=0) | ||
pl.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import scipy as sp | ||
import scipy.sparse as sps | ||
|
||
|
||
try: | ||
import cvxopt | ||
from cvxopt import solvers, matrix, spmatrix | ||
|
@@ -26,7 +27,7 @@ def scipy_sparse_to_spmatrix(A): | |
|
||
|
||
def barycenter(A, M, weights=None, verbose=False, log=False, solver='interior-point'): | ||
"""Compute the entropic regularized wasserstein barycenter of distributions A | ||
"""Compute the Wasserstein barycenter of distributions A | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch ! |
||
|
||
The function solves the following optimization problem [16]: | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
----
needs to have the proper length for good documentation generation.