diff options
Diffstat (limited to 'docs/source')
43 files changed, 2404 insertions, 60 deletions
diff --git a/docs/source/all.rst b/docs/source/all.rst index 1eaf3b1..32930fd 100644 --- a/docs/source/all.rst +++ b/docs/source/all.rst @@ -19,11 +19,6 @@ ot.bregman .. automodule:: ot.bregman :members: - -ot.smooth ------ -.. automodule:: ot.smooth - :members: ot.smooth ----- diff --git a/docs/source/auto_examples/auto_examples_jupyter.zip b/docs/source/auto_examples/auto_examples_jupyter.zip Binary files differindex 8102274..304bb06 100644 --- a/docs/source/auto_examples/auto_examples_jupyter.zip +++ b/docs/source/auto_examples/auto_examples_jupyter.zip diff --git a/docs/source/auto_examples/auto_examples_python.zip b/docs/source/auto_examples/auto_examples_python.zip Binary files differindex d685070..3be8a76 100644 --- a/docs/source/auto_examples/auto_examples_python.zip +++ b/docs/source/auto_examples/auto_examples_python.zip 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 Binary files differnew file mode 100644 index 0000000..6e74d89 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png 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 Binary files differnew file mode 100644 index 0000000..0407e44 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png 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 Binary files differnew file mode 100644 index 0000000..4421bc7 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png 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 Binary files differnew file mode 100644 index 0000000..52638e3 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png 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 Binary files differnew file mode 100644 index 0000000..c5078cf --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png 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 Binary files differnew file mode 100644 index 0000000..58e87b6 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png Binary files differindex 81cee52..d8db85e 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_005.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_005.png Binary files differindex eac9230..81cee52 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_005.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_005.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_006.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_006.png Binary files differindex 2e29ff9..bfa0873 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_006.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_006.png 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 Binary files differnew file mode 100644 index 0000000..eb04b1a --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_003.png 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 Binary files differnew file mode 100644 index 0000000..a9f44ba --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_004.png 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 Binary files differnew file mode 100644 index 0000000..e53928e --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_006.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png b/docs/source/auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png Binary files differnew file mode 100644 index 0000000..14a72a3 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png 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 Binary files differnew file mode 100644 index 0000000..d7bc78a --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png 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 Binary files differnew file mode 100644 index 0000000..8aada91 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_004.png 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 Binary files differnew file mode 100644 index 0000000..3d1e239 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_005.png 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 Binary files differnew file mode 100644 index 0000000..335ea95 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_006.png 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 Binary files differnew file mode 100644 index 0000000..986aa96 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png 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 Binary files differnew file mode 100644 index 0000000..335ea95 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_008.png 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 Binary files differnew file mode 100644 index 0000000..4679eb6 --- /dev/null +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png Binary files differnew file mode 100644 index 0000000..af8aad2 --- /dev/null +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png 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 Binary files differnew file mode 100644 index 0000000..0861d4d --- /dev/null +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png 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 Binary files differnew file mode 100644 index 0000000..609339d --- /dev/null +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png diff --git a/docs/source/auto_examples/index.rst b/docs/source/auto_examples/index.rst index 69fb320..259fca1 100644 --- a/docs/source/auto_examples/index.rst +++ b/docs/source/auto_examples/index.rst @@ -49,6 +49,46 @@ This is a gallery of all the POT example files. .. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="Illustration of 2D Wasserstein barycenters if discributions that are weighted sum of diracs."> + +.. 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 + + </div> + + +.. toctree:: + :hidden: + + /auto_examples/plot_free_support_barycenter + +.. raw:: html + + <div class="sphx-glr-thumbcontainer" tooltip="This example illustrates the computation of EMD, Sinkhorn and smooth OT plans and their visuali..."> + +.. 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 + + </div> + + +.. toctree:: + :hidden: + + /auto_examples/plot_OT_1D_smooth + +.. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="This example is designed to show how to use the Gromov-Wassertsein distance computation in POT...."> .. only:: html @@ -109,6 +149,26 @@ This is a gallery of all the POT example files. .. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="This example is designed to illustrate how the Convolutional Wasserstein Barycenter function of..."> + +.. only:: html + + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png + + :ref:`sphx_glr_auto_examples_plot_convolutional_barycenter.py` + +.. raw:: html + + </div> + + +.. toctree:: + :hidden: + + /auto_examples/plot_convolutional_barycenter + +.. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip=" "> .. only:: html @@ -149,13 +209,13 @@ This is a gallery of all the POT example files. .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="This example presents a way of transferring colors between two image with Optimal Transport as ..."> + <div class="sphx-glr-thumbcontainer" tooltip="This example is designed to show how to use the stochatic optimization algorithms for descrete ..."> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png - :ref:`sphx_glr_auto_examples_plot_otda_color_images.py` + :ref:`sphx_glr_auto_examples_plot_stochastic.py` .. raw:: html @@ -165,17 +225,17 @@ This is a gallery of all the POT example files. .. toctree:: :hidden: - /auto_examples/plot_otda_color_images + /auto_examples/plot_stochastic .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="OT for domain adaptation with image color adaptation [6] with mapping estimation [8]."> + <div class="sphx-glr-thumbcontainer" tooltip="This example presents a way of transferring colors between two image with Optimal Transport as ..."> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png - :ref:`sphx_glr_auto_examples_plot_otda_mapping_colors_images.py` + :ref:`sphx_glr_auto_examples_plot_otda_color_images.py` .. raw:: html @@ -185,7 +245,7 @@ This is a gallery of all the POT example files. .. toctree:: :hidden: - /auto_examples/plot_otda_mapping_colors_images + /auto_examples/plot_otda_color_images .. raw:: html @@ -209,6 +269,26 @@ This is a gallery of all the POT example files. .. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="OT for domain adaptation with image color adaptation [6] with mapping estimation [8]."> + +.. only:: html + + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png + + :ref:`sphx_glr_auto_examples_plot_otda_mapping_colors_images.py` + +.. raw:: html + + </div> + + +.. toctree:: + :hidden: + + /auto_examples/plot_otda_mapping_colors_images + +.. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="This example presents how to use MappingTransport to estimate at the same time both the couplin..."> .. only:: 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 <remi.flamary@unice.fr>\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 <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 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 <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 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 <plot_OT_1D_smooth.py>` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_OT_1D_smooth.ipynb <plot_OT_1D_smooth.ipynb>` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ diff --git a/docs/source/auto_examples/plot_barycenter_1D.ipynb b/docs/source/auto_examples/plot_barycenter_1D.ipynb index 5866088..fc60e1f 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.ipynb +++ b/docs/source/auto_examples/plot_barycenter_1D.ipynb @@ -26,7 +26,79 @@ }, "outputs": [], "source": [ - "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\n# necessary for 3d plot even if not used\nfrom mpl_toolkits.mplot3d import Axes3D # noqa\nfrom matplotlib.collections import PolyCollection\n\n#\n# Generate data\n# -------------\n\n#%% parameters\n\nn = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std\na2 = ot.datasets.make_1D_gauss(n, m=60, s=8)\n\n# creating matrix A containing all distributions\nA = np.vstack((a1, a2)).T\nn_distributions = A.shape[1]\n\n# loss matrix + normalization\nM = ot.utils.dist0(n)\nM /= M.max()\n\n#\n# Plot data\n# ---------\n\n#%% plot the distributions\n\npl.figure(1, figsize=(6.4, 3))\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\npl.tight_layout()\n\n#\n# Barycenter computation\n# ----------------------\n\n#%% barycenter computation\n\nalpha = 0.2 # 0<=alpha<=1\nweights = np.array([1 - alpha, alpha])\n\n# l2bary\nbary_l2 = A.dot(weights)\n\n# wasserstein\nreg = 1e-3\nbary_wass = ot.bregman.barycenter(A, M, reg, weights)\n\npl.figure(2)\npl.clf()\npl.subplot(2, 1, 1)\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\n\npl.subplot(2, 1, 2)\npl.plot(x, bary_l2, 'r', label='l2')\npl.plot(x, bary_wass, 'g', label='Wasserstein')\npl.legend()\npl.title('Barycenters')\npl.tight_layout()\n\n#\n# Barycentric interpolation\n# -------------------------\n\n#%% barycenter interpolation\n\nn_alpha = 11\nalpha_list = np.linspace(0, 1, n_alpha)\n\n\nB_l2 = np.zeros((n, n_alpha))\n\nB_wass = np.copy(B_l2)\n\nfor i in range(0, n_alpha):\n alpha = alpha_list[i]\n weights = np.array([1 - alpha, alpha])\n B_l2[:, i] = A.dot(weights)\n B_wass[:, i] = ot.bregman.barycenter(A, M, reg, weights)\n\n#%% plot interpolation\n\npl.figure(3)\n\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_l2[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with l2')\npl.tight_layout()\n\npl.figure(4)\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_wass[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with Wasserstein')\npl.tight_layout()\n\npl.show()" + "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\n# necessary for 3d plot even if not used\nfrom mpl_toolkits.mplot3d import Axes3D # noqa\nfrom matplotlib.collections import PolyCollection" + ] + }, + { + "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\na1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std\na2 = ot.datasets.make_1D_gauss(n, m=60, s=8)\n\n# creating matrix A containing all distributions\nA = np.vstack((a1, a2)).T\nn_distributions = A.shape[1]\n\n# loss matrix + normalization\nM = ot.utils.dist0(n)\nM /= M.max()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot data\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))\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\npl.tight_layout()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Barycenter computation\n----------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% barycenter computation\n\nalpha = 0.2 # 0<=alpha<=1\nweights = np.array([1 - alpha, alpha])\n\n# l2bary\nbary_l2 = A.dot(weights)\n\n# wasserstein\nreg = 1e-3\nbary_wass = ot.bregman.barycenter(A, M, reg, weights)\n\npl.figure(2)\npl.clf()\npl.subplot(2, 1, 1)\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\n\npl.subplot(2, 1, 2)\npl.plot(x, bary_l2, 'r', label='l2')\npl.plot(x, bary_wass, 'g', label='Wasserstein')\npl.legend()\npl.title('Barycenters')\npl.tight_layout()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Barycentric interpolation\n-------------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% barycenter interpolation\n\nn_alpha = 11\nalpha_list = np.linspace(0, 1, n_alpha)\n\n\nB_l2 = np.zeros((n, n_alpha))\n\nB_wass = np.copy(B_l2)\n\nfor i in range(0, n_alpha):\n alpha = alpha_list[i]\n weights = np.array([1 - alpha, alpha])\n B_l2[:, i] = A.dot(weights)\n B_wass[:, i] = ot.bregman.barycenter(A, M, reg, weights)\n\n#%% plot interpolation\n\npl.figure(3)\n\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_l2[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with l2')\npl.tight_layout()\n\npl.figure(4)\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_wass[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with Wasserstein')\npl.tight_layout()\n\npl.show()" ] } ], diff --git a/docs/source/auto_examples/plot_barycenter_1D.py b/docs/source/auto_examples/plot_barycenter_1D.py index 5ed9f3f..6864301 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.py +++ b/docs/source/auto_examples/plot_barycenter_1D.py @@ -25,7 +25,7 @@ import ot from mpl_toolkits.mplot3d import Axes3D # noqa from matplotlib.collections import PolyCollection -# +############################################################################## # Generate data # ------------- @@ -48,7 +48,7 @@ n_distributions = A.shape[1] M = ot.utils.dist0(n) M /= M.max() -# +############################################################################## # Plot data # --------- @@ -60,7 +60,7 @@ for i in range(n_distributions): pl.title('Distributions') pl.tight_layout() -# +############################################################################## # Barycenter computation # ---------------------- @@ -90,7 +90,7 @@ pl.legend() pl.title('Barycenters') pl.tight_layout() -# +############################################################################## # Barycentric interpolation # ------------------------- diff --git a/docs/source/auto_examples/plot_barycenter_1D.rst b/docs/source/auto_examples/plot_barycenter_1D.rst index b314dc1..66ac042 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.rst +++ b/docs/source/auto_examples/plot_barycenter_1D.rst @@ -18,52 +18,34 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. +.. code-block:: python -.. rst-class:: sphx-glr-horizontal - - - * - .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_001.png - :scale: 47 + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License - * + import numpy as np + import matplotlib.pylab as pl + import ot + # necessary for 3d plot even if not used + from mpl_toolkits.mplot3d import Axes3D # noqa + from matplotlib.collections import PolyCollection - .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_002.png - :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_003.png - :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_004.png - :scale: 47 +Generate data +------------- .. code-block:: python - # Author: Remi Flamary <remi.flamary@unice.fr> - # - # License: MIT License - - import numpy as np - import matplotlib.pylab as pl - import ot - # necessary for 3d plot even if not used - from mpl_toolkits.mplot3d import Axes3D # noqa - from matplotlib.collections import PolyCollection - - # - # Generate data - # ------------- - #%% parameters n = 100 # nb bins @@ -83,9 +65,19 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. M = ot.utils.dist0(n) M /= M.max() - # - # Plot data - # --------- + + + + + + +Plot data +--------- + + + +.. code-block:: python + #%% plot the distributions @@ -95,9 +87,22 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. pl.title('Distributions') pl.tight_layout() - # - # Barycenter computation - # ---------------------- + + + +.. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_001.png + :align: center + + + + +Barycenter computation +---------------------- + + + +.. code-block:: python + #%% barycenter computation @@ -125,9 +130,22 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. pl.title('Barycenters') pl.tight_layout() - # - # Barycentric interpolation - # ------------------------- + + + +.. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_003.png + :align: center + + + + +Barycentric interpolation +------------------------- + + + +.. code-block:: python + #%% barycenter interpolation @@ -194,7 +212,25 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. pl.show() -**Total running time of the script:** ( 0 minutes 0.363 seconds) + + +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_005.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_006.png + :scale: 47 + + + + +**Total running time of the script:** ( 0 minutes 0.413 seconds) diff --git a/docs/source/auto_examples/plot_convolutional_barycenter.ipynb b/docs/source/auto_examples/plot_convolutional_barycenter.ipynb new file mode 100644 index 0000000..4981ba3 --- /dev/null +++ b/docs/source/auto_examples/plot_convolutional_barycenter.ipynb @@ -0,0 +1,90 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n# Convolutional Wasserstein Barycenter example\n\n\nThis example is designed to illustrate how the Convolutional Wasserstein Barycenter\nfunction of POT works.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Author: Nicolas Courty <ncourty@irisa.fr>\n#\n# License: MIT License\n\n\nimport numpy as np\nimport pylab as pl\nimport ot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Data preparation\n----------------\n\nThe four distributions are constructed from 4 simple images\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "f1 = 1 - pl.imread('../data/redcross.png')[:, :, 2]\nf2 = 1 - pl.imread('../data/duck.png')[:, :, 2]\nf3 = 1 - pl.imread('../data/heart.png')[:, :, 2]\nf4 = 1 - pl.imread('../data/tooth.png')[:, :, 2]\n\nA = []\nf1 = f1 / np.sum(f1)\nf2 = f2 / np.sum(f2)\nf3 = f3 / np.sum(f3)\nf4 = f4 / np.sum(f4)\nA.append(f1)\nA.append(f2)\nA.append(f3)\nA.append(f4)\nA = np.array(A)\n\nnb_images = 5\n\n# those are the four corners coordinates that will be interpolated by bilinear\n# interpolation\nv1 = np.array((1, 0, 0, 0))\nv2 = np.array((0, 1, 0, 0))\nv3 = np.array((0, 0, 1, 0))\nv4 = np.array((0, 0, 0, 1))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Barycenter computation and visualization\n----------------------------------------\n\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(figsize=(10, 10))\npl.title('Convolutional Wasserstein Barycenters in POT')\ncm = 'Blues'\n# regularization parameter\nreg = 0.004\nfor i in range(nb_images):\n for j in range(nb_images):\n pl.subplot(nb_images, nb_images, i * nb_images + j + 1)\n tx = float(i) / (nb_images - 1)\n ty = float(j) / (nb_images - 1)\n\n # weights are constructed by bilinear interpolation\n tmp1 = (1 - tx) * v1 + tx * v2\n tmp2 = (1 - tx) * v3 + tx * v4\n weights = (1 - ty) * tmp1 + ty * tmp2\n\n if i == 0 and j == 0:\n pl.imshow(f1, cmap=cm)\n pl.axis('off')\n elif i == 0 and j == (nb_images - 1):\n pl.imshow(f3, cmap=cm)\n pl.axis('off')\n elif i == (nb_images - 1) and j == 0:\n pl.imshow(f2, cmap=cm)\n pl.axis('off')\n elif i == (nb_images - 1) and j == (nb_images - 1):\n pl.imshow(f4, cmap=cm)\n pl.axis('off')\n else:\n # call to barycenter computation\n pl.imshow(ot.bregman.convolutional_barycenter2d(A, reg, weights), cmap=cm)\n pl.axis('off')\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_convolutional_barycenter.py b/docs/source/auto_examples/plot_convolutional_barycenter.py new file mode 100644 index 0000000..e74db04 --- /dev/null +++ b/docs/source/auto_examples/plot_convolutional_barycenter.py @@ -0,0 +1,92 @@ + +#%% +# -*- coding: utf-8 -*- +""" +============================================ +Convolutional Wasserstein Barycenter example +============================================ + +This example is designed to illustrate how the Convolutional Wasserstein Barycenter +function of POT works. +""" + +# Author: Nicolas Courty <ncourty@irisa.fr> +# +# License: MIT License + + +import numpy as np +import pylab as pl +import ot + +############################################################################## +# Data preparation +# ---------------- +# +# The four distributions are constructed from 4 simple images + + +f1 = 1 - pl.imread('../data/redcross.png')[:, :, 2] +f2 = 1 - pl.imread('../data/duck.png')[:, :, 2] +f3 = 1 - pl.imread('../data/heart.png')[:, :, 2] +f4 = 1 - pl.imread('../data/tooth.png')[:, :, 2] + +A = [] +f1 = f1 / np.sum(f1) +f2 = f2 / np.sum(f2) +f3 = f3 / np.sum(f3) +f4 = f4 / np.sum(f4) +A.append(f1) +A.append(f2) +A.append(f3) +A.append(f4) +A = np.array(A) + +nb_images = 5 + +# those are the four corners coordinates that will be interpolated by bilinear +# interpolation +v1 = np.array((1, 0, 0, 0)) +v2 = np.array((0, 1, 0, 0)) +v3 = np.array((0, 0, 1, 0)) +v4 = np.array((0, 0, 0, 1)) + + +############################################################################## +# Barycenter computation and visualization +# ---------------------------------------- +# + +pl.figure(figsize=(10, 10)) +pl.title('Convolutional Wasserstein Barycenters in POT') +cm = 'Blues' +# regularization parameter +reg = 0.004 +for i in range(nb_images): + for j in range(nb_images): + pl.subplot(nb_images, nb_images, i * nb_images + j + 1) + tx = float(i) / (nb_images - 1) + ty = float(j) / (nb_images - 1) + + # weights are constructed by bilinear interpolation + tmp1 = (1 - tx) * v1 + tx * v2 + tmp2 = (1 - tx) * v3 + tx * v4 + weights = (1 - ty) * tmp1 + ty * tmp2 + + if i == 0 and j == 0: + pl.imshow(f1, cmap=cm) + pl.axis('off') + elif i == 0 and j == (nb_images - 1): + pl.imshow(f3, cmap=cm) + pl.axis('off') + elif i == (nb_images - 1) and j == 0: + pl.imshow(f2, cmap=cm) + pl.axis('off') + elif i == (nb_images - 1) and j == (nb_images - 1): + pl.imshow(f4, cmap=cm) + pl.axis('off') + else: + # call to barycenter computation + pl.imshow(ot.bregman.convolutional_barycenter2d(A, reg, weights), cmap=cm) + pl.axis('off') +pl.show() diff --git a/docs/source/auto_examples/plot_convolutional_barycenter.rst b/docs/source/auto_examples/plot_convolutional_barycenter.rst new file mode 100644 index 0000000..a28db2f --- /dev/null +++ b/docs/source/auto_examples/plot_convolutional_barycenter.rst @@ -0,0 +1,151 @@ + + +.. _sphx_glr_auto_examples_plot_convolutional_barycenter.py: + + +============================================ +Convolutional Wasserstein Barycenter example +============================================ + +This example is designed to illustrate how the Convolutional Wasserstein Barycenter +function of POT works. + + + +.. code-block:: python + + + # Author: Nicolas Courty <ncourty@irisa.fr> + # + # License: MIT License + + + import numpy as np + import pylab as pl + import ot + + + + + + + +Data preparation +---------------- + +The four distributions are constructed from 4 simple images + + + +.. code-block:: python + + + + f1 = 1 - pl.imread('../data/redcross.png')[:, :, 2] + f2 = 1 - pl.imread('../data/duck.png')[:, :, 2] + f3 = 1 - pl.imread('../data/heart.png')[:, :, 2] + f4 = 1 - pl.imread('../data/tooth.png')[:, :, 2] + + A = [] + f1 = f1 / np.sum(f1) + f2 = f2 / np.sum(f2) + f3 = f3 / np.sum(f3) + f4 = f4 / np.sum(f4) + A.append(f1) + A.append(f2) + A.append(f3) + A.append(f4) + A = np.array(A) + + nb_images = 5 + + # those are the four corners coordinates that will be interpolated by bilinear + # interpolation + v1 = np.array((1, 0, 0, 0)) + v2 = np.array((0, 1, 0, 0)) + v3 = np.array((0, 0, 1, 0)) + v4 = np.array((0, 0, 0, 1)) + + + + + + + + +Barycenter computation and visualization +---------------------------------------- + + + + +.. code-block:: python + + + pl.figure(figsize=(10, 10)) + pl.title('Convolutional Wasserstein Barycenters in POT') + cm = 'Blues' + # regularization parameter + reg = 0.004 + for i in range(nb_images): + for j in range(nb_images): + pl.subplot(nb_images, nb_images, i * nb_images + j + 1) + tx = float(i) / (nb_images - 1) + ty = float(j) / (nb_images - 1) + + # weights are constructed by bilinear interpolation + tmp1 = (1 - tx) * v1 + tx * v2 + tmp2 = (1 - tx) * v3 + tx * v4 + weights = (1 - ty) * tmp1 + ty * tmp2 + + if i == 0 and j == 0: + pl.imshow(f1, cmap=cm) + pl.axis('off') + elif i == 0 and j == (nb_images - 1): + pl.imshow(f3, cmap=cm) + pl.axis('off') + elif i == (nb_images - 1) and j == 0: + pl.imshow(f2, cmap=cm) + pl.axis('off') + elif i == (nb_images - 1) and j == (nb_images - 1): + pl.imshow(f4, cmap=cm) + pl.axis('off') + else: + # call to barycenter computation + pl.imshow(ot.bregman.convolutional_barycenter2d(A, reg, weights), cmap=cm) + pl.axis('off') + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png + :align: center + + + + +**Total running time of the script:** ( 1 minutes 11.608 seconds) + + + +.. only :: html + + .. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_convolutional_barycenter.py <plot_convolutional_barycenter.py>` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_convolutional_barycenter.ipynb <plot_convolutional_barycenter.ipynb>` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ 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 <vivien.seguy@iip.ist.i.kyoto-u.ac.jp>\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 <vivien.seguy@iip.ist.i.kyoto-u.ac.jp> +# +# 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 <vivien.seguy@iip.ist.i.kyoto-u.ac.jp> + # + # 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 <plot_free_support_barycenter.py>` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_free_support_barycenter.ipynb <plot_free_support_barycenter.ipynb>` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ 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 <kilian.fatras@gmail.com>\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 <kilian.fatras@gmail.com> +# +# 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 <kilian.fatras@gmail.com> + # + # 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 <plot_stochastic.py>` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_stochastic.ipynb <plot_stochastic.ipynb>` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ diff --git a/docs/source/readme.rst b/docs/source/readme.rst index a839231..e7c2bd1 100644 --- a/docs/source/readme.rst +++ b/docs/source/readme.rst @@ -2,7 +2,7 @@ POT: Python Optimal Transport ============================= |PyPI version| |Anaconda Cloud| |Build Status| |Documentation Status| -|Anaconda downloads| |License| +|Downloads| |Anaconda downloads| |License| This open source Python library provide several solvers for optimization problems related to Optimal Transport for signal, image processing and @@ -108,7 +108,7 @@ Dependencies Some sub-modules require additional dependences which are discussed below -- **ot.dr** (Wasserstein dimensionality rediuction) depends on autograd +- **ot.dr** (Wasserstein dimensionality reduction) depends on autograd and pymanopt that can be installed with: :: @@ -374,6 +374,8 @@ Advances in Neural Information Processing Systems (NIPS) 31 :target: https://travis-ci.org/rflamary/POT .. |Documentation Status| image:: https://readthedocs.org/projects/pot/badge/?version=latest :target: http://pot.readthedocs.io/en/latest/?badge=latest +.. |Downloads| image:: https://pepy.tech/badge/pot + :target: https://pepy.tech/project/pot .. |Anaconda downloads| image:: https://anaconda.org/conda-forge/pot/badges/downloads.svg :target: https://anaconda.org/conda-forge/pot .. |License| image:: https://anaconda.org/conda-forge/pot/badges/license.svg |