From f12153c0c1be6f6377ace0050201409ec1b7e829 Mon Sep 17 00:00:00 2001 From: RĂ©mi Flamary Date: Wed, 29 Aug 2018 14:10:04 +0200 Subject: update documentation examples --- docs/cache_nbrun | 2 +- .../source/auto_examples/auto_examples_jupyter.zip | Bin 99990 -> 118510 bytes docs/source/auto_examples/auto_examples_python.zip | Bin 68178 -> 79057 bytes .../images/sphx_glr_plot_OT_1D_smooth_001.png | Bin 0 -> 21372 bytes .../images/sphx_glr_plot_OT_1D_smooth_002.png | Bin 0 -> 22051 bytes .../images/sphx_glr_plot_OT_1D_smooth_005.png | Bin 0 -> 17080 bytes .../images/sphx_glr_plot_OT_1D_smooth_007.png | Bin 0 -> 19405 bytes .../images/sphx_glr_plot_OT_1D_smooth_009.png | Bin 0 -> 20630 bytes .../images/sphx_glr_plot_OT_1D_smooth_010.png | Bin 0 -> 19232 bytes ...sphx_glr_plot_barycenter_lp_vs_entropic_003.png | Bin 0 -> 14405 bytes ...sphx_glr_plot_barycenter_lp_vs_entropic_004.png | Bin 0 -> 33271 bytes ...sphx_glr_plot_barycenter_lp_vs_entropic_006.png | Bin 0 -> 70940 bytes .../sphx_glr_plot_free_support_barycenter_001.png | Bin 0 -> 31553 bytes .../images/sphx_glr_plot_stochastic_004.png | Bin 0 -> 10450 bytes .../images/sphx_glr_plot_stochastic_005.png | Bin 0 -> 10677 bytes .../images/sphx_glr_plot_stochastic_006.png | Bin 0 -> 9131 bytes .../images/sphx_glr_plot_stochastic_007.png | Bin 0 -> 9563 bytes .../images/sphx_glr_plot_stochastic_008.png | Bin 0 -> 9131 bytes .../thumb/sphx_glr_plot_OT_1D_smooth_thumb.png | Bin 0 -> 14983 bytes ...sphx_glr_plot_free_support_barycenter_thumb.png | Bin 0 -> 19601 bytes .../thumb/sphx_glr_plot_stochastic_thumb.png | Bin 0 -> 17541 bytes docs/source/auto_examples/index.rst | 60 +++ docs/source/auto_examples/plot_OT_1D_smooth.ipynb | 144 +++++++ docs/source/auto_examples/plot_OT_1D_smooth.py | 110 +++++ docs/source/auto_examples/plot_OT_1D_smooth.rst | 242 +++++++++++ .../plot_free_support_barycenter.ipynb | 108 +++++ .../auto_examples/plot_free_support_barycenter.py | 69 +++ .../auto_examples/plot_free_support_barycenter.rst | 140 ++++++ docs/source/auto_examples/plot_stochastic.ipynb | 331 ++++++++++++++ docs/source/auto_examples/plot_stochastic.py | 207 +++++++++ docs/source/auto_examples/plot_stochastic.rst | 475 +++++++++++++++++++++ 31 files changed, 1887 insertions(+), 1 deletion(-) create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_003.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_004.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_006.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_stochastic_004.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_stochastic_005.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_stochastic_006.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_stochastic_008.png create mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png create mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png create mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png create mode 100644 docs/source/auto_examples/plot_OT_1D_smooth.ipynb create mode 100644 docs/source/auto_examples/plot_OT_1D_smooth.py create mode 100644 docs/source/auto_examples/plot_OT_1D_smooth.rst create mode 100644 docs/source/auto_examples/plot_free_support_barycenter.ipynb create mode 100644 docs/source/auto_examples/plot_free_support_barycenter.py create mode 100644 docs/source/auto_examples/plot_free_support_barycenter.rst create mode 100644 docs/source/auto_examples/plot_stochastic.ipynb create mode 100644 docs/source/auto_examples/plot_stochastic.py create mode 100644 docs/source/auto_examples/plot_stochastic.rst diff --git a/docs/cache_nbrun b/docs/cache_nbrun index 318bcf4..2781d81 100644 --- a/docs/cache_nbrun +++ b/docs/cache_nbrun @@ -1 +1 @@ -{"plot_otda_mapping_colors_images.ipynb": "4f0587a00a3c082799a75a0ed36e9ce1", "plot_optim_OTreg.ipynb": "481801bb0d133ef350a65179cf8f739a", "plot_otda_color_images.ipynb": "d047d635f4987c81072383241590e21f", "plot_WDA.ipynb": "27f8de4c6d7db46497076523673eedfb", "plot_otda_linear_mapping.ipynb": "a472c767abe82020e0a58125a528785c", "plot_OT_L1_vs_L2.ipynb": "5d565b8aaf03be4309eba731127851dc", "plot_barycenter_1D.ipynb": "6063193f9ac87517acced2625edb9a54", "plot_otda_classes.ipynb": "39087b6e98217851575f2271c22853a4", "plot_otda_d2.ipynb": "e6feae588103f2a8fab942e5f4eff483", "plot_otda_mapping.ipynb": "2f1ebbdc0f855d9e2b7adf9edec24d25", "plot_gromov.ipynb": "24f2aea489714d34779521f46d5e2c47", "plot_compute_emd.ipynb": "f5cd71cad882ec157dc8222721e9820c", "plot_OT_1D.ipynb": "b5348bdc561c07ec168a1622e5af4b93", "plot_gromov_barycenter.ipynb": "953e5047b886ec69ec621ec52f5e21d1", "plot_otda_semi_supervised.ipynb": "f6dfb02ba2bbd939408ffcd22a3b007c", "plot_OT_2D_samples.ipynb": "07dbc14859fa019a966caa79fa0825bd", "plot_barycenter_lp_vs_entropic.ipynb": "51833e8c76aaedeba9599ac7a30eb357"} \ No newline at end of file +{"plot_otda_mapping_colors_images.ipynb": "4f0587a00a3c082799a75a0ed36e9ce1", "plot_optim_OTreg.ipynb": "481801bb0d133ef350a65179cf8f739a", "plot_barycenter_1D.ipynb": "6063193f9ac87517acced2625edb9a54", "plot_stochastic.ipynb": "e2c520150378ae4635f74509f687fa01", "plot_WDA.ipynb": "27f8de4c6d7db46497076523673eedfb", "plot_otda_linear_mapping.ipynb": "a472c767abe82020e0a58125a528785c", "plot_OT_1D_smooth.ipynb": "3a059103652225a0c78ea53895cf79e5", "plot_OT_L1_vs_L2.ipynb": "5d565b8aaf03be4309eba731127851dc", "plot_otda_color_images.ipynb": "d047d635f4987c81072383241590e21f", "plot_otda_classes.ipynb": "39087b6e98217851575f2271c22853a4", "plot_otda_d2.ipynb": "e6feae588103f2a8fab942e5f4eff483", "plot_otda_mapping.ipynb": "2f1ebbdc0f855d9e2b7adf9edec24d25", "plot_gromov.ipynb": "24f2aea489714d34779521f46d5e2c47", "plot_compute_emd.ipynb": "f5cd71cad882ec157dc8222721e9820c", "plot_OT_1D.ipynb": "b5348bdc561c07ec168a1622e5af4b93", "plot_gromov_barycenter.ipynb": "953e5047b886ec69ec621ec52f5e21d1", "plot_free_support_barycenter.ipynb": "246dd2feff4b233a4f1a553c5a202fdc", "plot_otda_semi_supervised.ipynb": "f6dfb02ba2bbd939408ffcd22a3b007c", "plot_OT_2D_samples.ipynb": "07dbc14859fa019a966caa79fa0825bd", "plot_barycenter_lp_vs_entropic.ipynb": "51833e8c76aaedeba9599ac7a30eb357"} \ No newline at end of file diff --git a/docs/source/auto_examples/auto_examples_jupyter.zip b/docs/source/auto_examples/auto_examples_jupyter.zip index 8102274..2325d59 100644 Binary files a/docs/source/auto_examples/auto_examples_jupyter.zip and b/docs/source/auto_examples/auto_examples_jupyter.zip differ diff --git a/docs/source/auto_examples/auto_examples_python.zip b/docs/source/auto_examples/auto_examples_python.zip index d685070..694d704 100644 Binary files a/docs/source/auto_examples/auto_examples_python.zip and b/docs/source/auto_examples/auto_examples_python.zip differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png new file mode 100644 index 0000000..6e74d89 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png new file mode 100644 index 0000000..0407e44 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png new file mode 100644 index 0000000..4421bc7 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png new file mode 100644 index 0000000..52638e3 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png new file mode 100644 index 0000000..c5078cf Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png new file mode 100644 index 0000000..58e87b6 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_003.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_003.png new file mode 100644 index 0000000..eb04b1a Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_004.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_004.png new file mode 100644 index 0000000..a9f44ba Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_004.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_006.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_006.png new file mode 100644 index 0000000..e53928e Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_006.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png b/docs/source/auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png new file mode 100644 index 0000000..d7bc78a Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_004.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_004.png new file mode 100644 index 0000000..8aada91 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_004.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_005.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_005.png new file mode 100644 index 0000000..3d1e239 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_005.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_006.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_006.png new file mode 100644 index 0000000..335ea95 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_006.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png new file mode 100644 index 0000000..986aa96 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_008.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_008.png new file mode 100644 index 0000000..335ea95 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_008.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png new file mode 100644 index 0000000..4679eb6 Binary files /dev/null and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png new file mode 100644 index 0000000..0861d4d Binary files /dev/null and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png new file mode 100644 index 0000000..609339d Binary files /dev/null and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png differ diff --git a/docs/source/auto_examples/index.rst b/docs/source/auto_examples/index.rst index 69fb320..77a46aa 100644 --- a/docs/source/auto_examples/index.rst +++ b/docs/source/auto_examples/index.rst @@ -47,6 +47,46 @@ This is a gallery of all the POT example files. /auto_examples/plot_optim_OTreg +.. raw:: html + +
+ +.. only:: html + + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png + + :ref:`sphx_glr_auto_examples_plot_free_support_barycenter.py` + +.. raw:: html + +
+ + +.. toctree:: + :hidden: + + /auto_examples/plot_free_support_barycenter + +.. raw:: html + +
+ +.. only:: html + + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png + + :ref:`sphx_glr_auto_examples_plot_OT_1D_smooth.py` + +.. raw:: html + +
+ + +.. toctree:: + :hidden: + + /auto_examples/plot_OT_1D_smooth + .. raw:: html
@@ -147,6 +187,26 @@ This is a gallery of all the POT example files. /auto_examples/plot_WDA +.. raw:: html + +
+ +.. only:: html + + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png + + :ref:`sphx_glr_auto_examples_plot_stochastic.py` + +.. raw:: html + +
+ + +.. toctree:: + :hidden: + + /auto_examples/plot_stochastic + .. raw:: html
diff --git a/docs/source/auto_examples/plot_OT_1D_smooth.ipynb b/docs/source/auto_examples/plot_OT_1D_smooth.ipynb new file mode 100644 index 0000000..d523f6a --- /dev/null +++ b/docs/source/auto_examples/plot_OT_1D_smooth.ipynb @@ -0,0 +1,144 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n# 1D smooth optimal transport\n\n\nThis example illustrates the computation of EMD, Sinkhorn and smooth OT plans\nand their visualization.\n\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\nimport ot.plot\nfrom ot.datasets import make_1D_gauss as gauss" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate data\n-------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% parameters\n\nn = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na = gauss(n, m=20, s=5) # m= mean, s= std\nb = gauss(n, m=60, s=10)\n\n# loss matrix\nM = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)))\nM /= M.max()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot distributions and loss matrix\n----------------------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% plot the distributions\n\npl.figure(1, figsize=(6.4, 3))\npl.plot(x, a, 'b', label='Source distribution')\npl.plot(x, b, 'r', label='Target distribution')\npl.legend()\n\n#%% plot distributions and loss matrix\n\npl.figure(2, figsize=(5, 5))\not.plot.plot1D_mat(a, b, M, 'Cost matrix M')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Solve EMD\n---------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% EMD\n\nG0 = ot.emd(a, b, M)\n\npl.figure(3, figsize=(5, 5))\not.plot.plot1D_mat(a, b, G0, 'OT matrix G0')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Solve Sinkhorn\n--------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% Sinkhorn\n\nlambd = 2e-3\nGs = ot.sinkhorn(a, b, M, lambd, verbose=True)\n\npl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn')\n\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Solve Smooth OT\n--------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% Smooth OT with KL regularization\n\nlambd = 2e-3\nGsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='kl')\n\npl.figure(5, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT KL reg.')\n\npl.show()\n\n\n#%% Smooth OT with KL regularization\n\nlambd = 1e-1\nGsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='l2')\n\npl.figure(6, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT l2 reg.')\n\npl.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_OT_1D_smooth.py b/docs/source/auto_examples/plot_OT_1D_smooth.py new file mode 100644 index 0000000..b690751 --- /dev/null +++ b/docs/source/auto_examples/plot_OT_1D_smooth.py @@ -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 +# +# License: MIT License + +import numpy as np +import matplotlib.pylab as pl +import ot +import ot.plot +from ot.datasets import make_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() diff --git a/docs/source/auto_examples/plot_OT_1D_smooth.rst b/docs/source/auto_examples/plot_OT_1D_smooth.rst new file mode 100644 index 0000000..5a0ebd3 --- /dev/null +++ b/docs/source/auto_examples/plot_OT_1D_smooth.rst @@ -0,0 +1,242 @@ + + +.. _sphx_glr_auto_examples_plot_OT_1D_smooth.py: + + +=========================== +1D smooth optimal transport +=========================== + +This example illustrates the computation of EMD, Sinkhorn and smooth OT plans +and their visualization. + + + + +.. code-block:: python + + + # Author: Remi Flamary + # + # License: MIT License + + import numpy as np + import matplotlib.pylab as pl + import ot + import ot.plot + from ot.datasets import make_1D_gauss as gauss + + + + + + + +Generate data +------------- + + + +.. code-block:: python + + + + #%% 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 +---------------------------------- + + + +.. code-block:: python + + + #%% 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') + + + + +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png + :scale: 47 + + + + +Solve EMD +--------- + + + +.. code-block:: python + + + + #%% EMD + + G0 = ot.emd(a, b, M) + + pl.figure(3, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') + + + + +.. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png + :align: center + + + + +Solve Sinkhorn +-------------- + + + +.. code-block:: python + + + + #%% 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() + + + + +.. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png + :align: center + + +.. rst-class:: sphx-glr-script-out + + Out:: + + It. |Err + ------------------- + 0|7.958844e-02| + 10|5.921715e-03| + 20|1.238266e-04| + 30|2.469780e-06| + 40|4.919966e-08| + 50|9.800197e-10| + + +Solve Smooth OT +-------------- + + + +.. code-block:: python + + + + #%% 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() + + + +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png + :scale: 47 + + + + +**Total running time of the script:** ( 0 minutes 1.053 seconds) + + + +.. only :: html + + .. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_OT_1D_smooth.py ` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_OT_1D_smooth.ipynb ` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_free_support_barycenter.ipynb b/docs/source/auto_examples/plot_free_support_barycenter.ipynb new file mode 100644 index 0000000..05a81c8 --- /dev/null +++ b/docs/source/auto_examples/plot_free_support_barycenter.ipynb @@ -0,0 +1,108 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n# 2D free support Wasserstein barycenters of distributions\n\n\nIllustration of 2D Wasserstein barycenters if discributions that are weighted\nsum of diracs.\n\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Author: Vivien Seguy \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate data\n -------------\n%% parameters and data generation\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "N = 3\nd = 2\nmeasures_locations = []\nmeasures_weights = []\n\nfor i in range(N):\n\n n_i = np.random.randint(low=1, high=20) # nb samples\n\n mu_i = np.random.normal(0., 4., (d,)) # Gaussian mean\n\n A_i = np.random.rand(d, d)\n cov_i = np.dot(A_i, A_i.transpose()) # Gaussian covariance matrix\n\n x_i = ot.datasets.make_2D_samples_gauss(n_i, mu_i, cov_i) # Dirac locations\n b_i = np.random.uniform(0., 1., (n_i,))\n b_i = b_i / np.sum(b_i) # Dirac weights\n\n measures_locations.append(x_i)\n measures_weights.append(b_i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compute free support barycenter\n-------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "k = 10 # number of Diracs of the barycenter\nX_init = np.random.normal(0., 1., (k, d)) # initial Dirac locations\nb = np.ones((k,)) / k # weights of the barycenter (it will not be optimized, only the locations are optimized)\n\nX = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot data\n---------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(1)\nfor (x_i, b_i) in zip(measures_locations, measures_weights):\n color = np.random.randint(low=1, high=10 * N)\n pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure')\npl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter')\npl.title('Data measures and their barycenter')\npl.legend(loc=0)\npl.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_free_support_barycenter.py b/docs/source/auto_examples/plot_free_support_barycenter.py new file mode 100644 index 0000000..b6efc59 --- /dev/null +++ b/docs/source/auto_examples/plot_free_support_barycenter.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +""" +==================================================== +2D free support Wasserstein barycenters of distributions +==================================================== + +Illustration of 2D Wasserstein barycenters if discributions that are weighted +sum of diracs. + +""" + +# Author: Vivien Seguy +# +# License: MIT License + +import numpy as np +import matplotlib.pylab as pl +import ot + + +############################################################################## +# Generate data +# ------------- +#%% parameters and data generation +N = 3 +d = 2 +measures_locations = [] +measures_weights = [] + +for i in range(N): + + n_i = np.random.randint(low=1, high=20) # nb samples + + mu_i = np.random.normal(0., 4., (d,)) # Gaussian mean + + A_i = np.random.rand(d, d) + cov_i = np.dot(A_i, A_i.transpose()) # Gaussian covariance matrix + + x_i = ot.datasets.make_2D_samples_gauss(n_i, mu_i, cov_i) # Dirac locations + b_i = np.random.uniform(0., 1., (n_i,)) + b_i = b_i / np.sum(b_i) # Dirac weights + + measures_locations.append(x_i) + measures_weights.append(b_i) + + +############################################################################## +# Compute free support barycenter +# ------------- + +k = 10 # number of Diracs of the barycenter +X_init = np.random.normal(0., 1., (k, d)) # initial Dirac locations +b = np.ones((k,)) / k # weights of the barycenter (it will not be optimized, only the locations are optimized) + +X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b) + + +############################################################################## +# Plot data +# --------- + +pl.figure(1) +for (x_i, b_i) in zip(measures_locations, measures_weights): + color = np.random.randint(low=1, high=10 * N) + pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure') +pl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter') +pl.title('Data measures and their barycenter') +pl.legend(loc=0) +pl.show() diff --git a/docs/source/auto_examples/plot_free_support_barycenter.rst b/docs/source/auto_examples/plot_free_support_barycenter.rst new file mode 100644 index 0000000..d1b3b80 --- /dev/null +++ b/docs/source/auto_examples/plot_free_support_barycenter.rst @@ -0,0 +1,140 @@ + + +.. _sphx_glr_auto_examples_plot_free_support_barycenter.py: + + +==================================================== +2D free support Wasserstein barycenters of distributions +==================================================== + +Illustration of 2D Wasserstein barycenters if discributions that are weighted +sum of diracs. + + + + +.. code-block:: python + + + # Author: Vivien Seguy + # + # License: MIT License + + import numpy as np + import matplotlib.pylab as pl + import ot + + + + + + + + +Generate data + ------------- +%% parameters and data generation + + + +.. code-block:: python + + N = 3 + d = 2 + measures_locations = [] + measures_weights = [] + + for i in range(N): + + n_i = np.random.randint(low=1, high=20) # nb samples + + mu_i = np.random.normal(0., 4., (d,)) # Gaussian mean + + A_i = np.random.rand(d, d) + cov_i = np.dot(A_i, A_i.transpose()) # Gaussian covariance matrix + + x_i = ot.datasets.make_2D_samples_gauss(n_i, mu_i, cov_i) # Dirac locations + b_i = np.random.uniform(0., 1., (n_i,)) + b_i = b_i / np.sum(b_i) # Dirac weights + + measures_locations.append(x_i) + measures_weights.append(b_i) + + + + + + + + +Compute free support barycenter +------------- + + + +.. code-block:: python + + + k = 10 # number of Diracs of the barycenter + X_init = np.random.normal(0., 1., (k, d)) # initial Dirac locations + b = np.ones((k,)) / k # weights of the barycenter (it will not be optimized, only the locations are optimized) + + X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b) + + + + + + + + +Plot data +--------- + + + +.. code-block:: python + + + pl.figure(1) + for (x_i, b_i) in zip(measures_locations, measures_weights): + color = np.random.randint(low=1, high=10 * N) + pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure') + pl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter') + pl.title('Data measures and their barycenter') + pl.legend(loc=0) + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 0.129 seconds) + + + +.. only :: html + + .. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_free_support_barycenter.py ` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_free_support_barycenter.ipynb ` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_stochastic.ipynb b/docs/source/auto_examples/plot_stochastic.ipynb new file mode 100644 index 0000000..c6f0013 --- /dev/null +++ b/docs/source/auto_examples/plot_stochastic.ipynb @@ -0,0 +1,331 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n# Stochastic examples\n\n\nThis example is designed to show how to use the stochatic optimization\nalgorithms for descrete and semicontinous measures from the POT library.\n\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Author: Kilian Fatras \n#\n# License: MIT License\n\nimport matplotlib.pylab as pl\nimport numpy as np\nimport ot\nimport ot.plot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM\n############################################################################\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(\"------------SEMI-DUAL PROBLEM------------\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "DISCRETE CASE\nSample two discrete measures for the discrete case\n---------------------------------------------\n\nDefine 2 discrete measures a and b, the points where are defined the source\nand the target measures and finally the cost matrix c.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "n_source = 7\nn_target = 4\nreg = 1\nnumItermax = 1000\n\na = ot.utils.unif(n_source)\nb = ot.utils.unif(n_target)\n\nrng = np.random.RandomState(0)\nX_source = rng.randn(n_source, 2)\nY_target = rng.randn(n_target, 2)\nM = ot.dist(X_source, Y_target)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Call the \"SAG\" method to find the transportation matrix in the discrete case\n---------------------------------------------\n\nDefine the method \"SAG\", call ot.solve_semi_dual_entropic and plot the\nresults.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "method = \"SAG\"\nsag_pi = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method,\n numItermax)\nprint(sag_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "SEMICONTINOUS CASE\nSample one general measure a, one discrete measures b for the semicontinous\ncase\n---------------------------------------------\n\nDefine one general measure a, one discrete measures b, the points where\nare defined the source and the target measures and finally the cost matrix c.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "n_source = 7\nn_target = 4\nreg = 1\nnumItermax = 1000\nlog = True\n\na = ot.utils.unif(n_source)\nb = ot.utils.unif(n_target)\n\nrng = np.random.RandomState(0)\nX_source = rng.randn(n_source, 2)\nY_target = rng.randn(n_target, 2)\nM = ot.dist(X_source, Y_target)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Call the \"ASGD\" method to find the transportation matrix in the semicontinous\ncase\n---------------------------------------------\n\nDefine the method \"ASGD\", call ot.solve_semi_dual_entropic and plot the\nresults.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "method = \"ASGD\"\nasgd_pi, log_asgd = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method,\n numItermax, log=log)\nprint(log_asgd['alpha'], log_asgd['beta'])\nprint(asgd_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compare the results with the Sinkhorn algorithm\n---------------------------------------------\n\nCall the Sinkhorn algorithm from POT\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "sinkhorn_pi = ot.sinkhorn(a, b, M, reg)\nprint(sinkhorn_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "PLOT TRANSPORTATION MATRIX\n#############################################################################\n\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot SAG results\n----------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, sag_pi, 'semi-dual : OT matrix SAG')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot ASGD results\n-----------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, asgd_pi, 'semi-dual : OT matrix ASGD')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot Sinkhorn results\n---------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM\n############################################################################\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(\"------------DUAL PROBLEM------------\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "SEMICONTINOUS CASE\nSample one general measure a, one discrete measures b for the semicontinous\ncase\n---------------------------------------------\n\nDefine one general measure a, one discrete measures b, the points where\nare defined the source and the target measures and finally the cost matrix c.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "n_source = 7\nn_target = 4\nreg = 1\nnumItermax = 100000\nlr = 0.1\nbatch_size = 3\nlog = True\n\na = ot.utils.unif(n_source)\nb = ot.utils.unif(n_target)\n\nrng = np.random.RandomState(0)\nX_source = rng.randn(n_source, 2)\nY_target = rng.randn(n_target, 2)\nM = ot.dist(X_source, Y_target)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Call the \"SGD\" dual method to find the transportation matrix in the\nsemicontinous case\n---------------------------------------------\n\nCall ot.solve_dual_entropic and plot the results.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "sgd_dual_pi, log_sgd = ot.stochastic.solve_dual_entropic(a, b, M, reg,\n batch_size, numItermax,\n lr, log=log)\nprint(log_sgd['alpha'], log_sgd['beta'])\nprint(sgd_dual_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compare the results with the Sinkhorn algorithm\n---------------------------------------------\n\nCall the Sinkhorn algorithm from POT\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "sinkhorn_pi = ot.sinkhorn(a, b, M, reg)\nprint(sinkhorn_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot SGD results\n-----------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, sgd_dual_pi, 'dual : OT matrix SGD')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot Sinkhorn results\n---------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn')\npl.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_stochastic.py b/docs/source/auto_examples/plot_stochastic.py new file mode 100644 index 0000000..b9375d4 --- /dev/null +++ b/docs/source/auto_examples/plot_stochastic.py @@ -0,0 +1,207 @@ +""" +========================== +Stochastic examples +========================== + +This example is designed to show how to use the stochatic optimization +algorithms for descrete and semicontinous measures from the POT library. + +""" + +# Author: Kilian Fatras +# +# License: MIT License + +import matplotlib.pylab as pl +import numpy as np +import ot +import ot.plot + + +############################################################################# +# COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM +############################################################################# +print("------------SEMI-DUAL PROBLEM------------") +############################################################################# +# DISCRETE CASE +# Sample two discrete measures for the discrete case +# --------------------------------------------- +# +# Define 2 discrete measures a and b, the points where are defined the source +# and the target measures and finally the cost matrix c. + +n_source = 7 +n_target = 4 +reg = 1 +numItermax = 1000 + +a = ot.utils.unif(n_source) +b = ot.utils.unif(n_target) + +rng = np.random.RandomState(0) +X_source = rng.randn(n_source, 2) +Y_target = rng.randn(n_target, 2) +M = ot.dist(X_source, Y_target) + +############################################################################# +# +# Call the "SAG" method to find the transportation matrix in the discrete case +# --------------------------------------------- +# +# Define the method "SAG", call ot.solve_semi_dual_entropic and plot the +# results. + +method = "SAG" +sag_pi = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method, + numItermax) +print(sag_pi) + +############################################################################# +# SEMICONTINOUS CASE +# Sample one general measure a, one discrete measures b for the semicontinous +# case +# --------------------------------------------- +# +# Define one general measure a, one discrete measures b, the points where +# are defined the source and the target measures and finally the cost matrix c. + +n_source = 7 +n_target = 4 +reg = 1 +numItermax = 1000 +log = True + +a = ot.utils.unif(n_source) +b = ot.utils.unif(n_target) + +rng = np.random.RandomState(0) +X_source = rng.randn(n_source, 2) +Y_target = rng.randn(n_target, 2) +M = ot.dist(X_source, Y_target) + +############################################################################# +# +# Call the "ASGD" method to find the transportation matrix in the semicontinous +# case +# --------------------------------------------- +# +# Define the method "ASGD", call ot.solve_semi_dual_entropic and plot the +# results. + +method = "ASGD" +asgd_pi, log_asgd = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method, + numItermax, log=log) +print(log_asgd['alpha'], log_asgd['beta']) +print(asgd_pi) + +############################################################################# +# +# Compare the results with the Sinkhorn algorithm +# --------------------------------------------- +# +# Call the Sinkhorn algorithm from POT + +sinkhorn_pi = ot.sinkhorn(a, b, M, reg) +print(sinkhorn_pi) + + +############################################################################## +# PLOT TRANSPORTATION MATRIX +############################################################################## + +############################################################################## +# Plot SAG results +# ---------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, sag_pi, 'semi-dual : OT matrix SAG') +pl.show() + + +############################################################################## +# Plot ASGD results +# ----------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, asgd_pi, 'semi-dual : OT matrix ASGD') +pl.show() + + +############################################################################## +# Plot Sinkhorn results +# --------------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn') +pl.show() + + +############################################################################# +# COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM +############################################################################# +print("------------DUAL PROBLEM------------") +############################################################################# +# SEMICONTINOUS CASE +# Sample one general measure a, one discrete measures b for the semicontinous +# case +# --------------------------------------------- +# +# Define one general measure a, one discrete measures b, the points where +# are defined the source and the target measures and finally the cost matrix c. + +n_source = 7 +n_target = 4 +reg = 1 +numItermax = 100000 +lr = 0.1 +batch_size = 3 +log = True + +a = ot.utils.unif(n_source) +b = ot.utils.unif(n_target) + +rng = np.random.RandomState(0) +X_source = rng.randn(n_source, 2) +Y_target = rng.randn(n_target, 2) +M = ot.dist(X_source, Y_target) + +############################################################################# +# +# Call the "SGD" dual method to find the transportation matrix in the +# semicontinous case +# --------------------------------------------- +# +# Call ot.solve_dual_entropic and plot the results. + +sgd_dual_pi, log_sgd = ot.stochastic.solve_dual_entropic(a, b, M, reg, + batch_size, numItermax, + lr, log=log) +print(log_sgd['alpha'], log_sgd['beta']) +print(sgd_dual_pi) + +############################################################################# +# +# Compare the results with the Sinkhorn algorithm +# --------------------------------------------- +# +# Call the Sinkhorn algorithm from POT + +sinkhorn_pi = ot.sinkhorn(a, b, M, reg) +print(sinkhorn_pi) + +############################################################################## +# Plot SGD results +# ----------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, sgd_dual_pi, 'dual : OT matrix SGD') +pl.show() + + +############################################################################## +# Plot Sinkhorn results +# --------------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn') +pl.show() diff --git a/docs/source/auto_examples/plot_stochastic.rst b/docs/source/auto_examples/plot_stochastic.rst new file mode 100644 index 0000000..a49bc05 --- /dev/null +++ b/docs/source/auto_examples/plot_stochastic.rst @@ -0,0 +1,475 @@ + + +.. _sphx_glr_auto_examples_plot_stochastic.py: + + +========================== +Stochastic examples +========================== + +This example is designed to show how to use the stochatic optimization +algorithms for descrete and semicontinous measures from the POT library. + + + + +.. code-block:: python + + + # Author: Kilian Fatras + # + # License: MIT License + + import matplotlib.pylab as pl + import numpy as np + import ot + import ot.plot + + + + + + + + +COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM +############################################################################ + + + +.. code-block:: python + + print("------------SEMI-DUAL PROBLEM------------") + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + ------------SEMI-DUAL PROBLEM------------ + + +DISCRETE CASE +Sample two discrete measures for the discrete case +--------------------------------------------- + +Define 2 discrete measures a and b, the points where are defined the source +and the target measures and finally the cost matrix c. + + + +.. code-block:: python + + + n_source = 7 + n_target = 4 + reg = 1 + numItermax = 1000 + + a = ot.utils.unif(n_source) + b = ot.utils.unif(n_target) + + rng = np.random.RandomState(0) + X_source = rng.randn(n_source, 2) + Y_target = rng.randn(n_target, 2) + M = ot.dist(X_source, Y_target) + + + + + + + +Call the "SAG" method to find the transportation matrix in the discrete case +--------------------------------------------- + +Define the method "SAG", call ot.solve_semi_dual_entropic and plot the +results. + + + +.. code-block:: python + + + method = "SAG" + sag_pi = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method, + numItermax) + print(sag_pi) + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [[2.55553509e-02 9.96395660e-02 1.76579142e-02 4.31178196e-06] + [1.21640234e-01 1.25357448e-02 1.30225078e-03 7.37891338e-03] + [3.56123975e-03 7.61451746e-02 6.31505947e-02 1.33831456e-07] + [2.61515202e-02 3.34246014e-02 8.28734709e-02 4.07550428e-04] + [9.85500870e-03 7.52288517e-04 1.08262628e-02 1.21423583e-01] + [2.16904253e-02 9.03825797e-04 1.87178503e-03 1.18391107e-01] + [4.15462212e-02 2.65987989e-02 7.23177216e-02 2.39440107e-03]] + + +SEMICONTINOUS CASE +Sample one general measure a, one discrete measures b for the semicontinous +case +--------------------------------------------- + +Define one general measure a, one discrete measures b, the points where +are defined the source and the target measures and finally the cost matrix c. + + + +.. code-block:: python + + + n_source = 7 + n_target = 4 + reg = 1 + numItermax = 1000 + log = True + + a = ot.utils.unif(n_source) + b = ot.utils.unif(n_target) + + rng = np.random.RandomState(0) + X_source = rng.randn(n_source, 2) + Y_target = rng.randn(n_target, 2) + M = ot.dist(X_source, Y_target) + + + + + + + +Call the "ASGD" method to find the transportation matrix in the semicontinous +case +--------------------------------------------- + +Define the method "ASGD", call ot.solve_semi_dual_entropic and plot the +results. + + + +.. code-block:: python + + + method = "ASGD" + asgd_pi, log_asgd = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method, + numItermax, log=log) + print(log_asgd['alpha'], log_asgd['beta']) + print(asgd_pi) + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [3.9018759 7.63059124 3.93260224 2.67274989 1.43888443 3.26904884 + 2.78748299] [-2.48511647 -2.43621119 -0.93585194 5.8571796 ] + [[2.56614773e-02 9.96758169e-02 1.75151781e-02 4.67049862e-06] + [1.21201047e-01 1.24433535e-02 1.28173754e-03 7.93100436e-03] + [3.58778167e-03 7.64232233e-02 6.28459924e-02 1.45441936e-07] + [2.63551754e-02 3.35577920e-02 8.25011211e-02 4.43054320e-04] + [9.24518246e-03 7.03074064e-04 1.00325744e-02 1.22876312e-01] + [2.03656325e-02 8.45420425e-04 1.73604569e-03 1.19910044e-01] + [4.17781688e-02 2.66463708e-02 7.18353075e-02 2.59729583e-03]] + + +Compare the results with the Sinkhorn algorithm +--------------------------------------------- + +Call the Sinkhorn algorithm from POT + + + +.. code-block:: python + + + sinkhorn_pi = ot.sinkhorn(a, b, M, reg) + print(sinkhorn_pi) + + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [[2.55535622e-02 9.96413843e-02 1.76578860e-02 4.31043335e-06] + [1.21640742e-01 1.25369034e-02 1.30234529e-03 7.37715259e-03] + [3.56096458e-03 7.61460101e-02 6.31500344e-02 1.33788624e-07] + [2.61499607e-02 3.34255577e-02 8.28741973e-02 4.07427179e-04] + [9.85698720e-03 7.52505948e-04 1.08291770e-02 1.21418473e-01] + [2.16947591e-02 9.04086158e-04 1.87228707e-03 1.18386011e-01] + [4.15442692e-02 2.65998963e-02 7.23192701e-02 2.39370724e-03]] + + +PLOT TRANSPORTATION MATRIX +############################################################################# + + +Plot SAG results +---------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, sag_pi, 'semi-dual : OT matrix SAG') + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_004.png + :align: center + + + + +Plot ASGD results +----------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, asgd_pi, 'semi-dual : OT matrix ASGD') + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_005.png + :align: center + + + + +Plot Sinkhorn results +--------------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn') + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_006.png + :align: center + + + + +COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM +############################################################################ + + + +.. code-block:: python + + print("------------DUAL PROBLEM------------") + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + ------------DUAL PROBLEM------------ + + +SEMICONTINOUS CASE +Sample one general measure a, one discrete measures b for the semicontinous +case +--------------------------------------------- + +Define one general measure a, one discrete measures b, the points where +are defined the source and the target measures and finally the cost matrix c. + + + +.. code-block:: python + + + n_source = 7 + n_target = 4 + reg = 1 + numItermax = 100000 + lr = 0.1 + batch_size = 3 + log = True + + a = ot.utils.unif(n_source) + b = ot.utils.unif(n_target) + + rng = np.random.RandomState(0) + X_source = rng.randn(n_source, 2) + Y_target = rng.randn(n_target, 2) + M = ot.dist(X_source, Y_target) + + + + + + + +Call the "SGD" dual method to find the transportation matrix in the +semicontinous case +--------------------------------------------- + +Call ot.solve_dual_entropic and plot the results. + + + +.. code-block:: python + + + sgd_dual_pi, log_sgd = ot.stochastic.solve_dual_entropic(a, b, M, reg, + batch_size, numItermax, + lr, log=log) + print(log_sgd['alpha'], log_sgd['beta']) + print(sgd_dual_pi) + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [ 1.29325617 5.0435082 1.30996326 0.05538236 -1.08113283 0.73711558 + 0.18086364] [0.08840343 0.17710082 1.68604226 8.37377551] + [[2.47763879e-02 1.00144623e-01 1.77492330e-02 4.25988443e-06] + [1.19568278e-01 1.27740478e-02 1.32714202e-03 7.39121816e-03] + [3.41581121e-03 7.57137404e-02 6.27992039e-02 1.30808430e-07] + [2.52245323e-02 3.34219732e-02 8.28754229e-02 4.00582912e-04] + [9.75329554e-03 7.71824343e-04 1.11085400e-02 1.22456628e-01] + [2.12304276e-02 9.17096580e-04 1.89946234e-03 1.18084973e-01] + [4.04179693e-02 2.68253041e-02 7.29410047e-02 2.37369404e-03]] + + +Compare the results with the Sinkhorn algorithm +--------------------------------------------- + +Call the Sinkhorn algorithm from POT + + + +.. code-block:: python + + + sinkhorn_pi = ot.sinkhorn(a, b, M, reg) + print(sinkhorn_pi) + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [[2.55535622e-02 9.96413843e-02 1.76578860e-02 4.31043335e-06] + [1.21640742e-01 1.25369034e-02 1.30234529e-03 7.37715259e-03] + [3.56096458e-03 7.61460101e-02 6.31500344e-02 1.33788624e-07] + [2.61499607e-02 3.34255577e-02 8.28741973e-02 4.07427179e-04] + [9.85698720e-03 7.52505948e-04 1.08291770e-02 1.21418473e-01] + [2.16947591e-02 9.04086158e-04 1.87228707e-03 1.18386011e-01] + [4.15442692e-02 2.65998963e-02 7.23192701e-02 2.39370724e-03]] + + +Plot SGD results +----------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, sgd_dual_pi, 'dual : OT matrix SGD') + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_007.png + :align: center + + + + +Plot Sinkhorn results +--------------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn') + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_008.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 22.857 seconds) + + + +.. only :: html + + .. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_stochastic.py ` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_stochastic.ipynb ` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery `_ -- cgit v1.2.3