Skip to content

Commit 327b0c6

Browse files
authored
Merge pull request #50 from rflamary/smooth_ot
Smooth and Sparse OT
2 parents 530dc93 + ecb093b commit 327b0c6

File tree

8 files changed

+837
-21
lines changed

8 files changed

+837
-21
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ It provides the following solvers:
1515

1616
* OT Network Flow solver for the linear program/ Earth Movers Distance [1].
1717
* Entropic regularization OT solver with Sinkhorn Knopp Algorithm [2] and stabilized version [9][10] with optional GPU implementation (requires cudamat).
18-
* Non regularized Wasserstein barycenters [16] with LP solver.
18+
* Smooth optimal transport solvers (dual and semi-dual) for KL and squared L2 regularizations [17].
19+
* Non regularized Wasserstein barycenters [16] with LP solver (only small scale).
1920
* Bregman projections for Wasserstein barycenter [3] and unmixing [4].
2021
* Optimal transport for domain adaptation with group lasso regularization [5]
2122
* Conditional gradient [6] and Generalized conditional gradient for regularized OT [7].
@@ -29,10 +30,11 @@ Some demonstrations (both in Python and Jupyter Notebook format) are available i
2930

3031
If you use this toolbox in your research and find it useful, please cite POT using the following bibtex reference:
3132
```
32-
@article{flamary2017pot,
33-
title={POT Python Optimal Transport library},
34-
author={Flamary, R{\'e}mi and Courty, Nicolas},
35-
year={2017}
33+
@misc{flamary2017pot,
34+
title={POT Python Optimal Transport library},
35+
author={Flamary, R{'e}mi and Courty, Nicolas},
36+
url={https://github.com/rflamary/POT},
37+
year={2017}
3638
}
3739
```
3840

