From 10f9b0d1b02c2b5f4c4eeac0c1f803657c89764b Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Thu, 31 May 2018 12:53:11 +0200 Subject: add example file for smooth OT --- examples/plot_OT_1D_smooth.py | 110 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 examples/plot_OT_1D_smooth.py (limited to 'examples/plot_OT_1D_smooth.py') diff --git a/examples/plot_OT_1D_smooth.py b/examples/plot_OT_1D_smooth.py new file mode 100644 index 0000000..4927617 --- /dev/null +++ b/examples/plot_OT_1D_smooth.py @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- +""" +==================== +1D optimal transport +==================== + +This example illustrates the computation of EMD, Sinkhorn and smooth OT plans +and their visualization. + +""" + +# Author: Remi Flamary +# +# 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 + +############################################################################## +# 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() -- cgit v1.2.3 From ed0d4171c6291a15360bdb8a955b0783585da749 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Thu, 31 May 2018 13:03:40 +0200 Subject: update readme --- README.md | 3 +++ examples/plot_OT_1D_smooth.py | 6 +++--- ot/smooth.py | 9 +++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) (limited to 'examples/plot_OT_1D_smooth.py') diff --git a/README.md b/README.md index 466c09c..b3068ee 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ It provides the following solvers: * OT Network Flow solver for the linear program/ Earth Movers Distance [1]. * Entropic regularization OT solver with Sinkhorn Knopp Algorithm [2] and stabilized version [9][10] with optional GPU implementation (requires cudamat). +* Smooth optimal transport solvers (dual and semi-dual) for KL and squared L2 regularization [17]. * Non regularized Wasserstein barycenters [16] with LP solver. * Bregman projections for Wasserstein barycenter [3] and unmixing [4]. * Optimal transport for domain adaptation with group lasso regularization [5] @@ -213,3 +214,5 @@ You can also post bug reports and feature requests in Github issues. Make sure t [15] Peyré, G., & Cuturi, M. (2018). [Computational Optimal Transport](https://arxiv.org/pdf/1803.00567.pdf) . [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. + +[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). diff --git a/examples/plot_OT_1D_smooth.py b/examples/plot_OT_1D_smooth.py index 4927617..dec84db 100644 --- a/examples/plot_OT_1D_smooth.py +++ b/examples/plot_OT_1D_smooth.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- """ -==================== -1D optimal transport -==================== +=========================== +1D smooth optimal transport +=========================== This example illustrates the computation of EMD, Sinkhorn and smooth OT plans and their visualization. diff --git a/ot/smooth.py b/ot/smooth.py index 1a9972b..b3649e9 100644 --- a/ot/smooth.py +++ b/ot/smooth.py @@ -22,6 +22,7 @@ #OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF #THE POSSIBILITY OF SUCH DAMAGE. + # Author: Mathieu Blondel # Remi Flamary @@ -31,6 +32,13 @@ Smooth and Sparse Optimal Transport. Mathieu Blondel, Vivien Seguy, Antoine Rolet. In Proc. of AISTATS 2018. https://arxiv.org/abs/1710.06276 + +[17] Blondel, M., Seguy, V., & Rolet, A. (2018). Smooth and Sparse Optimal +Transport. Proceedings of the Twenty-First International Conference on +Artificial Intelligence and Statistics (AISTATS). + +Original code from https://github.com/mblondel/smooth-ot/ + """ import numpy as np @@ -402,6 +410,7 @@ def get_plan_from_semi_dual(alpha, b, C, regul): def smooth_ot_dual(a, b, M, reg, reg_type='l2', method="L-BFGS-B", stopThr=1e-9, numItermax=500, log=False): + if reg_type.lower() in ['l2', 'squaredl2']: regul = SquaredL2(gamma=reg) -- cgit v1.2.3 From 724984d3e13aa9390ef17a389fbd6ccf1f277d69 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Thu, 31 May 2018 13:05:37 +0200 Subject: pep8 --- examples/plot_OT_1D_smooth.py | 2 +- ot/smooth.py | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'examples/plot_OT_1D_smooth.py') diff --git a/examples/plot_OT_1D_smooth.py b/examples/plot_OT_1D_smooth.py index dec84db..ec43279 100644 --- a/examples/plot_OT_1D_smooth.py +++ b/examples/plot_OT_1D_smooth.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ =========================== -1D smooth optimal transport +1D smooth optimal transport =========================== This example illustrates the computation of EMD, Sinkhorn and smooth OT plans diff --git a/ot/smooth.py b/ot/smooth.py index b3649e9..237b67a 100644 --- a/ot/smooth.py +++ b/ot/smooth.py @@ -22,7 +22,6 @@ #OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF #THE POSSIBILITY OF SUCH DAMAGE. - # Author: Mathieu Blondel # Remi Flamary @@ -33,8 +32,8 @@ Mathieu Blondel, Vivien Seguy, Antoine Rolet. In Proc. of AISTATS 2018. https://arxiv.org/abs/1710.06276 -[17] Blondel, M., Seguy, V., & Rolet, A. (2018). Smooth and Sparse Optimal -Transport. Proceedings of the Twenty-First International Conference on +[17] Blondel, M., Seguy, V., & Rolet, A. (2018). Smooth and Sparse Optimal +Transport. Proceedings of the Twenty-First International Conference on Artificial Intelligence and Statistics (AISTATS). Original code from https://github.com/mblondel/smooth-ot/ @@ -410,7 +409,6 @@ def get_plan_from_semi_dual(alpha, b, C, regul): def smooth_ot_dual(a, b, M, reg, reg_type='l2', method="L-BFGS-B", stopThr=1e-9, numItermax=500, log=False): - if reg_type.lower() in ['l2', 'squaredl2']: regul = SquaredL2(gamma=reg) -- cgit v1.2.3 From ed1f8d5497a15d926b992b5a42b14dccd3eb97d4 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 11 Jun 2018 12:05:12 +0200 Subject: correct dataset function name --- examples/plot_OT_1D_smooth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/plot_OT_1D_smooth.py') diff --git a/examples/plot_OT_1D_smooth.py b/examples/plot_OT_1D_smooth.py index ec43279..b690751 100644 --- a/examples/plot_OT_1D_smooth.py +++ b/examples/plot_OT_1D_smooth.py @@ -17,7 +17,7 @@ import numpy as np import matplotlib.pylab as pl import ot import ot.plot -from ot.datasets import get_1D_gauss as gauss +from ot.datasets import make_1D_gauss as gauss ############################################################################## # Generate data -- cgit v1.2.3