-
Notifications
You must be signed in to change notification settings - Fork 547
[WIP] Wrapper for OT barycenter solvers with free support #730
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
Open
cedricvincentcuaz
wants to merge
45
commits into
PythonOT:master
Choose a base branch
from
cedricvincentcuaz:solvers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
44d4614
merge
cedricvincentcuaz 63477c2
Merge branch 'master' of https://github.com/cedricvincentcuaz/POT
cedricvincentcuaz a94c6ac
Merge branch 'master' of https://github.com/cedricvincentcuaz/POT
cedricvincentcuaz 27944a5
Merge branch 'master' of https://github.com/cedricvincentcuaz/POT
cedricvincentcuaz 0392961
Merge branch 'master' of https://github.com/cedricvincentcuaz/POT
cedricvincentcuaz 60d1295
Merge branch 'master' of https://github.com/cedricvincentcuaz/POT
cedricvincentcuaz a93c60c
first commit
cedricvincentcuaz 9e25e80
handle masses in unbalanced cases
cedricvincentcuaz 46c4638
update free support
cedricvincentcuaz 671788d
trying to fix tests
cedricvincentcuaz a5b0f70
Merge branch 'master' into solvers
rflamary 9cf60fd
Merge branch 'master' into solvers
rflamary 48a63ea
merge
cedricvincentcuaz df1da8d
small updates
cedricvincentcuaz 75d6c11
fix fun name
cedricvincentcuaz c71e544
update tests
cedricvincentcuaz ed9c992
update tests
cedricvincentcuaz 7132f62
fix tests
cedricvincentcuaz 8e55777
Merge branch 'master' into solvers
rflamary 4a0b5ec
fix tests
cedricvincentcuaz b8219ac
Merge branch 'solvers' of https://github.com/cedricvincentcuaz/POT in…
cedricvincentcuaz 6f900c6
Merge branch 'master' into solvers
cedricvincentcuaz 3d9f987
update docstring for solve_bary_sample
cedricvincentcuaz 7b7cfc0
update plot quickstart guide
cedricvincentcuaz 406d026
add ex
cedricvincentcuaz 0172a89
fix ex
cedricvincentcuaz d493194
fix docs
cedricvincentcuaz 95e5a1c
fix docs
cedricvincentcuaz 05b47fc
fix sphinx
cedricvincentcuaz ab50009
Merge branch 'master' into solvers
rflamary 1c70c2e
add callable cost functions
cedricvincentcuaz c8d48b2
merge
cedricvincentcuaz 53d103b
Merge branch 'PythonOT:master' into solvers
cedricvincentcuaz 1188611
merge
cedricvincentcuaz ea84a70
Merge branch 'master' into solvers
rflamary 0d5c4d2
Merge branch 'master' into solvers
rflamary 6098b72
update tests with callable metrics
cedricvincentcuaz 17f9bb6
merge
cedricvincentcuaz 38973c8
merge
cedricvincentcuaz 50582d2
merge
cedricvincentcuaz 82d2ff8
explicit ground_bary requirements in lp/_barycenter_solvers.py
cedricvincentcuaz 33a37a9
Merge branch 'master' into solvers
rflamary 515b24b
updates after first review
cedricvincentcuaz 59110f0
Merge branch 'solvers' of https://github.com/cedricvincentcuaz/POT in…
cedricvincentcuaz b0f51d2
Merge branch 'master' into solvers
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| ====================================== | ||
| Optimal Transport Barycenter solvers comparison | ||
| ====================================== | ||
|
|
||
| This example illustrates solutions returned for different variants of exact, | ||
| regularized and unbalanced OT barycenter problems with free support using our wrapper `ot.solve_bary_sample`. | ||
| """ | ||
|
|
||
| # Author: Cédric Vincent-Cuaz <cedvincentcuaz@gmail.com> | ||
| # | ||
| # License: MIT License | ||
| # sphinx_gallery_thumbnail_number = 2 | ||
|
|
||
| # %% | ||
|
|
||
| import numpy as np | ||
| import matplotlib.pylab as pl | ||
| import ot | ||
| from ot.plot import plot2D_samples_mat | ||
|
|
||
| # %% | ||
| # 2D data example | ||
| # --------------- | ||
| # | ||
| # We first generate two sets of samples in 2D of 8 and 16 | ||
| # points uniformly separated on circles. The weights of the samples are uniform. | ||
|
|
||
| # Problem size | ||
| n1, n2 = 8, 16 | ||
| nbary = 12 | ||
|
|
||
| # Generate random data | ||
| np.random.seed(0) | ||
|
|
||
| r1, r2 = 1, 3 | ||
| x1 = r1 * np.array( | ||
| [(np.cos(2 * i * np.pi / n1), np.sin(2 * i * np.pi / n1)) for i in range(n1)] | ||
| ) | ||
|
|
||
| x2 = r2 * np.array( | ||
| [(np.cos(2 * i * np.pi / n2), np.sin(2 * i * np.pi / n2)) for i in range(n2)] | ||
| ) | ||
|
|
||
| style = {"markeredgecolor": "k"} | ||
|
|
||
| pl.figure(1, (4, 4)) | ||
| pl.plot(x1[:, 0], x1[:, 1], "ob", **style) | ||
| pl.plot(x2[:, 0], x2[:, 1], "or", **style) | ||
| pl.title("Source distributions") | ||
| pl.show() | ||
|
|
||
|
|
||
| # %% | ||
| # Set up parameters for barycenter solvers and solve | ||
| # --------------------------------------- | ||
|
|
||
| lst_regs = [ | ||
| "No Reg.", | ||
| "Entropic", | ||
| ] # support e.g ["No Reg.", "Entropic", "L2", "Group Lasso + L2"] | ||
| lst_unbalanced = [ | ||
| "Balanced", | ||
| "Unbalanced KL", | ||
| ] # ["Balanced", "Unb. KL", "Unb. L2", "Unb L1 (partial)"] | ||
|
|
||
| lst_solvers = [ # name, param for ot.solve function | ||
| # balanced OT | ||
| ("Exact OT", dict()), | ||
| ("Entropic Reg. OT", dict(reg=1.0)), | ||
| # unbalanced OT KL | ||
| ("Unbalanced KL No Reg.", dict(unbalanced=0.05)), | ||
| ( | ||
| "Unbalanced KL with KL Reg.", | ||
| dict(reg=0.1, unbalanced=0.05, unbalanced_type="kl", reg_type="kl"), | ||
| ), | ||
| ] | ||
|
|
||
| lst_res = [] | ||
| for name, param in lst_solvers: | ||
| print(f"-- name = {name} / param = {param}") | ||
| res = ot.solve_bary_sample(X_a_list=[x1, x2], n=nbary, **param) | ||
| lst_res.append(res) | ||
| list_P = [res.list_res[k].plan for k in range(2)] | ||
| print("X:", res.X) | ||
| print("loss:", res.value) | ||
| print("loss:", res.log) | ||
| print( | ||
| "marginals OT 1:", | ||
| res.list_res[0].plan.sum(axis=1), | ||
| res.list_res[0].plan.sum(axis=0), | ||
| ) | ||
| print( | ||
| "marginals OT 2:", | ||
| res.list_res[1].plan.sum(axis=1), | ||
| res.list_res[1].plan.sum(axis=0), | ||
| ) | ||
|
|
||
| ############################################################################## | ||
| # Plot distributions and plans | ||
| # ---------- | ||
|
|
||
| pl.figure(2, figsize=(16, 16)) | ||
|
|
||
| style.update({"markersize": 20}) | ||
|
|
||
| for i, bname in enumerate(lst_unbalanced): | ||
| for j, rname in enumerate(lst_regs): | ||
| pl.subplot(len(lst_unbalanced), len(lst_regs), i * len(lst_regs) + j + 1) | ||
|
Collaborator
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. Maybe plot first the barycenter and belwo the barycenter + the OT plan? this is a bit dens and hard to parse visually. |
||
|
|
||
| X = lst_res[i * len(lst_regs) + j].X | ||
| list_P = [lst_res[i * len(lst_regs) + j].list_res[k].plan for k in range(2)] | ||
| loss = lst_res[i * len(lst_regs) + j].value | ||
|
|
||
| plot2D_samples_mat(x1, X, list_P[0]) | ||
| plot2D_samples_mat(x2, X, list_P[1]) | ||
|
|
||
| if i == 0 and j == 0: # add labels | ||
| pl.plot(x1[:, 0], x1[:, 1], "ob", label="Source distribution 1", **style) | ||
| pl.plot(x2[:, 0], x2[:, 1], "or", label="Source distribution 2", **style) | ||
| pl.plot(X[:, 0], X[:, 1], "og", label="Barycenter distribution", **style) | ||
| pl.legend(loc="best") | ||
| else: | ||
| pl.plot(x1[:, 0], x1[:, 1], "ob", **style) | ||
| pl.plot(x2[:, 0], x2[:, 1], "or", **style) | ||
| pl.plot(X[:, 0], X[:, 1], "og", **style) | ||
|
|
||
| if i == 0: | ||
| pl.title(rname) | ||
| if j == 0: | ||
| pl.ylabel(bname, fontsize=14) | ||
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
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,28 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| General OT solvers with unified API | ||
| """ | ||
|
|
||
| # Author: Remi Flamary <remi.flamary@polytechnique.edu> | ||
| # Cédric Vincent-Cuaz <cedvincentcuaz@gmail.com> | ||
| # | ||
| # License: MIT License | ||
|
|
||
| # All submodules and packages | ||
| from ._linear import solve, solve_sample | ||
|
|
||
| from ._gromov import ( | ||
| solve_gromov, | ||
| ) | ||
|
|
||
| from ._bary import ( | ||
| solve_bary_sample, | ||
| ) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "solve", | ||
| "solve_sample", | ||
| "solve_gromov", | ||
| "solve_bary_sample", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
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.
you should do it in two pats: first sharp vs entropic baryenter (easier to compare) and then a second section where you vary the marginal violation weight so that we see the transformation from exact bary to points on the left and right (as we can see there)