-
Notifications
You must be signed in to change notification settings - Fork 528
[MRG] Free support Sinkhorn barycenters #387
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
15 commits
Select commit
Hold shift + click to select a range
c6f9d65
Adding function for computing Sinkhorn Free Support barycenters
eddardd 85fe8ae
Adding exampel on Free Support Sinkhorn Barycenter
eddardd ca47bab
Fixing typo on free support sinkhorn barycenter example
eddardd 2ac61ab
Adding info on new Free Support Barycenter solver
eddardd e04f860
Removing extra line so that code follows pep8
eddardd b36935c
Fixing issues with pep8 in example
eddardd bc43f0f
Merge branch 'sinkhorn-barycenters' of https://github.com/eddardd/POT…
eddardd 943abee
Correcting issues with pep8 standards
eddardd 16f6c18
Adding tests for free support sinkhorn barycenter
eddardd 59c1457
Adding section on Sinkhorn barycenter to the example
eddardd eb691c6
Changing distributions for the Sinkhorn barycenter example
eddardd 551ac04
Removing file that should not be on the last commit
eddardd 1dc2a7c
Adding PR number to REALEASES.md
eddardd 8c258e5
Adding new contributors
eddardd 0f2fd24
Update CONTRIBUTORS.md
rflamary 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
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
151 changes: 151 additions & 0 deletions
151
examples/barycenters/plot_free_support_sinkhorn_barycenter.py
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,151 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
======================================================== | ||
2D free support Sinkhorn barycenters of distributions | ||
======================================================== | ||
|
||
Illustration of Sinkhorn barycenter calculation between empirical distributions understood as point clouds | ||
|
||
""" | ||
|
||
# Authors: Eduardo Fernandes Montesuma <eduardo.fernandes-montesuma@universite-paris-saclay.fr> | ||
# | ||
# License: MIT License | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import ot | ||
|
||
# %% | ||
# General Parameters | ||
# ------------------ | ||
reg = 1e-2 # Entropic Regularization | ||
numItermax = 20 # Maximum number of iterations for the Barycenter algorithm | ||
numInnerItermax = 50 # Maximum number of sinkhorn iterations | ||
n_samples = 200 | ||
|
||
# %% | ||
# Generate Data | ||
# ------------- | ||
|
||
X1 = np.random.randn(200, 2) | ||
X2 = 2 * np.concatenate([ | ||
np.concatenate([- np.ones([50, 1]), np.linspace(-1, 1, 50)[:, None]], axis=1), | ||
np.concatenate([np.linspace(-1, 1, 50)[:, None], np.ones([50, 1])], axis=1), | ||
np.concatenate([np.ones([50, 1]), np.linspace(1, -1, 50)[:, None]], axis=1), | ||
np.concatenate([np.linspace(1, -1, 50)[:, None], - np.ones([50, 1])], axis=1), | ||
], axis=0) | ||
X3 = np.random.randn(200, 2) | ||
X3 = 2 * (X3 / np.linalg.norm(X3, axis=1)[:, None]) | ||
X4 = np.random.multivariate_normal(np.array([0, 0]), np.array([[1., 0.5], [0.5, 1.]]), size=200) | ||
|
||
a1, a2, a3, a4 = ot.unif(len(X1)), ot.unif(len(X1)), ot.unif(len(X1)), ot.unif(len(X1)) | ||
|
||
# %% | ||
# Inspect generated distributions | ||
# ------------------------------- | ||
|
||
fig, axes = plt.subplots(1, 4, figsize=(16, 4)) | ||
|
||
axes[0].scatter(x=X1[:, 0], y=X1[:, 1], c='steelblue', edgecolor='k') | ||
axes[1].scatter(x=X2[:, 0], y=X2[:, 1], c='steelblue', edgecolor='k') | ||
axes[2].scatter(x=X3[:, 0], y=X3[:, 1], c='steelblue', edgecolor='k') | ||
axes[3].scatter(x=X4[:, 0], y=X4[:, 1], c='steelblue', edgecolor='k') | ||
|
||
axes[0].set_xlim([-3, 3]) | ||
axes[0].set_ylim([-3, 3]) | ||
axes[0].set_title('Distribution 1') | ||
|
||
axes[1].set_xlim([-3, 3]) | ||
axes[1].set_ylim([-3, 3]) | ||
axes[1].set_title('Distribution 2') | ||
|
||
axes[2].set_xlim([-3, 3]) | ||
axes[2].set_ylim([-3, 3]) | ||
axes[2].set_title('Distribution 3') | ||
|
||
axes[3].set_xlim([-3, 3]) | ||
axes[3].set_ylim([-3, 3]) | ||
axes[3].set_title('Distribution 4') | ||
|
||
plt.tight_layout() | ||
plt.show() | ||
|
||
# %% | ||
# Interpolating Empirical Distributions | ||
# ------------------------------------- | ||
|
||
fig = plt.figure(figsize=(10, 10)) | ||
|
||
weights = np.array([ | ||
[3 / 3, 0 / 3], | ||
[2 / 3, 1 / 3], | ||
[1 / 3, 2 / 3], | ||
[0 / 3, 3 / 3], | ||
]).astype(np.float32) | ||
|
||
for k in range(4): | ||
XB_init = np.random.randn(n_samples, 2) | ||
XB = ot.bregman.free_support_sinkhorn_barycenter( | ||
measures_locations=[X1, X2], | ||
measures_weights=[a1, a2], | ||
weights=weights[k], | ||
X_init=XB_init, | ||
reg=reg, | ||
numItermax=numItermax, | ||
numInnerItermax=numInnerItermax | ||
) | ||
ax = plt.subplot2grid((4, 4), (0, k)) | ||
ax.scatter(XB[:, 0], XB[:, 1], color='steelblue', edgecolor='k') | ||
ax.set_xlim([-3, 3]) | ||
ax.set_ylim([-3, 3]) | ||
|
||
for k in range(1, 4, 1): | ||
XB_init = np.random.randn(n_samples, 2) | ||
XB = ot.bregman.free_support_sinkhorn_barycenter( | ||
measures_locations=[X1, X3], | ||
measures_weights=[a1, a2], | ||
weights=weights[k], | ||
X_init=XB_init, | ||
reg=reg, | ||
numItermax=numItermax, | ||
numInnerItermax=numInnerItermax | ||
) | ||
ax = plt.subplot2grid((4, 4), (k, 0)) | ||
ax.scatter(XB[:, 0], XB[:, 1], color='steelblue', edgecolor='k') | ||
ax.set_xlim([-3, 3]) | ||
ax.set_ylim([-3, 3]) | ||
|
||
for k in range(1, 4, 1): | ||
XB_init = np.random.randn(n_samples, 2) | ||
XB = ot.bregman.free_support_sinkhorn_barycenter( | ||
measures_locations=[X3, X4], | ||
measures_weights=[a1, a2], | ||
weights=weights[k], | ||
X_init=XB_init, | ||
reg=reg, | ||
numItermax=numItermax, | ||
numInnerItermax=numInnerItermax | ||
) | ||
ax = plt.subplot2grid((4, 4), (3, k)) | ||
ax.scatter(XB[:, 0], XB[:, 1], color='steelblue', edgecolor='k') | ||
ax.set_xlim([-3, 3]) | ||
ax.set_ylim([-3, 3]) | ||
|
||
for k in range(1, 3, 1): | ||
XB_init = np.random.randn(n_samples, 2) | ||
XB = ot.bregman.free_support_sinkhorn_barycenter( | ||
measures_locations=[X2, X4], | ||
measures_weights=[a1, a2], | ||
weights=weights[k], | ||
X_init=XB_init, | ||
reg=reg, | ||
numItermax=numItermax, | ||
numInnerItermax=numInnerItermax | ||
) | ||
ax = plt.subplot2grid((4, 4), (k, 3)) | ||
ax.scatter(XB[:, 0], XB[:, 1], color='steelblue', edgecolor='k') | ||
ax.set_xlim([-3, 3]) | ||
ax.set_ylim([-3, 3]) | ||
|
||
plt.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
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.
Uh oh!
There was an error while loading. Please reload this page.