-
Notifications
You must be signed in to change notification settings - Fork 528
Smooth and Sparse OT #50
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
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8d28897
add smooth.py + first tests
rflamary af99d3f
add smooth.py + first tests
rflamary e585d64
pep8
rflamary b188381
add semidual
rflamary 6a048fe
add test worngregularization
rflamary 1d34716
remove unused variable
rflamary 10f9b0d
add example file for smooth OT
rflamary ed0d417
update readme
rflamary 724984d
pep8
rflamary fb883fc
proper documentation
rflamary d370f18
bug verbose semi-dual
rflamary 8046b8c
pep8
rflamary ef17fcd
Merge branch 'master' into smooth_ot
rflamary 4854277
correct bibtex reference
rflamary 927ec79
correct readme typo
rflamary 77d80f8
update documentation
rflamary ed1f8d5
correct dataset function name
rflamary f22be34
add smooth to documentation
rflamary de4f7ba
proper definition of all dor lp
rflamary ecb093b
ad documentation class Regularization
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,110 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
=========================== | ||
1D smooth optimal transport | ||
=========================== | ||
|
||
This example illustrates the computation of EMD, Sinkhorn and smooth OT plans | ||
and their visualization. | ||
|
||
""" | ||
|
||
# Author: Remi Flamary <remi.flamary@unice.fr> | ||
# | ||
# License: MIT License | ||
|
||
import numpy as np | ||
import matplotlib.pylab as pl | ||
import ot | ||
import ot.plot | ||
from ot.datasets import get_1D_gauss as gauss | ||
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. make_1D_gauss now |
||
|
||
############################################################################## | ||
# Generate data | ||
# ------------- | ||
|
||
|
||
#%% parameters | ||
|
||
n = 100 # nb bins | ||
|
||
# bin positions | ||
x = np.arange(n, dtype=np.float64) | ||
|
||
# Gaussian distributions | ||
a = gauss(n, m=20, s=5) # m= mean, s= std | ||
b = gauss(n, m=60, s=10) | ||
|
||
# loss matrix | ||
M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) | ||
M /= M.max() | ||
|
||
|
||
############################################################################## | ||
# Plot distributions and loss matrix | ||
# ---------------------------------- | ||
|
||
#%% plot the distributions | ||
|
||
pl.figure(1, figsize=(6.4, 3)) | ||
pl.plot(x, a, 'b', label='Source distribution') | ||
pl.plot(x, b, 'r', label='Target distribution') | ||
pl.legend() | ||
|
||
#%% plot distributions and loss matrix | ||
|
||
pl.figure(2, figsize=(5, 5)) | ||
ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') | ||
|
||
############################################################################## | ||
# Solve EMD | ||
# --------- | ||
|
||
|
||
#%% EMD | ||
|
||
G0 = ot.emd(a, b, M) | ||
|
||
pl.figure(3, figsize=(5, 5)) | ||
ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') | ||
|
||
############################################################################## | ||
# Solve Sinkhorn | ||
# -------------- | ||
|
||
|
||
#%% Sinkhorn | ||
|
||
lambd = 2e-3 | ||
Gs = ot.sinkhorn(a, b, M, lambd, verbose=True) | ||
|
||
pl.figure(4, figsize=(5, 5)) | ||
ot.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn') | ||
|
||
pl.show() | ||
|
||
############################################################################## | ||
# Solve Smooth OT | ||
# -------------- | ||
|
||
|
||
#%% Smooth OT with KL regularization | ||
|
||
lambd = 2e-3 | ||
Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='kl') | ||
|
||
pl.figure(5, figsize=(5, 5)) | ||
ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT KL reg.') | ||
|
||
pl.show() | ||
|
||
|
||
#%% Smooth OT with KL regularization | ||
|
||
lambd = 1e-1 | ||
Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='l2') | ||
|
||
pl.figure(6, figsize=(5, 5)) | ||
ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT l2 reg.') | ||
|
||
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
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.
regularization's'