@@ -215,3 +217,5 @@ You can also post bug reports and feature requests in Github issues. Make sure t
215217
[15] Peyré, G., & Cuturi, M. (2018). [Computational Optimal Transport](https://arxiv.org/pdf/1803.00567.pdf) .
216218

217219
[16] Agueh, M., & Carlier, G. (2011). [Barycenters in the Wasserstein space](https://hal.archives-ouvertes.fr/hal-00637399/document). SIAM Journal on Mathematical Analysis, 43(2), 904-924.
220+
221+
[17] Blondel, M., Seguy, V., & Rolet, A. (2018). [Smooth and Sparse Optimal Transport](https://arxiv.org/abs/1710.06276). Proceedings of the Twenty-First International Conference on Artificial Intelligence and Statistics (AISTATS).

docs/source/all.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ ot.bregman
1919

2020
.. automodule:: ot.bregman
2121
:members:
22+
23+
ot.smooth
24+
-----
25+
.. automodule:: ot.smooth
26+
:members:
2227

2328
ot.gromov
2429
----------

docs/source/readme.rst

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ It provides the following solvers:
1414
[1].
1515
- Entropic regularization OT solver with Sinkhorn Knopp Algorithm [2]
1616
and stabilized version [9][10] with optional GPU implementation
17-
(required cudamat).
17+
(requires cudamat).
18+
- Smooth optimal transport solvers (dual and semi-dual) for KL and
19+
squared L2 regularizations [17].
20+
- Non regularized Wasserstein barycenters [16] with LP solver (only
21+
small scale).
1822
- Bregman projections for Wasserstein barycenter [3] and unmixing [4].
1923
- Optimal transport for domain adaptation with group lasso
2024
regularization [5]
@@ -29,6 +33,21 @@ It provides the following solvers:
2933
Some demonstrations (both in Python and Jupyter Notebook format) are
3034
available in the examples folder.
3135

36+
Using and citing the toolbox
37+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
If you use this toolbox in your research and find it useful, please cite
40+
POT using the following bibtex reference:
41+
42+
::
43+
44+
@misc{flamary2017pot,
45+
title={POT Python Optimal Transport library},
46+
author={Flamary, R{'e}mi and Courty, Nicolas},
47+
url={https://github.com/rflamary/POT},
48+
year={2017}
49+
}
50+
3251
Installation
3352
------------
3453

@@ -37,7 +56,7 @@ C++ compiler for using the EMD solver and relies on the following Python
3756
modules:
3857

3958
- Numpy (>=1.11)
40-
- Scipy (>=0.17)
59+
- Scipy (>=1.0)
4160
- Cython (>=0.23)
4261
- Matplotlib (>=1.5)
4362

@@ -212,20 +231,6 @@ languages):
212231
- `Marco Cuturi <http://marcocuturi.net/>`__ (Sinkhorn Knopp in
213232
Matlab/Cuda)
214233

215-
Using and citing the toolbox
216-
----------------------------
217-
218-
If you use this toolbox in your research and find it useful, please cite
219-
POT using the following bibtex reference:
220-
221-
::
222-
223-
@article{flamary2017pot,
224-
title={POT Python Optimal Transport library},
225-
author={Flamary, R{\'e}mi and Courty, Nicolas},
226-
year={2017}
227-
}
228-
229234
Contributions and code of conduct
230235
---------------------------------
231236

@@ -320,6 +325,15 @@ Journal of Optimization Theory and Applications Vol 43.
320325
[15] Peyré, G., & Cuturi, M. (2018). `Computational Optimal
321326
Transport <https://arxiv.org/pdf/1803.00567.pdf>`__ .
322327

328+
[16] Agueh, M., & Carlier, G. (2011). `Barycenters in the Wasserstein
329+
space <https://hal.archives-ouvertes.fr/hal-00637399/document>`__. SIAM
330+
Journal on Mathematical Analysis, 43(2), 904-924.
331+
332+
[17] Blondel, M., Seguy, V., & Rolet, A. (2018). `Smooth and Sparse
333+
Optimal Transport <https://arxiv.org/abs/1710.06276>`__. Proceedings of
334+
the Twenty-First International Conference on Artificial Intelligence and
335+
Statistics (AISTATS).
336+
323337
.. |PyPI version| image:: https://badge.fury.io/py/POT.svg
324338
:target: https://badge.fury.io/py/POT
325339
.. |Anaconda Cloud| image:: https://anaconda.org/conda-forge/pot/badges/version.svg

examples/plot_OT_1D_smooth.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
===========================
4+
1D smooth optimal transport
5+
===========================
6+
7+
This example illustrates the computation of EMD, Sinkhorn and smooth OT plans
8+
and their visualization.
9+
10+
"""
11+
12+
# Author: Remi Flamary <remi.flamary@unice.fr>
13+
#
14+
# License: MIT License
15+
16+
import numpy as np
17+
import matplotlib.pylab as pl
18+
import ot
19+
import ot.plot
20+
from ot.datasets import make_1D_gauss as gauss
21+
22+
##############################################################################
23+
# Generate data
24+
# -------------
25+
26+
27+
#%% parameters
28+
29+
n = 100 # nb bins
30+
31+
# bin positions
32+
x = np.arange(n, dtype=np.float64)
33+
34+
# Gaussian distributions
35+
a = gauss(n, m=20, s=5) # m= mean, s= std
36+
b = gauss(n, m=60, s=10)
37+
38+
# loss matrix
39+
M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)))
40+
M /= M.max()
41+
42+
43+
##############################################################################
44+
# Plot distributions and loss matrix
45+
# ----------------------------------
46+
47+
#%% plot the distributions
48+
49+
pl.figure(1, figsize=(6.4, 3))
50+
pl.plot(x, a, 'b', label='Source distribution')
51+
pl.plot(x, b, 'r', label='Target distribution')
52+
pl.legend()
53+
54+
#%% plot distributions and loss matrix
55+
56+
pl.figure(2, figsize=(5, 5))
57+
ot.plot.plot1D_mat(a, b, M, 'Cost matrix M')
58+
59+
##############################################################################
60+
# Solve EMD
61+
# ---------
62+
63+
64+
#%% EMD
65+
66+
G0 = ot.emd(a, b, M)
67+
68+
pl.figure(3, figsize=(5, 5))
69+
ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0')
70+
71+
##############################################################################
72+
# Solve Sinkhorn
73+
# --------------
74+
75+
76+
#%% Sinkhorn
77+
78+
lambd = 2e-3
79+
Gs = ot.sinkhorn(a, b, M, lambd, verbose=True)
80+
81+
pl.figure(4, figsize=(5, 5))
82+
ot.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn')
83+
84+
pl.show()
85+
86+
##############################################################################
87+
# Solve Smooth OT
88+
# --------------
89+
90+
91+
#%% Smooth OT with KL regularization
92+
93+
lambd = 2e-3
94+
Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='kl')
95+
96+
pl.figure(5, figsize=(5, 5))
97+
ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT KL reg.')
98+
99+
pl.show()
100+
101+
102+
#%% Smooth OT with KL regularization
103+
104+
lambd = 1e-1
105+
Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='l2')
106+
107+
pl.figure(6, figsize=(5, 5))
108+
ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT l2 reg.')
109+
110+
pl.show()

ot/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from . import datasets
1919
from . import da
2020
from . import gromov
21+
from . import smooth
22+
2123

2224
# OT functions
2325
from .lp import emd, emd2

ot/lp/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from ..utils import parmap
1919
from .cvx import barycenter
2020

21+
__all__=['emd', 'emd2', 'barycenter', 'cvx']
22+
2123

2224
def emd(a, b, M, numItermax=100000, log=False):
2325
"""Solves the Earth Movers distance problem and returns the OT matrix

0 commit comments

Comments
 (0)