-
Notifications
You must be signed in to change notification settings - Fork 524
Implementation of Low Rank Gromov-Wasserstein #614
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
59 commits
Select commit
Hold shift + click to select a range
f49f6b4
new file for lr sinkhorn
laudavid 3c4b50f
lr sinkhorn, solve_sample, OTResultLazy
laudavid 3034e57
add test functions + small modif lr_sin/solve_sample
laudavid 085863a
add import to __init__
laudavid 9becafc
modify low rank, remove solve_sample,OTResultLazy
laudavid 855234d
pull from master
laudavid 6ea251c
new file for lr sinkhorn
laudavid 965e4d6
lr sinkhorn, solve_sample, OTResultLazy
laudavid fd5e26d
add test functions + small modif lr_sin/solve_sample
laudavid 3df3b77
add import to __init__
laudavid fae28f7
Merge branch 'master' of https://github.com/hi-paris/POT into lowrank_v2
laudavid ab5475b
remove test solve_sample
laudavid f1c8cdd
add value, value_linear, lazy_plan
laudavid b1a2136
Merge branch 'PythonOT:master' into master
laudavid 9e51a83
Merge branch 'master' of https://github.com/hi-paris/POT into lowrank_v2
laudavid df01cff
add comments to lr algorithm
laudavid a0b0a9d
Merge branch 'PythonOT:master' into master
laudavid 7075c8b
Merge branch 'master' of https://github.com/hi-paris/POT into lowrank_v2
laudavid 5f2af0e
Merge branch 'PythonOT:master' into lowrank_v2
laudavid 5bc9de9
modify test functions + add comments to lowrank
laudavid c66951b
Merge branch 'lowrank_v2' of https://github.com/hi-paris/POT into low…
laudavid 6040e6f
modify __init__ with lowrank
laudavid a7fdffd
debug lowrank + test
laudavid d90c186
debug test function low_rank
laudavid ea3a3e0
error test
laudavid f6a36bf
Merge branch 'PythonOT:master' into master
laudavid fe067fd
Merge branch 'master' of https://github.com/hi-paris/POT into lowrank_v2
laudavid 5d3ed32
Merge branch 'PythonOT:master' into lowrank_v2
laudavid 3e6b9aa
Merge branch 'lowrank_v2' of https://github.com/hi-paris/POT into low…
laudavid 165e8f5
final debug of lowrank + add new test functions
laudavid de54bb9
branch up to date with master
laudavid bdcfaf6
Merge branch 'master' of https://github.com/hi-paris/POT into lowrank_v2
laudavid d0d9f46
Merge branch 'master' of https://github.com/hi-paris/POT into lowrank_v2
laudavid ec96836
Merge branch 'PythonOT:master' into lowrank_v2
laudavid 8c6ac67
Debug tests + add lowrank to solve_sample
laudavid fafc5f6
Merge branch 'lowrank_v2' of https://github.com/hi-paris/POT into low…
laudavid bc7af6b
fix torch backend for lowrank
laudavid b40705c
fix jax backend and skip tf
laudavid 55c8d2b
fix pep 8 tests
laudavid 218c54a
merge master + doc for lowrank
laudavid e25c91d
add lowrank init + test functions
laudavid 0436e95
Add init strategies in lowrank + example (PythonOT#588)
laudavid 3649633
modified lowrank
laudavid 78091a0
merge with upstream POT
laudavid 5c4c4a8
changes from code review
laudavid 477deed
fix error test pep8
laudavid 077184f
fix linux-minimal-deps + code review
laudavid 7d3071f
Implementation of LR GW + add method in __init__
laudavid bd06dc4
add LR gw paper in README.md
laudavid 99b38d5
add tests for low rank GW
laudavid cbc4d1f
add examples for Low Rank GW
laudavid a0ddb7e
merge latest version of POT
laudavid 269ed30
fix __init__
laudavid 440f3a2
change atol of lr backends
laudavid ab770ce
fix pep8 errors
laudavid df28231
pull POT master + new gromov/_lowrank.py
laudavid 96bad89
modif for code review
laudavid a960b28
pull upstream master
laudavid cd3a0af
Merge branch 'master' of https://github.com/PythonOT/POT into lowrank…
laudavid 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
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,173 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
======================================== | ||
Low rank Gromov-Wasterstein between samples | ||
======================================== | ||
|
||
Comparaison between entropic Gromov-Wasserstein and Low Rank Gromov Wasserstein [67] | ||
on two curves in 2D and 3D, both sampled with 200 points. | ||
|
||
The squared Euclidean distance is considered as the ground cost for both samples. | ||
|
||
[67] Scetbon, M., Peyré, G. & Cuturi, M. (2022). | ||
"Linear-Time GromovWasserstein Distances using Low Rank Couplings and Costs". | ||
In International Conference on Machine Learning (ICML), 2022. | ||
""" | ||
|
||
# Author: Laurène David <laurene.david@ip-paris.fr> | ||
# | ||
# License: MIT License | ||
# | ||
# sphinx_gallery_thumbnail_number = 3 | ||
|
||
#%% | ||
import numpy as np | ||
import matplotlib.pylab as pl | ||
import ot.plot | ||
import time | ||
|
||
############################################################################## | ||
# Generate data | ||
# ------------- | ||
|
||
#%% parameters | ||
n_samples = 200 | ||
|
||
# Generate 2D and 3D curves | ||
theta = np.linspace(-4 * np.pi, 4 * np.pi, n_samples) | ||
z = np.linspace(1, 2, n_samples) | ||
r = z**2 + 1 | ||
x = r * np.sin(theta) | ||
y = r * np.cos(theta) | ||
|
||
# Source and target distribution | ||
X = np.concatenate([x.reshape(-1, 1), z.reshape(-1, 1)], axis=1) | ||
Y = np.concatenate([x.reshape(-1, 1), y.reshape(-1, 1), z.reshape(-1, 1)], axis=1) | ||
|
||
|
||
############################################################################## | ||
# Plot data | ||
# ------------ | ||
|
||
#%% | ||
# Plot the source and target samples | ||
fig = pl.figure(1, figsize=(10, 4)) | ||
|
||
ax = fig.add_subplot(121) | ||
ax.plot(X[:, 0], X[:, 1], color="blue", linewidth=6) | ||
ax.tick_params(left=False, right=False, labelleft=False, | ||
labelbottom=False, bottom=False) | ||
ax.set_title("2D curve (source)") | ||
|
||
ax2 = fig.add_subplot(122, projection="3d") | ||
ax2.plot(Y[:, 0], Y[:, 1], Y[:, 2], c='red', linewidth=6) | ||
ax2.tick_params(left=False, right=False, labelleft=False, | ||
labelbottom=False, bottom=False) | ||
ax2.view_init(15, -50) | ||
ax2.set_title("3D curve (target)") | ||
|
||
pl.tight_layout() | ||
pl.show() | ||
|
||
|
||
############################################################################## | ||
# Entropic Gromov-Wasserstein | ||
# ------------ | ||
|
||
#%% | ||
|
||
# Compute cost matrices | ||
C1 = ot.dist(X, X, metric="sqeuclidean") | ||
C2 = ot.dist(Y, Y, metric="sqeuclidean") | ||
|
||
# Scale cost matrices | ||
r1 = C1.max() | ||
r2 = C2.max() | ||
|
||
C1 = C1 / r1 | ||
C2 = C2 / r2 | ||
|
||
|
||
# Solve entropic gw | ||
reg = 5 * 1e-3 | ||
|
||
start = time.time() | ||
gw, log = ot.gromov.entropic_gromov_wasserstein( | ||
C1, C2, tol=1e-3, epsilon=reg, | ||
log=True, verbose=False) | ||
|
||
end = time.time() | ||
time_entropic = end - start | ||
|
||
entropic_gw_loss = np.round(log['gw_dist'], 3) | ||
|
||
# Plot entropic gw | ||
pl.figure(2) | ||
pl.imshow(gw, interpolation="nearest", aspect="auto") | ||
pl.title("Entropic Gromov-Wasserstein (loss={})".format(entropic_gw_loss)) | ||
pl.show() | ||
|
||
|
||
############################################################################## | ||
# Low rank squared euclidean cost matrices | ||
# ------------ | ||
# %% | ||
|
||
# Compute the low rank sqeuclidean cost decompositions | ||
A1, A2 = ot.lowrank.compute_lr_sqeuclidean_matrix(X, X, rescale_cost=False) | ||
B1, B2 = ot.lowrank.compute_lr_sqeuclidean_matrix(Y, Y, rescale_cost=False) | ||
|
||
# Scale the low rank cost matrices | ||
A1, A2 = A1 / np.sqrt(r1), A2 / np.sqrt(r1) | ||
B1, B2 = B1 / np.sqrt(r2), B2 / np.sqrt(r2) | ||
|
||
|
||
############################################################################## | ||
# Low rank Gromov-Wasserstein | ||
# ------------ | ||
# %% | ||
|
||
# Solve low rank gromov-wasserstein with different ranks | ||
list_rank = [10, 50] | ||
list_P_GW = [] | ||
list_loss_GW = [] | ||
list_time_GW = [] | ||
|
||
for rank in list_rank: | ||
start = time.time() | ||
|
||
Q, R, g, log = ot.lowrank_gromov_wasserstein_samples( | ||
X, Y, reg=0, rank=rank, rescale_cost=False, cost_factorized_Xs=(A1, A2), | ||
cost_factorized_Xt=(B1, B2), seed_init=49, numItermax=1000, log=True, stopThr=1e-6, | ||
) | ||
end = time.time() | ||
|
||
P = log["lazy_plan"][:] | ||
loss = log["value"] | ||
|
||
list_P_GW.append(P) | ||
list_loss_GW.append(np.round(loss, 3)) | ||
list_time_GW.append(end - start) | ||
|
||
|
||
# %% | ||
# Plot low rank GW with different ranks | ||
pl.figure(3, figsize=(10, 4)) | ||
|
||
pl.subplot(1, 2, 1) | ||
pl.imshow(list_P_GW[0], interpolation="nearest", aspect="auto") | ||
pl.title('Low rank GW (rank=10, loss={})'.format(list_loss_GW[0])) | ||
|
||
pl.subplot(1, 2, 2) | ||
pl.imshow(list_P_GW[1], interpolation="nearest", aspect="auto") | ||
pl.title('Low rank GW (rank=50, loss={})'.format(list_loss_GW[1])) | ||
|
||
pl.tight_layout() | ||
pl.show() | ||
|
||
|
||
# %% | ||
# Compare computation time between entropic GW and low rank GW | ||
print("Entropic GW: {:.2f}s".format(time_entropic)) | ||
print("Low rank GW (rank=10): {:.2f}s".format(list_time_GW[0])) | ||
print("Low rank GW (rank=50): {:.2f}s".format(list_time_GW[1])) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.