-
Notifications
You must be signed in to change notification settings - Fork 524
[MRG] Initialization for Low Rank Sinkhorn solver #588
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
47 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,129 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
======================================== | ||
Low rank Sinkhorn | ||
======================================== | ||
|
||
This example illustrates the computation of Low Rank Sinkhorn [26]. | ||
|
||
[65] Scetbon, M., Cuturi, M., & Peyré, G. (2021). | ||
"Low-rank Sinkhorn factorization". In International Conference on Machine Learning. | ||
""" | ||
|
||
# Author: Laurène David <laurene.david@ip-paris.fr> | ||
# | ||
# License: MIT License | ||
# | ||
# sphinx_gallery_thumbnail_number = 2 | ||
|
||
import numpy as np | ||
import matplotlib.pylab as pl | ||
import ot.plot | ||
from ot.datasets import make_1D_gauss as gauss | ||
|
||
############################################################################## | ||
# Generate data | ||
# ------------- | ||
|
||
#%% parameters | ||
|
||
n = 100 | ||
m = 120 | ||
|
||
# Gaussian distribution | ||
a = gauss(n, m=int(n / 3), s=25 / np.sqrt(2)) + 1.5 * gauss(n, m=int(5 * n / 6), s=15 / np.sqrt(2)) | ||
a = a / np.sum(a) | ||
|
||
b = 2 * gauss(m, m=int(m / 5), s=30 / np.sqrt(2)) + gauss(m, m=int(m / 2), s=35 / np.sqrt(2)) | ||
b = b / np.sum(b) | ||
|
||
# Source and target distribution | ||
X = np.arange(n).reshape(-1, 1) | ||
Y = np.arange(m).reshape(-1, 1) | ||
|
||
|
||
############################################################################## | ||
# Solve Low rank sinkhorn | ||
# ------------ | ||
|
||
#%% | ||
# Solve low rank sinkhorn | ||
Q, R, g, log = ot.lowrank_sinkhorn(X, Y, a, b, rank=10, init="random", gamma_init="rescale", rescale_cost=True, warn=False, log=True) | ||
P = log["lazy_plan"][:] | ||
|
||
ot.plot.plot1D_mat(a, b, P, 'OT matrix Low rank') | ||
|
||
|
||
############################################################################## | ||
# Sinkhorn vs Low Rank Sinkhorn | ||
# ----------------------- | ||
# Compare Sinkhorn and Low rank sinkhorn with different regularizations and ranks. | ||
|
||
#%% Sinkhorn | ||
|
||
# Compute cost matrix for sinkhorn OT | ||
M = ot.dist(X, Y) | ||
M = M / np.max(M) | ||
|
||
# Solve sinkhorn with different regularizations using ot.solve | ||
list_reg = [0.05, 0.005, 0.001] | ||
list_P_Sin = [] | ||
|
||
for reg in list_reg: | ||
P = ot.solve(M, a, b, reg=reg, max_iter=2000, tol=1e-8).plan | ||
list_P_Sin.append(P) | ||
|
||
#%% Low rank sinkhorn | ||
|
||
# Solve low rank sinkhorn with different ranks using ot.solve_sample | ||
list_rank = [3, 10, 50] | ||
list_P_LR = [] | ||
|
||
for rank in list_rank: | ||
P = ot.solve_sample(X, Y, a, b, method='lowrank', rank=rank).plan | ||
P = P[:] | ||
list_P_LR.append(P) | ||
|
||
|
||
#%% | ||
|
||
# Plot sinkhorn vs low rank sinkhorn | ||
pl.figure(1, figsize=(10, 4)) | ||
|
||
pl.subplot(1, 3, 1) | ||
pl.imshow(list_P_Sin[0], interpolation='nearest') | ||
pl.axis('off') | ||
pl.title('Sinkhorn (reg=0.05)') | ||
|
||
pl.subplot(1, 3, 2) | ||
pl.imshow(list_P_Sin[1], interpolation='nearest') | ||
pl.axis('off') | ||
pl.title('Sinkhorn (reg=0.005)') | ||
|
||
pl.subplot(1, 3, 3) | ||
pl.imshow(list_P_Sin[2], interpolation='nearest') | ||
pl.axis('off') | ||
pl.title('Sinkhorn (reg=0.001)') | ||
pl.show() | ||
|
||
|
||
#%% | ||
|
||
pl.figure(2, figsize=(10, 4)) | ||
|
||
pl.subplot(1, 3, 1) | ||
pl.imshow(list_P_LR[0], interpolation='nearest') | ||
pl.axis('off') | ||
pl.title('Low rank (rank=3)') | ||
|
||
pl.subplot(1, 3, 2) | ||
pl.imshow(list_P_LR[1], interpolation='nearest') | ||
pl.axis('off') | ||
pl.title('Low rank (rank=10)') | ||
|
||
pl.subplot(1, 3, 3) | ||
pl.imshow(list_P_LR[2], interpolation='nearest') | ||
pl.axis('off') | ||
pl.title('Low rank (rank=50)') | ||
|
||
pl.tight_layout() |
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.