From ab5918b2e2dc88a3520c059e6a79a6f81959381e Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 30 Aug 2017 17:02:59 +0200 Subject: add files and notebooks --- .../source/auto_examples/plot_otda_color_images.py | 165 +++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 docs/source/auto_examples/plot_otda_color_images.py (limited to 'docs/source/auto_examples/plot_otda_color_images.py') diff --git a/docs/source/auto_examples/plot_otda_color_images.py b/docs/source/auto_examples/plot_otda_color_images.py new file mode 100644 index 0000000..46ad44b --- /dev/null +++ b/docs/source/auto_examples/plot_otda_color_images.py @@ -0,0 +1,165 @@ +# -*- coding: utf-8 -*- +""" +======================================================== +OT for domain adaptation with image color adaptation [6] +======================================================== + +This example presents a way of transferring colors between two image +with Optimal Transport as introduced in [6] + +[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). +Regularized discrete optimal transport. +SIAM Journal on Imaging Sciences, 7(3), 1853-1882. +""" + +# Authors: Remi Flamary +# Stanislas Chambon +# +# License: MIT License + +import numpy as np +from scipy import ndimage +import matplotlib.pylab as pl +import ot + + +r = np.random.RandomState(42) + + +def im2mat(I): + """Converts and image to matrix (one pixel per line)""" + return I.reshape((I.shape[0] * I.shape[1], I.shape[2])) + + +def mat2im(X, shape): + """Converts back a matrix to an image""" + return X.reshape(shape) + + +def minmax(I): + return np.clip(I, 0, 1) + + +############################################################################## +# generate data +############################################################################## + +# Loading images +I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 +I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256 + +X1 = im2mat(I1) +X2 = im2mat(I2) + +# training samples +nb = 1000 +idx1 = r.randint(X1.shape[0], size=(nb,)) +idx2 = r.randint(X2.shape[0], size=(nb,)) + +Xs = X1[idx1, :] +Xt = X2[idx2, :] + + +############################################################################## +# Instantiate the different transport algorithms and fit them +############################################################################## + +# EMDTransport +ot_emd = ot.da.EMDTransport() +ot_emd.fit(Xs=Xs, Xt=Xt) + +# SinkhornTransport +ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) +ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + +# prediction between images (using out of sample prediction as in [6]) +transp_Xs_emd = ot_emd.transform(Xs=X1) +transp_Xt_emd = ot_emd.inverse_transform(Xt=X2) + +transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) +transp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2) + +I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) +I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) + +I1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) +I2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape)) + + +############################################################################## +# plot original image +############################################################################## + +pl.figure(1, figsize=(6.4, 3)) + +pl.subplot(1, 2, 1) +pl.imshow(I1) +pl.axis('off') +pl.title('Image 1') + +pl.subplot(1, 2, 2) +pl.imshow(I2) +pl.axis('off') +pl.title('Image 2') + + +############################################################################## +# scatter plot of colors +############################################################################## + +pl.figure(2, figsize=(6.4, 3)) + +pl.subplot(1, 2, 1) +pl.scatter(Xs[:, 0], Xs[:, 2], c=Xs) +pl.axis([0, 1, 0, 1]) +pl.xlabel('Red') +pl.ylabel('Blue') +pl.title('Image 1') + +pl.subplot(1, 2, 2) +pl.scatter(Xt[:, 0], Xt[:, 2], c=Xt) +pl.axis([0, 1, 0, 1]) +pl.xlabel('Red') +pl.ylabel('Blue') +pl.title('Image 2') +pl.tight_layout() + + +############################################################################## +# plot new images +############################################################################## + +pl.figure(3, figsize=(8, 4)) + +pl.subplot(2, 3, 1) +pl.imshow(I1) +pl.axis('off') +pl.title('Image 1') + +pl.subplot(2, 3, 2) +pl.imshow(I1t) +pl.axis('off') +pl.title('Image 1 Adapt') + +pl.subplot(2, 3, 3) +pl.imshow(I1te) +pl.axis('off') +pl.title('Image 1 Adapt (reg)') + +pl.subplot(2, 3, 4) +pl.imshow(I2) +pl.axis('off') +pl.title('Image 2') + +pl.subplot(2, 3, 5) +pl.imshow(I2t) +pl.axis('off') +pl.title('Image 2 Adapt') + +pl.subplot(2, 3, 6) +pl.imshow(I2te) +pl.axis('off') +pl.title('Image 2 Adapt (reg)') +pl.tight_layout() + +pl.show() -- cgit v1.2.3 From 062071b20d1d40c64bb619931bd11bd28e780485 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Fri, 1 Sep 2017 15:31:44 +0200 Subject: update example with rst titles --- .gitignore | 2 - .../source/auto_examples/auto_examples_jupyter.zip | Bin 91095 -> 70410 bytes docs/source/auto_examples/auto_examples_python.zip | Bin 62950 -> 46653 bytes docs/source/auto_examples/demo_OT_1D_test.ipynb | 54 ---- docs/source/auto_examples/demo_OT_1D_test.py | 71 ----- docs/source/auto_examples/demo_OT_1D_test.rst | 99 ------ .../auto_examples/demo_OT_2D_sampleslarge.ipynb | 54 ---- .../auto_examples/demo_OT_2D_sampleslarge.py | 78 ----- .../auto_examples/demo_OT_2D_sampleslarge.rst | 106 ------- .../images/sphx_glr_plot_OTDA_2D_001.png | Bin 52753 -> 0 bytes .../images/sphx_glr_plot_OTDA_2D_002.png | Bin 87798 -> 0 bytes .../images/sphx_glr_plot_OTDA_2D_003.png | Bin 167396 -> 0 bytes .../images/sphx_glr_plot_OTDA_2D_004.png | Bin 82929 -> 0 bytes .../images/sphx_glr_plot_OTDA_classes_001.png | Bin 53561 -> 0 bytes .../images/sphx_glr_plot_OTDA_classes_004.png | Bin 193523 -> 0 bytes .../images/sphx_glr_plot_OTDA_color_images_001.png | Bin 237854 -> 0 bytes .../images/sphx_glr_plot_OTDA_color_images_002.png | Bin 472911 -> 0 bytes .../images/sphx_glr_plot_OTDA_mapping_001.png | Bin 44168 -> 0 bytes .../images/sphx_glr_plot_OTDA_mapping_002.png | Bin 111565 -> 0 bytes ...sphx_glr_plot_OTDA_mapping_color_images_001.png | Bin 237854 -> 0 bytes ...sphx_glr_plot_OTDA_mapping_color_images_002.png | Bin 429859 -> 0 bytes .../images/sphx_glr_plot_OT_1D_003.png | Bin 16995 -> 0 bytes .../images/sphx_glr_plot_OT_1D_004.png | Bin 18923 -> 0 bytes .../images/sphx_glr_plot_OT_2D_samples_001.png | Bin 21092 -> 20707 bytes .../images/sphx_glr_plot_OT_2D_samples_002.png | Bin 21310 -> 21335 bytes .../images/sphx_glr_plot_OT_2D_samples_003.png | Bin 9625 -> 0 bytes .../images/sphx_glr_plot_OT_2D_samples_004.png | Bin 82376 -> 0 bytes .../images/sphx_glr_plot_OT_2D_samples_005.png | Bin 13913 -> 9613 bytes .../images/sphx_glr_plot_OT_2D_samples_006.png | Bin 102963 -> 83657 bytes .../images/sphx_glr_plot_OT_L1_vs_L2_001.png | Bin 14117 -> 11710 bytes .../images/sphx_glr_plot_OT_L1_vs_L2_002.png | Bin 18696 -> 17184 bytes .../images/sphx_glr_plot_OT_L1_vs_L2_003.png | Bin 21300 -> 38780 bytes .../images/sphx_glr_plot_OT_L1_vs_L2_005.png | Bin 17184 -> 38780 bytes .../auto_examples/images/sphx_glr_plot_WDA_001.png | Bin 56060 -> 55483 bytes .../auto_examples/images/sphx_glr_plot_WDA_002.png | Bin 90982 -> 0 bytes .../images/sphx_glr_plot_barycenter_1D_003.png | Bin 108687 -> 41555 bytes .../images/sphx_glr_plot_compute_emd_002.png | Bin 38746 -> 0 bytes .../images/sphx_glr_plot_optim_OTreg_005.png | Bin 20440 -> 0 bytes .../images/sphx_glr_plot_otda_classes_001.png | Bin 50114 -> 49949 bytes .../images/sphx_glr_plot_otda_classes_003.png | Bin 194170 -> 189153 bytes .../images/sphx_glr_plot_otda_color_images_001.png | Bin 144957 -> 144945 bytes .../images/sphx_glr_plot_otda_color_images_003.png | Bin 50401 -> 50403 bytes .../images/sphx_glr_plot_otda_color_images_005.png | Bin 234337 -> 234386 bytes .../images/sphx_glr_plot_otda_d2_001.png | Bin 130439 -> 131873 bytes .../images/sphx_glr_plot_otda_d2_003.png | Bin 224757 -> 240262 bytes .../images/sphx_glr_plot_otda_d2_006.png | Bin 99742 -> 104502 bytes .../images/sphx_glr_plot_otda_mapping_001.png | Bin 35810 -> 37940 bytes .../images/sphx_glr_plot_otda_mapping_003.png | Bin 71391 -> 76017 bytes ...phx_glr_plot_otda_mapping_colors_images_001.png | Bin 165592 -> 165589 bytes ...phx_glr_plot_otda_mapping_colors_images_003.png | Bin 80722 -> 80727 bytes ...phx_glr_plot_otda_mapping_colors_images_004.png | Bin 541483 -> 541463 bytes .../images/thumb/sphx_glr_plot_OTDA_2D_thumb.png | Bin 34799 -> 0 bytes .../thumb/sphx_glr_plot_OTDA_classes_thumb.png | Bin 34581 -> 0 bytes .../sphx_glr_plot_OTDA_color_images_thumb.png | Bin 52919 -> 0 bytes ...hx_glr_plot_OTDA_mapping_color_images_thumb.png | Bin 52919 -> 0 bytes .../thumb/sphx_glr_plot_OTDA_mapping_thumb.png | Bin 26370 -> 0 bytes .../images/thumb/sphx_glr_plot_OT_1D_thumb.png | Bin 18227 -> 18222 bytes .../thumb/sphx_glr_plot_OT_2D_samples_thumb.png | Bin 23844 -> 22370 bytes .../thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png | Bin 16407 -> 10935 bytes .../images/thumb/sphx_glr_plot_OT_conv_thumb.png | Bin 2894 -> 0 bytes .../images/thumb/sphx_glr_plot_WDA_thumb.png | Bin 87834 -> 88848 bytes .../thumb/sphx_glr_plot_barycenter_1D_thumb.png | Bin 16522 -> 16522 bytes .../thumb/sphx_glr_plot_compute_emd_thumb.png | Bin 80805 -> 80806 bytes .../thumb/sphx_glr_plot_optim_OTreg_thumb.png | Bin 21750 -> 3101 bytes .../thumb/sphx_glr_plot_otda_classes_thumb.png | Bin 30152 -> 29948 bytes .../sphx_glr_plot_otda_color_images_thumb.png | Bin 51085 -> 51088 bytes .../images/thumb/sphx_glr_plot_otda_d2_thumb.png | Bin 52925 -> 54746 bytes ...x_glr_plot_otda_mapping_colors_images_thumb.png | Bin 58315 -> 58321 bytes .../thumb/sphx_glr_plot_otda_mapping_thumb.png | Bin 18620 -> 19281 bytes ...phx_glr_test_OT_2D_samples_stabilized_thumb.png | Bin 3101 -> 0 bytes docs/source/auto_examples/index.rst | 64 ++-- docs/source/auto_examples/plot_OTDA_2D.ipynb | 54 ---- docs/source/auto_examples/plot_OTDA_2D.py | 120 ------- docs/source/auto_examples/plot_OTDA_2D.rst | 175 ---------- docs/source/auto_examples/plot_OTDA_classes.ipynb | 54 ---- docs/source/auto_examples/plot_OTDA_classes.py | 112 ------- docs/source/auto_examples/plot_OTDA_classes.rst | 190 ----------- .../auto_examples/plot_OTDA_color_images.ipynb | 54 ---- .../source/auto_examples/plot_OTDA_color_images.py | 145 --------- .../auto_examples/plot_OTDA_color_images.rst | 191 ----------- docs/source/auto_examples/plot_OTDA_mapping.ipynb | 54 ---- docs/source/auto_examples/plot_OTDA_mapping.py | 110 ------- docs/source/auto_examples/plot_OTDA_mapping.rst | 186 ----------- .../plot_OTDA_mapping_color_images.ipynb | 54 ---- .../plot_OTDA_mapping_color_images.py | 158 ---------- .../plot_OTDA_mapping_color_images.rst | 246 --------------- docs/source/auto_examples/plot_OT_1D.ipynb | 4 +- docs/source/auto_examples/plot_OT_1D.py | 5 +- docs/source/auto_examples/plot_OT_1D.rst | 9 +- docs/source/auto_examples/plot_OT_2D_samples.ipynb | 76 ++++- docs/source/auto_examples/plot_OT_2D_samples.py | 18 ++ docs/source/auto_examples/plot_OT_2D_samples.rst | 132 ++++++-- docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb | 76 ++++- docs/source/auto_examples/plot_OT_L1_vs_L2.py | 280 ++++++++++------ docs/source/auto_examples/plot_OT_L1_vs_L2.rst | 351 ++++++++++++++------- docs/source/auto_examples/plot_OT_conv.ipynb | 54 ---- docs/source/auto_examples/plot_OT_conv.py | 200 ------------ docs/source/auto_examples/plot_OT_conv.rst | 241 -------------- docs/source/auto_examples/plot_WDA.ipynb | 94 +++++- docs/source/auto_examples/plot_WDA.py | 27 ++ docs/source/auto_examples/plot_WDA.rst | 172 ++++++---- docs/source/auto_examples/plot_barycenter_1D.ipynb | 76 ++++- docs/source/auto_examples/plot_barycenter_1D.py | 23 ++ docs/source/auto_examples/plot_barycenter_1D.rst | 113 +++++-- docs/source/auto_examples/plot_compute_emd.ipynb | 76 ++++- docs/source/auto_examples/plot_compute_emd.py | 30 +- docs/source/auto_examples/plot_compute_emd.rst | 100 ++++-- docs/source/auto_examples/plot_optim_OTreg.ipynb | 8 +- docs/source/auto_examples/plot_optim_OTreg.py | 23 +- docs/source/auto_examples/plot_optim_OTreg.rst | 29 +- docs/source/auto_examples/plot_otda_classes.rst | 46 +-- .../auto_examples/plot_otda_color_images.ipynb | 18 +- .../source/auto_examples/plot_otda_color_images.py | 66 ++-- .../auto_examples/plot_otda_color_images.rst | 90 +++--- docs/source/auto_examples/plot_otda_d2.rst | 4 +- docs/source/auto_examples/plot_otda_mapping.ipynb | 14 +- docs/source/auto_examples/plot_otda_mapping.py | 35 +- docs/source/auto_examples/plot_otda_mapping.rst | 105 +++--- .../plot_otda_mapping_colors_images.ipynb | 8 +- .../plot_otda_mapping_colors_images.py | 15 +- .../plot_otda_mapping_colors_images.rst | 85 ++--- docs/source/conf.py | 4 +- docs/source/examples.rst | 39 --- examples/README.txt | 2 + examples/plot_OT_1D.py | 2 +- examples/plot_OT_2D_samples.py | 3 + examples/plot_OT_L1_vs_L2.py | 280 ++++++++++------ examples/plot_barycenter_1D.py | 17 +- examples/plot_compute_emd.py | 4 + examples/plot_optim_OTreg.py | 4 +- examples/plot_otda_mapping.py | 10 +- examples/plot_otda_mapping_colors_images.py | 6 +- 132 files changed, 1849 insertions(+), 3656 deletions(-) delete mode 100644 docs/source/auto_examples/demo_OT_1D_test.ipynb delete mode 100644 docs/source/auto_examples/demo_OT_1D_test.py delete mode 100644 docs/source/auto_examples/demo_OT_1D_test.rst delete mode 100644 docs/source/auto_examples/demo_OT_2D_sampleslarge.ipynb delete mode 100644 docs/source/auto_examples/demo_OT_2D_sampleslarge.py delete mode 100644 docs/source/auto_examples/demo_OT_2D_sampleslarge.rst delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_001.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_002.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_003.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_004.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_classes_001.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_classes_004.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_color_images_001.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_color_images_002.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_001.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_002.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_001.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_002.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_1D_003.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_1D_004.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_WDA_002.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_compute_emd_002.png delete mode 100644 docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_005.png delete mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_2D_thumb.png delete mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_classes_thumb.png delete mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_color_images_thumb.png delete mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_color_images_thumb.png delete mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_thumb.png delete mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_conv_thumb.png delete mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_test_OT_2D_samples_stabilized_thumb.png delete mode 100644 docs/source/auto_examples/plot_OTDA_2D.ipynb delete mode 100644 docs/source/auto_examples/plot_OTDA_2D.py delete mode 100644 docs/source/auto_examples/plot_OTDA_2D.rst delete mode 100644 docs/source/auto_examples/plot_OTDA_classes.ipynb delete mode 100644 docs/source/auto_examples/plot_OTDA_classes.py delete mode 100644 docs/source/auto_examples/plot_OTDA_classes.rst delete mode 100644 docs/source/auto_examples/plot_OTDA_color_images.ipynb delete mode 100644 docs/source/auto_examples/plot_OTDA_color_images.py delete mode 100644 docs/source/auto_examples/plot_OTDA_color_images.rst delete mode 100644 docs/source/auto_examples/plot_OTDA_mapping.ipynb delete mode 100644 docs/source/auto_examples/plot_OTDA_mapping.py delete mode 100644 docs/source/auto_examples/plot_OTDA_mapping.rst delete mode 100644 docs/source/auto_examples/plot_OTDA_mapping_color_images.ipynb delete mode 100644 docs/source/auto_examples/plot_OTDA_mapping_color_images.py delete mode 100644 docs/source/auto_examples/plot_OTDA_mapping_color_images.rst delete mode 100644 docs/source/auto_examples/plot_OT_conv.ipynb delete mode 100644 docs/source/auto_examples/plot_OT_conv.py delete mode 100644 docs/source/auto_examples/plot_OT_conv.rst delete mode 100644 docs/source/examples.rst (limited to 'docs/source/auto_examples/plot_otda_color_images.py') diff --git a/.gitignore b/.gitignore index 42a9aad..887a164 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,6 @@ __pycache__/ .spyproject # sphinx-gallery temp files -docs/source/auto_examples/*.pickle -docs/source/auto_examples/*.md5 docs/auto_examples/ docs/modules/ diff --git a/docs/source/auto_examples/auto_examples_jupyter.zip b/docs/source/auto_examples/auto_examples_jupyter.zip index 92aa027..96bc0bc 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 bc41a8c..6241b92 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/demo_OT_1D_test.ipynb b/docs/source/auto_examples/demo_OT_1D_test.ipynb deleted file mode 100644 index 87317ea..0000000 --- a/docs/source/auto_examples/demo_OT_1D_test.ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "nbformat_minor": 0, - "nbformat": 4, - "cells": [ - { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - }, - { - "source": [ - "\nDemo for 1D optimal transport\n\n@author: rflamary\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, - { - "execution_count": null, - "cell_type": "code", - "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom ot.datasets import get_1D_gauss as gauss\n\n\n#%% 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=n*.2,s=5) # m= mean, s= std\nb=gauss(n,m=n*.6,s=10)\n\n# loss matrix\nM=ot.dist(x.reshape((n,1)),x.reshape((n,1)))\nM/=M.max()\n\n#%% plot the distributions\n\npl.figure(1)\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)\not.plot.plot1D_mat(a,b,M,'Cost matrix M')\n\n#%% EMD\n\nG0=ot.emd(a,b,M)\n\npl.figure(3)\not.plot.plot1D_mat(a,b,G0,'OT matrix G0')\n\n#%% Sinkhorn\n\nlambd=1e-3\nGs=ot.sinkhorn(a,b,M,lambd,verbose=True)\n\npl.figure(4)\not.plot.plot1D_mat(a,b,Gs,'OT matrix Sinkhorn')\n\n#%% Sinkhorn\n\nlambd=1e-4\nGss,log=ot.bregman.sinkhorn_stabilized(a,b,M,lambd,verbose=True,log=True)\nGss2,log2=ot.bregman.sinkhorn_stabilized(a,b,M,lambd,verbose=True,log=True,warmstart=log['warmstart'])\n\npl.figure(5)\not.plot.plot1D_mat(a,b,Gss,'OT matrix Sinkhorn stabilized')\n\n#%% Sinkhorn\n\nlambd=1e-11\nGss=ot.bregman.sinkhorn_epsilon_scaling(a,b,M,lambd,verbose=True)\n\npl.figure(5)\not.plot.plot1D_mat(a,b,Gss,'OT matrix Sinkhorn stabilized')" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, - "language_info": { - "mimetype": "text/x-python", - "nbconvert_exporter": "python", - "name": "python", - "file_extension": ".py", - "version": "2.7.12", - "pygments_lexer": "ipython2", - "codemirror_mode": { - "version": 2, - "name": "ipython" - } - } - } -} \ No newline at end of file diff --git a/docs/source/auto_examples/demo_OT_1D_test.py b/docs/source/auto_examples/demo_OT_1D_test.py deleted file mode 100644 index 9edc377..0000000 --- a/docs/source/auto_examples/demo_OT_1D_test.py +++ /dev/null @@ -1,71 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Demo for 1D optimal transport - -@author: rflamary -""" - -import numpy as np -import matplotlib.pylab as pl -import ot -from ot.datasets import get_1D_gauss as gauss - - -#%% parameters - -n=100 # nb bins - -# bin positions -x=np.arange(n,dtype=np.float64) - -# Gaussian distributions -a=gauss(n,m=n*.2,s=5) # m= mean, s= std -b=gauss(n,m=n*.6,s=10) - -# loss matrix -M=ot.dist(x.reshape((n,1)),x.reshape((n,1))) -M/=M.max() - -#%% plot the distributions - -pl.figure(1) -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) -ot.plot.plot1D_mat(a,b,M,'Cost matrix M') - -#%% EMD - -G0=ot.emd(a,b,M) - -pl.figure(3) -ot.plot.plot1D_mat(a,b,G0,'OT matrix G0') - -#%% Sinkhorn - -lambd=1e-3 -Gs=ot.sinkhorn(a,b,M,lambd,verbose=True) - -pl.figure(4) -ot.plot.plot1D_mat(a,b,Gs,'OT matrix Sinkhorn') - -#%% Sinkhorn - -lambd=1e-4 -Gss,log=ot.bregman.sinkhorn_stabilized(a,b,M,lambd,verbose=True,log=True) -Gss2,log2=ot.bregman.sinkhorn_stabilized(a,b,M,lambd,verbose=True,log=True,warmstart=log['warmstart']) - -pl.figure(5) -ot.plot.plot1D_mat(a,b,Gss,'OT matrix Sinkhorn stabilized') - -#%% Sinkhorn - -lambd=1e-11 -Gss=ot.bregman.sinkhorn_epsilon_scaling(a,b,M,lambd,verbose=True) - -pl.figure(5) -ot.plot.plot1D_mat(a,b,Gss,'OT matrix Sinkhorn stabilized') diff --git a/docs/source/auto_examples/demo_OT_1D_test.rst b/docs/source/auto_examples/demo_OT_1D_test.rst deleted file mode 100644 index aebeb1d..0000000 --- a/docs/source/auto_examples/demo_OT_1D_test.rst +++ /dev/null @@ -1,99 +0,0 @@ - - -.. _sphx_glr_auto_examples_demo_OT_1D_test.py: - - -Demo for 1D optimal transport - -@author: rflamary - - - -.. code-block:: python - - - import numpy as np - import matplotlib.pylab as pl - import ot - from ot.datasets import get_1D_gauss as gauss - - - #%% parameters - - n=100 # nb bins - - # bin positions - x=np.arange(n,dtype=np.float64) - - # Gaussian distributions - a=gauss(n,m=n*.2,s=5) # m= mean, s= std - b=gauss(n,m=n*.6,s=10) - - # loss matrix - M=ot.dist(x.reshape((n,1)),x.reshape((n,1))) - M/=M.max() - - #%% plot the distributions - - pl.figure(1) - 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) - ot.plot.plot1D_mat(a,b,M,'Cost matrix M') - - #%% EMD - - G0=ot.emd(a,b,M) - - pl.figure(3) - ot.plot.plot1D_mat(a,b,G0,'OT matrix G0') - - #%% Sinkhorn - - lambd=1e-3 - Gs=ot.sinkhorn(a,b,M,lambd,verbose=True) - - pl.figure(4) - ot.plot.plot1D_mat(a,b,Gs,'OT matrix Sinkhorn') - - #%% Sinkhorn - - lambd=1e-4 - Gss,log=ot.bregman.sinkhorn_stabilized(a,b,M,lambd,verbose=True,log=True) - Gss2,log2=ot.bregman.sinkhorn_stabilized(a,b,M,lambd,verbose=True,log=True,warmstart=log['warmstart']) - - pl.figure(5) - ot.plot.plot1D_mat(a,b,Gss,'OT matrix Sinkhorn stabilized') - - #%% Sinkhorn - - lambd=1e-11 - Gss=ot.bregman.sinkhorn_epsilon_scaling(a,b,M,lambd,verbose=True) - - pl.figure(5) - ot.plot.plot1D_mat(a,b,Gss,'OT matrix Sinkhorn stabilized') - -**Total running time of the script:** ( 0 minutes 0.000 seconds) - - - -.. container:: sphx-glr-footer - - - .. container:: sphx-glr-download - - :download:`Download Python source code: demo_OT_1D_test.py ` - - - - .. container:: sphx-glr-download - - :download:`Download Jupyter notebook: demo_OT_1D_test.ipynb ` - -.. rst-class:: sphx-glr-signature - - `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/demo_OT_2D_sampleslarge.ipynb b/docs/source/auto_examples/demo_OT_2D_sampleslarge.ipynb deleted file mode 100644 index 584a936..0000000 --- a/docs/source/auto_examples/demo_OT_2D_sampleslarge.ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "nbformat_minor": 0, - "nbformat": 4, - "cells": [ - { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - }, - { - "source": [ - "\nDemo for 2D Optimal transport between empirical distributions\n\n@author: rflamary\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, - { - "execution_count": null, - "cell_type": "code", - "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n#%% parameters and data generation\n\nn=5000 # nb samples\n\nmu_s=np.array([0,0])\ncov_s=np.array([[1,0],[0,1]])\n\nmu_t=np.array([4,4])\ncov_t=np.array([[1,-.8],[-.8,1]])\n\nxs=ot.datasets.get_2D_samples_gauss(n,mu_s,cov_s)\nxt=ot.datasets.get_2D_samples_gauss(n,mu_t,cov_t)\n\na,b = ot.unif(n),ot.unif(n) # uniform distribution on samples\n\n# loss matrix\nM=ot.dist(xs,xt)\nM/=M.max()\n\n#%% plot samples\n\n#pl.figure(1)\n#pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples')\n#pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples')\n#pl.legend(loc=0)\n#pl.title('Source and traget distributions')\n#\n#pl.figure(2)\n#pl.imshow(M,interpolation='nearest')\n#pl.title('Cost matrix M')\n#\n\n#%% EMD\n\nG0=ot.emd(a,b,M)\n\n#pl.figure(3)\n#pl.imshow(G0,interpolation='nearest')\n#pl.title('OT matrix G0')\n#\n#pl.figure(4)\n#ot.plot.plot2D_samples_mat(xs,xt,G0,c=[.5,.5,1])\n#pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples')\n#pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples')\n#pl.legend(loc=0)\n#pl.title('OT matrix with samples')\n\n\n#%% sinkhorn\n\n# reg term\nlambd=5e-3\n\nGs=ot.sinkhorn(a,b,M,lambd)\n\n#pl.figure(5)\n#pl.imshow(Gs,interpolation='nearest')\n#pl.title('OT matrix sinkhorn')\n#\n#pl.figure(6)\n#ot.plot.plot2D_samples_mat(xs,xt,Gs,color=[.5,.5,1])\n#pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples')\n#pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples')\n#pl.legend(loc=0)\n#pl.title('OT matrix Sinkhorn with samples')\n#" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, - "language_info": { - "mimetype": "text/x-python", - "nbconvert_exporter": "python", - "name": "python", - "file_extension": ".py", - "version": "2.7.12", - "pygments_lexer": "ipython2", - "codemirror_mode": { - "version": 2, - "name": "ipython" - } - } - } -} \ No newline at end of file diff --git a/docs/source/auto_examples/demo_OT_2D_sampleslarge.py b/docs/source/auto_examples/demo_OT_2D_sampleslarge.py deleted file mode 100644 index ee3e8f7..0000000 --- a/docs/source/auto_examples/demo_OT_2D_sampleslarge.py +++ /dev/null @@ -1,78 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Demo for 2D Optimal transport between empirical distributions - -@author: rflamary -""" - -import numpy as np -import matplotlib.pylab as pl -import ot - -#%% parameters and data generation - -n=5000 # nb samples - -mu_s=np.array([0,0]) -cov_s=np.array([[1,0],[0,1]]) - -mu_t=np.array([4,4]) -cov_t=np.array([[1,-.8],[-.8,1]]) - -xs=ot.datasets.get_2D_samples_gauss(n,mu_s,cov_s) -xt=ot.datasets.get_2D_samples_gauss(n,mu_t,cov_t) - -a,b = ot.unif(n),ot.unif(n) # uniform distribution on samples - -# loss matrix -M=ot.dist(xs,xt) -M/=M.max() - -#%% plot samples - -#pl.figure(1) -#pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') -#pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') -#pl.legend(loc=0) -#pl.title('Source and traget distributions') -# -#pl.figure(2) -#pl.imshow(M,interpolation='nearest') -#pl.title('Cost matrix M') -# - -#%% EMD - -G0=ot.emd(a,b,M) - -#pl.figure(3) -#pl.imshow(G0,interpolation='nearest') -#pl.title('OT matrix G0') -# -#pl.figure(4) -#ot.plot.plot2D_samples_mat(xs,xt,G0,c=[.5,.5,1]) -#pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') -#pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') -#pl.legend(loc=0) -#pl.title('OT matrix with samples') - - -#%% sinkhorn - -# reg term -lambd=5e-3 - -Gs=ot.sinkhorn(a,b,M,lambd) - -#pl.figure(5) -#pl.imshow(Gs,interpolation='nearest') -#pl.title('OT matrix sinkhorn') -# -#pl.figure(6) -#ot.plot.plot2D_samples_mat(xs,xt,Gs,color=[.5,.5,1]) -#pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') -#pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') -#pl.legend(loc=0) -#pl.title('OT matrix Sinkhorn with samples') -# - diff --git a/docs/source/auto_examples/demo_OT_2D_sampleslarge.rst b/docs/source/auto_examples/demo_OT_2D_sampleslarge.rst deleted file mode 100644 index f5dbb0d..0000000 --- a/docs/source/auto_examples/demo_OT_2D_sampleslarge.rst +++ /dev/null @@ -1,106 +0,0 @@ - - -.. _sphx_glr_auto_examples_demo_OT_2D_sampleslarge.py: - - -Demo for 2D Optimal transport between empirical distributions - -@author: rflamary - - - -.. code-block:: python - - - import numpy as np - import matplotlib.pylab as pl - import ot - - #%% parameters and data generation - - n=5000 # nb samples - - mu_s=np.array([0,0]) - cov_s=np.array([[1,0],[0,1]]) - - mu_t=np.array([4,4]) - cov_t=np.array([[1,-.8],[-.8,1]]) - - xs=ot.datasets.get_2D_samples_gauss(n,mu_s,cov_s) - xt=ot.datasets.get_2D_samples_gauss(n,mu_t,cov_t) - - a,b = ot.unif(n),ot.unif(n) # uniform distribution on samples - - # loss matrix - M=ot.dist(xs,xt) - M/=M.max() - - #%% plot samples - - #pl.figure(1) - #pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - #pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') - #pl.legend(loc=0) - #pl.title('Source and traget distributions') - # - #pl.figure(2) - #pl.imshow(M,interpolation='nearest') - #pl.title('Cost matrix M') - # - - #%% EMD - - G0=ot.emd(a,b,M) - - #pl.figure(3) - #pl.imshow(G0,interpolation='nearest') - #pl.title('OT matrix G0') - # - #pl.figure(4) - #ot.plot.plot2D_samples_mat(xs,xt,G0,c=[.5,.5,1]) - #pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - #pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') - #pl.legend(loc=0) - #pl.title('OT matrix with samples') - - - #%% sinkhorn - - # reg term - lambd=5e-3 - - Gs=ot.sinkhorn(a,b,M,lambd) - - #pl.figure(5) - #pl.imshow(Gs,interpolation='nearest') - #pl.title('OT matrix sinkhorn') - # - #pl.figure(6) - #ot.plot.plot2D_samples_mat(xs,xt,Gs,color=[.5,.5,1]) - #pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - #pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') - #pl.legend(loc=0) - #pl.title('OT matrix Sinkhorn with samples') - # - - -**Total running time of the script:** ( 0 minutes 0.000 seconds) - - - -.. container:: sphx-glr-footer - - - .. container:: sphx-glr-download - - :download:`Download Python source code: demo_OT_2D_sampleslarge.py ` - - - - .. container:: sphx-glr-download - - :download:`Download Jupyter notebook: demo_OT_2D_sampleslarge.ipynb ` - -.. rst-class:: sphx-glr-signature - - `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_001.png deleted file mode 100644 index 7de2b45..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_001.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_002.png deleted file mode 100644 index dc34efd..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_002.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_003.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_003.png deleted file mode 100644 index fbd72d5..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_003.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_004.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_004.png deleted file mode 100644 index 227812d..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_2D_004.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_classes_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_classes_001.png deleted file mode 100644 index 2bf4015..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_classes_001.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_classes_004.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_classes_004.png deleted file mode 100644 index c1fbf57..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_classes_004.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_color_images_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_color_images_001.png deleted file mode 100644 index 36bc769..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_color_images_001.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_color_images_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_color_images_002.png deleted file mode 100644 index 307e384..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_color_images_002.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_001.png deleted file mode 100644 index 8c700ee..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_001.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_002.png deleted file mode 100644 index 792b404..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_002.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_001.png deleted file mode 100644 index 36bc769..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_001.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_002.png deleted file mode 100644 index 008bf15..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_002.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_003.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_003.png deleted file mode 100644 index a75e649..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_003.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_004.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_004.png deleted file mode 100644 index 96b42cd..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_004.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png index e675cd8..172d736 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png index 8c29b7b..3043a72 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png deleted file mode 100644 index 1308674..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png deleted file mode 100644 index 95d947e..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png index 82ab78c..5565d75 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png index f1d2bfe..06d1020 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_001.png index 22dba2b..6a21f35 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png index 5dbf96b..79e4710 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png index e1e9ba8..4860d96 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png index 79e4710..4860d96 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png b/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png index 41ec230..a9fff75 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_WDA_002.png b/docs/source/auto_examples/images/sphx_glr_plot_WDA_002.png deleted file mode 100644 index 95ee7ca..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_WDA_002.png and /dev/null differ 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 index eac9230..3b23af5 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_compute_emd_002.png b/docs/source/auto_examples/images/sphx_glr_plot_compute_emd_002.png deleted file mode 100644 index 7c06255..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_compute_emd_002.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_005.png b/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_005.png deleted file mode 100644 index 8a4882a..0000000 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_005.png and /dev/null differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png index 45a823d..a28f245 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png index 2798f3b..4d0b12d 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png index 95f882a..2d851c7 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png index aa1a5d3..a1d99ab 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png index b43c0cb..f76619b 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png index bc583a8..9e78aed 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png index 7d85e76..d37359b 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png index fba820a..c71284a 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png index 03a3130..d2ee139 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png index 9c9be23..fa1ab81 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png index 33134fc..1182082 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png index 42197e3..cc2e4cd 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png index ebf268b..7a68343 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_2D_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_2D_thumb.png deleted file mode 100644 index d15269d..0000000 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_2D_thumb.png and /dev/null differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_classes_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_classes_thumb.png deleted file mode 100644 index 5863d02..0000000 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_classes_thumb.png and /dev/null differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_color_images_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_color_images_thumb.png deleted file mode 100644 index 5bb43c4..0000000 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_color_images_thumb.png and /dev/null differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_color_images_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_color_images_thumb.png deleted file mode 100644 index 5bb43c4..0000000 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_color_images_thumb.png and /dev/null differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_thumb.png deleted file mode 100644 index c3d9a65..0000000 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_thumb.png and /dev/null differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_thumb.png index 63ff40c..a3b7039 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png index 48e1449..1f42900 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png index 9deebf0..95588f5 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_conv_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_conv_thumb.png deleted file mode 100644 index 3015582..0000000 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_conv_thumb.png and /dev/null differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png index 4b409a0..8db2b9a 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png index 5c17671..d8cdccb 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png index 68cbdf7..898cd72 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_optim_OTreg_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_optim_OTreg_thumb.png index 2a72060..cbc8e0f 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_optim_OTreg_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_optim_OTreg_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png index 3dfc6ca..a2571a5 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png index a919055..16b7572 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png index d9d673c..6c8f37f 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png index f7fd217..9666955 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png index 4ab5023..a042411 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_test_OT_2D_samples_stabilized_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_test_OT_2D_samples_stabilized_thumb.png deleted file mode 100644 index cbc8e0f..0000000 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_test_OT_2D_samples_stabilized_thumb.png and /dev/null differ diff --git a/docs/source/auto_examples/index.rst b/docs/source/auto_examples/index.rst index b932907..ebcca85 100644 --- a/docs/source/auto_examples/index.rst +++ b/docs/source/auto_examples/index.rst @@ -1,9 +1,15 @@ +:orphan: + POT Examples ============ +This is a gallery of all the POT example files. + + + .. raw:: html -
+
.. only:: html @@ -23,7 +29,7 @@ POT Examples .. raw:: html -
+
.. only:: html @@ -43,7 +49,7 @@ POT Examples .. raw:: html -
+
.. only:: html @@ -63,7 +69,7 @@ POT Examples .. raw:: html -
+
.. only:: html @@ -83,7 +89,7 @@ POT Examples .. raw:: html -
+
.. only:: html @@ -123,7 +129,7 @@ POT Examples .. raw:: html -
+
.. only:: html @@ -143,13 +149,13 @@ POT Examples .. raw:: html -
+
.. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png - :ref:`sphx_glr_auto_examples_plot_OT_L1_vs_L2.py` + :ref:`sphx_glr_auto_examples_plot_otda_mapping_colors_images.py` .. raw:: html @@ -159,17 +165,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_OT_L1_vs_L2 + /auto_examples/plot_otda_mapping_colors_images .. raw:: html -
+
.. 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_mapping_thumb.png - :ref:`sphx_glr_auto_examples_plot_otda_mapping_colors_images.py` + :ref:`sphx_glr_auto_examples_plot_otda_mapping.py` .. raw:: html @@ -179,17 +185,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_otda_mapping_colors_images + /auto_examples/plot_otda_mapping .. raw:: html -
+
.. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png - :ref:`sphx_glr_auto_examples_plot_otda_mapping.py` + :ref:`sphx_glr_auto_examples_plot_otda_classes.py` .. raw:: html @@ -199,17 +205,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_otda_mapping + /auto_examples/plot_otda_classes .. raw:: html -
+
.. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png - :ref:`sphx_glr_auto_examples_plot_otda_classes.py` + :ref:`sphx_glr_auto_examples_plot_otda_d2.py` .. raw:: html @@ -219,17 +225,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_otda_classes + /auto_examples/plot_otda_d2 .. raw:: html -
+
.. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png - :ref:`sphx_glr_auto_examples_plot_otda_d2.py` + :ref:`sphx_glr_auto_examples_plot_OT_L1_vs_L2.py` .. raw:: html @@ -239,7 +245,7 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_otda_d2 + /auto_examples/plot_OT_L1_vs_L2 .. raw:: html
@@ -251,14 +257,14 @@ POT Examples .. container:: sphx-glr-download - :download:`Download all examples in Python source code: auto_examples_python.zip ` + :download:`Download all examples in Python source code: auto_examples_python.zip ` .. container:: sphx-glr-download - :download:`Download all examples in Jupyter notebooks: auto_examples_jupyter.zip ` + :download:`Download all examples in Jupyter notebooks: auto_examples_jupyter.zip ` .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_OTDA_2D.ipynb b/docs/source/auto_examples/plot_OTDA_2D.ipynb deleted file mode 100644 index 2ffb256..0000000 --- a/docs/source/auto_examples/plot_OTDA_2D.ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "nbformat_minor": 0, - "nbformat": 4, - "cells": [ - { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - }, - { - "source": [ - "\n# OT for empirical distributions\n\n\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, - { - "execution_count": null, - "cell_type": "code", - "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n\n\n#%% parameters\n\nn=150 # nb bins\n\nxs,ys=ot.datasets.get_data_classif('3gauss',n)\nxt,yt=ot.datasets.get_data_classif('3gauss2',n)\n\na,b = ot.unif(n),ot.unif(n)\n# loss matrix\nM=ot.dist(xs,xt)\n#M/=M.max()\n\n#%% plot samples\n\npl.figure(1)\n\npl.subplot(2,2,1)\npl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples')\npl.legend(loc=0)\npl.title('Source distributions')\n\npl.subplot(2,2,2)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples')\npl.legend(loc=0)\npl.title('target distributions')\n\npl.figure(2)\npl.imshow(M,interpolation='nearest')\npl.title('Cost matrix M')\n\n\n#%% OT estimation\n\n# EMD\nG0=ot.emd(a,b,M)\n\n# sinkhorn\nlambd=1e-1\nGs=ot.sinkhorn(a,b,M,lambd)\n\n\n# Group lasso regularization\nreg=1e-1\neta=1e0\nGg=ot.da.sinkhorn_lpl1_mm(a,ys.astype(np.int),b,M,reg,eta)\n\n\n#%% visu matrices\n\npl.figure(3)\n\npl.subplot(2,3,1)\npl.imshow(G0,interpolation='nearest')\npl.title('OT matrix ')\n\npl.subplot(2,3,2)\npl.imshow(Gs,interpolation='nearest')\npl.title('OT matrix Sinkhorn')\n\npl.subplot(2,3,3)\npl.imshow(Gg,interpolation='nearest')\npl.title('OT matrix Group lasso')\n\npl.subplot(2,3,4)\not.plot.plot2D_samples_mat(xs,xt,G0,c=[.5,.5,1])\npl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples')\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples')\n\n\npl.subplot(2,3,5)\not.plot.plot2D_samples_mat(xs,xt,Gs,c=[.5,.5,1])\npl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples')\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples')\n\npl.subplot(2,3,6)\not.plot.plot2D_samples_mat(xs,xt,Gg,c=[.5,.5,1])\npl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples')\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples')\n\n#%% sample interpolation\n\nxst0=n*G0.dot(xt)\nxsts=n*Gs.dot(xt)\nxstg=n*Gg.dot(xt)\n\npl.figure(4)\npl.subplot(2,3,1)\n\n\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5)\npl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Transp samples',s=30)\npl.title('Interp samples')\npl.legend(loc=0)\n\npl.subplot(2,3,2)\n\n\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5)\npl.scatter(xsts[:,0],xsts[:,1],c=ys,marker='+',label='Transp samples',s=30)\npl.title('Interp samples Sinkhorn')\n\npl.subplot(2,3,3)\n\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5)\npl.scatter(xstg[:,0],xstg[:,1],c=ys,marker='+',label='Transp samples',s=30)\npl.title('Interp samples Grouplasso')" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, - "language_info": { - "mimetype": "text/x-python", - "nbconvert_exporter": "python", - "name": "python", - "file_extension": ".py", - "version": "2.7.12", - "pygments_lexer": "ipython2", - "codemirror_mode": { - "version": 2, - "name": "ipython" - } - } - } -} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_OTDA_2D.py b/docs/source/auto_examples/plot_OTDA_2D.py deleted file mode 100644 index a1fb804..0000000 --- a/docs/source/auto_examples/plot_OTDA_2D.py +++ /dev/null @@ -1,120 +0,0 @@ -# -*- coding: utf-8 -*- -""" -============================== -OT for empirical distributions -============================== - -""" - -import numpy as np -import matplotlib.pylab as pl -import ot - - - -#%% parameters - -n=150 # nb bins - -xs,ys=ot.datasets.get_data_classif('3gauss',n) -xt,yt=ot.datasets.get_data_classif('3gauss2',n) - -a,b = ot.unif(n),ot.unif(n) -# loss matrix -M=ot.dist(xs,xt) -#M/=M.max() - -#%% plot samples - -pl.figure(1) - -pl.subplot(2,2,1) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') -pl.legend(loc=0) -pl.title('Source distributions') - -pl.subplot(2,2,2) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') -pl.legend(loc=0) -pl.title('target distributions') - -pl.figure(2) -pl.imshow(M,interpolation='nearest') -pl.title('Cost matrix M') - - -#%% OT estimation - -# EMD -G0=ot.emd(a,b,M) - -# sinkhorn -lambd=1e-1 -Gs=ot.sinkhorn(a,b,M,lambd) - - -# Group lasso regularization -reg=1e-1 -eta=1e0 -Gg=ot.da.sinkhorn_lpl1_mm(a,ys.astype(np.int),b,M,reg,eta) - - -#%% visu matrices - -pl.figure(3) - -pl.subplot(2,3,1) -pl.imshow(G0,interpolation='nearest') -pl.title('OT matrix ') - -pl.subplot(2,3,2) -pl.imshow(Gs,interpolation='nearest') -pl.title('OT matrix Sinkhorn') - -pl.subplot(2,3,3) -pl.imshow(Gg,interpolation='nearest') -pl.title('OT matrix Group lasso') - -pl.subplot(2,3,4) -ot.plot.plot2D_samples_mat(xs,xt,G0,c=[.5,.5,1]) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - - -pl.subplot(2,3,5) -ot.plot.plot2D_samples_mat(xs,xt,Gs,c=[.5,.5,1]) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - -pl.subplot(2,3,6) -ot.plot.plot2D_samples_mat(xs,xt,Gg,c=[.5,.5,1]) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - -#%% sample interpolation - -xst0=n*G0.dot(xt) -xsts=n*Gs.dot(xt) -xstg=n*Gg.dot(xt) - -pl.figure(4) -pl.subplot(2,3,1) - - -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5) -pl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Transp samples',s=30) -pl.title('Interp samples') -pl.legend(loc=0) - -pl.subplot(2,3,2) - - -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5) -pl.scatter(xsts[:,0],xsts[:,1],c=ys,marker='+',label='Transp samples',s=30) -pl.title('Interp samples Sinkhorn') - -pl.subplot(2,3,3) - -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5) -pl.scatter(xstg[:,0],xstg[:,1],c=ys,marker='+',label='Transp samples',s=30) -pl.title('Interp samples Grouplasso') \ No newline at end of file diff --git a/docs/source/auto_examples/plot_OTDA_2D.rst b/docs/source/auto_examples/plot_OTDA_2D.rst deleted file mode 100644 index b535bb0..0000000 --- a/docs/source/auto_examples/plot_OTDA_2D.rst +++ /dev/null @@ -1,175 +0,0 @@ - - -.. _sphx_glr_auto_examples_plot_OTDA_2D.py: - - -============================== -OT for empirical distributions -============================== - - - - - -.. rst-class:: sphx-glr-horizontal - - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_2D_001.png - :scale: 47 - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_2D_002.png - :scale: 47 - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_2D_003.png - :scale: 47 - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_2D_004.png - :scale: 47 - - - - - -.. code-block:: python - - - import numpy as np - import matplotlib.pylab as pl - import ot - - - - #%% parameters - - n=150 # nb bins - - xs,ys=ot.datasets.get_data_classif('3gauss',n) - xt,yt=ot.datasets.get_data_classif('3gauss2',n) - - a,b = ot.unif(n),ot.unif(n) - # loss matrix - M=ot.dist(xs,xt) - #M/=M.max() - - #%% plot samples - - pl.figure(1) - - pl.subplot(2,2,1) - pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') - pl.legend(loc=0) - pl.title('Source distributions') - - pl.subplot(2,2,2) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - pl.legend(loc=0) - pl.title('target distributions') - - pl.figure(2) - pl.imshow(M,interpolation='nearest') - pl.title('Cost matrix M') - - - #%% OT estimation - - # EMD - G0=ot.emd(a,b,M) - - # sinkhorn - lambd=1e-1 - Gs=ot.sinkhorn(a,b,M,lambd) - - - # Group lasso regularization - reg=1e-1 - eta=1e0 - Gg=ot.da.sinkhorn_lpl1_mm(a,ys.astype(np.int),b,M,reg,eta) - - - #%% visu matrices - - pl.figure(3) - - pl.subplot(2,3,1) - pl.imshow(G0,interpolation='nearest') - pl.title('OT matrix ') - - pl.subplot(2,3,2) - pl.imshow(Gs,interpolation='nearest') - pl.title('OT matrix Sinkhorn') - - pl.subplot(2,3,3) - pl.imshow(Gg,interpolation='nearest') - pl.title('OT matrix Group lasso') - - pl.subplot(2,3,4) - ot.plot.plot2D_samples_mat(xs,xt,G0,c=[.5,.5,1]) - pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - - - pl.subplot(2,3,5) - ot.plot.plot2D_samples_mat(xs,xt,Gs,c=[.5,.5,1]) - pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - - pl.subplot(2,3,6) - ot.plot.plot2D_samples_mat(xs,xt,Gg,c=[.5,.5,1]) - pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - - #%% sample interpolation - - xst0=n*G0.dot(xt) - xsts=n*Gs.dot(xt) - xstg=n*Gg.dot(xt) - - pl.figure(4) - pl.subplot(2,3,1) - - - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5) - pl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Transp samples',s=30) - pl.title('Interp samples') - pl.legend(loc=0) - - pl.subplot(2,3,2) - - - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5) - pl.scatter(xsts[:,0],xsts[:,1],c=ys,marker='+',label='Transp samples',s=30) - pl.title('Interp samples Sinkhorn') - - pl.subplot(2,3,3) - - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5) - pl.scatter(xstg[:,0],xstg[:,1],c=ys,marker='+',label='Transp samples',s=30) - pl.title('Interp samples Grouplasso') -**Total running time of the script:** ( 0 minutes 17.372 seconds) - - - -.. container:: sphx-glr-footer - - - .. container:: sphx-glr-download - - :download:`Download Python source code: plot_OTDA_2D.py ` - - - - .. container:: sphx-glr-download - - :download:`Download Jupyter notebook: plot_OTDA_2D.ipynb ` - -.. rst-class:: sphx-glr-signature - - `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_OTDA_classes.ipynb b/docs/source/auto_examples/plot_OTDA_classes.ipynb deleted file mode 100644 index d9fcb87..0000000 --- a/docs/source/auto_examples/plot_OTDA_classes.ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "nbformat_minor": 0, - "nbformat": 4, - "cells": [ - { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - }, - { - "source": [ - "\n# OT for domain adaptation\n\n\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, - { - "execution_count": null, - "cell_type": "code", - "source": [ - "import matplotlib.pylab as pl\nimport ot\n\n\n\n\n#%% parameters\n\nn=150 # nb samples in source and target datasets\n\nxs,ys=ot.datasets.get_data_classif('3gauss',n)\nxt,yt=ot.datasets.get_data_classif('3gauss2',n)\n\n\n\n\n#%% plot samples\n\npl.figure(1)\n\npl.subplot(2,2,1)\npl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples')\npl.legend(loc=0)\npl.title('Source distributions')\n\npl.subplot(2,2,2)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples')\npl.legend(loc=0)\npl.title('target distributions')\n\n\n#%% OT estimation\n\n# LP problem\nda_emd=ot.da.OTDA() # init class\nda_emd.fit(xs,xt) # fit distributions\nxst0=da_emd.interp() # interpolation of source samples\n\n\n# sinkhorn regularization\nlambd=1e-1\nda_entrop=ot.da.OTDA_sinkhorn()\nda_entrop.fit(xs,xt,reg=lambd)\nxsts=da_entrop.interp()\n\n# non-convex Group lasso regularization\nreg=1e-1\neta=1e0\nda_lpl1=ot.da.OTDA_lpl1()\nda_lpl1.fit(xs,ys,xt,reg=reg,eta=eta)\nxstg=da_lpl1.interp()\n\n\n# True Group lasso regularization\nreg=1e-1\neta=2e0\nda_l1l2=ot.da.OTDA_l1l2()\nda_l1l2.fit(xs,ys,xt,reg=reg,eta=eta,numItermax=20,verbose=True)\nxstgl=da_l1l2.interp()\n\n\n#%% plot interpolated source samples\npl.figure(4,(15,8))\n\nparam_img={'interpolation':'nearest','cmap':'jet'}\n\npl.subplot(2,4,1)\npl.imshow(da_emd.G,**param_img)\npl.title('OT matrix')\n\n\npl.subplot(2,4,2)\npl.imshow(da_entrop.G,**param_img)\npl.title('OT matrix sinkhorn')\n\npl.subplot(2,4,3)\npl.imshow(da_lpl1.G,**param_img)\npl.title('OT matrix non-convex Group Lasso')\n\npl.subplot(2,4,4)\npl.imshow(da_l1l2.G,**param_img)\npl.title('OT matrix Group Lasso')\n\n\npl.subplot(2,4,5)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3)\npl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Transp samples',s=30)\npl.title('Interp samples')\npl.legend(loc=0)\n\npl.subplot(2,4,6)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3)\npl.scatter(xsts[:,0],xsts[:,1],c=ys,marker='+',label='Transp samples',s=30)\npl.title('Interp samples Sinkhorn')\n\npl.subplot(2,4,7)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3)\npl.scatter(xstg[:,0],xstg[:,1],c=ys,marker='+',label='Transp samples',s=30)\npl.title('Interp samples non-convex Group Lasso')\n\npl.subplot(2,4,8)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3)\npl.scatter(xstgl[:,0],xstgl[:,1],c=ys,marker='+',label='Transp samples',s=30)\npl.title('Interp samples Group Lasso')" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, - "language_info": { - "mimetype": "text/x-python", - "nbconvert_exporter": "python", - "name": "python", - "file_extension": ".py", - "version": "2.7.12", - "pygments_lexer": "ipython2", - "codemirror_mode": { - "version": 2, - "name": "ipython" - } - } - } -} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_OTDA_classes.py b/docs/source/auto_examples/plot_OTDA_classes.py deleted file mode 100644 index 089b45b..0000000 --- a/docs/source/auto_examples/plot_OTDA_classes.py +++ /dev/null @@ -1,112 +0,0 @@ -# -*- coding: utf-8 -*- -""" -======================== -OT for domain adaptation -======================== - -""" - -import matplotlib.pylab as pl -import ot - - - - -#%% parameters - -n=150 # nb samples in source and target datasets - -xs,ys=ot.datasets.get_data_classif('3gauss',n) -xt,yt=ot.datasets.get_data_classif('3gauss2',n) - - - - -#%% plot samples - -pl.figure(1) - -pl.subplot(2,2,1) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') -pl.legend(loc=0) -pl.title('Source distributions') - -pl.subplot(2,2,2) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') -pl.legend(loc=0) -pl.title('target distributions') - - -#%% OT estimation - -# LP problem -da_emd=ot.da.OTDA() # init class -da_emd.fit(xs,xt) # fit distributions -xst0=da_emd.interp() # interpolation of source samples - - -# sinkhorn regularization -lambd=1e-1 -da_entrop=ot.da.OTDA_sinkhorn() -da_entrop.fit(xs,xt,reg=lambd) -xsts=da_entrop.interp() - -# non-convex Group lasso regularization -reg=1e-1 -eta=1e0 -da_lpl1=ot.da.OTDA_lpl1() -da_lpl1.fit(xs,ys,xt,reg=reg,eta=eta) -xstg=da_lpl1.interp() - - -# True Group lasso regularization -reg=1e-1 -eta=2e0 -da_l1l2=ot.da.OTDA_l1l2() -da_l1l2.fit(xs,ys,xt,reg=reg,eta=eta,numItermax=20,verbose=True) -xstgl=da_l1l2.interp() - - -#%% plot interpolated source samples -pl.figure(4,(15,8)) - -param_img={'interpolation':'nearest','cmap':'jet'} - -pl.subplot(2,4,1) -pl.imshow(da_emd.G,**param_img) -pl.title('OT matrix') - - -pl.subplot(2,4,2) -pl.imshow(da_entrop.G,**param_img) -pl.title('OT matrix sinkhorn') - -pl.subplot(2,4,3) -pl.imshow(da_lpl1.G,**param_img) -pl.title('OT matrix non-convex Group Lasso') - -pl.subplot(2,4,4) -pl.imshow(da_l1l2.G,**param_img) -pl.title('OT matrix Group Lasso') - - -pl.subplot(2,4,5) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3) -pl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Transp samples',s=30) -pl.title('Interp samples') -pl.legend(loc=0) - -pl.subplot(2,4,6) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3) -pl.scatter(xsts[:,0],xsts[:,1],c=ys,marker='+',label='Transp samples',s=30) -pl.title('Interp samples Sinkhorn') - -pl.subplot(2,4,7) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3) -pl.scatter(xstg[:,0],xstg[:,1],c=ys,marker='+',label='Transp samples',s=30) -pl.title('Interp samples non-convex Group Lasso') - -pl.subplot(2,4,8) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3) -pl.scatter(xstgl[:,0],xstgl[:,1],c=ys,marker='+',label='Transp samples',s=30) -pl.title('Interp samples Group Lasso') \ No newline at end of file diff --git a/docs/source/auto_examples/plot_OTDA_classes.rst b/docs/source/auto_examples/plot_OTDA_classes.rst deleted file mode 100644 index 097e9fc..0000000 --- a/docs/source/auto_examples/plot_OTDA_classes.rst +++ /dev/null @@ -1,190 +0,0 @@ - - -.. _sphx_glr_auto_examples_plot_OTDA_classes.py: - - -======================== -OT for domain adaptation -======================== - - - - - -.. rst-class:: sphx-glr-horizontal - - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_classes_001.png - :scale: 47 - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_classes_004.png - :scale: 47 - - -.. rst-class:: sphx-glr-script-out - - Out:: - - It. |Loss |Delta loss - -------------------------------- - 0|9.171271e+00|0.000000e+00 - 1|2.133783e+00|-3.298127e+00 - 2|1.895941e+00|-1.254484e-01 - 3|1.844628e+00|-2.781709e-02 - 4|1.824983e+00|-1.076467e-02 - 5|1.815453e+00|-5.249337e-03 - 6|1.808104e+00|-4.064733e-03 - 7|1.803558e+00|-2.520475e-03 - 8|1.801061e+00|-1.386155e-03 - 9|1.799391e+00|-9.279565e-04 - 10|1.797176e+00|-1.232778e-03 - 11|1.795465e+00|-9.529479e-04 - 12|1.795316e+00|-8.322362e-05 - 13|1.794523e+00|-4.418932e-04 - 14|1.794444e+00|-4.390599e-05 - 15|1.794395e+00|-2.710318e-05 - 16|1.793713e+00|-3.804028e-04 - 17|1.793110e+00|-3.359479e-04 - 18|1.792829e+00|-1.569563e-04 - 19|1.792621e+00|-1.159469e-04 - It. |Loss |Delta loss - -------------------------------- - 20|1.791334e+00|-7.187689e-04 - - - - -| - - -.. code-block:: python - - - import matplotlib.pylab as pl - import ot - - - - - #%% parameters - - n=150 # nb samples in source and target datasets - - xs,ys=ot.datasets.get_data_classif('3gauss',n) - xt,yt=ot.datasets.get_data_classif('3gauss2',n) - - - - - #%% plot samples - - pl.figure(1) - - pl.subplot(2,2,1) - pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') - pl.legend(loc=0) - pl.title('Source distributions') - - pl.subplot(2,2,2) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - pl.legend(loc=0) - pl.title('target distributions') - - - #%% OT estimation - - # LP problem - da_emd=ot.da.OTDA() # init class - da_emd.fit(xs,xt) # fit distributions - xst0=da_emd.interp() # interpolation of source samples - - - # sinkhorn regularization - lambd=1e-1 - da_entrop=ot.da.OTDA_sinkhorn() - da_entrop.fit(xs,xt,reg=lambd) - xsts=da_entrop.interp() - - # non-convex Group lasso regularization - reg=1e-1 - eta=1e0 - da_lpl1=ot.da.OTDA_lpl1() - da_lpl1.fit(xs,ys,xt,reg=reg,eta=eta) - xstg=da_lpl1.interp() - - - # True Group lasso regularization - reg=1e-1 - eta=2e0 - da_l1l2=ot.da.OTDA_l1l2() - da_l1l2.fit(xs,ys,xt,reg=reg,eta=eta,numItermax=20,verbose=True) - xstgl=da_l1l2.interp() - - - #%% plot interpolated source samples - pl.figure(4,(15,8)) - - param_img={'interpolation':'nearest','cmap':'jet'} - - pl.subplot(2,4,1) - pl.imshow(da_emd.G,**param_img) - pl.title('OT matrix') - - - pl.subplot(2,4,2) - pl.imshow(da_entrop.G,**param_img) - pl.title('OT matrix sinkhorn') - - pl.subplot(2,4,3) - pl.imshow(da_lpl1.G,**param_img) - pl.title('OT matrix non-convex Group Lasso') - - pl.subplot(2,4,4) - pl.imshow(da_l1l2.G,**param_img) - pl.title('OT matrix Group Lasso') - - - pl.subplot(2,4,5) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3) - pl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Transp samples',s=30) - pl.title('Interp samples') - pl.legend(loc=0) - - pl.subplot(2,4,6) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3) - pl.scatter(xsts[:,0],xsts[:,1],c=ys,marker='+',label='Transp samples',s=30) - pl.title('Interp samples Sinkhorn') - - pl.subplot(2,4,7) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3) - pl.scatter(xstg[:,0],xstg[:,1],c=ys,marker='+',label='Transp samples',s=30) - pl.title('Interp samples non-convex Group Lasso') - - pl.subplot(2,4,8) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.3) - pl.scatter(xstgl[:,0],xstgl[:,1],c=ys,marker='+',label='Transp samples',s=30) - pl.title('Interp samples Group Lasso') -**Total running time of the script:** ( 0 minutes 2.225 seconds) - - - -.. container:: sphx-glr-footer - - - .. container:: sphx-glr-download - - :download:`Download Python source code: plot_OTDA_classes.py ` - - - - .. container:: sphx-glr-download - - :download:`Download Jupyter notebook: plot_OTDA_classes.ipynb ` - -.. rst-class:: sphx-glr-signature - - `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_OTDA_color_images.ipynb b/docs/source/auto_examples/plot_OTDA_color_images.ipynb deleted file mode 100644 index d174828..0000000 --- a/docs/source/auto_examples/plot_OTDA_color_images.ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "nbformat_minor": 0, - "nbformat": 4, - "cells": [ - { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - }, - { - "source": [ - "\n========================================================\nOT for domain adaptation with image color adaptation [6]\n========================================================\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882.\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, - { - "execution_count": null, - "cell_type": "code", - "source": [ - "import numpy as np\nimport scipy.ndimage as spi\nimport matplotlib.pylab as pl\nimport ot\n\n\n#%% Loading images\n\nI1=spi.imread('../data/ocean_day.jpg').astype(np.float64)/256\nI2=spi.imread('../data/ocean_sunset.jpg').astype(np.float64)/256\n\n#%% Plot images\n\npl.figure(1)\n\npl.subplot(1,2,1)\npl.imshow(I1)\npl.title('Image 1')\n\npl.subplot(1,2,2)\npl.imshow(I2)\npl.title('Image 2')\n\npl.show()\n\n#%% Image conversion and dataset generation\n\ndef im2mat(I):\n \"\"\"Converts and image to matrix (one pixel per line)\"\"\"\n return I.reshape((I.shape[0]*I.shape[1],I.shape[2]))\n\ndef mat2im(X,shape):\n \"\"\"Converts back a matrix to an image\"\"\"\n return X.reshape(shape)\n\nX1=im2mat(I1)\nX2=im2mat(I2)\n\n# training samples\nnb=1000\nidx1=np.random.randint(X1.shape[0],size=(nb,))\nidx2=np.random.randint(X2.shape[0],size=(nb,))\n\nxs=X1[idx1,:]\nxt=X2[idx2,:]\n\n#%% Plot image distributions\n\n\npl.figure(2,(10,5))\n\npl.subplot(1,2,1)\npl.scatter(xs[:,0],xs[:,2],c=xs)\npl.axis([0,1,0,1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1,2,2)\n#pl.imshow(I2)\npl.scatter(xt[:,0],xt[:,2],c=xt)\npl.axis([0,1,0,1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\n\npl.show()\n\n\n\n#%% domain adaptation between images\n\n# LP problem\nda_emd=ot.da.OTDA() # init class\nda_emd.fit(xs,xt) # fit distributions\n\n\n# sinkhorn regularization\nlambd=1e-1\nda_entrop=ot.da.OTDA_sinkhorn()\nda_entrop.fit(xs,xt,reg=lambd)\n\n\n\n#%% prediction between images (using out of sample prediction as in [6])\n\nX1t=da_emd.predict(X1)\nX2t=da_emd.predict(X2,-1)\n\n\nX1te=da_entrop.predict(X1)\nX2te=da_entrop.predict(X2,-1)\n\n\ndef minmax(I):\n return np.minimum(np.maximum(I,0),1)\n\nI1t=minmax(mat2im(X1t,I1.shape))\nI2t=minmax(mat2im(X2t,I2.shape))\n\nI1te=minmax(mat2im(X1te,I1.shape))\nI2te=minmax(mat2im(X2te,I2.shape))\n\n#%% plot all images\n\npl.figure(2,(10,8))\n\npl.subplot(2,3,1)\n\npl.imshow(I1)\npl.title('Image 1')\n\npl.subplot(2,3,2)\npl.imshow(I1t)\npl.title('Image 1 Adapt')\n\n\npl.subplot(2,3,3)\npl.imshow(I1te)\npl.title('Image 1 Adapt (reg)')\n\npl.subplot(2,3,4)\n\npl.imshow(I2)\npl.title('Image 2')\n\npl.subplot(2,3,5)\npl.imshow(I2t)\npl.title('Image 2 Adapt')\n\n\npl.subplot(2,3,6)\npl.imshow(I2te)\npl.title('Image 2 Adapt (reg)')\n\npl.show()" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, - "language_info": { - "mimetype": "text/x-python", - "nbconvert_exporter": "python", - "name": "python", - "file_extension": ".py", - "version": "2.7.12", - "pygments_lexer": "ipython2", - "codemirror_mode": { - "version": 2, - "name": "ipython" - } - } - } -} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_OTDA_color_images.py b/docs/source/auto_examples/plot_OTDA_color_images.py deleted file mode 100644 index 68eee44..0000000 --- a/docs/source/auto_examples/plot_OTDA_color_images.py +++ /dev/null @@ -1,145 +0,0 @@ -# -*- coding: utf-8 -*- -""" -======================================================== -OT for domain adaptation with image color adaptation [6] -======================================================== - -[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. -""" - -import numpy as np -import scipy.ndimage as spi -import matplotlib.pylab as pl -import ot - - -#%% Loading images - -I1=spi.imread('../data/ocean_day.jpg').astype(np.float64)/256 -I2=spi.imread('../data/ocean_sunset.jpg').astype(np.float64)/256 - -#%% Plot images - -pl.figure(1) - -pl.subplot(1,2,1) -pl.imshow(I1) -pl.title('Image 1') - -pl.subplot(1,2,2) -pl.imshow(I2) -pl.title('Image 2') - -pl.show() - -#%% Image conversion and dataset generation - -def im2mat(I): - """Converts and image to matrix (one pixel per line)""" - return I.reshape((I.shape[0]*I.shape[1],I.shape[2])) - -def mat2im(X,shape): - """Converts back a matrix to an image""" - return X.reshape(shape) - -X1=im2mat(I1) -X2=im2mat(I2) - -# training samples -nb=1000 -idx1=np.random.randint(X1.shape[0],size=(nb,)) -idx2=np.random.randint(X2.shape[0],size=(nb,)) - -xs=X1[idx1,:] -xt=X2[idx2,:] - -#%% Plot image distributions - - -pl.figure(2,(10,5)) - -pl.subplot(1,2,1) -pl.scatter(xs[:,0],xs[:,2],c=xs) -pl.axis([0,1,0,1]) -pl.xlabel('Red') -pl.ylabel('Blue') -pl.title('Image 1') - -pl.subplot(1,2,2) -#pl.imshow(I2) -pl.scatter(xt[:,0],xt[:,2],c=xt) -pl.axis([0,1,0,1]) -pl.xlabel('Red') -pl.ylabel('Blue') -pl.title('Image 2') - -pl.show() - - - -#%% domain adaptation between images - -# LP problem -da_emd=ot.da.OTDA() # init class -da_emd.fit(xs,xt) # fit distributions - - -# sinkhorn regularization -lambd=1e-1 -da_entrop=ot.da.OTDA_sinkhorn() -da_entrop.fit(xs,xt,reg=lambd) - - - -#%% prediction between images (using out of sample prediction as in [6]) - -X1t=da_emd.predict(X1) -X2t=da_emd.predict(X2,-1) - - -X1te=da_entrop.predict(X1) -X2te=da_entrop.predict(X2,-1) - - -def minmax(I): - return np.minimum(np.maximum(I,0),1) - -I1t=minmax(mat2im(X1t,I1.shape)) -I2t=minmax(mat2im(X2t,I2.shape)) - -I1te=minmax(mat2im(X1te,I1.shape)) -I2te=minmax(mat2im(X2te,I2.shape)) - -#%% plot all images - -pl.figure(2,(10,8)) - -pl.subplot(2,3,1) - -pl.imshow(I1) -pl.title('Image 1') - -pl.subplot(2,3,2) -pl.imshow(I1t) -pl.title('Image 1 Adapt') - - -pl.subplot(2,3,3) -pl.imshow(I1te) -pl.title('Image 1 Adapt (reg)') - -pl.subplot(2,3,4) - -pl.imshow(I2) -pl.title('Image 2') - -pl.subplot(2,3,5) -pl.imshow(I2t) -pl.title('Image 2 Adapt') - - -pl.subplot(2,3,6) -pl.imshow(I2te) -pl.title('Image 2 Adapt (reg)') - -pl.show() diff --git a/docs/source/auto_examples/plot_OTDA_color_images.rst b/docs/source/auto_examples/plot_OTDA_color_images.rst deleted file mode 100644 index a982a90..0000000 --- a/docs/source/auto_examples/plot_OTDA_color_images.rst +++ /dev/null @@ -1,191 +0,0 @@ - - -.. _sphx_glr_auto_examples_plot_OTDA_color_images.py: - - -======================================================== -OT for domain adaptation with image color adaptation [6] -======================================================== - -[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. - - - - -.. rst-class:: sphx-glr-horizontal - - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_color_images_001.png - :scale: 47 - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_color_images_002.png - :scale: 47 - - - - - -.. code-block:: python - - - import numpy as np - import scipy.ndimage as spi - import matplotlib.pylab as pl - import ot - - - #%% Loading images - - I1=spi.imread('../data/ocean_day.jpg').astype(np.float64)/256 - I2=spi.imread('../data/ocean_sunset.jpg').astype(np.float64)/256 - - #%% Plot images - - pl.figure(1) - - pl.subplot(1,2,1) - pl.imshow(I1) - pl.title('Image 1') - - pl.subplot(1,2,2) - pl.imshow(I2) - pl.title('Image 2') - - pl.show() - - #%% Image conversion and dataset generation - - def im2mat(I): - """Converts and image to matrix (one pixel per line)""" - return I.reshape((I.shape[0]*I.shape[1],I.shape[2])) - - def mat2im(X,shape): - """Converts back a matrix to an image""" - return X.reshape(shape) - - X1=im2mat(I1) - X2=im2mat(I2) - - # training samples - nb=1000 - idx1=np.random.randint(X1.shape[0],size=(nb,)) - idx2=np.random.randint(X2.shape[0],size=(nb,)) - - xs=X1[idx1,:] - xt=X2[idx2,:] - - #%% Plot image distributions - - - pl.figure(2,(10,5)) - - pl.subplot(1,2,1) - pl.scatter(xs[:,0],xs[:,2],c=xs) - pl.axis([0,1,0,1]) - pl.xlabel('Red') - pl.ylabel('Blue') - pl.title('Image 1') - - pl.subplot(1,2,2) - #pl.imshow(I2) - pl.scatter(xt[:,0],xt[:,2],c=xt) - pl.axis([0,1,0,1]) - pl.xlabel('Red') - pl.ylabel('Blue') - pl.title('Image 2') - - pl.show() - - - - #%% domain adaptation between images - - # LP problem - da_emd=ot.da.OTDA() # init class - da_emd.fit(xs,xt) # fit distributions - - - # sinkhorn regularization - lambd=1e-1 - da_entrop=ot.da.OTDA_sinkhorn() - da_entrop.fit(xs,xt,reg=lambd) - - - - #%% prediction between images (using out of sample prediction as in [6]) - - X1t=da_emd.predict(X1) - X2t=da_emd.predict(X2,-1) - - - X1te=da_entrop.predict(X1) - X2te=da_entrop.predict(X2,-1) - - - def minmax(I): - return np.minimum(np.maximum(I,0),1) - - I1t=minmax(mat2im(X1t,I1.shape)) - I2t=minmax(mat2im(X2t,I2.shape)) - - I1te=minmax(mat2im(X1te,I1.shape)) - I2te=minmax(mat2im(X2te,I2.shape)) - - #%% plot all images - - pl.figure(2,(10,8)) - - pl.subplot(2,3,1) - - pl.imshow(I1) - pl.title('Image 1') - - pl.subplot(2,3,2) - pl.imshow(I1t) - pl.title('Image 1 Adapt') - - - pl.subplot(2,3,3) - pl.imshow(I1te) - pl.title('Image 1 Adapt (reg)') - - pl.subplot(2,3,4) - - pl.imshow(I2) - pl.title('Image 2') - - pl.subplot(2,3,5) - pl.imshow(I2t) - pl.title('Image 2 Adapt') - - - pl.subplot(2,3,6) - pl.imshow(I2te) - pl.title('Image 2 Adapt (reg)') - - pl.show() - -**Total running time of the script:** ( 0 minutes 24.815 seconds) - - - -.. container:: sphx-glr-footer - - - .. container:: sphx-glr-download - - :download:`Download Python source code: plot_OTDA_color_images.py ` - - - - .. container:: sphx-glr-download - - :download:`Download Jupyter notebook: plot_OTDA_color_images.ipynb ` - -.. rst-class:: sphx-glr-signature - - `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_OTDA_mapping.ipynb b/docs/source/auto_examples/plot_OTDA_mapping.ipynb deleted file mode 100644 index ec405af..0000000 --- a/docs/source/auto_examples/plot_OTDA_mapping.ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "nbformat_minor": 0, - "nbformat": 4, - "cells": [ - { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - }, - { - "source": [ - "\n===============================================\nOT mapping estimation for domain adaptation [8]\n===============================================\n\n[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, \"Mapping estimation for\n discrete optimal transport\", Neural Information Processing Systems (NIPS), 2016.\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, - { - "execution_count": null, - "cell_type": "code", - "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n\n\n#%% dataset generation\n\nnp.random.seed(0) # makes example reproducible\n\nn=100 # nb samples in source and target datasets\ntheta=2*np.pi/20\nnz=0.1\nxs,ys=ot.datasets.get_data_classif('gaussrot',n,nz=nz)\nxt,yt=ot.datasets.get_data_classif('gaussrot',n,theta=theta,nz=nz)\n\n# one of the target mode changes its variance (no linear mapping)\nxt[yt==2]*=3\nxt=xt+4\n\n\n#%% plot samples\n\npl.figure(1,(8,5))\npl.clf()\n\npl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples')\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples')\n\npl.legend(loc=0)\npl.title('Source and target distributions')\n\n\n\n#%% OT linear mapping estimation\n\neta=1e-8 # quadratic regularization for regression\nmu=1e0 # weight of the OT linear term\nbias=True # estimate a bias\n\not_mapping=ot.da.OTDA_mapping_linear()\not_mapping.fit(xs,xt,mu=mu,eta=eta,bias=bias,numItermax = 20,verbose=True)\n\nxst=ot_mapping.predict(xs) # use the estimated mapping\nxst0=ot_mapping.interp() # use barycentric mapping\n\n\npl.figure(2,(10,7))\npl.clf()\npl.subplot(2,2,1)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.3)\npl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='barycentric mapping')\npl.title(\"barycentric mapping\")\n\npl.subplot(2,2,2)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.3)\npl.scatter(xst[:,0],xst[:,1],c=ys,marker='+',label='Learned mapping')\npl.title(\"Learned mapping\")\n\n\n\n#%% Kernel mapping estimation\n\neta=1e-5 # quadratic regularization for regression\nmu=1e-1 # weight of the OT linear term\nbias=True # estimate a bias\nsigma=1 # sigma bandwidth fot gaussian kernel\n\n\not_mapping_kernel=ot.da.OTDA_mapping_kernel()\not_mapping_kernel.fit(xs,xt,mu=mu,eta=eta,sigma=sigma,bias=bias,numItermax = 10,verbose=True)\n\nxst_kernel=ot_mapping_kernel.predict(xs) # use the estimated mapping\nxst0_kernel=ot_mapping_kernel.interp() # use barycentric mapping\n\n\n#%% Plotting the mapped samples\n\npl.figure(2,(10,7))\npl.clf()\npl.subplot(2,2,1)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2)\npl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Mapped source samples')\npl.title(\"Bary. mapping (linear)\")\npl.legend(loc=0)\n\npl.subplot(2,2,2)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2)\npl.scatter(xst[:,0],xst[:,1],c=ys,marker='+',label='Learned mapping')\npl.title(\"Estim. mapping (linear)\")\n\npl.subplot(2,2,3)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2)\npl.scatter(xst0_kernel[:,0],xst0_kernel[:,1],c=ys,marker='+',label='barycentric mapping')\npl.title(\"Bary. mapping (kernel)\")\n\npl.subplot(2,2,4)\npl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2)\npl.scatter(xst_kernel[:,0],xst_kernel[:,1],c=ys,marker='+',label='Learned mapping')\npl.title(\"Estim. mapping (kernel)\")" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, - "language_info": { - "mimetype": "text/x-python", - "nbconvert_exporter": "python", - "name": "python", - "file_extension": ".py", - "version": "2.7.12", - "pygments_lexer": "ipython2", - "codemirror_mode": { - "version": 2, - "name": "ipython" - } - } - } -} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_OTDA_mapping.py b/docs/source/auto_examples/plot_OTDA_mapping.py deleted file mode 100644 index 78b57e7..0000000 --- a/docs/source/auto_examples/plot_OTDA_mapping.py +++ /dev/null @@ -1,110 +0,0 @@ -# -*- coding: utf-8 -*- -""" -=============================================== -OT mapping estimation for domain adaptation [8] -=============================================== - -[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for - discrete optimal transport", Neural Information Processing Systems (NIPS), 2016. -""" - -import numpy as np -import matplotlib.pylab as pl -import ot - - - -#%% dataset generation - -np.random.seed(0) # makes example reproducible - -n=100 # nb samples in source and target datasets -theta=2*np.pi/20 -nz=0.1 -xs,ys=ot.datasets.get_data_classif('gaussrot',n,nz=nz) -xt,yt=ot.datasets.get_data_classif('gaussrot',n,theta=theta,nz=nz) - -# one of the target mode changes its variance (no linear mapping) -xt[yt==2]*=3 -xt=xt+4 - - -#%% plot samples - -pl.figure(1,(8,5)) -pl.clf() - -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - -pl.legend(loc=0) -pl.title('Source and target distributions') - - - -#%% OT linear mapping estimation - -eta=1e-8 # quadratic regularization for regression -mu=1e0 # weight of the OT linear term -bias=True # estimate a bias - -ot_mapping=ot.da.OTDA_mapping_linear() -ot_mapping.fit(xs,xt,mu=mu,eta=eta,bias=bias,numItermax = 20,verbose=True) - -xst=ot_mapping.predict(xs) # use the estimated mapping -xst0=ot_mapping.interp() # use barycentric mapping - - -pl.figure(2,(10,7)) -pl.clf() -pl.subplot(2,2,1) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.3) -pl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='barycentric mapping') -pl.title("barycentric mapping") - -pl.subplot(2,2,2) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.3) -pl.scatter(xst[:,0],xst[:,1],c=ys,marker='+',label='Learned mapping') -pl.title("Learned mapping") - - - -#%% Kernel mapping estimation - -eta=1e-5 # quadratic regularization for regression -mu=1e-1 # weight of the OT linear term -bias=True # estimate a bias -sigma=1 # sigma bandwidth fot gaussian kernel - - -ot_mapping_kernel=ot.da.OTDA_mapping_kernel() -ot_mapping_kernel.fit(xs,xt,mu=mu,eta=eta,sigma=sigma,bias=bias,numItermax = 10,verbose=True) - -xst_kernel=ot_mapping_kernel.predict(xs) # use the estimated mapping -xst0_kernel=ot_mapping_kernel.interp() # use barycentric mapping - - -#%% Plotting the mapped samples - -pl.figure(2,(10,7)) -pl.clf() -pl.subplot(2,2,1) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2) -pl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Mapped source samples') -pl.title("Bary. mapping (linear)") -pl.legend(loc=0) - -pl.subplot(2,2,2) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2) -pl.scatter(xst[:,0],xst[:,1],c=ys,marker='+',label='Learned mapping') -pl.title("Estim. mapping (linear)") - -pl.subplot(2,2,3) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2) -pl.scatter(xst0_kernel[:,0],xst0_kernel[:,1],c=ys,marker='+',label='barycentric mapping') -pl.title("Bary. mapping (kernel)") - -pl.subplot(2,2,4) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2) -pl.scatter(xst_kernel[:,0],xst_kernel[:,1],c=ys,marker='+',label='Learned mapping') -pl.title("Estim. mapping (kernel)") diff --git a/docs/source/auto_examples/plot_OTDA_mapping.rst b/docs/source/auto_examples/plot_OTDA_mapping.rst deleted file mode 100644 index 18da90d..0000000 --- a/docs/source/auto_examples/plot_OTDA_mapping.rst +++ /dev/null @@ -1,186 +0,0 @@ - - -.. _sphx_glr_auto_examples_plot_OTDA_mapping.py: - - -=============================================== -OT mapping estimation for domain adaptation [8] -=============================================== - -[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for - discrete optimal transport", Neural Information Processing Systems (NIPS), 2016. - - - - -.. rst-class:: sphx-glr-horizontal - - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_mapping_001.png - :scale: 47 - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_mapping_002.png - :scale: 47 - - -.. rst-class:: sphx-glr-script-out - - Out:: - - It. |Loss |Delta loss - -------------------------------- - 0|4.009366e+03|0.000000e+00 - 1|3.999933e+03|-2.352753e-03 - 2|3.999520e+03|-1.031984e-04 - 3|3.999362e+03|-3.936391e-05 - 4|3.999281e+03|-2.032868e-05 - 5|3.999238e+03|-1.083083e-05 - 6|3.999229e+03|-2.125291e-06 - It. |Loss |Delta loss - -------------------------------- - 0|4.026841e+02|0.000000e+00 - 1|3.990791e+02|-8.952439e-03 - 2|3.987954e+02|-7.107124e-04 - 3|3.986554e+02|-3.512453e-04 - 4|3.985721e+02|-2.087997e-04 - 5|3.985141e+02|-1.456184e-04 - 6|3.984729e+02|-1.034624e-04 - 7|3.984435e+02|-7.366943e-05 - 8|3.984199e+02|-5.922497e-05 - 9|3.984016e+02|-4.593063e-05 - 10|3.983867e+02|-3.733061e-05 - - - - -| - - -.. code-block:: python - - - import numpy as np - import matplotlib.pylab as pl - import ot - - - - #%% dataset generation - - np.random.seed(0) # makes example reproducible - - n=100 # nb samples in source and target datasets - theta=2*np.pi/20 - nz=0.1 - xs,ys=ot.datasets.get_data_classif('gaussrot',n,nz=nz) - xt,yt=ot.datasets.get_data_classif('gaussrot',n,theta=theta,nz=nz) - - # one of the target mode changes its variance (no linear mapping) - xt[yt==2]*=3 - xt=xt+4 - - - #%% plot samples - - pl.figure(1,(8,5)) - pl.clf() - - pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') - - pl.legend(loc=0) - pl.title('Source and target distributions') - - - - #%% OT linear mapping estimation - - eta=1e-8 # quadratic regularization for regression - mu=1e0 # weight of the OT linear term - bias=True # estimate a bias - - ot_mapping=ot.da.OTDA_mapping_linear() - ot_mapping.fit(xs,xt,mu=mu,eta=eta,bias=bias,numItermax = 20,verbose=True) - - xst=ot_mapping.predict(xs) # use the estimated mapping - xst0=ot_mapping.interp() # use barycentric mapping - - - pl.figure(2,(10,7)) - pl.clf() - pl.subplot(2,2,1) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.3) - pl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='barycentric mapping') - pl.title("barycentric mapping") - - pl.subplot(2,2,2) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.3) - pl.scatter(xst[:,0],xst[:,1],c=ys,marker='+',label='Learned mapping') - pl.title("Learned mapping") - - - - #%% Kernel mapping estimation - - eta=1e-5 # quadratic regularization for regression - mu=1e-1 # weight of the OT linear term - bias=True # estimate a bias - sigma=1 # sigma bandwidth fot gaussian kernel - - - ot_mapping_kernel=ot.da.OTDA_mapping_kernel() - ot_mapping_kernel.fit(xs,xt,mu=mu,eta=eta,sigma=sigma,bias=bias,numItermax = 10,verbose=True) - - xst_kernel=ot_mapping_kernel.predict(xs) # use the estimated mapping - xst0_kernel=ot_mapping_kernel.interp() # use barycentric mapping - - - #%% Plotting the mapped samples - - pl.figure(2,(10,7)) - pl.clf() - pl.subplot(2,2,1) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2) - pl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Mapped source samples') - pl.title("Bary. mapping (linear)") - pl.legend(loc=0) - - pl.subplot(2,2,2) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2) - pl.scatter(xst[:,0],xst[:,1],c=ys,marker='+',label='Learned mapping') - pl.title("Estim. mapping (linear)") - - pl.subplot(2,2,3) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2) - pl.scatter(xst0_kernel[:,0],xst0_kernel[:,1],c=ys,marker='+',label='barycentric mapping') - pl.title("Bary. mapping (kernel)") - - pl.subplot(2,2,4) - pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=.2) - pl.scatter(xst_kernel[:,0],xst_kernel[:,1],c=ys,marker='+',label='Learned mapping') - pl.title("Estim. mapping (kernel)") - -**Total running time of the script:** ( 0 minutes 0.882 seconds) - - - -.. container:: sphx-glr-footer - - - .. container:: sphx-glr-download - - :download:`Download Python source code: plot_OTDA_mapping.py ` - - - - .. container:: sphx-glr-download - - :download:`Download Jupyter notebook: plot_OTDA_mapping.ipynb ` - -.. rst-class:: sphx-glr-signature - - `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_OTDA_mapping_color_images.ipynb b/docs/source/auto_examples/plot_OTDA_mapping_color_images.ipynb deleted file mode 100644 index 1136cc3..0000000 --- a/docs/source/auto_examples/plot_OTDA_mapping_color_images.ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "nbformat_minor": 0, - "nbformat": 4, - "cells": [ - { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - }, - { - "source": [ - "\n====================================================================================\nOT for domain adaptation with image color adaptation [6] with mapping estimation [8]\n====================================================================================\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized\n discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882.\n[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, \"Mapping estimation for\n discrete optimal transport\", Neural Information Processing Systems (NIPS), 2016.\n\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, - { - "execution_count": null, - "cell_type": "code", - "source": [ - "import numpy as np\nimport scipy.ndimage as spi\nimport matplotlib.pylab as pl\nimport ot\n\n\n#%% Loading images\n\nI1=spi.imread('../data/ocean_day.jpg').astype(np.float64)/256\nI2=spi.imread('../data/ocean_sunset.jpg').astype(np.float64)/256\n\n#%% Plot images\n\npl.figure(1)\n\npl.subplot(1,2,1)\npl.imshow(I1)\npl.title('Image 1')\n\npl.subplot(1,2,2)\npl.imshow(I2)\npl.title('Image 2')\n\npl.show()\n\n#%% Image conversion and dataset generation\n\ndef im2mat(I):\n \"\"\"Converts and image to matrix (one pixel per line)\"\"\"\n return I.reshape((I.shape[0]*I.shape[1],I.shape[2]))\n\ndef mat2im(X,shape):\n \"\"\"Converts back a matrix to an image\"\"\"\n return X.reshape(shape)\n\nX1=im2mat(I1)\nX2=im2mat(I2)\n\n# training samples\nnb=1000\nidx1=np.random.randint(X1.shape[0],size=(nb,))\nidx2=np.random.randint(X2.shape[0],size=(nb,))\n\nxs=X1[idx1,:]\nxt=X2[idx2,:]\n\n#%% Plot image distributions\n\n\npl.figure(2,(10,5))\n\npl.subplot(1,2,1)\npl.scatter(xs[:,0],xs[:,2],c=xs)\npl.axis([0,1,0,1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1,2,2)\n#pl.imshow(I2)\npl.scatter(xt[:,0],xt[:,2],c=xt)\npl.axis([0,1,0,1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\n\npl.show()\n\n\n\n#%% domain adaptation between images\ndef minmax(I):\n return np.minimum(np.maximum(I,0),1)\n# LP problem\nda_emd=ot.da.OTDA() # init class\nda_emd.fit(xs,xt) # fit distributions\n\nX1t=da_emd.predict(X1) # out of sample\nI1t=minmax(mat2im(X1t,I1.shape))\n\n# sinkhorn regularization\nlambd=1e-1\nda_entrop=ot.da.OTDA_sinkhorn()\nda_entrop.fit(xs,xt,reg=lambd)\n\nX1te=da_entrop.predict(X1)\nI1te=minmax(mat2im(X1te,I1.shape))\n\n# linear mapping estimation\neta=1e-8 # quadratic regularization for regression\nmu=1e0 # weight of the OT linear term\nbias=True # estimate a bias\n\not_mapping=ot.da.OTDA_mapping_linear()\not_mapping.fit(xs,xt,mu=mu,eta=eta,bias=bias,numItermax = 20,verbose=True)\n\nX1tl=ot_mapping.predict(X1) # use the estimated mapping\nI1tl=minmax(mat2im(X1tl,I1.shape))\n\n# nonlinear mapping estimation\neta=1e-2 # quadratic regularization for regression\nmu=1e0 # weight of the OT linear term\nbias=False # estimate a bias\nsigma=1 # sigma bandwidth fot gaussian kernel\n\n\not_mapping_kernel=ot.da.OTDA_mapping_kernel()\not_mapping_kernel.fit(xs,xt,mu=mu,eta=eta,sigma=sigma,bias=bias,numItermax = 10,verbose=True)\n\nX1tn=ot_mapping_kernel.predict(X1) # use the estimated mapping\nI1tn=minmax(mat2im(X1tn,I1.shape))\n#%% plot images\n\n\npl.figure(2,(10,8))\n\npl.subplot(2,3,1)\n\npl.imshow(I1)\npl.title('Im. 1')\n\npl.subplot(2,3,2)\n\npl.imshow(I2)\npl.title('Im. 2')\n\n\npl.subplot(2,3,3)\npl.imshow(I1t)\npl.title('Im. 1 Interp LP')\n\npl.subplot(2,3,4)\npl.imshow(I1te)\npl.title('Im. 1 Interp Entrop')\n\n\npl.subplot(2,3,5)\npl.imshow(I1tl)\npl.title('Im. 1 Linear mapping')\n\npl.subplot(2,3,6)\npl.imshow(I1tn)\npl.title('Im. 1 nonlinear mapping')\n\npl.show()" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, - "language_info": { - "mimetype": "text/x-python", - "nbconvert_exporter": "python", - "name": "python", - "file_extension": ".py", - "version": "2.7.12", - "pygments_lexer": "ipython2", - "codemirror_mode": { - "version": 2, - "name": "ipython" - } - } - } -} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_OTDA_mapping_color_images.py b/docs/source/auto_examples/plot_OTDA_mapping_color_images.py deleted file mode 100644 index f07dc6c..0000000 --- a/docs/source/auto_examples/plot_OTDA_mapping_color_images.py +++ /dev/null @@ -1,158 +0,0 @@ -# -*- coding: utf-8 -*- -""" -==================================================================================== -OT for domain adaptation with image color adaptation [6] with mapping estimation [8] -==================================================================================== - -[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized - discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. -[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for - discrete optimal transport", Neural Information Processing Systems (NIPS), 2016. - -""" - -import numpy as np -import scipy.ndimage as spi -import matplotlib.pylab as pl -import ot - - -#%% Loading images - -I1=spi.imread('../data/ocean_day.jpg').astype(np.float64)/256 -I2=spi.imread('../data/ocean_sunset.jpg').astype(np.float64)/256 - -#%% Plot images - -pl.figure(1) - -pl.subplot(1,2,1) -pl.imshow(I1) -pl.title('Image 1') - -pl.subplot(1,2,2) -pl.imshow(I2) -pl.title('Image 2') - -pl.show() - -#%% Image conversion and dataset generation - -def im2mat(I): - """Converts and image to matrix (one pixel per line)""" - return I.reshape((I.shape[0]*I.shape[1],I.shape[2])) - -def mat2im(X,shape): - """Converts back a matrix to an image""" - return X.reshape(shape) - -X1=im2mat(I1) -X2=im2mat(I2) - -# training samples -nb=1000 -idx1=np.random.randint(X1.shape[0],size=(nb,)) -idx2=np.random.randint(X2.shape[0],size=(nb,)) - -xs=X1[idx1,:] -xt=X2[idx2,:] - -#%% Plot image distributions - - -pl.figure(2,(10,5)) - -pl.subplot(1,2,1) -pl.scatter(xs[:,0],xs[:,2],c=xs) -pl.axis([0,1,0,1]) -pl.xlabel('Red') -pl.ylabel('Blue') -pl.title('Image 1') - -pl.subplot(1,2,2) -#pl.imshow(I2) -pl.scatter(xt[:,0],xt[:,2],c=xt) -pl.axis([0,1,0,1]) -pl.xlabel('Red') -pl.ylabel('Blue') -pl.title('Image 2') - -pl.show() - - - -#%% domain adaptation between images -def minmax(I): - return np.minimum(np.maximum(I,0),1) -# LP problem -da_emd=ot.da.OTDA() # init class -da_emd.fit(xs,xt) # fit distributions - -X1t=da_emd.predict(X1) # out of sample -I1t=minmax(mat2im(X1t,I1.shape)) - -# sinkhorn regularization -lambd=1e-1 -da_entrop=ot.da.OTDA_sinkhorn() -da_entrop.fit(xs,xt,reg=lambd) - -X1te=da_entrop.predict(X1) -I1te=minmax(mat2im(X1te,I1.shape)) - -# linear mapping estimation -eta=1e-8 # quadratic regularization for regression -mu=1e0 # weight of the OT linear term -bias=True # estimate a bias - -ot_mapping=ot.da.OTDA_mapping_linear() -ot_mapping.fit(xs,xt,mu=mu,eta=eta,bias=bias,numItermax = 20,verbose=True) - -X1tl=ot_mapping.predict(X1) # use the estimated mapping -I1tl=minmax(mat2im(X1tl,I1.shape)) - -# nonlinear mapping estimation -eta=1e-2 # quadratic regularization for regression -mu=1e0 # weight of the OT linear term -bias=False # estimate a bias -sigma=1 # sigma bandwidth fot gaussian kernel - - -ot_mapping_kernel=ot.da.OTDA_mapping_kernel() -ot_mapping_kernel.fit(xs,xt,mu=mu,eta=eta,sigma=sigma,bias=bias,numItermax = 10,verbose=True) - -X1tn=ot_mapping_kernel.predict(X1) # use the estimated mapping -I1tn=minmax(mat2im(X1tn,I1.shape)) -#%% plot images - - -pl.figure(2,(10,8)) - -pl.subplot(2,3,1) - -pl.imshow(I1) -pl.title('Im. 1') - -pl.subplot(2,3,2) - -pl.imshow(I2) -pl.title('Im. 2') - - -pl.subplot(2,3,3) -pl.imshow(I1t) -pl.title('Im. 1 Interp LP') - -pl.subplot(2,3,4) -pl.imshow(I1te) -pl.title('Im. 1 Interp Entrop') - - -pl.subplot(2,3,5) -pl.imshow(I1tl) -pl.title('Im. 1 Linear mapping') - -pl.subplot(2,3,6) -pl.imshow(I1tn) -pl.title('Im. 1 nonlinear mapping') - -pl.show() diff --git a/docs/source/auto_examples/plot_OTDA_mapping_color_images.rst b/docs/source/auto_examples/plot_OTDA_mapping_color_images.rst deleted file mode 100644 index 60be3a4..0000000 --- a/docs/source/auto_examples/plot_OTDA_mapping_color_images.rst +++ /dev/null @@ -1,246 +0,0 @@ - - -.. _sphx_glr_auto_examples_plot_OTDA_mapping_color_images.py: - - -==================================================================================== -OT for domain adaptation with image color adaptation [6] with mapping estimation [8] -==================================================================================== - -[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized - discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. -[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for - discrete optimal transport", Neural Information Processing Systems (NIPS), 2016. - - - - - -.. rst-class:: sphx-glr-horizontal - - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_001.png - :scale: 47 - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OTDA_mapping_color_images_002.png - :scale: 47 - - -.. rst-class:: sphx-glr-script-out - - Out:: - - It. |Loss |Delta loss - -------------------------------- - 0|3.624802e+02|0.000000e+00 - 1|3.547180e+02|-2.141395e-02 - 2|3.545494e+02|-4.753955e-04 - 3|3.544646e+02|-2.391784e-04 - 4|3.544126e+02|-1.466280e-04 - 5|3.543775e+02|-9.921805e-05 - 6|3.543518e+02|-7.245828e-05 - 7|3.543323e+02|-5.491924e-05 - 8|3.543170e+02|-4.342401e-05 - 9|3.543046e+02|-3.472174e-05 - 10|3.542945e+02|-2.878681e-05 - 11|3.542859e+02|-2.417065e-05 - 12|3.542786e+02|-2.058131e-05 - 13|3.542723e+02|-1.768262e-05 - 14|3.542668e+02|-1.551616e-05 - 15|3.542620e+02|-1.371909e-05 - 16|3.542577e+02|-1.213326e-05 - 17|3.542538e+02|-1.085481e-05 - 18|3.542531e+02|-1.996006e-06 - It. |Loss |Delta loss - -------------------------------- - 0|3.555768e+02|0.000000e+00 - 1|3.510071e+02|-1.285164e-02 - 2|3.509110e+02|-2.736701e-04 - 3|3.508748e+02|-1.031476e-04 - 4|3.508506e+02|-6.910585e-05 - 5|3.508330e+02|-5.014608e-05 - 6|3.508195e+02|-3.839166e-05 - 7|3.508090e+02|-3.004218e-05 - 8|3.508005e+02|-2.417627e-05 - 9|3.507935e+02|-2.004621e-05 - 10|3.507876e+02|-1.681731e-05 - - - - -| - - -.. code-block:: python - - - import numpy as np - import scipy.ndimage as spi - import matplotlib.pylab as pl - import ot - - - #%% Loading images - - I1=spi.imread('../data/ocean_day.jpg').astype(np.float64)/256 - I2=spi.imread('../data/ocean_sunset.jpg').astype(np.float64)/256 - - #%% Plot images - - pl.figure(1) - - pl.subplot(1,2,1) - pl.imshow(I1) - pl.title('Image 1') - - pl.subplot(1,2,2) - pl.imshow(I2) - pl.title('Image 2') - - pl.show() - - #%% Image conversion and dataset generation - - def im2mat(I): - """Converts and image to matrix (one pixel per line)""" - return I.reshape((I.shape[0]*I.shape[1],I.shape[2])) - - def mat2im(X,shape): - """Converts back a matrix to an image""" - return X.reshape(shape) - - X1=im2mat(I1) - X2=im2mat(I2) - - # training samples - nb=1000 - idx1=np.random.randint(X1.shape[0],size=(nb,)) - idx2=np.random.randint(X2.shape[0],size=(nb,)) - - xs=X1[idx1,:] - xt=X2[idx2,:] - - #%% Plot image distributions - - - pl.figure(2,(10,5)) - - pl.subplot(1,2,1) - pl.scatter(xs[:,0],xs[:,2],c=xs) - pl.axis([0,1,0,1]) - pl.xlabel('Red') - pl.ylabel('Blue') - pl.title('Image 1') - - pl.subplot(1,2,2) - #pl.imshow(I2) - pl.scatter(xt[:,0],xt[:,2],c=xt) - pl.axis([0,1,0,1]) - pl.xlabel('Red') - pl.ylabel('Blue') - pl.title('Image 2') - - pl.show() - - - - #%% domain adaptation between images - def minmax(I): - return np.minimum(np.maximum(I,0),1) - # LP problem - da_emd=ot.da.OTDA() # init class - da_emd.fit(xs,xt) # fit distributions - - X1t=da_emd.predict(X1) # out of sample - I1t=minmax(mat2im(X1t,I1.shape)) - - # sinkhorn regularization - lambd=1e-1 - da_entrop=ot.da.OTDA_sinkhorn() - da_entrop.fit(xs,xt,reg=lambd) - - X1te=da_entrop.predict(X1) - I1te=minmax(mat2im(X1te,I1.shape)) - - # linear mapping estimation - eta=1e-8 # quadratic regularization for regression - mu=1e0 # weight of the OT linear term - bias=True # estimate a bias - - ot_mapping=ot.da.OTDA_mapping_linear() - ot_mapping.fit(xs,xt,mu=mu,eta=eta,bias=bias,numItermax = 20,verbose=True) - - X1tl=ot_mapping.predict(X1) # use the estimated mapping - I1tl=minmax(mat2im(X1tl,I1.shape)) - - # nonlinear mapping estimation - eta=1e-2 # quadratic regularization for regression - mu=1e0 # weight of the OT linear term - bias=False # estimate a bias - sigma=1 # sigma bandwidth fot gaussian kernel - - - ot_mapping_kernel=ot.da.OTDA_mapping_kernel() - ot_mapping_kernel.fit(xs,xt,mu=mu,eta=eta,sigma=sigma,bias=bias,numItermax = 10,verbose=True) - - X1tn=ot_mapping_kernel.predict(X1) # use the estimated mapping - I1tn=minmax(mat2im(X1tn,I1.shape)) - #%% plot images - - - pl.figure(2,(10,8)) - - pl.subplot(2,3,1) - - pl.imshow(I1) - pl.title('Im. 1') - - pl.subplot(2,3,2) - - pl.imshow(I2) - pl.title('Im. 2') - - - pl.subplot(2,3,3) - pl.imshow(I1t) - pl.title('Im. 1 Interp LP') - - pl.subplot(2,3,4) - pl.imshow(I1te) - pl.title('Im. 1 Interp Entrop') - - - pl.subplot(2,3,5) - pl.imshow(I1tl) - pl.title('Im. 1 Linear mapping') - - pl.subplot(2,3,6) - pl.imshow(I1tn) - pl.title('Im. 1 nonlinear mapping') - - pl.show() - -**Total running time of the script:** ( 1 minutes 59.537 seconds) - - - -.. container:: sphx-glr-footer - - - .. container:: sphx-glr-download - - :download:`Download Python source code: plot_OTDA_mapping_color_images.py ` - - - - .. container:: sphx-glr-download - - :download:`Download Jupyter notebook: plot_OTDA_mapping_color_images.ipynb ` - -.. rst-class:: sphx-glr-signature - - `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_OT_1D.ipynb b/docs/source/auto_examples/plot_OT_1D.ipynb index 97c593e..3126b6f 100644 --- a/docs/source/auto_examples/plot_OT_1D.ipynb +++ b/docs/source/auto_examples/plot_OT_1D.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# 1D optimal transport\n\n\n\n" + "\n# 1D optimal transport\n\n\nThis example illustrates the computation of EMD and Sinkhorn transport plans \nand their visualization.\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Solve EMD \n#############################################################################\n\n" + "Solve EMD\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_OT_1D.py b/docs/source/auto_examples/plot_OT_1D.py index be6f5b3..a63f29a 100644 --- a/docs/source/auto_examples/plot_OT_1D.py +++ b/docs/source/auto_examples/plot_OT_1D.py @@ -4,6 +4,9 @@ 1D optimal transport ==================== +This example illustrates the computation of EMD and Sinkhorn transport plans +and their visualization. + """ # Author: Remi Flamary @@ -52,7 +55,7 @@ pl.figure(2, figsize=(5, 5)) ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') ############################################################################## -# Solve EMD +# Solve EMD ############################################################################## #%% EMD diff --git a/docs/source/auto_examples/plot_OT_1D.rst b/docs/source/auto_examples/plot_OT_1D.rst index 252d387..ff02180 100644 --- a/docs/source/auto_examples/plot_OT_1D.rst +++ b/docs/source/auto_examples/plot_OT_1D.rst @@ -7,6 +7,9 @@ 1D optimal transport ==================== +This example illustrates the computation of EMD and Sinkhorn transport plans +and their visualization. + @@ -97,7 +100,7 @@ Plot distributions and loss matrix -Solve EMD +Solve EMD ############################################################################# @@ -165,7 +168,7 @@ Solve Sinkhorn 110|1.527180e-10| -**Total running time of the script:** ( 0 minutes 1.065 seconds) +**Total running time of the script:** ( 0 minutes 0.770 seconds) @@ -184,4 +187,4 @@ Solve Sinkhorn .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_OT_2D_samples.ipynb b/docs/source/auto_examples/plot_OT_2D_samples.ipynb index fc4ce50..0ed7367 100644 --- a/docs/source/auto_examples/plot_OT_2D_samples.ipynb +++ b/docs/source/auto_examples/plot_OT_2D_samples.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# 2D Optimal transport between empirical distributions\n\n\n\n" + "\n# 2D Optimal transport between empirical distributions\n\n\nIllustration of 2D optimal transport between discributions that are weighted\nsum of diracs. The OT matrix is plotted with the samples.\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,79 @@ "execution_count": null, "cell_type": "code", "source": [ - "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n#%% parameters and data generation\n\nn = 50 # nb samples\n\nmu_s = np.array([0, 0])\ncov_s = np.array([[1, 0], [0, 1]])\n\nmu_t = np.array([4, 4])\ncov_t = np.array([[1, -.8], [-.8, 1]])\n\nxs = ot.datasets.get_2D_samples_gauss(n, mu_s, cov_s)\nxt = ot.datasets.get_2D_samples_gauss(n, mu_t, cov_t)\n\na, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples\n\n# loss matrix\nM = ot.dist(xs, xt)\nM /= M.max()\n\n#%% plot samples\n\npl.figure(1)\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('Source and target distributions')\n\npl.figure(2)\npl.imshow(M, interpolation='nearest')\npl.title('Cost matrix M')\n\n\n#%% EMD\n\nG0 = ot.emd(a, b, M)\n\npl.figure(3)\npl.imshow(G0, interpolation='nearest')\npl.title('OT matrix G0')\n\npl.figure(4)\not.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('OT matrix with samples')\n\n\n#%% sinkhorn\n\n# reg term\nlambd = 1e-3\n\nGs = ot.sinkhorn(a, b, M, lambd)\n\npl.figure(5)\npl.imshow(Gs, interpolation='nearest')\npl.title('OT matrix sinkhorn')\n\npl.figure(6)\not.plot.plot2D_samples_mat(xs, xt, Gs, color=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('OT matrix Sinkhorn with samples')\n\npl.show()" + "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Generate data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% parameters and data generation\n\nn = 50 # nb samples\n\nmu_s = np.array([0, 0])\ncov_s = np.array([[1, 0], [0, 1]])\n\nmu_t = np.array([4, 4])\ncov_t = np.array([[1, -.8], [-.8, 1]])\n\nxs = ot.datasets.get_2D_samples_gauss(n, mu_s, cov_s)\nxt = ot.datasets.get_2D_samples_gauss(n, mu_t, cov_t)\n\na, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples\n\n# loss matrix\nM = ot.dist(xs, xt)\nM /= M.max()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Plot data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% plot samples\n\npl.figure(1)\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('Source and target distributions')\n\npl.figure(2)\npl.imshow(M, interpolation='nearest')\npl.title('Cost matrix M')" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Compute EMD\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% EMD\n\nG0 = ot.emd(a, b, M)\n\npl.figure(3)\npl.imshow(G0, interpolation='nearest')\npl.title('OT matrix G0')\n\npl.figure(4)\not.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('OT matrix with samples')" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Compute Sinkhorn\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% sinkhorn\n\n# reg term\nlambd = 1e-3\n\nGs = ot.sinkhorn(a, b, M, lambd)\n\npl.figure(5)\npl.imshow(Gs, interpolation='nearest')\npl.title('OT matrix sinkhorn')\n\npl.figure(6)\not.plot.plot2D_samples_mat(xs, xt, Gs, color=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('OT matrix Sinkhorn with samples')\n\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_OT_2D_samples.py b/docs/source/auto_examples/plot_OT_2D_samples.py index 2a42dc0..f57d631 100644 --- a/docs/source/auto_examples/plot_OT_2D_samples.py +++ b/docs/source/auto_examples/plot_OT_2D_samples.py @@ -4,6 +4,9 @@ 2D Optimal transport between empirical distributions ==================================================== +Illustration of 2D optimal transport between discributions that are weighted +sum of diracs. The OT matrix is plotted with the samples. + """ # Author: Remi Flamary @@ -14,6 +17,10 @@ import numpy as np import matplotlib.pylab as pl import ot +############################################################################## +# Generate data +############################################################################## + #%% parameters and data generation n = 50 # nb samples @@ -33,6 +40,10 @@ a, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples M = ot.dist(xs, xt) M /= M.max() +############################################################################## +# Plot data +############################################################################## + #%% plot samples pl.figure(1) @@ -45,6 +56,9 @@ pl.figure(2) pl.imshow(M, interpolation='nearest') pl.title('Cost matrix M') +############################################################################## +# Compute EMD +############################################################################## #%% EMD @@ -62,6 +76,10 @@ pl.legend(loc=0) pl.title('OT matrix with samples') +############################################################################## +# Compute Sinkhorn +############################################################################## + #%% sinkhorn # reg term diff --git a/docs/source/auto_examples/plot_OT_2D_samples.rst b/docs/source/auto_examples/plot_OT_2D_samples.rst index c472c6a..f95ffaf 100644 --- a/docs/source/auto_examples/plot_OT_2D_samples.rst +++ b/docs/source/auto_examples/plot_OT_2D_samples.rst @@ -7,58 +7,37 @@ 2D Optimal transport between empirical distributions ==================================================== +Illustration of 2D optimal transport between discributions that are weighted +sum of diracs. The OT matrix is plotted with the samples. -.. rst-class:: sphx-glr-horizontal - - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png - :scale: 47 - - * - - .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png - :scale: 47 +.. code-block:: python - * - .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png - :scale: 47 + # Author: Remi Flamary + # + # License: MIT License - * + import numpy as np + import matplotlib.pylab as pl + import ot - .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png - :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png - :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png - :scale: 47 +Generate data +############################################################################# .. code-block:: python - # Author: Remi Flamary - # - # License: MIT License - - import numpy as np - import matplotlib.pylab as pl - import ot - #%% parameters and data generation n = 50 # nb samples @@ -78,6 +57,20 @@ M = ot.dist(xs, xt) M /= M.max() + + + + + + +Plot data +############################################################################# + + + +.. code-block:: python + + #%% plot samples pl.figure(1) @@ -91,6 +84,32 @@ pl.title('Cost matrix M') + + +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png + :scale: 47 + + + + +Compute EMD +############################################################################# + + + +.. code-block:: python + + #%% EMD G0 = ot.emd(a, b, M) @@ -107,6 +126,33 @@ pl.title('OT matrix with samples') + + + +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png + :scale: 47 + + + + +Compute Sinkhorn +############################################################################# + + + +.. code-block:: python + + #%% sinkhorn # reg term @@ -127,7 +173,25 @@ pl.show() -**Total running time of the script:** ( 0 minutes 2.908 seconds) + + +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_009.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_010.png + :scale: 47 + + + + +**Total running time of the script:** ( 0 minutes 1.990 seconds) @@ -146,4 +210,4 @@ .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb b/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb index 04ef5c8..e738db7 100644 --- a/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb +++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# 2D Optimal transport for different metrics\n\n\nStole the figure idea from Fig. 1 and 2 in\nhttps://arxiv.org/pdf/1706.07650.pdf\n\n\n\n" + "\n# 2D Optimal transport for different metrics\n\n\n2D OT on empirical distributio with different gound metric.\n\nStole the figure idea from Fig. 1 and 2 in\nhttps://arxiv.org/pdf/1706.07650.pdf\n\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,79 @@ "execution_count": null, "cell_type": "code", "source": [ - "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n#%% parameters and data generation\n\nfor data in range(2):\n\n if data:\n n = 20 # nb samples\n xs = np.zeros((n, 2))\n xs[:, 0] = np.arange(n) + 1\n xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex...\n\n xt = np.zeros((n, 2))\n xt[:, 1] = np.arange(n) + 1\n else:\n\n n = 50 # nb samples\n xtot = np.zeros((n + 1, 2))\n xtot[:, 0] = np.cos(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\n xtot[:, 1] = np.sin(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\n\n xs = xtot[:n, :]\n xt = xtot[1:, :]\n\n a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples\n\n # loss matrix\n M1 = ot.dist(xs, xt, metric='euclidean')\n M1 /= M1.max()\n\n # loss matrix\n M2 = ot.dist(xs, xt, metric='sqeuclidean')\n M2 /= M2.max()\n\n # loss matrix\n Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean'))\n Mp /= Mp.max()\n\n #%% plot samples\n\n pl.figure(1 + 3 * data, figsize=(7, 3))\n pl.clf()\n pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\n pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\n pl.axis('equal')\n pl.title('Source and traget distributions')\n\n pl.figure(2 + 3 * data, figsize=(7, 3))\n\n pl.subplot(1, 3, 1)\n pl.imshow(M1, interpolation='nearest')\n pl.title('Euclidean cost')\n\n pl.subplot(1, 3, 2)\n pl.imshow(M2, interpolation='nearest')\n pl.title('Squared Euclidean cost')\n\n pl.subplot(1, 3, 3)\n pl.imshow(Mp, interpolation='nearest')\n pl.title('Sqrt Euclidean cost')\n pl.tight_layout()\n\n #%% EMD\n G1 = ot.emd(a, b, M1)\n G2 = ot.emd(a, b, M2)\n Gp = ot.emd(a, b, Mp)\n\n pl.figure(3 + 3 * data, figsize=(7, 3))\n\n pl.subplot(1, 3, 1)\n ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1])\n pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\n pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\n pl.axis('equal')\n # pl.legend(loc=0)\n pl.title('OT Euclidean')\n\n pl.subplot(1, 3, 2)\n ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1])\n pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\n pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\n pl.axis('equal')\n # pl.legend(loc=0)\n pl.title('OT squared Euclidean')\n\n pl.subplot(1, 3, 3)\n ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1])\n pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\n pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\n pl.axis('equal')\n # pl.legend(loc=0)\n pl.title('OT sqrt Euclidean')\n pl.tight_layout()\n\npl.show()" + "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Dataset 1 : uniform sampling\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "n = 20 # nb samples\nxs = np.zeros((n, 2))\nxs[:, 0] = np.arange(n) + 1\nxs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex...\n\nxt = np.zeros((n, 2))\nxt[:, 1] = np.arange(n) + 1\n\na, b = ot.unif(n), ot.unif(n) # uniform distribution on samples\n\n# loss matrix\nM1 = ot.dist(xs, xt, metric='euclidean')\nM1 /= M1.max()\n\n# loss matrix\nM2 = ot.dist(xs, xt, metric='sqeuclidean')\nM2 /= M2.max()\n\n# loss matrix\nMp = np.sqrt(ot.dist(xs, xt, metric='euclidean'))\nMp /= Mp.max()\n\n# Data\npl.figure(1, figsize=(7, 3))\npl.clf()\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\npl.title('Source and traget distributions')\n\n\n# Cost matrices\npl.figure(2, figsize=(7, 3))\n\npl.subplot(1, 3, 1)\npl.imshow(M1, interpolation='nearest')\npl.title('Euclidean cost')\n\npl.subplot(1, 3, 2)\npl.imshow(M2, interpolation='nearest')\npl.title('Squared Euclidean cost')\n\npl.subplot(1, 3, 3)\npl.imshow(Mp, interpolation='nearest')\npl.title('Sqrt Euclidean cost')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Dataset 1 : Plot OT Matrices\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% EMD\nG1 = ot.emd(a, b, M1)\nG2 = ot.emd(a, b, M2)\nGp = ot.emd(a, b, Mp)\n\n# OT matrices\npl.figure(3, figsize=(7, 3))\n\npl.subplot(1, 3, 1)\not.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT Euclidean')\n\npl.subplot(1, 3, 2)\not.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT squared Euclidean')\n\npl.subplot(1, 3, 3)\not.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT sqrt Euclidean')\npl.tight_layout()\n\npl.show()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Dataset 2 : Partial circle\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "n = 50 # nb samples\nxtot = np.zeros((n + 1, 2))\nxtot[:, 0] = np.cos(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\nxtot[:, 1] = np.sin(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\n\nxs = xtot[:n, :]\nxt = xtot[1:, :]\n\na, b = ot.unif(n), ot.unif(n) # uniform distribution on samples\n\n# loss matrix\nM1 = ot.dist(xs, xt, metric='euclidean')\nM1 /= M1.max()\n\n# loss matrix\nM2 = ot.dist(xs, xt, metric='sqeuclidean')\nM2 /= M2.max()\n\n# loss matrix\nMp = np.sqrt(ot.dist(xs, xt, metric='euclidean'))\nMp /= Mp.max()\n\n\n# Data\npl.figure(4, figsize=(7, 3))\npl.clf()\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\npl.title('Source and traget distributions')\n\n\n# Cost matrices\npl.figure(5, figsize=(7, 3))\n\npl.subplot(1, 3, 1)\npl.imshow(M1, interpolation='nearest')\npl.title('Euclidean cost')\n\npl.subplot(1, 3, 2)\npl.imshow(M2, interpolation='nearest')\npl.title('Squared Euclidean cost')\n\npl.subplot(1, 3, 3)\npl.imshow(Mp, interpolation='nearest')\npl.title('Sqrt Euclidean cost')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Dataset 2 : Plot OT Matrices\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% EMD\nG1 = ot.emd(a, b, M1)\nG2 = ot.emd(a, b, M2)\nGp = ot.emd(a, b, Mp)\n\n# OT matrices\npl.figure(6, figsize=(7, 3))\n\npl.subplot(1, 3, 1)\not.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT Euclidean')\n\npl.subplot(1, 3, 2)\not.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT squared Euclidean')\n\npl.subplot(1, 3, 3)\not.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT sqrt Euclidean')\npl.tight_layout()\n\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.py b/docs/source/auto_examples/plot_OT_L1_vs_L2.py index dfc9462..77bde22 100644 --- a/docs/source/auto_examples/plot_OT_L1_vs_L2.py +++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.py @@ -4,6 +4,8 @@ 2D Optimal transport for different metrics ========================================== +2D OT on empirical distributio with different gound metric. + Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf @@ -18,98 +20,190 @@ import numpy as np import matplotlib.pylab as pl import ot -#%% parameters and data generation - -for data in range(2): - - if data: - n = 20 # nb samples - xs = np.zeros((n, 2)) - xs[:, 0] = np.arange(n) + 1 - xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... - - xt = np.zeros((n, 2)) - xt[:, 1] = np.arange(n) + 1 - else: - - n = 50 # nb samples - xtot = np.zeros((n + 1, 2)) - xtot[:, 0] = np.cos( - (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) - xtot[:, 1] = np.sin( - (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) - - xs = xtot[:n, :] - xt = xtot[1:, :] - - a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples - - # loss matrix - M1 = ot.dist(xs, xt, metric='euclidean') - M1 /= M1.max() - - # loss matrix - M2 = ot.dist(xs, xt, metric='sqeuclidean') - M2 /= M2.max() - - # loss matrix - Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) - Mp /= Mp.max() - - #%% plot samples - - pl.figure(1 + 3 * data, figsize=(7, 3)) - pl.clf() - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - pl.title('Source and traget distributions') - - pl.figure(2 + 3 * data, figsize=(7, 3)) - - pl.subplot(1, 3, 1) - pl.imshow(M1, interpolation='nearest') - pl.title('Euclidean cost') - - pl.subplot(1, 3, 2) - pl.imshow(M2, interpolation='nearest') - pl.title('Squared Euclidean cost') - - pl.subplot(1, 3, 3) - pl.imshow(Mp, interpolation='nearest') - pl.title('Sqrt Euclidean cost') - pl.tight_layout() - - #%% EMD - G1 = ot.emd(a, b, M1) - G2 = ot.emd(a, b, M2) - Gp = ot.emd(a, b, Mp) - - pl.figure(3 + 3 * data, figsize=(7, 3)) - - pl.subplot(1, 3, 1) - ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - # pl.legend(loc=0) - pl.title('OT Euclidean') - - pl.subplot(1, 3, 2) - ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - # pl.legend(loc=0) - pl.title('OT squared Euclidean') - - pl.subplot(1, 3, 3) - ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - # pl.legend(loc=0) - pl.title('OT sqrt Euclidean') - pl.tight_layout() +############################################################################## +# Dataset 1 : uniform sampling +############################################################################## + +n = 20 # nb samples +xs = np.zeros((n, 2)) +xs[:, 0] = np.arange(n) + 1 +xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... + +xt = np.zeros((n, 2)) +xt[:, 1] = np.arange(n) + 1 + +a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + +# loss matrix +M1 = ot.dist(xs, xt, metric='euclidean') +M1 /= M1.max() + +# loss matrix +M2 = ot.dist(xs, xt, metric='sqeuclidean') +M2 /= M2.max() + +# loss matrix +Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) +Mp /= Mp.max() + +# Data +pl.figure(1, figsize=(7, 3)) +pl.clf() +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +pl.title('Source and traget distributions') + + +# Cost matrices +pl.figure(2, figsize=(7, 3)) + +pl.subplot(1, 3, 1) +pl.imshow(M1, interpolation='nearest') +pl.title('Euclidean cost') + +pl.subplot(1, 3, 2) +pl.imshow(M2, interpolation='nearest') +pl.title('Squared Euclidean cost') + +pl.subplot(1, 3, 3) +pl.imshow(Mp, interpolation='nearest') +pl.title('Sqrt Euclidean cost') +pl.tight_layout() + +############################################################################## +# Dataset 1 : Plot OT Matrices +############################################################################## + + + +#%% EMD +G1 = ot.emd(a, b, M1) +G2 = ot.emd(a, b, M2) +Gp = ot.emd(a, b, Mp) + +# OT matrices +pl.figure(3, figsize=(7, 3)) + +pl.subplot(1, 3, 1) +ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT Euclidean') + +pl.subplot(1, 3, 2) +ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT squared Euclidean') + +pl.subplot(1, 3, 3) +ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT sqrt Euclidean') +pl.tight_layout() + +pl.show() + + +############################################################################## +# Dataset 2 : Partial circle +############################################################################## + +n = 50 # nb samples +xtot = np.zeros((n + 1, 2)) +xtot[:, 0] = np.cos( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) +xtot[:, 1] = np.sin( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + +xs = xtot[:n, :] +xt = xtot[1:, :] + +a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + +# loss matrix +M1 = ot.dist(xs, xt, metric='euclidean') +M1 /= M1.max() + +# loss matrix +M2 = ot.dist(xs, xt, metric='sqeuclidean') +M2 /= M2.max() + +# loss matrix +Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) +Mp /= Mp.max() + + +# Data +pl.figure(4, figsize=(7, 3)) +pl.clf() +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +pl.title('Source and traget distributions') + + +# Cost matrices +pl.figure(5, figsize=(7, 3)) + +pl.subplot(1, 3, 1) +pl.imshow(M1, interpolation='nearest') +pl.title('Euclidean cost') + +pl.subplot(1, 3, 2) +pl.imshow(M2, interpolation='nearest') +pl.title('Squared Euclidean cost') + +pl.subplot(1, 3, 3) +pl.imshow(Mp, interpolation='nearest') +pl.title('Sqrt Euclidean cost') +pl.tight_layout() + +############################################################################## +# Dataset 2 : Plot OT Matrices +############################################################################## + + + +#%% EMD +G1 = ot.emd(a, b, M1) +G2 = ot.emd(a, b, M2) +Gp = ot.emd(a, b, Mp) + +# OT matrices +pl.figure(6, figsize=(7, 3)) + +pl.subplot(1, 3, 1) +ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT Euclidean') + +pl.subplot(1, 3, 2) +ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT squared Euclidean') + +pl.subplot(1, 3, 3) +ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT sqrt Euclidean') +pl.tight_layout() pl.show() diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.rst b/docs/source/auto_examples/plot_OT_L1_vs_L2.rst index ba52bfe..83a7491 100644 --- a/docs/source/auto_examples/plot_OT_L1_vs_L2.rst +++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.rst @@ -7,6 +7,8 @@ 2D Optimal transport for different metrics ========================================== +2D OT on empirical distributio with different gound metric. + Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf @@ -14,6 +16,80 @@ https://arxiv.org/pdf/1706.07650.pdf +.. code-block:: python + + + # Author: Remi Flamary + # + # License: MIT License + + import numpy as np + import matplotlib.pylab as pl + import ot + + + + + + + +Dataset 1 : uniform sampling +############################################################################# + + + +.. code-block:: python + + + n = 20 # nb samples + xs = np.zeros((n, 2)) + xs[:, 0] = np.arange(n) + 1 + xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... + + xt = np.zeros((n, 2)) + xt[:, 1] = np.arange(n) + 1 + + a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + + # loss matrix + M1 = ot.dist(xs, xt, metric='euclidean') + M1 /= M1.max() + + # loss matrix + M2 = ot.dist(xs, xt, metric='sqeuclidean') + M2 /= M2.max() + + # loss matrix + Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) + Mp /= Mp.max() + + # Data + pl.figure(1, figsize=(7, 3)) + pl.clf() + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + pl.title('Source and traget distributions') + + + # Cost matrices + pl.figure(2, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + pl.imshow(M1, interpolation='nearest') + pl.title('Euclidean cost') + + pl.subplot(1, 3, 2) + pl.imshow(M2, interpolation='nearest') + pl.title('Squared Euclidean cost') + + pl.subplot(1, 3, 3) + pl.imshow(Mp, interpolation='nearest') + pl.title('Sqrt Euclidean cost') + pl.tight_layout() + + + .. rst-class:: sphx-glr-horizontal @@ -28,138 +104,195 @@ https://arxiv.org/pdf/1706.07650.pdf .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png - :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_004.png - :scale: 47 +Dataset 1 : Plot OT Matrices +############################################################################# + + + +.. code-block:: python + + + + + #%% EMD + G1 = ot.emd(a, b, M1) + G2 = ot.emd(a, b, M2) + Gp = ot.emd(a, b, Mp) + + # OT matrices + pl.figure(3, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + # pl.legend(loc=0) + pl.title('OT Euclidean') + + pl.subplot(1, 3, 2) + ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + # pl.legend(loc=0) + pl.title('OT squared Euclidean') + + pl.subplot(1, 3, 3) + ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + # pl.legend(loc=0) + pl.title('OT sqrt Euclidean') + pl.tight_layout() + + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png + :align: center + + + + +Dataset 2 : Partial circle +############################################################################# + + + +.. code-block:: python + + + n = 50 # nb samples + xtot = np.zeros((n + 1, 2)) + xtot[:, 0] = np.cos( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + xtot[:, 1] = np.sin( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + + xs = xtot[:n, :] + xt = xtot[1:, :] + + a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + + # loss matrix + M1 = ot.dist(xs, xt, metric='euclidean') + M1 /= M1.max() + + # loss matrix + M2 = ot.dist(xs, xt, metric='sqeuclidean') + M2 /= M2.max() + + # loss matrix + Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) + Mp /= Mp.max() + + + # Data + pl.figure(4, figsize=(7, 3)) + pl.clf() + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + pl.title('Source and traget distributions') + + + # Cost matrices + pl.figure(5, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + pl.imshow(M1, interpolation='nearest') + pl.title('Euclidean cost') + + pl.subplot(1, 3, 2) + pl.imshow(M2, interpolation='nearest') + pl.title('Squared Euclidean cost') + + pl.subplot(1, 3, 3) + pl.imshow(Mp, interpolation='nearest') + pl.title('Sqrt Euclidean cost') + pl.tight_layout() + + + + +.. rst-class:: sphx-glr-horizontal + * - .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png + .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_007.png :scale: 47 * - .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_006.png + .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_008.png :scale: 47 +Dataset 2 : Plot OT Matrices +############################################################################# + + .. code-block:: python - # Author: Remi Flamary - # - # License: MIT License - import numpy as np - import matplotlib.pylab as pl - import ot - #%% parameters and data generation - - for data in range(2): - - if data: - n = 20 # nb samples - xs = np.zeros((n, 2)) - xs[:, 0] = np.arange(n) + 1 - xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... - - xt = np.zeros((n, 2)) - xt[:, 1] = np.arange(n) + 1 - else: - - n = 50 # nb samples - xtot = np.zeros((n + 1, 2)) - xtot[:, 0] = np.cos( - (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) - xtot[:, 1] = np.sin( - (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) - - xs = xtot[:n, :] - xt = xtot[1:, :] - - a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples - - # loss matrix - M1 = ot.dist(xs, xt, metric='euclidean') - M1 /= M1.max() - - # loss matrix - M2 = ot.dist(xs, xt, metric='sqeuclidean') - M2 /= M2.max() - - # loss matrix - Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) - Mp /= Mp.max() - - #%% plot samples - - pl.figure(1 + 3 * data, figsize=(7, 3)) - pl.clf() - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - pl.title('Source and traget distributions') - - pl.figure(2 + 3 * data, figsize=(7, 3)) - - pl.subplot(1, 3, 1) - pl.imshow(M1, interpolation='nearest') - pl.title('Euclidean cost') - - pl.subplot(1, 3, 2) - pl.imshow(M2, interpolation='nearest') - pl.title('Squared Euclidean cost') - - pl.subplot(1, 3, 3) - pl.imshow(Mp, interpolation='nearest') - pl.title('Sqrt Euclidean cost') - pl.tight_layout() - - #%% EMD - G1 = ot.emd(a, b, M1) - G2 = ot.emd(a, b, M2) - Gp = ot.emd(a, b, Mp) - - pl.figure(3 + 3 * data, figsize=(7, 3)) - - pl.subplot(1, 3, 1) - ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - # pl.legend(loc=0) - pl.title('OT Euclidean') - - pl.subplot(1, 3, 2) - ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - # pl.legend(loc=0) - pl.title('OT squared Euclidean') - - pl.subplot(1, 3, 3) - ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - # pl.legend(loc=0) - pl.title('OT sqrt Euclidean') - pl.tight_layout() + #%% EMD + G1 = ot.emd(a, b, M1) + G2 = ot.emd(a, b, M2) + Gp = ot.emd(a, b, Mp) + + # OT matrices + pl.figure(6, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + # pl.legend(loc=0) + pl.title('OT Euclidean') + + pl.subplot(1, 3, 2) + ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + # pl.legend(loc=0) + pl.title('OT squared Euclidean') + + pl.subplot(1, 3, 3) + ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + # pl.legend(loc=0) + pl.title('OT sqrt Euclidean') + pl.tight_layout() pl.show() -**Total running time of the script:** ( 0 minutes 1.906 seconds) + + +.. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_011.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 1.217 seconds) @@ -178,4 +311,4 @@ https://arxiv.org/pdf/1706.07650.pdf .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_OT_conv.ipynb b/docs/source/auto_examples/plot_OT_conv.ipynb deleted file mode 100644 index 7fc4af0..0000000 --- a/docs/source/auto_examples/plot_OT_conv.ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "nbformat_minor": 0, - "nbformat": 4, - "cells": [ - { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], - "metadata": { - "collapsed": false - } - }, - { - "source": [ - "\n# 1D Wasserstein barycenter demo\n\n\n\n@author: rflamary\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, - { - "execution_count": null, - "cell_type": "code", - "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom mpl_toolkits.mplot3d import Axes3D #necessary for 3d plot even if not used\nimport scipy as sp\nimport scipy.signal as sps\n#%% parameters\n\nn=10 # nb bins\n\n# bin positions\nx=np.arange(n,dtype=np.float64)\n\nxx,yy=np.meshgrid(x,x)\n\n\nxpos=np.hstack((xx.reshape(-1,1),yy.reshape(-1,1)))\n\nM=ot.dist(xpos)\n\n\nI0=((xx-5)**2+(yy-5)**2<3**2)*1.0\nI1=((xx-7)**2+(yy-7)**2<3**2)*1.0\n\nI0/=I0.sum()\nI1/=I1.sum()\n\ni0=I0.ravel()\ni1=I1.ravel()\n\nM=M[i0>0,:][:,i1>0].copy()\ni0=i0[i0>0]\ni1=i1[i1>0]\nItot=np.concatenate((I0[:,:,np.newaxis],I1[:,:,np.newaxis]),2)\n\n\n#%% plot the distributions\n\npl.figure(1)\npl.subplot(2,2,1)\npl.imshow(I0)\npl.subplot(2,2,2)\npl.imshow(I1)\n\n\n#%% barycenter computation\n\nalpha=0.5 # 0<=alpha<=1\nweights=np.array([1-alpha,alpha])\n\n\ndef conv2(I,k):\n return sp.ndimage.convolve1d(sp.ndimage.convolve1d(I,k,axis=1),k,axis=0)\n\ndef conv2n(I,k):\n res=np.zeros_like(I)\n for i in range(I.shape[2]):\n res[:,:,i]=conv2(I[:,:,i],k)\n return res\n\n\ndef get_1Dkernel(reg,thr=1e-16,wmax=1024):\n w=max(min(wmax,2*int((-np.log(thr)*reg)**(.5))),3)\n x=np.arange(w,dtype=np.float64)\n return np.exp(-((x-w/2)**2)/reg)\n \nthr=1e-16\nreg=1e0\n\nk=get_1Dkernel(reg)\npl.figure(2)\npl.plot(k)\n\nI05=conv2(I0,k)\n\npl.figure(1)\npl.subplot(2,2,1)\npl.imshow(I0)\npl.subplot(2,2,2)\npl.imshow(I05)\n\n#%%\n\nG=ot.emd(i0,i1,M)\nr0=np.sum(M*G)\n\nreg=1e-1\nGs=ot.bregman.sinkhorn_knopp(i0,i1,M,reg=reg)\nrs=np.sum(M*Gs)\n\n#%%\n\ndef mylog(u):\n tmp=np.log(u)\n tmp[np.isnan(tmp)]=0\n return tmp\n\ndef sinkhorn_conv(a,b, reg, numItermax = 1000, stopThr=1e-9, verbose=False, log=False,**kwargs):\n\n\n a=np.asarray(a,dtype=np.float64)\n b=np.asarray(b,dtype=np.float64)\n \n \n if len(b.shape)>2:\n nbb=b.shape[2]\n a=a[:,:,np.newaxis]\n else:\n nbb=0\n \n\n if log:\n log={'err':[]}\n\n # we assume that no distances are null except those of the diagonal of distances\n if nbb:\n u = np.ones((a.shape[0],a.shape[1],nbb))/(np.prod(a.shape[:2]))\n v = np.ones((a.shape[0],a.shape[1],nbb))/(np.prod(b.shape[:2]))\n a0=1.0/(np.prod(b.shape[:2]))\n else:\n u = np.ones((a.shape[0],a.shape[1]))/(np.prod(a.shape[:2]))\n v = np.ones((a.shape[0],a.shape[1]))/(np.prod(b.shape[:2]))\n a0=1.0/(np.prod(b.shape[:2]))\n \n \n k=get_1Dkernel(reg)\n \n if nbb:\n K=lambda I: conv2n(I,k)\n else:\n K=lambda I: conv2(I,k)\n\n cpt = 0\n err=1\n while (err>stopThr and cpt0,:][:,i1>0].copy() -i0=i0[i0>0] -i1=i1[i1>0] -Itot=np.concatenate((I0[:,:,np.newaxis],I1[:,:,np.newaxis]),2) - - -#%% plot the distributions - -pl.figure(1) -pl.subplot(2,2,1) -pl.imshow(I0) -pl.subplot(2,2,2) -pl.imshow(I1) - - -#%% barycenter computation - -alpha=0.5 # 0<=alpha<=1 -weights=np.array([1-alpha,alpha]) - - -def conv2(I,k): - return sp.ndimage.convolve1d(sp.ndimage.convolve1d(I,k,axis=1),k,axis=0) - -def conv2n(I,k): - res=np.zeros_like(I) - for i in range(I.shape[2]): - res[:,:,i]=conv2(I[:,:,i],k) - return res - - -def get_1Dkernel(reg,thr=1e-16,wmax=1024): - w=max(min(wmax,2*int((-np.log(thr)*reg)**(.5))),3) - x=np.arange(w,dtype=np.float64) - return np.exp(-((x-w/2)**2)/reg) - -thr=1e-16 -reg=1e0 - -k=get_1Dkernel(reg) -pl.figure(2) -pl.plot(k) - -I05=conv2(I0,k) - -pl.figure(1) -pl.subplot(2,2,1) -pl.imshow(I0) -pl.subplot(2,2,2) -pl.imshow(I05) - -#%% - -G=ot.emd(i0,i1,M) -r0=np.sum(M*G) - -reg=1e-1 -Gs=ot.bregman.sinkhorn_knopp(i0,i1,M,reg=reg) -rs=np.sum(M*Gs) - -#%% - -def mylog(u): - tmp=np.log(u) - tmp[np.isnan(tmp)]=0 - return tmp - -def sinkhorn_conv(a,b, reg, numItermax = 1000, stopThr=1e-9, verbose=False, log=False,**kwargs): - - - a=np.asarray(a,dtype=np.float64) - b=np.asarray(b,dtype=np.float64) - - - if len(b.shape)>2: - nbb=b.shape[2] - a=a[:,:,np.newaxis] - else: - nbb=0 - - - if log: - log={'err':[]} - - # we assume that no distances are null except those of the diagonal of distances - if nbb: - u = np.ones((a.shape[0],a.shape[1],nbb))/(np.prod(a.shape[:2])) - v = np.ones((a.shape[0],a.shape[1],nbb))/(np.prod(b.shape[:2])) - a0=1.0/(np.prod(b.shape[:2])) - else: - u = np.ones((a.shape[0],a.shape[1]))/(np.prod(a.shape[:2])) - v = np.ones((a.shape[0],a.shape[1]))/(np.prod(b.shape[:2])) - a0=1.0/(np.prod(b.shape[:2])) - - - k=get_1Dkernel(reg) - - if nbb: - K=lambda I: conv2n(I,k) - else: - K=lambda I: conv2(I,k) - - cpt = 0 - err=1 - while (err>stopThr and cpt", line 86, in - TypeError: unsupported operand type(s) for *: 'float' and 'Mock' - - - - - -.. code-block:: python - - - import numpy as np - import matplotlib.pylab as pl - import ot - from mpl_toolkits.mplot3d import Axes3D #necessary for 3d plot even if not used - import scipy as sp - import scipy.signal as sps - #%% parameters - - n=10 # nb bins - - # bin positions - x=np.arange(n,dtype=np.float64) - - xx,yy=np.meshgrid(x,x) - - - xpos=np.hstack((xx.reshape(-1,1),yy.reshape(-1,1))) - - M=ot.dist(xpos) - - - I0=((xx-5)**2+(yy-5)**2<3**2)*1.0 - I1=((xx-7)**2+(yy-7)**2<3**2)*1.0 - - I0/=I0.sum() - I1/=I1.sum() - - i0=I0.ravel() - i1=I1.ravel() - - M=M[i0>0,:][:,i1>0].copy() - i0=i0[i0>0] - i1=i1[i1>0] - Itot=np.concatenate((I0[:,:,np.newaxis],I1[:,:,np.newaxis]),2) - - - #%% plot the distributions - - pl.figure(1) - pl.subplot(2,2,1) - pl.imshow(I0) - pl.subplot(2,2,2) - pl.imshow(I1) - - - #%% barycenter computation - - alpha=0.5 # 0<=alpha<=1 - weights=np.array([1-alpha,alpha]) - - - def conv2(I,k): - return sp.ndimage.convolve1d(sp.ndimage.convolve1d(I,k,axis=1),k,axis=0) - - def conv2n(I,k): - res=np.zeros_like(I) - for i in range(I.shape[2]): - res[:,:,i]=conv2(I[:,:,i],k) - return res - - - def get_1Dkernel(reg,thr=1e-16,wmax=1024): - w=max(min(wmax,2*int((-np.log(thr)*reg)**(.5))),3) - x=np.arange(w,dtype=np.float64) - return np.exp(-((x-w/2)**2)/reg) - - thr=1e-16 - reg=1e0 - - k=get_1Dkernel(reg) - pl.figure(2) - pl.plot(k) - - I05=conv2(I0,k) - - pl.figure(1) - pl.subplot(2,2,1) - pl.imshow(I0) - pl.subplot(2,2,2) - pl.imshow(I05) - - #%% - - G=ot.emd(i0,i1,M) - r0=np.sum(M*G) - - reg=1e-1 - Gs=ot.bregman.sinkhorn_knopp(i0,i1,M,reg=reg) - rs=np.sum(M*Gs) - - #%% - - def mylog(u): - tmp=np.log(u) - tmp[np.isnan(tmp)]=0 - return tmp - - def sinkhorn_conv(a,b, reg, numItermax = 1000, stopThr=1e-9, verbose=False, log=False,**kwargs): - - - a=np.asarray(a,dtype=np.float64) - b=np.asarray(b,dtype=np.float64) - - - if len(b.shape)>2: - nbb=b.shape[2] - a=a[:,:,np.newaxis] - else: - nbb=0 - - - if log: - log={'err':[]} - - # we assume that no distances are null except those of the diagonal of distances - if nbb: - u = np.ones((a.shape[0],a.shape[1],nbb))/(np.prod(a.shape[:2])) - v = np.ones((a.shape[0],a.shape[1],nbb))/(np.prod(b.shape[:2])) - a0=1.0/(np.prod(b.shape[:2])) - else: - u = np.ones((a.shape[0],a.shape[1]))/(np.prod(a.shape[:2])) - v = np.ones((a.shape[0],a.shape[1]))/(np.prod(b.shape[:2])) - a0=1.0/(np.prod(b.shape[:2])) - - - k=get_1Dkernel(reg) - - if nbb: - K=lambda I: conv2n(I,k) - else: - K=lambda I: conv2(I,k) - - cpt = 0 - err=1 - while (err>stopThr and cpt` - - - - .. container:: sphx-glr-download - - :download:`Download Jupyter notebook: plot_OT_conv.ipynb ` - -.. rst-class:: sphx-glr-signature - - `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_WDA.ipynb b/docs/source/auto_examples/plot_WDA.ipynb index 5568128..8e0db41 100644 --- a/docs/source/auto_examples/plot_WDA.ipynb +++ b/docs/source/auto_examples/plot_WDA.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# Wasserstein Discriminant Analysis\n\n\n\n" + "\n# Wasserstein Discriminant Analysis\n\n\nThis example illustrate the use of WDA as proposed in [11].\n\n\n[11] Flamary, R., Cuturi, M., Courty, N., & Rakotomamonjy, A. (2016). \nWasserstein Discriminant Analysis.\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,97 @@ "execution_count": null, "cell_type": "code", "source": [ - "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\n\nfrom ot.dr import wda, fda\n\n\n#%% parameters\n\nn = 1000 # nb samples in source and target datasets\nnz = 0.2\n\n# generate circle dataset\nt = np.random.rand(n) * 2 * np.pi\nys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1\nxs = np.concatenate(\n (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1)\nxs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2)\n\nt = np.random.rand(n) * 2 * np.pi\nyt = np.floor((np.arange(n) * 1.0 / n * 3)) + 1\nxt = np.concatenate(\n (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1)\nxt = xt * yt.reshape(-1, 1) + nz * np.random.randn(n, 2)\n\nnbnoise = 8\n\nxs = np.hstack((xs, np.random.randn(n, nbnoise)))\nxt = np.hstack((xt, np.random.randn(n, nbnoise)))\n\n#%% plot samples\npl.figure(1, figsize=(6.4, 3.5))\n\npl.subplot(1, 2, 1)\npl.scatter(xt[:, 0], xt[:, 1], c=ys, marker='+', label='Source samples')\npl.legend(loc=0)\npl.title('Discriminant dimensions')\n\npl.subplot(1, 2, 2)\npl.scatter(xt[:, 2], xt[:, 3], c=ys, marker='+', label='Source samples')\npl.legend(loc=0)\npl.title('Other dimensions')\npl.tight_layout()\n\n#%% Compute FDA\np = 2\n\nPfda, projfda = fda(xs, ys, p)\n\n#%% Compute WDA\np = 2\nreg = 1e0\nk = 10\nmaxiter = 100\n\nPwda, projwda = wda(xs, ys, p, reg, k, maxiter=maxiter)\n\n#%% plot samples\n\nxsp = projfda(xs)\nxtp = projfda(xt)\n\nxspw = projwda(xs)\nxtpw = projwda(xt)\n\npl.figure(2)\n\npl.subplot(2, 2, 1)\npl.scatter(xsp[:, 0], xsp[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected training samples FDA')\n\npl.subplot(2, 2, 2)\npl.scatter(xtp[:, 0], xtp[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected test samples FDA')\n\npl.subplot(2, 2, 3)\npl.scatter(xspw[:, 0], xspw[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected training samples WDA')\n\npl.subplot(2, 2, 4)\npl.scatter(xtpw[:, 0], xtpw[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected test samples WDA')\npl.tight_layout()\n\npl.show()" + "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\n\nfrom ot.dr import wda, fda" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Generate data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% parameters\n\nn = 1000 # nb samples in source and target datasets\nnz = 0.2\n\n# generate circle dataset\nt = np.random.rand(n) * 2 * np.pi\nys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1\nxs = np.concatenate(\n (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1)\nxs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2)\n\nt = np.random.rand(n) * 2 * np.pi\nyt = np.floor((np.arange(n) * 1.0 / n * 3)) + 1\nxt = np.concatenate(\n (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1)\nxt = xt * yt.reshape(-1, 1) + nz * np.random.randn(n, 2)\n\nnbnoise = 8\n\nxs = np.hstack((xs, np.random.randn(n, nbnoise)))\nxt = np.hstack((xt, np.random.randn(n, nbnoise)))" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Plot data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% plot samples\npl.figure(1, figsize=(6.4, 3.5))\n\npl.subplot(1, 2, 1)\npl.scatter(xt[:, 0], xt[:, 1], c=ys, marker='+', label='Source samples')\npl.legend(loc=0)\npl.title('Discriminant dimensions')\n\npl.subplot(1, 2, 2)\npl.scatter(xt[:, 2], xt[:, 3], c=ys, marker='+', label='Source samples')\npl.legend(loc=0)\npl.title('Other dimensions')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Compute Fisher Discriminant Analysis\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% Compute FDA\np = 2\n\nPfda, projfda = fda(xs, ys, p)" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Compute Wasserstein Discriminant Analysis\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% Compute WDA\np = 2\nreg = 1e0\nk = 10\nmaxiter = 100\n\nPwda, projwda = wda(xs, ys, p, reg, k, maxiter=maxiter)" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Plot 2D projections\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% plot samples\n\nxsp = projfda(xs)\nxtp = projfda(xt)\n\nxspw = projwda(xs)\nxtpw = projwda(xt)\n\npl.figure(2)\n\npl.subplot(2, 2, 1)\npl.scatter(xsp[:, 0], xsp[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected training samples FDA')\n\npl.subplot(2, 2, 2)\npl.scatter(xtp[:, 0], xtp[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected test samples FDA')\n\npl.subplot(2, 2, 3)\npl.scatter(xspw[:, 0], xspw[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected training samples WDA')\n\npl.subplot(2, 2, 4)\npl.scatter(xtpw[:, 0], xtpw[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected test samples WDA')\npl.tight_layout()\n\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_WDA.py b/docs/source/auto_examples/plot_WDA.py index 42789f2..06a2e38 100644 --- a/docs/source/auto_examples/plot_WDA.py +++ b/docs/source/auto_examples/plot_WDA.py @@ -4,6 +4,12 @@ Wasserstein Discriminant Analysis ================================= +This example illustrate the use of WDA as proposed in [11]. + + +[11] Flamary, R., Cuturi, M., Courty, N., & Rakotomamonjy, A. (2016). +Wasserstein Discriminant Analysis. + """ # Author: Remi Flamary @@ -16,6 +22,10 @@ import matplotlib.pylab as pl from ot.dr import wda, fda +############################################################################## +# Generate data +############################################################################## + #%% parameters n = 1000 # nb samples in source and target datasets @@ -39,6 +49,10 @@ nbnoise = 8 xs = np.hstack((xs, np.random.randn(n, nbnoise))) xt = np.hstack((xt, np.random.randn(n, nbnoise))) +############################################################################## +# Plot data +############################################################################## + #%% plot samples pl.figure(1, figsize=(6.4, 3.5)) @@ -53,11 +67,19 @@ pl.legend(loc=0) pl.title('Other dimensions') pl.tight_layout() +############################################################################## +# Compute Fisher Discriminant Analysis +############################################################################## + #%% Compute FDA p = 2 Pfda, projfda = fda(xs, ys, p) +############################################################################## +# Compute Wasserstein Discriminant Analysis +############################################################################## + #%% Compute WDA p = 2 reg = 1e0 @@ -66,6 +88,11 @@ maxiter = 100 Pwda, projwda = wda(xs, ys, p, reg, k, maxiter=maxiter) + +############################################################################## +# Plot 2D projections +############################################################################## + #%% plot samples xsp = projfda(xs) diff --git a/docs/source/auto_examples/plot_WDA.rst b/docs/source/auto_examples/plot_WDA.rst index 76ebaf5..8c9ee29 100644 --- a/docs/source/auto_examples/plot_WDA.rst +++ b/docs/source/auto_examples/plot_WDA.rst @@ -7,86 +7,40 @@ Wasserstein Discriminant Analysis ================================= +This example illustrate the use of WDA as proposed in [11]. +[11] Flamary, R., Cuturi, M., Courty, N., & Rakotomamonjy, A. (2016). +Wasserstein Discriminant Analysis. -.. rst-class:: sphx-glr-horizontal - * +.. code-block:: python - .. image:: /auto_examples/images/sphx_glr_plot_WDA_001.png - :scale: 47 - * + # Author: Remi Flamary + # + # License: MIT License - .. image:: /auto_examples/images/sphx_glr_plot_WDA_002.png - :scale: 47 + import numpy as np + import matplotlib.pylab as pl + from ot.dr import wda, fda -.. rst-class:: sphx-glr-script-out - Out:: - Compiling cost function... - Computing gradient of cost function... - iter cost val grad. norm - 1 +8.9741888001949222e-01 3.71269078e-01 - 2 +4.9103998133976140e-01 3.46687543e-01 - 3 +4.2142651893148553e-01 1.04789602e-01 - 4 +4.1573609749588841e-01 5.21726648e-02 - 5 +4.1486046805261961e-01 5.35335513e-02 - 6 +4.1315953904635105e-01 2.17803599e-02 - 7 +4.1313030162717523e-01 6.06901182e-02 - 8 +4.1301511591963386e-01 5.88598758e-02 - 9 +4.1258349404769817e-01 5.14307874e-02 - 10 +4.1139242901051226e-01 2.03198793e-02 - 11 +4.1113798965164017e-01 1.18944721e-02 - 12 +4.1103446820878486e-01 2.21783648e-02 - 13 +4.1076586830791861e-01 9.51495863e-03 - 14 +4.1036935287519144e-01 3.74973214e-02 - 15 +4.0958729714575060e-01 1.23810902e-02 - 16 +4.0898266309095005e-01 4.01999918e-02 - 17 +4.0816076944357715e-01 2.27240277e-02 - 18 +4.0788116701894767e-01 4.42815945e-02 - 19 +4.0695443744952403e-01 3.28464304e-02 - 20 +4.0293834480911150e-01 7.76000681e-02 - 21 +3.8488003705202750e-01 1.49378022e-01 - 22 +3.0767344927282614e-01 2.15432117e-01 - 23 +2.3849425361868334e-01 1.07942382e-01 - 24 +2.3845125762548214e-01 1.08953278e-01 - 25 +2.3828007730494005e-01 1.07934830e-01 - 26 +2.3760839060570119e-01 1.03822134e-01 - 27 +2.3514215179705886e-01 8.67263481e-02 - 28 +2.2978886197588613e-01 9.26609306e-03 - 29 +2.2972671019495342e-01 2.59476089e-03 - 30 +2.2972355865247496e-01 1.57205146e-03 - 31 +2.2972296662351968e-01 1.29300760e-03 - 32 +2.2972181557051569e-01 8.82375756e-05 - 33 +2.2972181277025336e-01 6.20536544e-05 - 34 +2.2972181023486152e-01 7.01884014e-06 - 35 +2.2972181020400181e-01 1.60415765e-06 - 36 +2.2972181020236590e-01 2.44290966e-07 - Terminated - min grad norm reached after 36 iterations, 13.41 seconds. - - - - -| -.. code-block:: python - # Author: Remi Flamary - # - # License: MIT License - import numpy as np - import matplotlib.pylab as pl +Generate data +############################################################################# - from ot.dr import wda, fda + + +.. code-block:: python #%% parameters @@ -112,6 +66,20 @@ Wasserstein Discriminant Analysis xs = np.hstack((xs, np.random.randn(n, nbnoise))) xt = np.hstack((xt, np.random.randn(n, nbnoise))) + + + + + + +Plot data +############################################################################# + + + +.. code-block:: python + + #%% plot samples pl.figure(1, figsize=(6.4, 3.5)) @@ -126,11 +94,42 @@ Wasserstein Discriminant Analysis pl.title('Other dimensions') pl.tight_layout() + + + +.. image:: /auto_examples/images/sphx_glr_plot_WDA_001.png + :align: center + + + + +Compute Fisher Discriminant Analysis +############################################################################# + + + +.. code-block:: python + + #%% Compute FDA p = 2 Pfda, projfda = fda(xs, ys, p) + + + + + + +Compute Wasserstein Discriminant Analysis +############################################################################# + + + +.. code-block:: python + + #%% Compute WDA p = 2 reg = 1e0 @@ -139,6 +138,45 @@ Wasserstein Discriminant Analysis Pwda, projwda = wda(xs, ys, p, reg, k, maxiter=maxiter) + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + Compiling cost function... + Computing gradient of cost function... + iter cost val grad. norm + 1 +7.7038877420882157e-01 6.30647522e-01 + 2 +3.3969600919721271e-01 2.83791849e-01 + 3 +3.0014000762425608e-01 2.56139137e-01 + 4 +2.3397191702411621e-01 6.41134216e-02 + 5 +2.3107227220070231e-01 2.24837190e-02 + 6 +2.3072327156158298e-01 1.71334761e-03 + 7 +2.3072143589220098e-01 6.30059431e-04 + 8 +2.3072133109125159e-01 4.88673790e-04 + 9 +2.3072119579341774e-01 1.74129117e-04 + 10 +2.3072118662364521e-01 1.27046386e-04 + 11 +2.3072118228917746e-01 9.70877451e-05 + 12 +2.3072117734120351e-01 4.17292699e-05 + 13 +2.3072117623493599e-01 4.46062100e-06 + 14 +2.3072117622383431e-01 1.59801454e-06 + 15 +2.3072117622300498e-01 1.12117391e-06 + 16 +2.3072117622220378e-01 4.14581994e-08 + Terminated - min grad norm reached after 16 iterations, 7.77 seconds. + + +Plot 2D projections +############################################################################# + + + +.. code-block:: python + + #%% plot samples xsp = projfda(xs) @@ -172,7 +210,15 @@ Wasserstein Discriminant Analysis pl.show() -**Total running time of the script:** ( 0 minutes 19.853 seconds) + + +.. image:: /auto_examples/images/sphx_glr_plot_WDA_003.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 8.568 seconds) @@ -191,4 +237,4 @@ Wasserstein Discriminant Analysis .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_barycenter_1D.ipynb b/docs/source/auto_examples/plot_barycenter_1D.ipynb index 239b8b8..657782d 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.ipynb +++ b/docs/source/auto_examples/plot_barycenter_1D.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# 1D Wasserstein barycenter demo\n\n\n\n" + "\n# 1D Wasserstein barycenter demo\n\n\nThis example illustrates the computation of regularized Wassersyein Barycenter \nas proposed in [3].\n\n\n[3] Benamou, J. D., Carlier, G., Cuturi, M., Nenna, L., & Peyr\u00e9, G. (2015). \nIterative Bregman projections for regularized transportation problems\nSIAM Journal on Scientific Computing, 37(2), A1111-A1138.\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,79 @@ "execution_count": null, "cell_type": "code", "source": [ - "# Author: Remi Flamary \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#%% parameters\n\nn = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na1 = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std\na2 = ot.datasets.get_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#%% 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#%% 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#%% 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 \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" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Generate data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "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.get_1D_gauss(n, m=20, s=5) # m= mean, s= std\na2 = ot.datasets.get_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()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Plot data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "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()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Barycenter computation\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "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()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Barycentric interpolation\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "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()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_barycenter_1D.py b/docs/source/auto_examples/plot_barycenter_1D.py index 875f44c..142b05e 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.py +++ b/docs/source/auto_examples/plot_barycenter_1D.py @@ -4,6 +4,14 @@ 1D Wasserstein barycenter demo ============================== +This example illustrates the computation of regularized Wassersyein Barycenter +as proposed in [3]. + + +[3] Benamou, J. D., Carlier, G., Cuturi, M., Nenna, L., & Peyré, G. (2015). +Iterative Bregman projections for regularized transportation problems +SIAM Journal on Scientific Computing, 37(2), A1111-A1138. + """ # Author: Remi Flamary @@ -17,6 +25,9 @@ import ot from mpl_toolkits.mplot3d import Axes3D # noqa from matplotlib.collections import PolyCollection +############################################################################## +# Generate data +############################################################################## #%% parameters @@ -37,6 +48,10 @@ n_distributions = A.shape[1] M = ot.utils.dist0(n) M /= M.max() +############################################################################## +# Plot data +############################################################################## + #%% plot the distributions pl.figure(1, figsize=(6.4, 3)) @@ -45,6 +60,10 @@ for i in range(n_distributions): pl.title('Distributions') pl.tight_layout() +############################################################################## +# Barycenter computation +############################################################################## + #%% barycenter computation alpha = 0.2 # 0<=alpha<=1 @@ -71,6 +90,10 @@ pl.legend() pl.title('Barycenters') pl.tight_layout() +############################################################################## +# Barycentric interpolation +############################################################################## + #%% barycenter interpolation n_alpha = 11 diff --git a/docs/source/auto_examples/plot_barycenter_1D.rst b/docs/source/auto_examples/plot_barycenter_1D.rst index af88e80..d3f243f 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.rst +++ b/docs/source/auto_examples/plot_barycenter_1D.rst @@ -7,33 +7,13 @@ 1D Wasserstein barycenter demo ============================== +This example illustrates the computation of regularized Wassersyein Barycenter +as proposed in [3]. - - -.. rst-class:: sphx-glr-horizontal - - - * - - .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_001.png - :scale: 47 - - * - - .. 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 - +[3] Benamou, J. D., Carlier, G., Cuturi, M., Nenna, L., & Peyré, G. (2015). +Iterative Bregman projections for regularized transportation problems +SIAM Journal on Scientific Computing, 37(2), A1111-A1138. @@ -53,6 +33,19 @@ from matplotlib.collections import PolyCollection + + + + + +Generate data +############################################################################# + + + +.. code-block:: python + + #%% parameters n = 100 # nb bins @@ -72,6 +65,20 @@ M = ot.utils.dist0(n) M /= M.max() + + + + + + +Plot data +############################################################################# + + + +.. code-block:: python + + #%% plot the distributions pl.figure(1, figsize=(6.4, 3)) @@ -80,6 +87,23 @@ pl.title('Distributions') pl.tight_layout() + + + +.. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_001.png + :align: center + + + + +Barycenter computation +############################################################################# + + + +.. code-block:: python + + #%% barycenter computation alpha = 0.2 # 0<=alpha<=1 @@ -106,6 +130,23 @@ pl.title('Barycenters') pl.tight_layout() + + + +.. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_003.png + :align: center + + + + +Barycentric interpolation +############################################################################# + + + +.. code-block:: python + + #%% barycenter interpolation n_alpha = 11 @@ -171,7 +212,25 @@ pl.show() -**Total running time of the script:** ( 0 minutes 0.546 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.520 seconds) @@ -190,4 +249,4 @@ .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_compute_emd.ipynb b/docs/source/auto_examples/plot_compute_emd.ipynb index ce3f8c6..b28413b 100644 --- a/docs/source/auto_examples/plot_compute_emd.ipynb +++ b/docs/source/auto_examples/plot_compute_emd.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# 1D optimal transport\n\n\n\n" + "\n# Plot multiple EMD\n\n\nShows how to compute multiple EMD and Sinkhorn with two differnt \nground metrics and plot their values for diffeent distributions.\n\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,79 @@ "execution_count": null, "cell_type": "code", "source": [ - "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom ot.datasets import get_1D_gauss as gauss\n\n\n#%% parameters\n\nn = 100 # nb bins\nn_target = 50 # nb target distributions\n\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\nlst_m = np.linspace(20, 90, n_target)\n\n# Gaussian distributions\na = gauss(n, m=20, s=5) # m= mean, s= std\n\nB = np.zeros((n, n_target))\n\nfor i, m in enumerate(lst_m):\n B[:, i] = gauss(n, m=m, s=5)\n\n# loss matrix and normalization\nM = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'euclidean')\nM /= M.max()\nM2 = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'sqeuclidean')\nM2 /= M2.max()\n#%% plot the distributions\n\npl.figure(1)\npl.subplot(2, 1, 1)\npl.plot(x, a, 'b', label='Source distribution')\npl.title('Source distribution')\npl.subplot(2, 1, 2)\npl.plot(x, B, label='Target distributions')\npl.title('Target distributions')\npl.tight_layout()\n\n#%% Compute and plot distributions and loss matrix\n\nd_emd = ot.emd2(a, B, M) # direct computation of EMD\nd_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M3\n\n\npl.figure(2)\npl.plot(d_emd, label='Euclidean EMD')\npl.plot(d_emd2, label='Squared Euclidean EMD')\npl.title('EMD distances')\npl.legend()\n\n#%%\nreg = 1e-2\nd_sinkhorn = ot.sinkhorn2(a, B, M, reg)\nd_sinkhorn2 = ot.sinkhorn2(a, B, M2, reg)\n\npl.figure(2)\npl.clf()\npl.plot(d_emd, label='Euclidean EMD')\npl.plot(d_emd2, label='Squared Euclidean EMD')\npl.plot(d_sinkhorn, '+', label='Euclidean Sinkhorn')\npl.plot(d_sinkhorn2, '+', label='Squared Euclidean Sinkhorn')\npl.title('EMD distances')\npl.legend()\n\npl.show()" + "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom ot.datasets import get_1D_gauss as gauss" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Generate data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% parameters\n\nn = 100 # nb bins\nn_target = 50 # nb target distributions\n\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\nlst_m = np.linspace(20, 90, n_target)\n\n# Gaussian distributions\na = gauss(n, m=20, s=5) # m= mean, s= std\n\nB = np.zeros((n, n_target))\n\nfor i, m in enumerate(lst_m):\n B[:, i] = gauss(n, m=m, s=5)\n\n# loss matrix and normalization\nM = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'euclidean')\nM /= M.max()\nM2 = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'sqeuclidean')\nM2 /= M2.max()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Plot data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% plot the distributions\n\npl.figure(1)\npl.subplot(2, 1, 1)\npl.plot(x, a, 'b', label='Source distribution')\npl.title('Source distribution')\npl.subplot(2, 1, 2)\npl.plot(x, B, label='Target distributions')\npl.title('Target distributions')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Compute EMD for the different losses\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%% Compute and plot distributions and loss matrix\n\nd_emd = ot.emd2(a, B, M) # direct computation of EMD\nd_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M2\n\n\npl.figure(2)\npl.plot(d_emd, label='Euclidean EMD')\npl.plot(d_emd2, label='Squared Euclidean EMD')\npl.title('EMD distances')\npl.legend()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Compute Sinkhorn for the different losses\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "#%%\nreg = 1e-2\nd_sinkhorn = ot.sinkhorn2(a, B, M, reg)\nd_sinkhorn2 = ot.sinkhorn2(a, B, M2, reg)\n\npl.figure(2)\npl.clf()\npl.plot(d_emd, label='Euclidean EMD')\npl.plot(d_emd2, label='Squared Euclidean EMD')\npl.plot(d_sinkhorn, '+', label='Euclidean Sinkhorn')\npl.plot(d_sinkhorn2, '+', label='Squared Euclidean Sinkhorn')\npl.title('EMD distances')\npl.legend()\n\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_compute_emd.py b/docs/source/auto_examples/plot_compute_emd.py index 893eecf..b688f93 100644 --- a/docs/source/auto_examples/plot_compute_emd.py +++ b/docs/source/auto_examples/plot_compute_emd.py @@ -1,8 +1,12 @@ # -*- coding: utf-8 -*- """ -==================== -1D optimal transport -==================== +================= +Plot multiple EMD +================= + +Shows how to compute multiple EMD and Sinkhorn with two differnt +ground metrics and plot their values for diffeent distributions. + """ @@ -16,6 +20,10 @@ import ot from ot.datasets import get_1D_gauss as gauss +############################################################################## +# Generate data +############################################################################## + #%% parameters n = 100 # nb bins @@ -40,6 +48,11 @@ M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'euclidean') M /= M.max() M2 = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'sqeuclidean') M2 /= M2.max() + +############################################################################## +# Plot data +############################################################################## + #%% plot the distributions pl.figure(1) @@ -51,10 +64,15 @@ pl.plot(x, B, label='Target distributions') pl.title('Target distributions') pl.tight_layout() + +############################################################################## +# Compute EMD for the different losses +############################################################################## + #%% Compute and plot distributions and loss matrix d_emd = ot.emd2(a, B, M) # direct computation of EMD -d_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M3 +d_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M2 pl.figure(2) @@ -63,6 +81,10 @@ pl.plot(d_emd2, label='Squared Euclidean EMD') pl.title('EMD distances') pl.legend() +############################################################################## +# Compute Sinkhorn for the different losses +############################################################################## + #%% reg = 1e-2 d_sinkhorn = ot.sinkhorn2(a, B, M, reg) diff --git a/docs/source/auto_examples/plot_compute_emd.rst b/docs/source/auto_examples/plot_compute_emd.rst index f2e2005..b489255 100644 --- a/docs/source/auto_examples/plot_compute_emd.rst +++ b/docs/source/auto_examples/plot_compute_emd.rst @@ -3,42 +3,42 @@ .. _sphx_glr_auto_examples_plot_compute_emd.py: -==================== -1D optimal transport -==================== +================= +Plot multiple EMD +================= +Shows how to compute multiple EMD and Sinkhorn with two differnt +ground metrics and plot their values for diffeent distributions. -.. rst-class:: sphx-glr-horizontal +.. code-block:: python - * - .. image:: /auto_examples/images/sphx_glr_plot_compute_emd_001.png - :scale: 47 + # Author: Remi Flamary + # + # License: MIT License - * + import numpy as np + import matplotlib.pylab as pl + import ot + from ot.datasets import get_1D_gauss as gauss - .. image:: /auto_examples/images/sphx_glr_plot_compute_emd_002.png - :scale: 47 -.. code-block:: python - # Author: Remi Flamary - # - # License: MIT License +Generate data +############################################################################# - import numpy as np - import matplotlib.pylab as pl - import ot - from ot.datasets import get_1D_gauss as gauss + + +.. code-block:: python #%% parameters @@ -65,6 +65,21 @@ M /= M.max() M2 = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'sqeuclidean') M2 /= M2.max() + + + + + + + +Plot data +############################################################################# + + + +.. code-block:: python + + #%% plot the distributions pl.figure(1) @@ -76,10 +91,28 @@ pl.title('Target distributions') pl.tight_layout() + + + + +.. image:: /auto_examples/images/sphx_glr_plot_compute_emd_001.png + :align: center + + + + +Compute EMD for the different losses +############################################################################# + + + +.. code-block:: python + + #%% Compute and plot distributions and loss matrix d_emd = ot.emd2(a, B, M) # direct computation of EMD - d_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M3 + d_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M2 pl.figure(2) @@ -88,6 +121,23 @@ pl.title('EMD distances') pl.legend() + + + +.. image:: /auto_examples/images/sphx_glr_plot_compute_emd_003.png + :align: center + + + + +Compute Sinkhorn for the different losses +############################################################################# + + + +.. code-block:: python + + #%% reg = 1e-2 d_sinkhorn = ot.sinkhorn2(a, B, M, reg) @@ -104,7 +154,15 @@ pl.show() -**Total running time of the script:** ( 0 minutes 0.906 seconds) + + +.. image:: /auto_examples/images/sphx_glr_plot_compute_emd_004.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 0.427 seconds) @@ -123,4 +181,4 @@ .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_optim_OTreg.ipynb b/docs/source/auto_examples/plot_optim_OTreg.ipynb index 9d26e4d..290100f 100644 --- a/docs/source/auto_examples/plot_optim_OTreg.ipynb +++ b/docs/source/auto_examples/plot_optim_OTreg.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# Regularized OT with generic solver\n\n\n\n\n" + "\n# Regularized OT with generic solver\n\n\nIllustrates the use of the generic solver for regularized OT with\nuser-designed regularization term. It uses Conditional gradient as in [6] and \ngeneralized Conditional Gradient as proposed in [5][7].\n\n\n[5] N. Courty; R. Flamary; D. Tuia; A. Rakotomamonjy, Optimal Transport for \nDomain Adaptation, in IEEE Transactions on Pattern Analysis and Machine \nIntelligence , vol.PP, no.99, pp.1-1.\n\n[6] Ferradans, S., Papadakis, N., Peyr\u00e9, G., & Aujol, J. F. (2014). \nRegularized discrete optimal transport. SIAM Journal on Imaging Sciences, \n7(3), 1853-1882.\n\n[7] Rakotomamonjy, A., Flamary, R., & Courty, N. (2015). Generalized \nconditional gradient: analysis of convergence and applications. \narXiv preprint arXiv:1510.06567.\n\n\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -33,7 +33,7 @@ }, { "source": [ - "Generate data \n#############################################################################\n\n" + "Generate data\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Solve EMD \n#############################################################################\n\n" + "Solve EMD\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -114,7 +114,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "#%% Example with Frobenius norm + entropic regularization with gcg\n\ndef f(G):\n return 0.5 * np.sum(G**2)\n\n\ndef df(G):\n return G\n\n\nreg1 = 1e-3\nreg2 = 1e-1\n\nGel2 = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True)\n\npl.figure(5, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gel2, 'OT entropic + matrix Frob. reg')\npl.show()" + "#%% Example with Frobenius norm + entropic regularization with gcg\n\n\ndef f(G):\n return 0.5 * np.sum(G**2)\n\n\ndef df(G):\n return G\n\n\nreg1 = 1e-3\nreg2 = 1e-1\n\nGel2 = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True)\n\npl.figure(5, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gel2, 'OT entropic + matrix Frob. reg')\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_optim_OTreg.py b/docs/source/auto_examples/plot_optim_OTreg.py index d36b269..b362662 100644 --- a/docs/source/auto_examples/plot_optim_OTreg.py +++ b/docs/source/auto_examples/plot_optim_OTreg.py @@ -4,6 +4,24 @@ Regularized OT with generic solver ================================== +Illustrates the use of the generic solver for regularized OT with +user-designed regularization term. It uses Conditional gradient as in [6] and +generalized Conditional Gradient as proposed in [5][7]. + + +[5] N. Courty; R. Flamary; D. Tuia; A. Rakotomamonjy, Optimal Transport for +Domain Adaptation, in IEEE Transactions on Pattern Analysis and Machine +Intelligence , vol.PP, no.99, pp.1-1. + +[6] Ferradans, S., Papadakis, N., Peyré, G., & Aujol, J. F. (2014). +Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, +7(3), 1853-1882. + +[7] Rakotomamonjy, A., Flamary, R., & Courty, N. (2015). Generalized +conditional gradient: analysis of convergence and applications. +arXiv preprint arXiv:1510.06567. + + """ @@ -13,7 +31,7 @@ import ot ############################################################################## -# Generate data +# Generate data ############################################################################## #%% parameters @@ -32,7 +50,7 @@ M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) M /= M.max() ############################################################################## -# Solve EMD +# Solve EMD ############################################################################## #%% EMD @@ -92,6 +110,7 @@ ot.plot.plot1D_mat(a, b, Ge, 'OT matrix Entrop. reg') #%% Example with Frobenius norm + entropic regularization with gcg + def f(G): return 0.5 * np.sum(G**2) diff --git a/docs/source/auto_examples/plot_optim_OTreg.rst b/docs/source/auto_examples/plot_optim_OTreg.rst index 532d4ca..d444631 100644 --- a/docs/source/auto_examples/plot_optim_OTreg.rst +++ b/docs/source/auto_examples/plot_optim_OTreg.rst @@ -7,6 +7,24 @@ Regularized OT with generic solver ================================== +Illustrates the use of the generic solver for regularized OT with +user-designed regularization term. It uses Conditional gradient as in [6] and +generalized Conditional Gradient as proposed in [5][7]. + + +[5] N. Courty; R. Flamary; D. Tuia; A. Rakotomamonjy, Optimal Transport for +Domain Adaptation, in IEEE Transactions on Pattern Analysis and Machine +Intelligence , vol.PP, no.99, pp.1-1. + +[6] Ferradans, S., Papadakis, N., Peyré, G., & Aujol, J. F. (2014). +Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, +7(3), 1853-1882. + +[7] Rakotomamonjy, A., Flamary, R., & Courty, N. (2015). Generalized +conditional gradient: analysis of convergence and applications. +arXiv preprint arXiv:1510.06567. + + @@ -25,7 +43,7 @@ Regularized OT with generic solver -Generate data +Generate data ############################################################################# @@ -54,7 +72,7 @@ Generate data -Solve EMD +Solve EMD ############################################################################# @@ -612,6 +630,7 @@ Solve EMD with Frobenius norm + entropic regularization #%% Example with Frobenius norm + entropic regularization with gcg + def f(G): return 0.5 * np.sum(G**2) @@ -645,10 +664,10 @@ Solve EMD with Frobenius norm + entropic regularization 1|1.610121e-01|-5.152589e-02 2|1.609378e-01|-4.622297e-04 3|1.609284e-01|-5.830043e-05 - 4|1.609284e-01|-1.111580e-12 + 4|1.609284e-01|-1.111407e-12 -**Total running time of the script:** ( 0 minutes 2.719 seconds) +**Total running time of the script:** ( 0 minutes 1.867 seconds) @@ -667,4 +686,4 @@ Solve EMD with Frobenius norm + entropic regularization .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_classes.rst b/docs/source/auto_examples/plot_otda_classes.rst index 227a819..d1a13b1 100644 --- a/docs/source/auto_examples/plot_otda_classes.rst +++ b/docs/source/auto_examples/plot_otda_classes.rst @@ -94,29 +94,29 @@ Instantiate the different transport algorithms and fit them It. |Loss |Delta loss -------------------------------- - 0|9.456043e+00|0.000000e+00 - 1|2.059035e+00|-3.592463e+00 - 2|1.839814e+00|-1.191540e-01 - 3|1.787860e+00|-2.905942e-02 - 4|1.766582e+00|-1.204485e-02 - 5|1.760573e+00|-3.413038e-03 - 6|1.755288e+00|-3.010556e-03 - 7|1.749124e+00|-3.523968e-03 - 8|1.744159e+00|-2.846760e-03 - 9|1.741007e+00|-1.810862e-03 - 10|1.739839e+00|-6.710130e-04 - 11|1.737221e+00|-1.507260e-03 - 12|1.736011e+00|-6.970742e-04 - 13|1.734948e+00|-6.126425e-04 - 14|1.733901e+00|-6.038775e-04 - 15|1.733768e+00|-7.618542e-05 - 16|1.732821e+00|-5.467723e-04 - 17|1.732678e+00|-8.226843e-05 - 18|1.731934e+00|-4.300066e-04 - 19|1.731850e+00|-4.848002e-05 + 0|9.984935e+00|0.000000e+00 + 1|2.126803e+00|-3.694808e+00 + 2|1.867272e+00|-1.389895e-01 + 3|1.803858e+00|-3.515488e-02 + 4|1.783036e+00|-1.167761e-02 + 5|1.774823e+00|-4.627422e-03 + 6|1.771947e+00|-1.623526e-03 + 7|1.767564e+00|-2.479535e-03 + 8|1.763484e+00|-2.313667e-03 + 9|1.761138e+00|-1.331780e-03 + 10|1.758879e+00|-1.284576e-03 + 11|1.758034e+00|-4.806014e-04 + 12|1.757595e+00|-2.497155e-04 + 13|1.756749e+00|-4.818562e-04 + 14|1.755316e+00|-8.161432e-04 + 15|1.754988e+00|-1.866236e-04 + 16|1.754964e+00|-1.382474e-05 + 17|1.754032e+00|-5.315971e-04 + 18|1.753595e+00|-2.492359e-04 + 19|1.752900e+00|-3.961403e-04 It. |Loss |Delta loss -------------------------------- - 20|1.731699e+00|-8.729590e-05 + 20|1.752850e+00|-2.869262e-05 Fig 1 : plots source and target samples @@ -236,7 +236,7 @@ Fig 2 : plot optimal couplings and transported samples -**Total running time of the script:** ( 0 minutes 1.906 seconds) +**Total running time of the script:** ( 0 minutes 1.576 seconds) @@ -255,4 +255,4 @@ Fig 2 : plot optimal couplings and transported samples .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_color_images.ipynb b/docs/source/auto_examples/plot_otda_color_images.ipynb index c45c307..797b27d 100644 --- a/docs/source/auto_examples/plot_otda_color_images.ipynb +++ b/docs/source/auto_examples/plot_otda_color_images.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n========================================================\nOT for domain adaptation with image color adaptation [6]\n========================================================\n\nThis example presents a way of transferring colors between two image\nwith Optimal Transport as introduced in [6]\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014).\nRegularized discrete optimal transport.\nSIAM Journal on Imaging Sciences, 7(3), 1853-1882.\n\n" + "\n# OT for image color adaptation\n\n\nThis example presents a way of transferring colors between two image\nwith Optimal Transport as introduced in [6]\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014).\nRegularized discrete optimal transport.\nSIAM Journal on Imaging Sciences, 7(3), 1853-1882.\n\n" ], "cell_type": "markdown", "metadata": {} @@ -33,7 +33,7 @@ }, { "source": [ - "generate data\n#############################################################################\n\n" + "Generate data\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + "Plot original image\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -60,7 +60,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "# EMDTransport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\n\n# SinkhornTransport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\n\n# prediction between images (using out of sample prediction as in [6])\ntransp_Xs_emd = ot_emd.transform(Xs=X1)\ntransp_Xt_emd = ot_emd.inverse_transform(Xt=X2)\n\ntransp_Xs_sinkhorn = ot_emd.transform(Xs=X1)\ntransp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2)\n\nI1t = minmax(mat2im(transp_Xs_emd, I1.shape))\nI2t = minmax(mat2im(transp_Xt_emd, I2.shape))\n\nI1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape))\nI2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape))" + "pl.figure(1, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')" ], "outputs": [], "metadata": { @@ -69,7 +69,7 @@ }, { "source": [ - "plot original image\n#############################################################################\n\n" + "Scatter plot of colors\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -78,7 +78,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "pl.figure(1, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')" + "pl.figure(2, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 2], c=Xs)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 2], c=Xt)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\npl.tight_layout()" ], "outputs": [], "metadata": { @@ -87,7 +87,7 @@ }, { "source": [ - "scatter plot of colors\n#############################################################################\n\n" + "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -96,7 +96,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "pl.figure(2, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 2], c=Xs)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 2], c=Xt)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\npl.tight_layout()" + "# EMDTransport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\n\n# SinkhornTransport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\n\n# prediction between images (using out of sample prediction as in [6])\ntransp_Xs_emd = ot_emd.transform(Xs=X1)\ntransp_Xt_emd = ot_emd.inverse_transform(Xt=X2)\n\ntransp_Xs_sinkhorn = ot_emd.transform(Xs=X1)\ntransp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2)\n\nI1t = minmax(mat2im(transp_Xs_emd, I1.shape))\nI2t = minmax(mat2im(transp_Xt_emd, I2.shape))\n\nI1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape))\nI2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape))" ], "outputs": [], "metadata": { @@ -105,7 +105,7 @@ }, { "source": [ - "plot new images\n#############################################################################\n\n" + "Plot new images\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_otda_color_images.py b/docs/source/auto_examples/plot_otda_color_images.py index 46ad44b..f1df9d9 100644 --- a/docs/source/auto_examples/plot_otda_color_images.py +++ b/docs/source/auto_examples/plot_otda_color_images.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- """ -======================================================== -OT for domain adaptation with image color adaptation [6] -======================================================== +============================= +OT for image color adaptation +============================= This example presents a way of transferring colors between two image with Optimal Transport as introduced in [6] @@ -41,7 +41,7 @@ def minmax(I): ############################################################################## -# generate data +# Generate data ############################################################################## # Loading images @@ -61,33 +61,7 @@ Xt = X2[idx2, :] ############################################################################## -# Instantiate the different transport algorithms and fit them -############################################################################## - -# EMDTransport -ot_emd = ot.da.EMDTransport() -ot_emd.fit(Xs=Xs, Xt=Xt) - -# SinkhornTransport -ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) -ot_sinkhorn.fit(Xs=Xs, Xt=Xt) - -# prediction between images (using out of sample prediction as in [6]) -transp_Xs_emd = ot_emd.transform(Xs=X1) -transp_Xt_emd = ot_emd.inverse_transform(Xt=X2) - -transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) -transp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2) - -I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) -I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) - -I1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) -I2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape)) - - -############################################################################## -# plot original image +# Plot original image ############################################################################## pl.figure(1, figsize=(6.4, 3)) @@ -104,7 +78,7 @@ pl.title('Image 2') ############################################################################## -# scatter plot of colors +# Scatter plot of colors ############################################################################## pl.figure(2, figsize=(6.4, 3)) @@ -126,7 +100,33 @@ pl.tight_layout() ############################################################################## -# plot new images +# Instantiate the different transport algorithms and fit them +############################################################################## + +# EMDTransport +ot_emd = ot.da.EMDTransport() +ot_emd.fit(Xs=Xs, Xt=Xt) + +# SinkhornTransport +ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) +ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + +# prediction between images (using out of sample prediction as in [6]) +transp_Xs_emd = ot_emd.transform(Xs=X1) +transp_Xt_emd = ot_emd.inverse_transform(Xt=X2) + +transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) +transp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2) + +I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) +I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) + +I1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) +I2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape)) + + +############################################################################## +# Plot new images ############################################################################## pl.figure(3, figsize=(8, 4)) diff --git a/docs/source/auto_examples/plot_otda_color_images.rst b/docs/source/auto_examples/plot_otda_color_images.rst index e3989c8..88e93d2 100644 --- a/docs/source/auto_examples/plot_otda_color_images.rst +++ b/docs/source/auto_examples/plot_otda_color_images.rst @@ -3,9 +3,9 @@ .. _sphx_glr_auto_examples_plot_otda_color_images.py: -======================================================== -OT for domain adaptation with image color adaptation [6] -======================================================== +============================= +OT for image color adaptation +============================= This example presents a way of transferring colors between two image with Optimal Transport as introduced in [6] @@ -53,7 +53,7 @@ SIAM Journal on Imaging Sciences, 7(3), 1853-1882. -generate data +Generate data ############################################################################# @@ -83,43 +83,7 @@ generate data -Instantiate the different transport algorithms and fit them -############################################################################# - - - -.. code-block:: python - - - # EMDTransport - ot_emd = ot.da.EMDTransport() - ot_emd.fit(Xs=Xs, Xt=Xt) - - # SinkhornTransport - ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) - ot_sinkhorn.fit(Xs=Xs, Xt=Xt) - - # prediction between images (using out of sample prediction as in [6]) - transp_Xs_emd = ot_emd.transform(Xs=X1) - transp_Xt_emd = ot_emd.inverse_transform(Xt=X2) - - transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) - transp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2) - - I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) - I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) - - I1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) - I2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape)) - - - - - - - - -plot original image +Plot original image ############################################################################# @@ -149,7 +113,7 @@ plot original image -scatter plot of colors +Scatter plot of colors ############################################################################# @@ -184,7 +148,43 @@ scatter plot of colors -plot new images +Instantiate the different transport algorithms and fit them +############################################################################# + + + +.. code-block:: python + + + # EMDTransport + ot_emd = ot.da.EMDTransport() + ot_emd.fit(Xs=Xs, Xt=Xt) + + # SinkhornTransport + ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) + ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + + # prediction between images (using out of sample prediction as in [6]) + transp_Xs_emd = ot_emd.transform(Xs=X1) + transp_Xt_emd = ot_emd.inverse_transform(Xt=X2) + + transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) + transp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2) + + I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) + I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) + + I1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) + I2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape)) + + + + + + + + +Plot new images ############################################################################# @@ -235,7 +235,7 @@ plot new images -**Total running time of the script:** ( 3 minutes 16.043 seconds) +**Total running time of the script:** ( 2 minutes 28.053 seconds) @@ -254,4 +254,4 @@ plot new images .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_d2.rst b/docs/source/auto_examples/plot_otda_d2.rst index 20b76ba..3aa1149 100644 --- a/docs/source/auto_examples/plot_otda_d2.rst +++ b/docs/source/auto_examples/plot_otda_d2.rst @@ -243,7 +243,7 @@ Fig 3 : plot transported samples -**Total running time of the script:** ( 0 minutes 46.009 seconds) +**Total running time of the script:** ( 0 minutes 32.275 seconds) @@ -262,4 +262,4 @@ Fig 3 : plot transported samples .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_mapping.ipynb b/docs/source/auto_examples/plot_otda_mapping.ipynb index 0b5ca5c..5b3fd06 100644 --- a/docs/source/auto_examples/plot_otda_mapping.ipynb +++ b/docs/source/auto_examples/plot_otda_mapping.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n===============================================\nOT mapping estimation for domain adaptation [8]\n===============================================\n\nThis example presents how to use MappingTransport to estimate at the same\ntime both the coupling transport and approximate the transport map with either\na linear or a kernelized mapping as introduced in [8]\n\n[8] M. Perrot, N. Courty, R. Flamary, A. Habrard,\n \"Mapping estimation for discrete optimal transport\",\n Neural Information Processing Systems (NIPS), 2016.\n\n" + "\n# OT mapping estimation for domain adaptation\n\n\nThis example presents how to use MappingTransport to estimate at the same\ntime both the coupling transport and approximate the transport map with either\na linear or a kernelized mapping as introduced in [8].\n\n[8] M. Perrot, N. Courty, R. Flamary, A. Habrard,\n \"Mapping estimation for discrete optimal transport\",\n Neural Information Processing Systems (NIPS), 2016.\n\n" ], "cell_type": "markdown", "metadata": {} @@ -33,7 +33,7 @@ }, { "source": [ - "generate data\n#############################################################################\n\n" + "Generate data\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + "Plot data\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -60,7 +60,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "# MappingTransport with linear kernel\not_mapping_linear = ot.da.MappingTransport(\n kernel=\"linear\", mu=1e0, eta=1e-8, bias=True,\n max_iter=20, verbose=True)\n\not_mapping_linear.fit(Xs=Xs, Xt=Xt)\n\n# for original source samples, transform applies barycentric mapping\ntransp_Xs_linear = ot_mapping_linear.transform(Xs=Xs)\n\n# for out of source samples, transform applies the linear mapping\ntransp_Xs_linear_new = ot_mapping_linear.transform(Xs=Xs_new)\n\n\n# MappingTransport with gaussian kernel\not_mapping_gaussian = ot.da.MappingTransport(\n kernel=\"gaussian\", eta=1e-5, mu=1e-1, bias=True, sigma=1,\n max_iter=10, verbose=True)\not_mapping_gaussian.fit(Xs=Xs, Xt=Xt)\n\n# for original source samples, transform applies barycentric mapping\ntransp_Xs_gaussian = ot_mapping_gaussian.transform(Xs=Xs)\n\n# for out of source samples, transform applies the gaussian mapping\ntransp_Xs_gaussian_new = ot_mapping_gaussian.transform(Xs=Xs_new)" + "pl.figure(1, (10, 5))\npl.clf()\npl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples')\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples')\npl.legend(loc=0)\npl.title('Source and target distributions')" ], "outputs": [], "metadata": { @@ -69,7 +69,7 @@ }, { "source": [ - "plot data\n#############################################################################\n\n" + "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -78,7 +78,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "pl.figure(1, (10, 5))\npl.clf()\npl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples')\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples')\npl.legend(loc=0)\npl.title('Source and target distributions')" + "# MappingTransport with linear kernel\not_mapping_linear = ot.da.MappingTransport(\n kernel=\"linear\", mu=1e0, eta=1e-8, bias=True,\n max_iter=20, verbose=True)\n\not_mapping_linear.fit(Xs=Xs, Xt=Xt)\n\n# for original source samples, transform applies barycentric mapping\ntransp_Xs_linear = ot_mapping_linear.transform(Xs=Xs)\n\n# for out of source samples, transform applies the linear mapping\ntransp_Xs_linear_new = ot_mapping_linear.transform(Xs=Xs_new)\n\n\n# MappingTransport with gaussian kernel\not_mapping_gaussian = ot.da.MappingTransport(\n kernel=\"gaussian\", eta=1e-5, mu=1e-1, bias=True, sigma=1,\n max_iter=10, verbose=True)\not_mapping_gaussian.fit(Xs=Xs, Xt=Xt)\n\n# for original source samples, transform applies barycentric mapping\ntransp_Xs_gaussian = ot_mapping_gaussian.transform(Xs=Xs)\n\n# for out of source samples, transform applies the gaussian mapping\ntransp_Xs_gaussian_new = ot_mapping_gaussian.transform(Xs=Xs_new)" ], "outputs": [], "metadata": { @@ -87,7 +87,7 @@ }, { "source": [ - "plot transported samples\n#############################################################################\n\n" + "Plot transported samples\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_otda_mapping.py b/docs/source/auto_examples/plot_otda_mapping.py index 09d2cb4..e78fef4 100644 --- a/docs/source/auto_examples/plot_otda_mapping.py +++ b/docs/source/auto_examples/plot_otda_mapping.py @@ -1,12 +1,12 @@ # -*- coding: utf-8 -*- """ -=============================================== -OT mapping estimation for domain adaptation [8] -=============================================== +=========================================== +OT mapping estimation for domain adaptation +=========================================== This example presents how to use MappingTransport to estimate at the same time both the coupling transport and approximate the transport map with either -a linear or a kernelized mapping as introduced in [8] +a linear or a kernelized mapping as introduced in [8]. [8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for discrete optimal transport", @@ -24,7 +24,7 @@ import ot ############################################################################## -# generate data +# Generate data ############################################################################## n_source_samples = 100 @@ -43,6 +43,17 @@ Xt, yt = ot.datasets.get_data_classif( Xt[yt == 2] *= 3 Xt = Xt + 4 +############################################################################## +# Plot data +############################################################################## + +pl.figure(1, (10, 5)) +pl.clf() +pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples') +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') +pl.legend(loc=0) +pl.title('Source and target distributions') + ############################################################################## # Instantiate the different transport algorithms and fit them @@ -76,19 +87,7 @@ transp_Xs_gaussian_new = ot_mapping_gaussian.transform(Xs=Xs_new) ############################################################################## -# plot data -############################################################################## - -pl.figure(1, (10, 5)) -pl.clf() -pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples') -pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') -pl.legend(loc=0) -pl.title('Source and target distributions') - - -############################################################################## -# plot transported samples +# Plot transported samples ############################################################################## pl.figure(2) diff --git a/docs/source/auto_examples/plot_otda_mapping.rst b/docs/source/auto_examples/plot_otda_mapping.rst index 088da31..ddc1ee9 100644 --- a/docs/source/auto_examples/plot_otda_mapping.rst +++ b/docs/source/auto_examples/plot_otda_mapping.rst @@ -3,13 +3,13 @@ .. _sphx_glr_auto_examples_plot_otda_mapping.py: -=============================================== -OT mapping estimation for domain adaptation [8] -=============================================== +=========================================== +OT mapping estimation for domain adaptation +=========================================== This example presents how to use MappingTransport to estimate at the same time both the coupling transport and approximate the transport map with either -a linear or a kernelized mapping as introduced in [8] +a linear or a kernelized mapping as introduced in [8]. [8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for discrete optimal transport", @@ -36,7 +36,7 @@ a linear or a kernelized mapping as introduced in [8] -generate data +Generate data ############################################################################# @@ -66,6 +66,30 @@ generate data +Plot data +############################################################################# + + + +.. code-block:: python + + + pl.figure(1, (10, 5)) + pl.clf() + pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples') + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') + pl.legend(loc=0) + pl.title('Source and target distributions') + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_mapping_001.png + :align: center + + + Instantiate the different transport algorithms and fit them ############################################################################# @@ -112,54 +136,29 @@ Instantiate the different transport algorithms and fit them It. |Loss |Delta loss -------------------------------- - 0|4.273804e+03|0.000000e+00 - 1|4.264510e+03|-2.174580e-03 - 2|4.264209e+03|-7.047095e-05 - 3|4.264078e+03|-3.069822e-05 - 4|4.264018e+03|-1.412924e-05 - 5|4.263961e+03|-1.341165e-05 - 6|4.263946e+03|-3.586522e-06 + 0|4.481482e+03|0.000000e+00 + 1|4.469389e+03|-2.698549e-03 + 2|4.468825e+03|-1.261217e-04 + 3|4.468580e+03|-5.486064e-05 + 4|4.468438e+03|-3.161220e-05 + 5|4.468352e+03|-1.930800e-05 + 6|4.468309e+03|-9.570658e-06 It. |Loss |Delta loss -------------------------------- - 0|4.294523e+02|0.000000e+00 - 1|4.247737e+02|-1.089443e-02 - 2|4.245516e+02|-5.228765e-04 - 3|4.244430e+02|-2.557417e-04 - 4|4.243724e+02|-1.663904e-04 - 5|4.243196e+02|-1.244111e-04 - 6|4.242808e+02|-9.132500e-05 - 7|4.242497e+02|-7.331710e-05 - 8|4.242271e+02|-5.326612e-05 - 9|4.242063e+02|-4.916026e-05 - 10|4.241906e+02|-3.699617e-05 - - -plot data -############################################################################# - - - -.. code-block:: python - - - pl.figure(1, (10, 5)) - pl.clf() - pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples') - pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') - pl.legend(loc=0) - pl.title('Source and target distributions') - - - - - -.. image:: /auto_examples/images/sphx_glr_plot_otda_mapping_001.png - :align: center - - - - -plot transported samples + 0|4.504654e+02|0.000000e+00 + 1|4.461571e+02|-9.564116e-03 + 2|4.459105e+02|-5.528043e-04 + 3|4.457895e+02|-2.712398e-04 + 4|4.457041e+02|-1.914829e-04 + 5|4.456431e+02|-1.369704e-04 + 6|4.456032e+02|-8.944784e-05 + 7|4.455700e+02|-7.447824e-05 + 8|4.455447e+02|-5.688965e-05 + 9|4.455229e+02|-4.890051e-05 + 10|4.455084e+02|-3.262490e-05 + + +Plot transported samples ############################################################################# @@ -209,7 +208,7 @@ plot transported samples -**Total running time of the script:** ( 0 minutes 0.853 seconds) +**Total running time of the script:** ( 0 minutes 0.869 seconds) @@ -228,4 +227,4 @@ plot transported samples .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb index 4b2ec02..c8c1d95 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n====================================================================================\nOT for domain adaptation with image color adaptation [6] with mapping estimation [8]\n====================================================================================\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized\n discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3),\n 1853-1882.\n[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, \"Mapping estimation for\n discrete optimal transport\", Neural Information Processing Systems (NIPS),\n 2016.\n\n\n" + "\n# OT for image color adaptation with mapping estimation \n\n\nOT for domain adaptation with image color adaptation [6] with mapping \nestimation [8].\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized\n discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3),\n 1853-1882.\n[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, \"Mapping estimation for\n discrete optimal transport\", Neural Information Processing Systems (NIPS),\n 2016.\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "plot original images\n#############################################################################\n\n" + "Plot original images\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "plot pixel values distribution\n#############################################################################\n\n" + "Plot pixel values distribution\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} @@ -105,7 +105,7 @@ }, { "source": [ - "plot transformed images\n#############################################################################\n\n" + "Plot transformed images\n#############################################################################\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.py b/docs/source/auto_examples/plot_otda_mapping_colors_images.py index 936206c..162c24b 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.py +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.py @@ -1,8 +1,11 @@ # -*- coding: utf-8 -*- """ -==================================================================================== -OT for domain adaptation with image color adaptation [6] with mapping estimation [8] -==================================================================================== +===================================================== +OT for image color adaptation with mapping estimation +===================================================== + +OT for domain adaptation with image color adaptation [6] with mapping +estimation [8]. [6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), @@ -93,7 +96,7 @@ Image_mapping_gaussian = minmax(mat2im(X1tn, I1.shape)) ############################################################################## -# plot original images +# Plot original images ############################################################################## pl.figure(1, figsize=(6.4, 3)) @@ -110,7 +113,7 @@ pl.tight_layout() ############################################################################## -# plot pixel values distribution +# Plot pixel values distribution ############################################################################## pl.figure(2, figsize=(6.4, 5)) @@ -132,7 +135,7 @@ pl.tight_layout() ############################################################################## -# plot transformed images +# Plot transformed images ############################################################################## pl.figure(2, figsize=(10, 5)) diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.rst b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst index 1107067..29823f1 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.rst +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst @@ -3,9 +3,12 @@ .. _sphx_glr_auto_examples_plot_otda_mapping_colors_images.py: -==================================================================================== -OT for domain adaptation with image color adaptation [6] with mapping estimation [8] -==================================================================================== +===================================================== +OT for image color adaptation with mapping estimation +===================================================== + +OT for domain adaptation with image color adaptation [6] with mapping +estimation [8]. [6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), @@ -129,42 +132,42 @@ Domain adaptation for pixel distribution transfer It. |Loss |Delta loss -------------------------------- - 0|3.680514e+02|0.000000e+00 - 1|3.592359e+02|-2.395185e-02 - 2|3.590581e+02|-4.947749e-04 - 3|3.589663e+02|-2.556471e-04 - 4|3.589095e+02|-1.582289e-04 - 5|3.588707e+02|-1.081994e-04 - 6|3.588423e+02|-7.911661e-05 - 7|3.588206e+02|-6.055473e-05 - 8|3.588034e+02|-4.778202e-05 - 9|3.587895e+02|-3.886420e-05 - 10|3.587781e+02|-3.182249e-05 - 11|3.587684e+02|-2.695669e-05 - 12|3.587602e+02|-2.298642e-05 - 13|3.587530e+02|-1.993240e-05 - 14|3.587468e+02|-1.736014e-05 - 15|3.587413e+02|-1.518037e-05 - 16|3.587365e+02|-1.358038e-05 - 17|3.587321e+02|-1.215346e-05 - 18|3.587282e+02|-1.091639e-05 - 19|3.587278e+02|-9.877929e-07 + 0|3.680512e+02|0.000000e+00 + 1|3.592454e+02|-2.392562e-02 + 2|3.590671e+02|-4.960473e-04 + 3|3.589736e+02|-2.604894e-04 + 4|3.589161e+02|-1.602816e-04 + 5|3.588766e+02|-1.099971e-04 + 6|3.588476e+02|-8.084400e-05 + 7|3.588256e+02|-6.131161e-05 + 8|3.588083e+02|-4.807549e-05 + 9|3.587943e+02|-3.899414e-05 + 10|3.587827e+02|-3.245280e-05 + 11|3.587729e+02|-2.721256e-05 + 12|3.587646e+02|-2.316249e-05 + 13|3.587574e+02|-2.000192e-05 + 14|3.587512e+02|-1.748898e-05 + 15|3.587457e+02|-1.535131e-05 + 16|3.587408e+02|-1.366515e-05 + 17|3.587364e+02|-1.210563e-05 + 18|3.587325e+02|-1.097138e-05 + 19|3.587310e+02|-4.099596e-06 It. |Loss |Delta loss -------------------------------- - 0|3.784725e+02|0.000000e+00 - 1|3.646380e+02|-3.655332e-02 - 2|3.642858e+02|-9.660434e-04 - 3|3.641516e+02|-3.683776e-04 - 4|3.640785e+02|-2.008220e-04 - 5|3.640320e+02|-1.276966e-04 - 6|3.639999e+02|-8.796173e-05 - 7|3.639764e+02|-6.455658e-05 - 8|3.639583e+02|-4.976436e-05 - 9|3.639440e+02|-3.946556e-05 - 10|3.639322e+02|-3.222132e-05 - - -plot original images + 0|3.784805e+02|0.000000e+00 + 1|3.646476e+02|-3.654847e-02 + 2|3.642970e+02|-9.615381e-04 + 3|3.641622e+02|-3.699897e-04 + 4|3.640886e+02|-2.021154e-04 + 5|3.640419e+02|-1.280913e-04 + 6|3.640096e+02|-8.898145e-05 + 7|3.639858e+02|-6.514301e-05 + 8|3.639677e+02|-4.977195e-05 + 9|3.639534e+02|-3.936050e-05 + 10|3.639417e+02|-3.205223e-05 + + +Plot original images ############################################################################# @@ -194,7 +197,7 @@ plot original images -plot pixel values distribution +Plot pixel values distribution ############################################################################# @@ -229,7 +232,7 @@ plot pixel values distribution -plot transformed images +Plot transformed images ############################################################################# @@ -280,7 +283,7 @@ plot transformed images -**Total running time of the script:** ( 2 minutes 45.618 seconds) +**Total running time of the script:** ( 2 minutes 12.535 seconds) @@ -299,4 +302,4 @@ plot transformed images .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/conf.py b/docs/source/conf.py index ffdb1a2..0a822e5 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -33,7 +33,7 @@ class Mock(MagicMock): return MagicMock() MOCK_MODULES = ['ot.lp.emd_wrap','autograd','pymanopt','cudamat','autograd.numpy','pymanopt.manifolds','pymanopt.solvers'] # 'autograd.numpy','pymanopt.manifolds','pymanopt.solvers', -sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES) +##sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES) # !!!! # If extensions (or modules to document with autodoc) are in another directory, @@ -328,7 +328,7 @@ intersphinx_mapping = {'https://docs.python.org/': None} sphinx_gallery_conf = { 'examples_dirs': ['../../examples','../../examples/da'], 'gallery_dirs': 'auto_examples', - 'mod_example_dir': '../modules/generated/', + 'backreferences_dir': '../modules/generated/', 'reference_url': { 'numpy': 'http://docs.scipy.org/doc/numpy-1.9.1', 'scipy': 'http://docs.scipy.org/doc/scipy-0.17.0/reference'} diff --git a/docs/source/examples.rst b/docs/source/examples.rst deleted file mode 100644 index f209543..0000000 --- a/docs/source/examples.rst +++ /dev/null @@ -1,39 +0,0 @@ - - -Examples -============ - -1D Optimal transport ---------------------- - -.. literalinclude:: ../../examples/demo_OT_1D.py - -2D Optimal transport on empirical distributions ------------------------------------------------ - -.. literalinclude:: ../../examples/demo_OT_2D_samples.py - -1D Wasserstein barycenter -------------------------- - -.. literalinclude:: ../../examples/demo_barycenter_1D.py - -OT with user provided regularization ------------------------------------- - -.. literalinclude:: ../../examples/demo_optim_OTreg.py - -Domain adaptation with optimal transport ----------------------------------------- - -.. literalinclude:: ../../examples/demo_OTDA_classes.py - -Color transfer in images ------------------------- - -.. literalinclude:: ../../examples/demo_OTDA_color_images.py - -OT mapping estimation for domain adaptation -------------------------------------------- - -.. literalinclude:: ../../examples/demo_OTDA_mapping.py diff --git a/examples/README.txt b/examples/README.txt index f8643b8..c3d556d 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -1,2 +1,4 @@ POT Examples ============ + +This is a gallery of all the POT example files. diff --git a/examples/plot_OT_1D.py b/examples/plot_OT_1D.py index a1473c4..a63f29a 100644 --- a/examples/plot_OT_1D.py +++ b/examples/plot_OT_1D.py @@ -4,7 +4,7 @@ 1D optimal transport ==================== -This example illustrate the computation of EMD and Sinkhorn transport plans +This example illustrates the computation of EMD and Sinkhorn transport plans and their visualization. """ diff --git a/examples/plot_OT_2D_samples.py b/examples/plot_OT_2D_samples.py index a913b8c..f57d631 100644 --- a/examples/plot_OT_2D_samples.py +++ b/examples/plot_OT_2D_samples.py @@ -4,6 +4,9 @@ 2D Optimal transport between empirical distributions ==================================================== +Illustration of 2D optimal transport between discributions that are weighted +sum of diracs. The OT matrix is plotted with the samples. + """ # Author: Remi Flamary diff --git a/examples/plot_OT_L1_vs_L2.py b/examples/plot_OT_L1_vs_L2.py index dfc9462..77bde22 100644 --- a/examples/plot_OT_L1_vs_L2.py +++ b/examples/plot_OT_L1_vs_L2.py @@ -4,6 +4,8 @@ 2D Optimal transport for different metrics ========================================== +2D OT on empirical distributio with different gound metric. + Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf @@ -18,98 +20,190 @@ import numpy as np import matplotlib.pylab as pl import ot -#%% parameters and data generation - -for data in range(2): - - if data: - n = 20 # nb samples - xs = np.zeros((n, 2)) - xs[:, 0] = np.arange(n) + 1 - xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... - - xt = np.zeros((n, 2)) - xt[:, 1] = np.arange(n) + 1 - else: - - n = 50 # nb samples - xtot = np.zeros((n + 1, 2)) - xtot[:, 0] = np.cos( - (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) - xtot[:, 1] = np.sin( - (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) - - xs = xtot[:n, :] - xt = xtot[1:, :] - - a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples - - # loss matrix - M1 = ot.dist(xs, xt, metric='euclidean') - M1 /= M1.max() - - # loss matrix - M2 = ot.dist(xs, xt, metric='sqeuclidean') - M2 /= M2.max() - - # loss matrix - Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) - Mp /= Mp.max() - - #%% plot samples - - pl.figure(1 + 3 * data, figsize=(7, 3)) - pl.clf() - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - pl.title('Source and traget distributions') - - pl.figure(2 + 3 * data, figsize=(7, 3)) - - pl.subplot(1, 3, 1) - pl.imshow(M1, interpolation='nearest') - pl.title('Euclidean cost') - - pl.subplot(1, 3, 2) - pl.imshow(M2, interpolation='nearest') - pl.title('Squared Euclidean cost') - - pl.subplot(1, 3, 3) - pl.imshow(Mp, interpolation='nearest') - pl.title('Sqrt Euclidean cost') - pl.tight_layout() - - #%% EMD - G1 = ot.emd(a, b, M1) - G2 = ot.emd(a, b, M2) - Gp = ot.emd(a, b, Mp) - - pl.figure(3 + 3 * data, figsize=(7, 3)) - - pl.subplot(1, 3, 1) - ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - # pl.legend(loc=0) - pl.title('OT Euclidean') - - pl.subplot(1, 3, 2) - ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - # pl.legend(loc=0) - pl.title('OT squared Euclidean') - - pl.subplot(1, 3, 3) - ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - # pl.legend(loc=0) - pl.title('OT sqrt Euclidean') - pl.tight_layout() +############################################################################## +# Dataset 1 : uniform sampling +############################################################################## + +n = 20 # nb samples +xs = np.zeros((n, 2)) +xs[:, 0] = np.arange(n) + 1 +xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... + +xt = np.zeros((n, 2)) +xt[:, 1] = np.arange(n) + 1 + +a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + +# loss matrix +M1 = ot.dist(xs, xt, metric='euclidean') +M1 /= M1.max() + +# loss matrix +M2 = ot.dist(xs, xt, metric='sqeuclidean') +M2 /= M2.max() + +# loss matrix +Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) +Mp /= Mp.max() + +# Data +pl.figure(1, figsize=(7, 3)) +pl.clf() +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +pl.title('Source and traget distributions') + + +# Cost matrices +pl.figure(2, figsize=(7, 3)) + +pl.subplot(1, 3, 1) +pl.imshow(M1, interpolation='nearest') +pl.title('Euclidean cost') + +pl.subplot(1, 3, 2) +pl.imshow(M2, interpolation='nearest') +pl.title('Squared Euclidean cost') + +pl.subplot(1, 3, 3) +pl.imshow(Mp, interpolation='nearest') +pl.title('Sqrt Euclidean cost') +pl.tight_layout() + +############################################################################## +# Dataset 1 : Plot OT Matrices +############################################################################## + + + +#%% EMD +G1 = ot.emd(a, b, M1) +G2 = ot.emd(a, b, M2) +Gp = ot.emd(a, b, Mp) + +# OT matrices +pl.figure(3, figsize=(7, 3)) + +pl.subplot(1, 3, 1) +ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT Euclidean') + +pl.subplot(1, 3, 2) +ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT squared Euclidean') + +pl.subplot(1, 3, 3) +ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT sqrt Euclidean') +pl.tight_layout() + +pl.show() + + +############################################################################## +# Dataset 2 : Partial circle +############################################################################## + +n = 50 # nb samples +xtot = np.zeros((n + 1, 2)) +xtot[:, 0] = np.cos( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) +xtot[:, 1] = np.sin( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + +xs = xtot[:n, :] +xt = xtot[1:, :] + +a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + +# loss matrix +M1 = ot.dist(xs, xt, metric='euclidean') +M1 /= M1.max() + +# loss matrix +M2 = ot.dist(xs, xt, metric='sqeuclidean') +M2 /= M2.max() + +# loss matrix +Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) +Mp /= Mp.max() + + +# Data +pl.figure(4, figsize=(7, 3)) +pl.clf() +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +pl.title('Source and traget distributions') + + +# Cost matrices +pl.figure(5, figsize=(7, 3)) + +pl.subplot(1, 3, 1) +pl.imshow(M1, interpolation='nearest') +pl.title('Euclidean cost') + +pl.subplot(1, 3, 2) +pl.imshow(M2, interpolation='nearest') +pl.title('Squared Euclidean cost') + +pl.subplot(1, 3, 3) +pl.imshow(Mp, interpolation='nearest') +pl.title('Sqrt Euclidean cost') +pl.tight_layout() + +############################################################################## +# Dataset 2 : Plot OT Matrices +############################################################################## + + + +#%% EMD +G1 = ot.emd(a, b, M1) +G2 = ot.emd(a, b, M2) +Gp = ot.emd(a, b, Mp) + +# OT matrices +pl.figure(6, figsize=(7, 3)) + +pl.subplot(1, 3, 1) +ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT Euclidean') + +pl.subplot(1, 3, 2) +ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT squared Euclidean') + +pl.subplot(1, 3, 3) +ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.axis('equal') +# pl.legend(loc=0) +pl.title('OT sqrt Euclidean') +pl.tight_layout() pl.show() diff --git a/examples/plot_barycenter_1D.py b/examples/plot_barycenter_1D.py index f3be247..142b05e 100644 --- a/examples/plot_barycenter_1D.py +++ b/examples/plot_barycenter_1D.py @@ -4,7 +4,7 @@ 1D Wasserstein barycenter demo ============================== -This example illustrate the computation of regularized Wassersyein Barycenter +This example illustrates the computation of regularized Wassersyein Barycenter as proposed in [3]. @@ -25,6 +25,9 @@ import ot from mpl_toolkits.mplot3d import Axes3D # noqa from matplotlib.collections import PolyCollection +############################################################################## +# Generate data +############################################################################## #%% parameters @@ -45,6 +48,10 @@ n_distributions = A.shape[1] M = ot.utils.dist0(n) M /= M.max() +############################################################################## +# Plot data +############################################################################## + #%% plot the distributions pl.figure(1, figsize=(6.4, 3)) @@ -53,6 +60,10 @@ for i in range(n_distributions): pl.title('Distributions') pl.tight_layout() +############################################################################## +# Barycenter computation +############################################################################## + #%% barycenter computation alpha = 0.2 # 0<=alpha<=1 @@ -79,6 +90,10 @@ pl.legend() pl.title('Barycenters') pl.tight_layout() +############################################################################## +# Barycentric interpolation +############################################################################## + #%% barycenter interpolation n_alpha = 11 diff --git a/examples/plot_compute_emd.py b/examples/plot_compute_emd.py index 704da0e..b688f93 100644 --- a/examples/plot_compute_emd.py +++ b/examples/plot_compute_emd.py @@ -4,6 +4,10 @@ Plot multiple EMD ================= +Shows how to compute multiple EMD and Sinkhorn with two differnt +ground metrics and plot their values for diffeent distributions. + + """ # Author: Remi Flamary diff --git a/examples/plot_optim_OTreg.py b/examples/plot_optim_OTreg.py index 95bcdaf..b362662 100644 --- a/examples/plot_optim_OTreg.py +++ b/examples/plot_optim_OTreg.py @@ -4,8 +4,8 @@ Regularized OT with generic solver ================================== -This example illustrate the use of the generic solver for regularized OT with -user designed regularization term. It uses Conditional gradient as in [6] and +Illustrates the use of the generic solver for regularized OT with +user-designed regularization term. It uses Conditional gradient as in [6] and generalized Conditional Gradient as proposed in [5][7]. diff --git a/examples/plot_otda_mapping.py b/examples/plot_otda_mapping.py index e0da2d8..e78fef4 100644 --- a/examples/plot_otda_mapping.py +++ b/examples/plot_otda_mapping.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- """ -=============================================== -OT mapping estimation for domain adaptation [8] -=============================================== +=========================================== +OT mapping estimation for domain adaptation +=========================================== This example presents how to use MappingTransport to estimate at the same time both the coupling transport and approximate the transport map with either @@ -24,7 +24,7 @@ import ot ############################################################################## -# generate data +# Generate data ############################################################################## n_source_samples = 100 @@ -44,7 +44,7 @@ Xt[yt == 2] *= 3 Xt = Xt + 4 ############################################################################## -# plot data +# Plot data ############################################################################## pl.figure(1, (10, 5)) diff --git a/examples/plot_otda_mapping_colors_images.py b/examples/plot_otda_mapping_colors_images.py index a8b2ca8..162c24b 100644 --- a/examples/plot_otda_mapping_colors_images.py +++ b/examples/plot_otda_mapping_colors_images.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- """ -=============================================== -OT for color adaptation with mapping estimation -=============================================== +===================================================== +OT for image color adaptation with mapping estimation +===================================================== OT for domain adaptation with image color adaptation [6] with mapping estimation [8]. -- cgit v1.2.3 From 8ea3504c3bfe9c9440982f8ad0d172a240d54aff Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Fri, 1 Sep 2017 17:55:32 +0200 Subject: good notebook format generated by sphinx gallery --- docs/nb_build | 3 +- .../source/auto_examples/auto_examples_jupyter.zip | Bin 70289 -> 67774 bytes docs/source/auto_examples/auto_examples_python.zip | Bin 46532 -> 44112 bytes .../images/sphx_glr_plot_OT_2D_samples_001.png | Bin 20707 -> 22153 bytes .../images/sphx_glr_plot_OT_2D_samples_002.png | Bin 21335 -> 21589 bytes .../images/sphx_glr_plot_OT_2D_samples_005.png | Bin 9613 -> 9645 bytes .../images/sphx_glr_plot_OT_2D_samples_006.png | Bin 83657 -> 91095 bytes .../images/sphx_glr_plot_OT_2D_samples_009.png | Bin 14178 -> 13987 bytes .../images/sphx_glr_plot_OT_2D_samples_010.png | Bin 98951 -> 109742 bytes .../auto_examples/images/sphx_glr_plot_WDA_001.png | Bin 55744 -> 54285 bytes .../auto_examples/images/sphx_glr_plot_WDA_003.png | Bin 86112 -> 86366 bytes .../images/sphx_glr_plot_otda_classes_001.png | Bin 49949 -> 51418 bytes .../images/sphx_glr_plot_otda_classes_003.png | Bin 189153 -> 199721 bytes .../images/sphx_glr_plot_otda_d2_001.png | Bin 131873 -> 135206 bytes .../images/sphx_glr_plot_otda_d2_003.png | Bin 240262 -> 241976 bytes .../images/sphx_glr_plot_otda_d2_006.png | Bin 104502 -> 108946 bytes .../images/sphx_glr_plot_otda_mapping_001.png | Bin 37940 -> 35881 bytes .../images/sphx_glr_plot_otda_mapping_003.png | Bin 76017 -> 73815 bytes .../thumb/sphx_glr_plot_OT_2D_samples_thumb.png | Bin 22370 -> 24711 bytes .../images/thumb/sphx_glr_plot_WDA_thumb.png | Bin 85660 -> 87479 bytes .../thumb/sphx_glr_plot_otda_classes_thumb.png | Bin 29948 -> 30678 bytes .../images/thumb/sphx_glr_plot_otda_d2_thumb.png | Bin 54746 -> 53014 bytes .../thumb/sphx_glr_plot_otda_mapping_thumb.png | Bin 19281 -> 18473 bytes docs/source/auto_examples/plot_OT_1D.ipynb | 6 +- docs/source/auto_examples/plot_OT_1D.py | 8 +- docs/source/auto_examples/plot_OT_1D.rst | 10 ++- docs/source/auto_examples/plot_OT_2D_samples.ipynb | 8 +- docs/source/auto_examples/plot_OT_2D_samples.py | 8 +- docs/source/auto_examples/plot_OT_2D_samples.rst | 10 +-- docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb | 8 +- docs/source/auto_examples/plot_OT_L1_vs_L2.py | 8 +- docs/source/auto_examples/plot_OT_L1_vs_L2.rst | 10 +-- docs/source/auto_examples/plot_WDA.ipynb | 10 +-- docs/source/auto_examples/plot_WDA.py | 10 +-- docs/source/auto_examples/plot_WDA.rst | 82 ++++++--------------- docs/source/auto_examples/plot_barycenter_1D.ipynb | 8 +- docs/source/auto_examples/plot_barycenter_1D.py | 8 +- docs/source/auto_examples/plot_barycenter_1D.rst | 10 +-- docs/source/auto_examples/plot_compute_emd.ipynb | 8 +- docs/source/auto_examples/plot_compute_emd.py | 8 +- docs/source/auto_examples/plot_compute_emd.rst | 10 +-- docs/source/auto_examples/plot_optim_OTreg.ipynb | 10 +-- docs/source/auto_examples/plot_optim_OTreg.py | 10 +-- docs/source/auto_examples/plot_optim_OTreg.rst | 12 +-- docs/source/auto_examples/plot_otda_classes.ipynb | 8 +- docs/source/auto_examples/plot_otda_classes.py | 10 +-- docs/source/auto_examples/plot_otda_classes.rst | 54 +++++++------- .../auto_examples/plot_otda_color_images.ipynb | 10 +-- .../source/auto_examples/plot_otda_color_images.py | 10 +-- .../auto_examples/plot_otda_color_images.rst | 12 +-- docs/source/auto_examples/plot_otda_d2.ipynb | 12 +-- docs/source/auto_examples/plot_otda_d2.py | 17 ++--- docs/source/auto_examples/plot_otda_d2.rst | 19 +++-- docs/source/auto_examples/plot_otda_mapping.ipynb | 8 +- docs/source/auto_examples/plot_otda_mapping.py | 8 +- docs/source/auto_examples/plot_otda_mapping.rst | 44 ++++++----- .../plot_otda_mapping_colors_images.ipynb | 10 +-- .../plot_otda_mapping_colors_images.py | 10 +-- .../plot_otda_mapping_colors_images.rst | 12 +-- docs/source/conf.py | 2 +- 60 files changed, 235 insertions(+), 276 deletions(-) (limited to 'docs/source/auto_examples/plot_otda_color_images.py') diff --git a/docs/nb_build b/docs/nb_build index 979a913..7f74c70 100755 --- a/docs/nb_build +++ b/docs/nb_build @@ -11,5 +11,4 @@ make html sed -i "s/'sphinx\_gallery/#'sphinx\_gallery/" source/conf.py sed -i "s/#sys.modules.update/sys.modules.update/" source/conf.py - - +#rsync --out-format="%n" --update source/auto_examples/*.ipynb ../notebooks2 diff --git a/docs/source/auto_examples/auto_examples_jupyter.zip b/docs/source/auto_examples/auto_examples_jupyter.zip index f7698ad..fc1d4de 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 6a8e449..92adf10 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_2D_samples_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png index 172d736..ba50e23 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png index 3043a72..19978ff 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png index 5565d75..aed13b2 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png index 06d1020..8ea40f1 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_009.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_009.png index 8c16aea..404e9d8 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_009.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_009.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_010.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_010.png index ccfe1e0..56b79cf 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_010.png and b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_010.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png b/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png index 9048051..f724332 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_WDA_003.png b/docs/source/auto_examples/images/sphx_glr_plot_WDA_003.png index fcf13c6..b231020 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_WDA_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_WDA_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png index a28f245..bedc950 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png index 4d0b12d..8e3ccad 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png index 9e78aed..3d6a740 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png index d37359b..aa16585 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png index c71284a..5dc3ba7 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png index d2ee139..4239465 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png index fa1ab81..620105e 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png index 1f42900..dbb5cfd 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png index 8db78a1..f55490c 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png index a2571a5..a72fe37 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png index 6c8f37f..cddf768 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png index a042411..959cc44 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png differ diff --git a/docs/source/auto_examples/plot_OT_1D.ipynb b/docs/source/auto_examples/plot_OT_1D.ipynb index c8925ac..26748c2 100644 --- a/docs/source/auto_examples/plot_OT_1D.ipynb +++ b/docs/source/auto_examples/plot_OT_1D.ipynb @@ -51,7 +51,7 @@ }, { "source": [ - "Plot distributions and loss matrix\n##################################\n\n" + "Plot distributions and loss matrix\n----------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Solve EMD\n#############################################################################\n\n" + "Solve EMD\n---------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Solve Sinkhorn\n#############################################################################\n\n" + "Solve Sinkhorn\n--------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_OT_1D.py b/docs/source/auto_examples/plot_OT_1D.py index 36f823f..719058f 100644 --- a/docs/source/auto_examples/plot_OT_1D.py +++ b/docs/source/auto_examples/plot_OT_1D.py @@ -41,7 +41,7 @@ M /= M.max() ############################################################################## # Plot distributions and loss matrix -################################### +# ---------------------------------- #%% plot the distributions @@ -57,7 +57,8 @@ ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') ############################################################################## # Solve EMD -############################################################################## +# --------- + #%% EMD @@ -68,7 +69,8 @@ ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') ############################################################################## # Solve Sinkhorn -############################################################################## +# -------------- + #%% Sinkhorn diff --git a/docs/source/auto_examples/plot_OT_1D.rst b/docs/source/auto_examples/plot_OT_1D.rst index 32a88e7..b91916e 100644 --- a/docs/source/auto_examples/plot_OT_1D.rst +++ b/docs/source/auto_examples/plot_OT_1D.rst @@ -63,7 +63,7 @@ Generate data Plot distributions and loss matrix -################################## +---------------------------------- @@ -102,13 +102,14 @@ Plot distributions and loss matrix Solve EMD -############################################################################# +--------- .. code-block:: python + #%% EMD G0 = ot.emd(a, b, M) @@ -126,13 +127,14 @@ Solve EMD Solve Sinkhorn -############################################################################# +-------------- .. code-block:: python + #%% Sinkhorn lambd = 1e-3 @@ -169,7 +171,7 @@ Solve Sinkhorn 110|1.527180e-10| -**Total running time of the script:** ( 0 minutes 0.754 seconds) +**Total running time of the script:** ( 0 minutes 0.748 seconds) diff --git a/docs/source/auto_examples/plot_OT_2D_samples.ipynb b/docs/source/auto_examples/plot_OT_2D_samples.ipynb index 0ed7367..41a37f3 100644 --- a/docs/source/auto_examples/plot_OT_2D_samples.ipynb +++ b/docs/source/auto_examples/plot_OT_2D_samples.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "Generate data\n#############################################################################\n\n" + "Generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Plot data\n#############################################################################\n\n" + "Plot data\n---------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Compute EMD\n#############################################################################\n\n" + "Compute EMD\n-----------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Compute Sinkhorn\n#############################################################################\n\n" + "Compute Sinkhorn\n----------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_OT_2D_samples.py b/docs/source/auto_examples/plot_OT_2D_samples.py index f57d631..9818ec5 100644 --- a/docs/source/auto_examples/plot_OT_2D_samples.py +++ b/docs/source/auto_examples/plot_OT_2D_samples.py @@ -19,7 +19,7 @@ import ot ############################################################################## # Generate data -############################################################################## +# ------------- #%% parameters and data generation @@ -42,7 +42,7 @@ M /= M.max() ############################################################################## # Plot data -############################################################################## +# --------- #%% plot samples @@ -58,7 +58,7 @@ pl.title('Cost matrix M') ############################################################################## # Compute EMD -############################################################################## +# ----------- #%% EMD @@ -78,7 +78,7 @@ pl.title('OT matrix with samples') ############################################################################## # Compute Sinkhorn -############################################################################## +# ---------------- #%% sinkhorn diff --git a/docs/source/auto_examples/plot_OT_2D_samples.rst b/docs/source/auto_examples/plot_OT_2D_samples.rst index f95ffaf..0ad9cf0 100644 --- a/docs/source/auto_examples/plot_OT_2D_samples.rst +++ b/docs/source/auto_examples/plot_OT_2D_samples.rst @@ -31,7 +31,7 @@ sum of diracs. The OT matrix is plotted with the samples. Generate data -############################################################################# +------------- @@ -64,7 +64,7 @@ Generate data Plot data -############################################################################# +--------- @@ -103,7 +103,7 @@ Plot data Compute EMD -############################################################################# +----------- @@ -146,7 +146,7 @@ Compute EMD Compute Sinkhorn -############################################################################# +---------------- @@ -191,7 +191,7 @@ Compute Sinkhorn -**Total running time of the script:** ( 0 minutes 1.990 seconds) +**Total running time of the script:** ( 0 minutes 1.743 seconds) diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb b/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb index e738db7..2b9a364 100644 --- a/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb +++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "Dataset 1 : uniform sampling\n#############################################################################\n\n" + "Dataset 1 : uniform sampling\n----------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Dataset 1 : Plot OT Matrices\n#############################################################################\n\n" + "Dataset 1 : Plot OT Matrices\n----------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Dataset 2 : Partial circle\n#############################################################################\n\n" + "Dataset 2 : Partial circle\n--------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Dataset 2 : Plot OT Matrices\n#############################################################################\n\n" + "Dataset 2 : Plot OT Matrices\n-----------------------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.py b/docs/source/auto_examples/plot_OT_L1_vs_L2.py index 49d37e1..090e809 100644 --- a/docs/source/auto_examples/plot_OT_L1_vs_L2.py +++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.py @@ -22,7 +22,7 @@ import ot ############################################################################## # Dataset 1 : uniform sampling -############################################################################## +# ---------------------------- n = 20 # nb samples xs = np.zeros((n, 2)) @@ -73,7 +73,7 @@ pl.tight_layout() ############################################################################## # Dataset 1 : Plot OT Matrices -############################################################################## +# ---------------------------- #%% EMD @@ -114,7 +114,7 @@ pl.show() ############################################################################## # Dataset 2 : Partial circle -############################################################################## +# -------------------------- n = 50 # nb samples xtot = np.zeros((n + 1, 2)) @@ -168,7 +168,7 @@ pl.tight_layout() ############################################################################## # Dataset 2 : Plot OT Matrices -############################################################################## +# ----------------------------- #%% EMD diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.rst b/docs/source/auto_examples/plot_OT_L1_vs_L2.rst index 8b5b133..f97b373 100644 --- a/docs/source/auto_examples/plot_OT_L1_vs_L2.rst +++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.rst @@ -34,7 +34,7 @@ https://arxiv.org/pdf/1706.07650.pdf Dataset 1 : uniform sampling -############################################################################# +---------------------------- @@ -108,7 +108,7 @@ Dataset 1 : uniform sampling Dataset 1 : Plot OT Matrices -############################################################################# +---------------------------- @@ -162,7 +162,7 @@ Dataset 1 : Plot OT Matrices Dataset 2 : Partial circle -############################################################################# +-------------------------- @@ -239,7 +239,7 @@ Dataset 2 : Partial circle Dataset 2 : Plot OT Matrices -############################################################################# +----------------------------- @@ -290,7 +290,7 @@ Dataset 2 : Plot OT Matrices -**Total running time of the script:** ( 0 minutes 1.218 seconds) +**Total running time of the script:** ( 0 minutes 1.134 seconds) diff --git a/docs/source/auto_examples/plot_WDA.ipynb b/docs/source/auto_examples/plot_WDA.ipynb index 47a6eca..1661c53 100644 --- a/docs/source/auto_examples/plot_WDA.ipynb +++ b/docs/source/auto_examples/plot_WDA.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "Generate data\n#############################################################################\n\n" + "Generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Plot data\n#############################################################################\n\n" + "Plot data\n---------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Compute Fisher Discriminant Analysis\n#############################################################################\n\n" + "Compute Fisher Discriminant Analysis\n------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Compute Wasserstein Discriminant Analysis\n#############################################################################\n\n" + "Compute Wasserstein Discriminant Analysis\n-----------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -105,7 +105,7 @@ }, { "source": [ - "Plot 2D projections\n#############################################################################\n\n" + "Plot 2D projections\n-------------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_WDA.py b/docs/source/auto_examples/plot_WDA.py index 5928621..93cc237 100644 --- a/docs/source/auto_examples/plot_WDA.py +++ b/docs/source/auto_examples/plot_WDA.py @@ -24,7 +24,7 @@ from ot.dr import wda, fda ############################################################################## # Generate data -############################################################################## +# ------------- #%% parameters @@ -51,7 +51,7 @@ xt = np.hstack((xt, np.random.randn(n, nbnoise))) ############################################################################## # Plot data -############################################################################## +# --------- #%% plot samples pl.figure(1, figsize=(6.4, 3.5)) @@ -69,7 +69,7 @@ pl.tight_layout() ############################################################################## # Compute Fisher Discriminant Analysis -############################################################################## +# ------------------------------------ #%% Compute FDA p = 2 @@ -78,7 +78,7 @@ Pfda, projfda = fda(xs, ys, p) ############################################################################## # Compute Wasserstein Discriminant Analysis -############################################################################## +# ----------------------------------------- #%% Compute WDA p = 2 @@ -91,7 +91,7 @@ Pwda, projwda = wda(xs, ys, p, reg, k, maxiter=maxiter) ############################################################################## # Plot 2D projections -############################################################################## +# ------------------- #%% plot samples diff --git a/docs/source/auto_examples/plot_WDA.rst b/docs/source/auto_examples/plot_WDA.rst index a0c3389..64ddb47 100644 --- a/docs/source/auto_examples/plot_WDA.rst +++ b/docs/source/auto_examples/plot_WDA.rst @@ -36,7 +36,7 @@ Wasserstein Discriminant Analysis. Generate data -############################################################################# +------------- @@ -73,7 +73,7 @@ Generate data Plot data -############################################################################# +--------- @@ -104,7 +104,7 @@ Plot data Compute Fisher Discriminant Analysis -############################################################################# +------------------------------------ @@ -123,7 +123,7 @@ Compute Fisher Discriminant Analysis Compute Wasserstein Discriminant Analysis -############################################################################# +----------------------------------------- @@ -150,65 +150,25 @@ Compute Wasserstein Discriminant Analysis Compiling cost function... Computing gradient of cost function... iter cost val grad. norm - 1 +8.6305817354868675e-01 4.10110152e-01 - 2 +4.6939060757969131e-01 2.98553763e-01 - 3 +4.2106314200107775e-01 1.48552668e-01 - 4 +4.1376389458568069e-01 1.12319011e-01 - 5 +4.0984854988792835e-01 1.01126129e-01 - 6 +4.0415292614140025e-01 3.90875165e-02 - 7 +4.0297967887432584e-01 2.73716014e-02 - 8 +4.0252319029045258e-01 3.76498956e-02 - 9 +4.0158635935184972e-01 1.31986577e-02 - 10 +4.0118906894272482e-01 3.40307273e-02 - 11 +4.0052579694802176e-01 7.79567347e-03 - 12 +4.0049330810825384e-01 9.77921941e-03 - 13 +4.0042500151972926e-01 4.63602913e-03 - 14 +4.0031705300038767e-01 1.69742018e-02 - 15 +4.0013705338124350e-01 7.40310798e-03 - 16 +4.0006224569843946e-01 1.08829949e-02 - 17 +3.9998280287782945e-01 1.25733450e-02 - 18 +3.9986405111843215e-01 1.05626807e-02 - 19 +3.9974905002724365e-01 9.93566406e-03 - 20 +3.9971323753531823e-01 2.21199533e-02 - 21 +3.9958582328238779e-01 1.73335808e-02 - 22 +3.9937139582811110e-01 1.09182412e-02 - 23 +3.9923748818499571e-01 1.77304913e-02 - 24 +3.9900530515251881e-01 1.15381586e-02 - 25 +3.9883316307006128e-01 1.80225446e-02 - 26 +3.9860317631835845e-01 1.65011032e-02 - 27 +3.9852130309759393e-01 2.81245689e-02 - 28 +3.9824281033694675e-01 2.01114810e-02 - 29 +3.9799657608114836e-01 2.66040929e-02 - 30 +3.9746233677210713e-01 1.45779937e-02 - 31 +3.9671794378467928e-01 4.27487207e-02 - 32 +3.9573357685391913e-01 2.20071520e-02 - 33 +3.9536725156297214e-01 2.00817458e-02 - 34 +3.9515994339814914e-01 3.81472315e-02 - 35 +3.9448966390371887e-01 2.52129049e-02 - 36 +3.9351423238681266e-01 5.60677866e-02 - 37 +3.9082703288308568e-01 4.26859586e-02 - 38 +3.7139409489868136e-01 1.26067835e-01 - 39 +2.8085932518253526e-01 1.70133509e-01 - 40 +2.7330384726281814e-01 1.95523507e-01 - 41 +2.4806985554269162e-01 1.31192016e-01 - 42 +2.3748356968454920e-01 8.71616829e-02 - 43 +2.3501927152342389e-01 7.02789537e-02 - 44 +2.3183578112546338e-01 2.62025296e-02 - 45 +2.3154208568082749e-01 1.67845346e-02 - 46 +2.3139316710346300e-01 8.27285074e-03 - 47 +2.3136034106523354e-01 4.64818210e-03 - 48 +2.3134548827742521e-01 4.53144806e-04 - 49 +2.3134540503271503e-01 2.91010390e-04 - 50 +2.3134535764073319e-01 1.25662481e-04 - 51 +2.3134534692621381e-01 1.24751216e-05 - 52 +2.3134534685831357e-01 7.44008265e-06 - 53 +2.3134534684658337e-01 6.16933546e-06 - 54 +2.3134534682129679e-01 5.12152219e-07 - Terminated - min grad norm reached after 54 iterations, 24.53 seconds. + 1 +5.4993226050368416e-01 5.18285173e-01 + 2 +3.4883000507542844e-01 1.96795818e-01 + 3 +2.9841234004693890e-01 2.33029475e-01 + 4 +2.3976476757548179e-01 1.38593951e-01 + 5 +2.3614468346177828e-01 1.19615394e-01 + 6 +2.2586536502789240e-01 4.82430685e-02 + 7 +2.2451030967794622e-01 2.56564039e-02 + 8 +2.2421446331083625e-01 1.47932578e-02 + 9 +2.2407441444450052e-01 1.12040327e-03 + 10 +2.2407365923337522e-01 3.78899763e-04 + 11 +2.2407356874011675e-01 1.79740810e-05 + 12 +2.2407356862959993e-01 1.25643005e-05 + 13 +2.2407356853043561e-01 1.40415001e-06 + 14 +2.2407356852925220e-01 3.41183585e-07 + Terminated - min grad norm reached after 14 iterations, 6.78 seconds. Plot 2D projections -############################################################################# +------------------- @@ -256,7 +216,7 @@ Plot 2D projections -**Total running time of the script:** ( 0 minutes 25.326 seconds) +**Total running time of the script:** ( 0 minutes 7.637 seconds) diff --git a/docs/source/auto_examples/plot_barycenter_1D.ipynb b/docs/source/auto_examples/plot_barycenter_1D.ipynb index 32cb2ec..a19e0fd 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.ipynb +++ b/docs/source/auto_examples/plot_barycenter_1D.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "Generate data\n#############################################################################\n\n" + "Generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Plot data\n#############################################################################\n\n" + "Plot data\n---------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Barycenter computation\n#############################################################################\n\n" + "Barycenter computation\n----------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Barycentric interpolation\n#############################################################################\n\n" + "Barycentric interpolation\n-------------------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_barycenter_1D.py b/docs/source/auto_examples/plot_barycenter_1D.py index eef8536..620936b 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.py +++ b/docs/source/auto_examples/plot_barycenter_1D.py @@ -27,7 +27,7 @@ from matplotlib.collections import PolyCollection ############################################################################## # Generate data -############################################################################## +# ------------- #%% parameters @@ -50,7 +50,7 @@ M /= M.max() ############################################################################## # Plot data -############################################################################## +# --------- #%% plot the distributions @@ -62,7 +62,7 @@ pl.tight_layout() ############################################################################## # Barycenter computation -############################################################################## +# ---------------------- #%% barycenter computation @@ -92,7 +92,7 @@ pl.tight_layout() ############################################################################## # Barycentric interpolation -############################################################################## +# ------------------------- #%% barycenter interpolation diff --git a/docs/source/auto_examples/plot_barycenter_1D.rst b/docs/source/auto_examples/plot_barycenter_1D.rst index b1794cd..413fae3 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.rst +++ b/docs/source/auto_examples/plot_barycenter_1D.rst @@ -39,7 +39,7 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. Generate data -############################################################################# +------------- @@ -72,7 +72,7 @@ Generate data Plot data -############################################################################# +--------- @@ -97,7 +97,7 @@ Plot data Barycenter computation -############################################################################# +---------------------- @@ -140,7 +140,7 @@ Barycenter computation Barycentric interpolation -############################################################################# +------------------------- @@ -230,7 +230,7 @@ Barycentric interpolation -**Total running time of the script:** ( 0 minutes 0.416 seconds) +**Total running time of the script:** ( 0 minutes 0.431 seconds) diff --git a/docs/source/auto_examples/plot_compute_emd.ipynb b/docs/source/auto_examples/plot_compute_emd.ipynb index a882bd1..b9b8bc5 100644 --- a/docs/source/auto_examples/plot_compute_emd.ipynb +++ b/docs/source/auto_examples/plot_compute_emd.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "Generate data\n#############################################################################\n\n" + "Generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Plot data\n#############################################################################\n\n" + "Plot data\n---------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Compute EMD for the different losses\n#############################################################################\n\n" + "Compute EMD for the different losses\n------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Compute Sinkhorn for the different losses\n#############################################################################\n\n" + "Compute Sinkhorn for the different losses\n-----------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_compute_emd.py b/docs/source/auto_examples/plot_compute_emd.py index a84b249..73b42c3 100644 --- a/docs/source/auto_examples/plot_compute_emd.py +++ b/docs/source/auto_examples/plot_compute_emd.py @@ -22,7 +22,7 @@ from ot.datasets import get_1D_gauss as gauss ############################################################################## # Generate data -############################################################################## +# ------------- #%% parameters @@ -51,7 +51,7 @@ M2 /= M2.max() ############################################################################## # Plot data -############################################################################## +# --------- #%% plot the distributions @@ -67,7 +67,7 @@ pl.tight_layout() ############################################################################## # Compute EMD for the different losses -############################################################################## +# ------------------------------------ #%% Compute and plot distributions and loss matrix @@ -83,7 +83,7 @@ pl.legend() ############################################################################## # Compute Sinkhorn for the different losses -############################################################################## +# ----------------------------------------- #%% reg = 1e-2 diff --git a/docs/source/auto_examples/plot_compute_emd.rst b/docs/source/auto_examples/plot_compute_emd.rst index 58220a4..ce79e20 100644 --- a/docs/source/auto_examples/plot_compute_emd.rst +++ b/docs/source/auto_examples/plot_compute_emd.rst @@ -34,7 +34,7 @@ ground metrics and plot their values for diffeent distributions. Generate data -############################################################################# +------------- @@ -73,7 +73,7 @@ Generate data Plot data -############################################################################# +--------- @@ -102,7 +102,7 @@ Plot data Compute EMD for the different losses -############################################################################# +------------------------------------ @@ -131,7 +131,7 @@ Compute EMD for the different losses Compute Sinkhorn for the different losses -############################################################################# +----------------------------------------- @@ -162,7 +162,7 @@ Compute Sinkhorn for the different losses -**Total running time of the script:** ( 0 minutes 0.471 seconds) +**Total running time of the script:** ( 0 minutes 0.441 seconds) diff --git a/docs/source/auto_examples/plot_optim_OTreg.ipynb b/docs/source/auto_examples/plot_optim_OTreg.ipynb index f9fec33..333331b 100644 --- a/docs/source/auto_examples/plot_optim_OTreg.ipynb +++ b/docs/source/auto_examples/plot_optim_OTreg.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "Generate data\n#############################################################################\n\n" + "Generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Solve EMD\n#############################################################################\n\n" + "Solve EMD\n---------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Solve EMD with Frobenius norm regularization\n#############################################################################\n\n" + "Solve EMD with Frobenius norm regularization\n--------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Solve EMD with entropic regularization\n#############################################################################\n\n" + "Solve EMD with entropic regularization\n--------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -105,7 +105,7 @@ }, { "source": [ - "Solve EMD with Frobenius norm + entropic regularization\n#############################################################################\n\n" + "Solve EMD with Frobenius norm + entropic regularization\n-------------------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_optim_OTreg.py b/docs/source/auto_examples/plot_optim_OTreg.py index d753414..e1a737e 100644 --- a/docs/source/auto_examples/plot_optim_OTreg.py +++ b/docs/source/auto_examples/plot_optim_OTreg.py @@ -32,7 +32,7 @@ import ot ############################################################################## # Generate data -############################################################################## +# ------------- #%% parameters @@ -51,7 +51,7 @@ M /= M.max() ############################################################################## # Solve EMD -############################################################################## +# --------- #%% EMD @@ -62,7 +62,7 @@ ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') ############################################################################## # Solve EMD with Frobenius norm regularization -############################################################################## +# -------------------------------------------- #%% Example with Frobenius norm regularization @@ -84,7 +84,7 @@ ot.plot.plot1D_mat(a, b, Gl2, 'OT matrix Frob. reg') ############################################################################## # Solve EMD with entropic regularization -############################################################################## +# -------------------------------------- #%% Example with entropic regularization @@ -106,7 +106,7 @@ ot.plot.plot1D_mat(a, b, Ge, 'OT matrix Entrop. reg') ############################################################################## # Solve EMD with Frobenius norm + entropic regularization -############################################################################## +# ------------------------------------------------------- #%% Example with Frobenius norm + entropic regularization with gcg diff --git a/docs/source/auto_examples/plot_optim_OTreg.rst b/docs/source/auto_examples/plot_optim_OTreg.rst index c3ec03b..f628024 100644 --- a/docs/source/auto_examples/plot_optim_OTreg.rst +++ b/docs/source/auto_examples/plot_optim_OTreg.rst @@ -44,7 +44,7 @@ arXiv preprint arXiv:1510.06567. Generate data -############################################################################# +------------- @@ -73,7 +73,7 @@ Generate data Solve EMD -############################################################################# +--------- @@ -97,7 +97,7 @@ Solve EMD Solve EMD with Frobenius norm regularization -############################################################################# +-------------------------------------------- @@ -359,7 +359,7 @@ Solve EMD with Frobenius norm regularization Solve EMD with entropic regularization -############################################################################# +-------------------------------------- @@ -621,7 +621,7 @@ Solve EMD with entropic regularization Solve EMD with Frobenius norm + entropic regularization -############################################################################# +------------------------------------------------------- @@ -667,7 +667,7 @@ Solve EMD with Frobenius norm + entropic regularization 4|1.609284e-01|-1.111407e-12 -**Total running time of the script:** ( 0 minutes 1.913 seconds) +**Total running time of the script:** ( 0 minutes 1.809 seconds) diff --git a/docs/source/auto_examples/plot_otda_classes.ipynb b/docs/source/auto_examples/plot_otda_classes.ipynb index 16634b1..6754fa5 100644 --- a/docs/source/auto_examples/plot_otda_classes.ipynb +++ b/docs/source/auto_examples/plot_otda_classes.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "generate data\n#############################################################################\n\n" + "Generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + "Instantiate the different transport algorithms and fit them\n-----------------------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Fig 1 : plots source and target samples\n#############################################################################\n\n" + "Fig 1 : plots source and target samples\n---------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Fig 2 : plot optimal couplings and transported samples\n#############################################################################\n\n" + "Fig 2 : plot optimal couplings and transported samples\n------------------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_otda_classes.py b/docs/source/auto_examples/plot_otda_classes.py index ec57a37..b14c11a 100644 --- a/docs/source/auto_examples/plot_otda_classes.py +++ b/docs/source/auto_examples/plot_otda_classes.py @@ -19,8 +19,8 @@ import ot ############################################################################## -# generate data -############################################################################## +# Generate data +# ------------- n_source_samples = 150 n_target_samples = 150 @@ -31,7 +31,7 @@ Xt, yt = ot.datasets.get_data_classif('3gauss2', n_target_samples) ############################################################################## # Instantiate the different transport algorithms and fit them -############################################################################## +# ----------------------------------------------------------- # EMD Transport ot_emd = ot.da.EMDTransport() @@ -59,7 +59,7 @@ transp_Xs_l1l2 = ot_l1l2.transform(Xs=Xs) ############################################################################## # Fig 1 : plots source and target samples -############################################################################## +# --------------------------------------- pl.figure(1, figsize=(10, 5)) pl.subplot(1, 2, 1) @@ -80,7 +80,7 @@ pl.tight_layout() ############################################################################## # Fig 2 : plot optimal couplings and transported samples -############################################################################## +# ------------------------------------------------------ param_img = {'interpolation': 'nearest', 'cmap': 'spectral'} diff --git a/docs/source/auto_examples/plot_otda_classes.rst b/docs/source/auto_examples/plot_otda_classes.rst index d1a13b1..f19a99f 100644 --- a/docs/source/auto_examples/plot_otda_classes.rst +++ b/docs/source/auto_examples/plot_otda_classes.rst @@ -31,8 +31,8 @@ approaches currently supported in POT. -generate data -############################################################################# +Generate data +------------- @@ -53,7 +53,7 @@ generate data Instantiate the different transport algorithms and fit them -############################################################################# +----------------------------------------------------------- @@ -94,33 +94,33 @@ Instantiate the different transport algorithms and fit them It. |Loss |Delta loss -------------------------------- - 0|9.984935e+00|0.000000e+00 - 1|2.126803e+00|-3.694808e+00 - 2|1.867272e+00|-1.389895e-01 - 3|1.803858e+00|-3.515488e-02 - 4|1.783036e+00|-1.167761e-02 - 5|1.774823e+00|-4.627422e-03 - 6|1.771947e+00|-1.623526e-03 - 7|1.767564e+00|-2.479535e-03 - 8|1.763484e+00|-2.313667e-03 - 9|1.761138e+00|-1.331780e-03 - 10|1.758879e+00|-1.284576e-03 - 11|1.758034e+00|-4.806014e-04 - 12|1.757595e+00|-2.497155e-04 - 13|1.756749e+00|-4.818562e-04 - 14|1.755316e+00|-8.161432e-04 - 15|1.754988e+00|-1.866236e-04 - 16|1.754964e+00|-1.382474e-05 - 17|1.754032e+00|-5.315971e-04 - 18|1.753595e+00|-2.492359e-04 - 19|1.752900e+00|-3.961403e-04 + 0|9.552437e+00|0.000000e+00 + 1|1.921833e+00|-3.970483e+00 + 2|1.671022e+00|-1.500942e-01 + 3|1.615147e+00|-3.459458e-02 + 4|1.594289e+00|-1.308252e-02 + 5|1.587287e+00|-4.411254e-03 + 6|1.581665e+00|-3.554702e-03 + 7|1.577022e+00|-2.943809e-03 + 8|1.573870e+00|-2.002870e-03 + 9|1.571645e+00|-1.415696e-03 + 10|1.569342e+00|-1.467590e-03 + 11|1.567863e+00|-9.432233e-04 + 12|1.566558e+00|-8.329769e-04 + 13|1.565414e+00|-7.311320e-04 + 14|1.564425e+00|-6.319985e-04 + 15|1.563955e+00|-3.007604e-04 + 16|1.563658e+00|-1.894627e-04 + 17|1.562886e+00|-4.941143e-04 + 18|1.562578e+00|-1.974031e-04 + 19|1.562445e+00|-8.468825e-05 It. |Loss |Delta loss -------------------------------- - 20|1.752850e+00|-2.869262e-05 + 20|1.562007e+00|-2.805136e-04 Fig 1 : plots source and target samples -############################################################################# +--------------------------------------- @@ -154,7 +154,7 @@ Fig 1 : plots source and target samples Fig 2 : plot optimal couplings and transported samples -############################################################################# +------------------------------------------------------ @@ -236,7 +236,7 @@ Fig 2 : plot optimal couplings and transported samples -**Total running time of the script:** ( 0 minutes 1.576 seconds) +**Total running time of the script:** ( 0 minutes 1.596 seconds) diff --git a/docs/source/auto_examples/plot_otda_color_images.ipynb b/docs/source/auto_examples/plot_otda_color_images.ipynb index 797b27d..2daf406 100644 --- a/docs/source/auto_examples/plot_otda_color_images.ipynb +++ b/docs/source/auto_examples/plot_otda_color_images.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "Generate data\n#############################################################################\n\n" + "Generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Plot original image\n#############################################################################\n\n" + "Plot original image\n-------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Scatter plot of colors\n#############################################################################\n\n" + "Scatter plot of colors\n----------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + "Instantiate the different transport algorithms and fit them\n-----------------------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -105,7 +105,7 @@ }, { "source": [ - "Plot new images\n#############################################################################\n\n" + "Plot new images\n---------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_otda_color_images.py b/docs/source/auto_examples/plot_otda_color_images.py index f1df9d9..e77aec0 100644 --- a/docs/source/auto_examples/plot_otda_color_images.py +++ b/docs/source/auto_examples/plot_otda_color_images.py @@ -42,7 +42,7 @@ def minmax(I): ############################################################################## # Generate data -############################################################################## +# ------------- # Loading images I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 @@ -62,7 +62,7 @@ Xt = X2[idx2, :] ############################################################################## # Plot original image -############################################################################## +# ------------------- pl.figure(1, figsize=(6.4, 3)) @@ -79,7 +79,7 @@ pl.title('Image 2') ############################################################################## # Scatter plot of colors -############################################################################## +# ---------------------- pl.figure(2, figsize=(6.4, 3)) @@ -101,7 +101,7 @@ pl.tight_layout() ############################################################################## # Instantiate the different transport algorithms and fit them -############################################################################## +# ----------------------------------------------------------- # EMDTransport ot_emd = ot.da.EMDTransport() @@ -127,7 +127,7 @@ I2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape)) ############################################################################## # Plot new images -############################################################################## +# --------------- pl.figure(3, figsize=(8, 4)) diff --git a/docs/source/auto_examples/plot_otda_color_images.rst b/docs/source/auto_examples/plot_otda_color_images.rst index 88e93d2..4772bed 100644 --- a/docs/source/auto_examples/plot_otda_color_images.rst +++ b/docs/source/auto_examples/plot_otda_color_images.rst @@ -54,7 +54,7 @@ SIAM Journal on Imaging Sciences, 7(3), 1853-1882. Generate data -############################################################################# +------------- @@ -84,7 +84,7 @@ Generate data Plot original image -############################################################################# +------------------- @@ -114,7 +114,7 @@ Plot original image Scatter plot of colors -############################################################################# +---------------------- @@ -149,7 +149,7 @@ Scatter plot of colors Instantiate the different transport algorithms and fit them -############################################################################# +----------------------------------------------------------- @@ -185,7 +185,7 @@ Instantiate the different transport algorithms and fit them Plot new images -############################################################################# +--------------- @@ -235,7 +235,7 @@ Plot new images -**Total running time of the script:** ( 2 minutes 28.053 seconds) +**Total running time of the script:** ( 2 minutes 24.561 seconds) diff --git a/docs/source/auto_examples/plot_otda_d2.ipynb b/docs/source/auto_examples/plot_otda_d2.ipynb index 2331f8c..7bfcc9a 100644 --- a/docs/source/auto_examples/plot_otda_d2.ipynb +++ b/docs/source/auto_examples/plot_otda_d2.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# OT for empirical distributions\n\n\nThis example introduces a domain adaptation in a 2D setting. It explicits\nthe problem of domain adaptation and introduces some optimal transport\napproaches to solve it.\n\nQuantities such as optimal couplings, greater coupling coefficients and\ntransported samples are represented in order to give a visual understanding\nof what the transport methods are doing.\n\n" + "\n# OT for domain adaptation on empirical distributions\n\n\nThis example introduces a domain adaptation in a 2D setting. It explicits\nthe problem of domain adaptation and introduces some optimal transport\napproaches to solve it.\n\nQuantities such as optimal couplings, greater coupling coefficients and\ntransported samples are represented in order to give a visual understanding\nof what the transport methods are doing.\n\n" ], "cell_type": "markdown", "metadata": {} @@ -33,7 +33,7 @@ }, { "source": [ - "generate data\n#############################################################################\n\n" + "generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + "Instantiate the different transport algorithms and fit them\n-----------------------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Fig 1 : plots source and target samples + matrix of pairwise distance\n#############################################################################\n\n" + "Fig 1 : plots source and target samples + matrix of pairwise distance\n---------------------------------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Fig 2 : plots optimal couplings for the different methods\n#############################################################################\n\n" + "Fig 2 : plots optimal couplings for the different methods\n---------------------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -105,7 +105,7 @@ }, { "source": [ - "Fig 3 : plot transported samples\n#############################################################################\n\n" + "Fig 3 : plot transported samples\n--------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_otda_d2.py b/docs/source/auto_examples/plot_otda_d2.py index 3daa0a6..e53d7d6 100644 --- a/docs/source/auto_examples/plot_otda_d2.py +++ b/docs/source/auto_examples/plot_otda_d2.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- """ -============================== -OT for empirical distributions -============================== +=================================================== +OT for domain adaptation on empirical distributions +=================================================== This example introduces a domain adaptation in a 2D setting. It explicits the problem of domain adaptation and introduces some optimal transport @@ -24,7 +24,7 @@ import ot ############################################################################## # generate data -############################################################################## +# ------------- n_samples_source = 150 n_samples_target = 150 @@ -38,7 +38,7 @@ M = ot.dist(Xs, Xt, metric='sqeuclidean') ############################################################################## # Instantiate the different transport algorithms and fit them -############################################################################## +# ----------------------------------------------------------- # EMD Transport ot_emd = ot.da.EMDTransport() @@ -60,7 +60,7 @@ transp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs) ############################################################################## # Fig 1 : plots source and target samples + matrix of pairwise distance -############################################################################## +# --------------------------------------------------------------------- pl.figure(1, figsize=(10, 10)) pl.subplot(2, 2, 1) @@ -87,8 +87,7 @@ pl.tight_layout() ############################################################################## # Fig 2 : plots optimal couplings for the different methods -############################################################################## - +# --------------------------------------------------------- pl.figure(2, figsize=(10, 6)) pl.subplot(2, 3, 1) @@ -137,7 +136,7 @@ pl.tight_layout() ############################################################################## # Fig 3 : plot transported samples -############################################################################## +# -------------------------------- # display transported samples pl.figure(4, figsize=(10, 4)) diff --git a/docs/source/auto_examples/plot_otda_d2.rst b/docs/source/auto_examples/plot_otda_d2.rst index 3aa1149..2b716e1 100644 --- a/docs/source/auto_examples/plot_otda_d2.rst +++ b/docs/source/auto_examples/plot_otda_d2.rst @@ -3,9 +3,9 @@ .. _sphx_glr_auto_examples_plot_otda_d2.py: -============================== -OT for empirical distributions -============================== +=================================================== +OT for domain adaptation on empirical distributions +=================================================== This example introduces a domain adaptation in a 2D setting. It explicits the problem of domain adaptation and introduces some optimal transport @@ -36,7 +36,7 @@ of what the transport methods are doing. generate data -############################################################################# +------------- @@ -60,7 +60,7 @@ generate data Instantiate the different transport algorithms and fit them -############################################################################# +----------------------------------------------------------- @@ -92,7 +92,7 @@ Instantiate the different transport algorithms and fit them Fig 1 : plots source and target samples + matrix of pairwise distance -############################################################################# +--------------------------------------------------------------------- @@ -132,13 +132,12 @@ Fig 1 : plots source and target samples + matrix of pairwise distance Fig 2 : plots optimal couplings for the different methods -############################################################################# +--------------------------------------------------------- .. code-block:: python - pl.figure(2, figsize=(10, 6)) pl.subplot(2, 3, 1) @@ -195,7 +194,7 @@ Fig 2 : plots optimal couplings for the different methods Fig 3 : plot transported samples -############################################################################# +-------------------------------- @@ -243,7 +242,7 @@ Fig 3 : plot transported samples -**Total running time of the script:** ( 0 minutes 32.275 seconds) +**Total running time of the script:** ( 0 minutes 32.084 seconds) diff --git a/docs/source/auto_examples/plot_otda_mapping.ipynb b/docs/source/auto_examples/plot_otda_mapping.ipynb index 5b3fd06..0374146 100644 --- a/docs/source/auto_examples/plot_otda_mapping.ipynb +++ b/docs/source/auto_examples/plot_otda_mapping.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "Generate data\n#############################################################################\n\n" + "Generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Plot data\n#############################################################################\n\n" + "Plot data\n---------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + "Instantiate the different transport algorithms and fit them\n-----------------------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Plot transported samples\n#############################################################################\n\n" + "Plot transported samples\n------------------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_otda_mapping.py b/docs/source/auto_examples/plot_otda_mapping.py index e78fef4..167c3a1 100644 --- a/docs/source/auto_examples/plot_otda_mapping.py +++ b/docs/source/auto_examples/plot_otda_mapping.py @@ -25,7 +25,7 @@ import ot ############################################################################## # Generate data -############################################################################## +# ------------- n_source_samples = 100 n_target_samples = 100 @@ -45,7 +45,7 @@ Xt = Xt + 4 ############################################################################## # Plot data -############################################################################## +# --------- pl.figure(1, (10, 5)) pl.clf() @@ -57,7 +57,7 @@ pl.title('Source and target distributions') ############################################################################## # Instantiate the different transport algorithms and fit them -############################################################################## +# ----------------------------------------------------------- # MappingTransport with linear kernel ot_mapping_linear = ot.da.MappingTransport( @@ -88,7 +88,7 @@ transp_Xs_gaussian_new = ot_mapping_gaussian.transform(Xs=Xs_new) ############################################################################## # Plot transported samples -############################################################################## +# ------------------------ pl.figure(2) pl.clf() diff --git a/docs/source/auto_examples/plot_otda_mapping.rst b/docs/source/auto_examples/plot_otda_mapping.rst index ddc1ee9..6c1c780 100644 --- a/docs/source/auto_examples/plot_otda_mapping.rst +++ b/docs/source/auto_examples/plot_otda_mapping.rst @@ -37,7 +37,7 @@ a linear or a kernelized mapping as introduced in [8]. Generate data -############################################################################# +------------- @@ -67,7 +67,7 @@ Generate data Plot data -############################################################################# +--------- @@ -92,7 +92,7 @@ Plot data Instantiate the different transport algorithms and fit them -############################################################################# +----------------------------------------------------------- @@ -136,30 +136,28 @@ Instantiate the different transport algorithms and fit them It. |Loss |Delta loss -------------------------------- - 0|4.481482e+03|0.000000e+00 - 1|4.469389e+03|-2.698549e-03 - 2|4.468825e+03|-1.261217e-04 - 3|4.468580e+03|-5.486064e-05 - 4|4.468438e+03|-3.161220e-05 - 5|4.468352e+03|-1.930800e-05 - 6|4.468309e+03|-9.570658e-06 + 0|4.307233e+03|0.000000e+00 + 1|4.296694e+03|-2.446759e-03 + 2|4.296419e+03|-6.417421e-05 + 3|4.296328e+03|-2.110209e-05 + 4|4.296305e+03|-5.298603e-06 It. |Loss |Delta loss -------------------------------- - 0|4.504654e+02|0.000000e+00 - 1|4.461571e+02|-9.564116e-03 - 2|4.459105e+02|-5.528043e-04 - 3|4.457895e+02|-2.712398e-04 - 4|4.457041e+02|-1.914829e-04 - 5|4.456431e+02|-1.369704e-04 - 6|4.456032e+02|-8.944784e-05 - 7|4.455700e+02|-7.447824e-05 - 8|4.455447e+02|-5.688965e-05 - 9|4.455229e+02|-4.890051e-05 - 10|4.455084e+02|-3.262490e-05 + 0|4.325624e+02|0.000000e+00 + 1|4.281958e+02|-1.009489e-02 + 2|4.279370e+02|-6.042202e-04 + 3|4.278109e+02|-2.947651e-04 + 4|4.277212e+02|-2.096651e-04 + 5|4.276589e+02|-1.456221e-04 + 6|4.276141e+02|-1.048476e-04 + 7|4.275803e+02|-7.906213e-05 + 8|4.275531e+02|-6.360573e-05 + 9|4.275314e+02|-5.076642e-05 + 10|4.275129e+02|-4.325858e-05 Plot transported samples -############################################################################# +------------------------ @@ -208,7 +206,7 @@ Plot transported samples -**Total running time of the script:** ( 0 minutes 0.869 seconds) +**Total running time of the script:** ( 0 minutes 0.747 seconds) diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb index 3b3987a..56caa8a 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb @@ -33,7 +33,7 @@ }, { "source": [ - "Generate data\n#############################################################################\n\n" + "Generate data\n-------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -51,7 +51,7 @@ }, { "source": [ - "Domain adaptation for pixel distribution transfer\n#############################################################################\n\n" + "Domain adaptation for pixel distribution transfer\n-------------------------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -69,7 +69,7 @@ }, { "source": [ - "Plot original images\n#############################################################################\n\n" + "Plot original images\n--------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -87,7 +87,7 @@ }, { "source": [ - "Plot pixel values distribution\n#############################################################################\n\n" + "Plot pixel values distribution\n------------------------------\n\n" ], "cell_type": "markdown", "metadata": {} @@ -105,7 +105,7 @@ }, { "source": [ - "Plot transformed images\n#############################################################################\n\n" + "Plot transformed images\n-----------------------\n\n" ], "cell_type": "markdown", "metadata": {} diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.py b/docs/source/auto_examples/plot_otda_mapping_colors_images.py index 5590286..5f1e844 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.py +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.py @@ -45,7 +45,7 @@ def minmax(I): ############################################################################## # Generate data -############################################################################## +# ------------- # Loading images I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 @@ -66,7 +66,7 @@ Xt = X2[idx2, :] ############################################################################## # Domain adaptation for pixel distribution transfer -############################################################################## +# ------------------------------------------------- # EMDTransport ot_emd = ot.da.EMDTransport() @@ -97,7 +97,7 @@ Image_mapping_gaussian = minmax(mat2im(X1tn, I1.shape)) ############################################################################## # Plot original images -############################################################################## +# -------------------- pl.figure(1, figsize=(6.4, 3)) pl.subplot(1, 2, 1) @@ -114,7 +114,7 @@ pl.tight_layout() ############################################################################## # Plot pixel values distribution -############################################################################## +# ------------------------------ pl.figure(2, figsize=(6.4, 5)) @@ -136,7 +136,7 @@ pl.tight_layout() ############################################################################## # Plot transformed images -############################################################################## +# ----------------------- pl.figure(2, figsize=(10, 5)) diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.rst b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst index 9995103..86b1312 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.rst +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst @@ -57,7 +57,7 @@ estimation [8]. Generate data -############################################################################# +------------- @@ -88,7 +88,7 @@ Generate data Domain adaptation for pixel distribution transfer -############################################################################# +------------------------------------------------- @@ -168,7 +168,7 @@ Domain adaptation for pixel distribution transfer Plot original images -############################################################################# +-------------------- @@ -198,7 +198,7 @@ Plot original images Plot pixel values distribution -############################################################################# +------------------------------ @@ -233,7 +233,7 @@ Plot pixel values distribution Plot transformed images -############################################################################# +----------------------- @@ -283,7 +283,7 @@ Plot transformed images -**Total running time of the script:** ( 2 minutes 8.746 seconds) +**Total running time of the script:** ( 2 minutes 5.213 seconds) diff --git a/docs/source/conf.py b/docs/source/conf.py index 0a822e5..156b878 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -33,7 +33,7 @@ class Mock(MagicMock): return MagicMock() MOCK_MODULES = ['ot.lp.emd_wrap','autograd','pymanopt','cudamat','autograd.numpy','pymanopt.manifolds','pymanopt.solvers'] # 'autograd.numpy','pymanopt.manifolds','pymanopt.solvers', -##sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES) +###sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES) # !!!! # If extensions (or modules to document with autodoc) are in another directory, -- cgit v1.2.3 From 9cd97796797b9b2853c6458a7f4e9347bb212978 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Fri, 15 Mar 2019 11:56:10 +0100 Subject: update notebooks and documentation --- docs/cache_nbrun | 2 +- .../source/auto_examples/auto_examples_jupyter.zip | Bin 123577 -> 122957 bytes docs/source/auto_examples/auto_examples_python.zip | Bin 81978 -> 81905 bytes .../images/sphx_glr_plot_otda_color_images_001.png | Bin 144957 -> 145014 bytes .../images/sphx_glr_plot_otda_color_images_003.png | Bin 50401 -> 50472 bytes .../images/sphx_glr_plot_otda_color_images_005.png | Bin 234564 -> 326766 bytes ...phx_glr_plot_otda_mapping_colors_images_001.png | Bin 165592 -> 165658 bytes ...phx_glr_plot_otda_mapping_colors_images_003.png | Bin 80722 -> 80796 bytes ...phx_glr_plot_otda_mapping_colors_images_004.png | Bin 541314 -> 512309 bytes .../images/sphx_glr_plot_stochastic_005.png | Bin 10677 -> 10677 bytes .../images/sphx_glr_plot_stochastic_007.png | Bin 9563 -> 9483 bytes .../sphx_glr_plot_otda_color_images_thumb.png | Bin 51085 -> 49131 bytes ...x_glr_plot_otda_mapping_colors_images_thumb.png | Bin 58315 -> 56216 bytes docs/source/auto_examples/index.rst | 2 +- .../auto_examples/plot_otda_color_images.ipynb | 194 ++++++++++----------- .../source/auto_examples/plot_otda_color_images.py | 8 +- .../auto_examples/plot_otda_color_images.rst | 21 ++- .../plot_otda_mapping_colors_images.ipynb | 192 ++++++++++---------- .../plot_otda_mapping_colors_images.py | 2 +- .../plot_otda_mapping_colors_images.rst | 77 ++++---- docs/source/auto_examples/plot_stochastic.ipynb | 44 +---- docs/source/auto_examples/plot_stochastic.py | 11 +- docs/source/auto_examples/plot_stochastic.rst | 97 ++++------- 23 files changed, 298 insertions(+), 352 deletions(-) (limited to 'docs/source/auto_examples/plot_otda_color_images.py') diff --git a/docs/cache_nbrun b/docs/cache_nbrun index 575adc8..6f10375 100644 --- a/docs/cache_nbrun +++ b/docs/cache_nbrun @@ -1 +1 @@ -{"plot_otda_mapping_colors_images.ipynb": "4f0587a00a3c082799a75a0ed36e9ce1", "plot_optim_OTreg.ipynb": "481801bb0d133ef350a65179cf8f739a", "plot_barycenter_1D.ipynb": "5f6fb8aebd8e2e91ebc77c923cb112b3", "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_convolutional_barycenter.ipynb": "a72bb3716a1baaffd81ae267a673f9b6", "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": "cc8bf9a857f52e4a159fe71dfda19018", "plot_optim_OTreg.ipynb": "481801bb0d133ef350a65179cf8f739a", "plot_otda_color_images.ipynb": "f804d5806c7ac1a0901e4542b1eaa77b", "plot_stochastic.ipynb": "e18253354c8c1d72567a4259eb1094f7", "plot_WDA.ipynb": "27f8de4c6d7db46497076523673eedfb", "plot_otda_linear_mapping.ipynb": "a472c767abe82020e0a58125a528785c", "plot_OT_1D_smooth.ipynb": "3a059103652225a0c78ea53895cf79e5", "plot_OT_L1_vs_L2.ipynb": "5d565b8aaf03be4309eba731127851dc", "plot_barycenter_1D.ipynb": "5f6fb8aebd8e2e91ebc77c923cb112b3", "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_convolutional_barycenter.ipynb": "a72bb3716a1baaffd81ae267a673f9b6", "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 304bb06..88e1e9b 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 3be8a76..120a586 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_otda_color_images_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png index 95f882a..7de991a 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png index aa1a5d3..aac929b 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png index d219bb3..5b8101b 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png index 33134fc..d77e68a 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png index 42197e3..1199903 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png index d9101da..1c73e43 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_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 index 3d1e239..42e5007 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_005.png 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_007.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png index 986aa96..cda643b 100644 Binary files a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png and b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png index a919055..4d90437 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png index f7fd217..61a5137 100644 Binary files a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png differ diff --git a/docs/source/auto_examples/index.rst b/docs/source/auto_examples/index.rst index 259fca1..17a9710 100644 --- a/docs/source/auto_examples/index.rst +++ b/docs/source/auto_examples/index.rst @@ -229,7 +229,7 @@ This is a gallery of all the POT example files. .. raw:: html -
+
.. only:: html diff --git a/docs/source/auto_examples/plot_otda_color_images.ipynb b/docs/source/auto_examples/plot_otda_color_images.ipynb index 2daf406..103bdec 100644 --- a/docs/source/auto_examples/plot_otda_color_images.ipynb +++ b/docs/source/auto_examples/plot_otda_color_images.ipynb @@ -1,144 +1,144 @@ { - "nbformat_minor": 0, - "nbformat": 4, "cells": [ { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, - { + }, + "outputs": [], "source": [ - "\n# OT for image color adaptation\n\n\nThis example presents a way of transferring colors between two image\nwith Optimal Transport as introduced in [6]\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014).\nRegularized discrete optimal transport.\nSIAM Journal on Imaging Sciences, 7(3), 1853-1882.\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + "%matplotlib inline" + ] + }, { - "execution_count": null, - "cell_type": "code", + "cell_type": "markdown", + "metadata": {}, "source": [ - "# Authors: Remi Flamary \n# Stanislas Chambon \n#\n# License: MIT License\n\nimport numpy as np\nfrom scipy import ndimage\nimport matplotlib.pylab as pl\nimport ot\n\n\nr = np.random.RandomState(42)\n\n\ndef im2mat(I):\n \"\"\"Converts and image to matrix (one pixel per line)\"\"\"\n return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))\n\n\ndef mat2im(X, shape):\n \"\"\"Converts back a matrix to an image\"\"\"\n return X.reshape(shape)\n\n\ndef minmax(I):\n return np.clip(I, 0, 1)" - ], - "outputs": [], + "\n# OT for image color adaptation\n\n\nThis example presents a way of transferring colors between two images\nwith Optimal Transport as introduced in [6]\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014).\nRegularized discrete optimal transport.\nSIAM Journal on Imaging Sciences, 7(3), 1853-1882.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "# Authors: Remi Flamary \n# Stanislas Chambon \n#\n# License: MIT License\n\nimport numpy as np\nfrom scipy import ndimage\nimport matplotlib.pylab as pl\nimport ot\n\n\nr = np.random.RandomState(42)\n\n\ndef im2mat(I):\n \"\"\"Converts an image to matrix (one pixel per line)\"\"\"\n return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))\n\n\ndef mat2im(X, shape):\n \"\"\"Converts back a matrix to an image\"\"\"\n return X.reshape(shape)\n\n\ndef minmax(I):\n return np.clip(I, 0, 1)" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Generate data\n-------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "# Loading images\nI1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256\nI2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256\n\nX1 = im2mat(I1)\nX2 = im2mat(I2)\n\n# training samples\nnb = 1000\nidx1 = r.randint(X1.shape[0], size=(nb,))\nidx2 = r.randint(X2.shape[0], size=(nb,))\n\nXs = X1[idx1, :]\nXt = X2[idx2, :]" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "# Loading images\nI1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256\nI2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256\n\nX1 = im2mat(I1)\nX2 = im2mat(I2)\n\n# training samples\nnb = 1000\nidx1 = r.randint(X1.shape[0], size=(nb,))\nidx2 = r.randint(X2.shape[0], size=(nb,))\n\nXs = X1[idx1, :]\nXt = X2[idx2, :]" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Plot original image\n-------------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "pl.figure(1, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "pl.figure(1, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Scatter plot of colors\n----------------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "pl.figure(2, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 2], c=Xs)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 2], c=Xt)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\npl.tight_layout()" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "pl.figure(2, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 2], c=Xs)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 2], c=Xt)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\npl.tight_layout()" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Instantiate the different transport algorithms and fit them\n-----------------------------------------------------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "# EMDTransport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\n\n# SinkhornTransport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\n\n# prediction between images (using out of sample prediction as in [6])\ntransp_Xs_emd = ot_emd.transform(Xs=X1)\ntransp_Xt_emd = ot_emd.inverse_transform(Xt=X2)\n\ntransp_Xs_sinkhorn = ot_emd.transform(Xs=X1)\ntransp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2)\n\nI1t = minmax(mat2im(transp_Xs_emd, I1.shape))\nI2t = minmax(mat2im(transp_Xt_emd, I2.shape))\n\nI1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape))\nI2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape))" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "# EMDTransport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\n\n# SinkhornTransport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\n\n# prediction between images (using out of sample prediction as in [6])\ntransp_Xs_emd = ot_emd.transform(Xs=X1)\ntransp_Xt_emd = ot_emd.inverse_transform(Xt=X2)\n\ntransp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1)\ntransp_Xt_sinkhorn = ot_sinkhorn.inverse_transform(Xt=X2)\n\nI1t = minmax(mat2im(transp_Xs_emd, I1.shape))\nI2t = minmax(mat2im(transp_Xt_emd, I2.shape))\n\nI1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape))\nI2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape))" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Plot new images\n---------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "pl.figure(3, figsize=(8, 4))\n\npl.subplot(2, 3, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(2, 3, 2)\npl.imshow(I1t)\npl.axis('off')\npl.title('Image 1 Adapt')\n\npl.subplot(2, 3, 3)\npl.imshow(I1te)\npl.axis('off')\npl.title('Image 1 Adapt (reg)')\n\npl.subplot(2, 3, 4)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')\n\npl.subplot(2, 3, 5)\npl.imshow(I2t)\npl.axis('off')\npl.title('Image 2 Adapt')\n\npl.subplot(2, 3, 6)\npl.imshow(I2te)\npl.axis('off')\npl.title('Image 2 Adapt (reg)')\npl.tight_layout()\n\npl.show()" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } + }, + "outputs": [], + "source": [ + "pl.figure(3, figsize=(8, 4))\n\npl.subplot(2, 3, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(2, 3, 2)\npl.imshow(I1t)\npl.axis('off')\npl.title('Image 1 Adapt')\n\npl.subplot(2, 3, 3)\npl.imshow(I1te)\npl.axis('off')\npl.title('Image 1 Adapt (reg)')\n\npl.subplot(2, 3, 4)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')\n\npl.subplot(2, 3, 5)\npl.imshow(I2t)\npl.axis('off')\npl.title('Image 2 Adapt')\n\npl.subplot(2, 3, 6)\npl.imshow(I2te)\npl.axis('off')\npl.title('Image 2 Adapt (reg)')\npl.tight_layout()\n\npl.show()" + ] } - ], + ], "metadata": { "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, "language_info": { - "mimetype": "text/x-python", - "nbconvert_exporter": "python", - "name": "python", - "file_extension": ".py", - "version": "2.7.12", - "pygments_lexer": "ipython2", "codemirror_mode": { - "version": 2, - "name": "ipython" - } + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.7" } - } + }, + "nbformat": 4, + "nbformat_minor": 0 } \ No newline at end of file diff --git a/docs/source/auto_examples/plot_otda_color_images.py b/docs/source/auto_examples/plot_otda_color_images.py index e77aec0..62383a2 100644 --- a/docs/source/auto_examples/plot_otda_color_images.py +++ b/docs/source/auto_examples/plot_otda_color_images.py @@ -4,7 +4,7 @@ OT for image color adaptation ============================= -This example presents a way of transferring colors between two image +This example presents a way of transferring colors between two images with Optimal Transport as introduced in [6] [6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). @@ -27,7 +27,7 @@ r = np.random.RandomState(42) def im2mat(I): - """Converts and image to matrix (one pixel per line)""" + """Converts an image to matrix (one pixel per line)""" return I.reshape((I.shape[0] * I.shape[1], I.shape[2])) @@ -115,8 +115,8 @@ ot_sinkhorn.fit(Xs=Xs, Xt=Xt) transp_Xs_emd = ot_emd.transform(Xs=X1) transp_Xt_emd = ot_emd.inverse_transform(Xt=X2) -transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) -transp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2) +transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1) +transp_Xt_sinkhorn = ot_sinkhorn.inverse_transform(Xt=X2) I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) diff --git a/docs/source/auto_examples/plot_otda_color_images.rst b/docs/source/auto_examples/plot_otda_color_images.rst index 9c31ba7..ab0406e 100644 --- a/docs/source/auto_examples/plot_otda_color_images.rst +++ b/docs/source/auto_examples/plot_otda_color_images.rst @@ -7,7 +7,7 @@ OT for image color adaptation ============================= -This example presents a way of transferring colors between two image +This example presents a way of transferring colors between two images with Optimal Transport as introduced in [6] [6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). @@ -34,7 +34,7 @@ SIAM Journal on Imaging Sciences, 7(3), 1853-1882. def im2mat(I): - """Converts and image to matrix (one pixel per line)""" + """Converts an image to matrix (one pixel per line)""" return I.reshape((I.shape[0] * I.shape[1], I.shape[2])) @@ -168,8 +168,8 @@ Instantiate the different transport algorithms and fit them transp_Xs_emd = ot_emd.transform(Xs=X1) transp_Xt_emd = ot_emd.inverse_transform(Xt=X2) - transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) - transp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2) + transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1) + transp_Xt_sinkhorn = ot_sinkhorn.inverse_transform(Xt=X2) I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) @@ -235,11 +235,13 @@ Plot new images -**Total running time of the script:** ( 3 minutes 16.469 seconds) +**Total running time of the script:** ( 3 minutes 55.541 seconds) -.. container:: sphx-glr-footer +.. only :: html + + .. container:: sphx-glr-footer .. container:: sphx-glr-download @@ -252,6 +254,9 @@ Plot new images :download:`Download Jupyter notebook: plot_otda_color_images.ipynb ` -.. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb index 56caa8a..baffef4 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb @@ -1,144 +1,144 @@ { - "nbformat_minor": 0, - "nbformat": 4, "cells": [ { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "\n# OT for image color adaptation with mapping estimation\n\n\nOT for domain adaptation with image color adaptation [6] with mapping\nestimation [8].\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized\n discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3),\n 1853-1882.\n[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, \"Mapping estimation for\n discrete optimal transport\", Neural Information Processing Systems (NIPS),\n 2016.\n\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "# Authors: Remi Flamary \n# Stanislas Chambon \n#\n# License: MIT License\n\nimport numpy as np\nfrom scipy import ndimage\nimport matplotlib.pylab as pl\nimport ot\n\nr = np.random.RandomState(42)\n\n\ndef im2mat(I):\n \"\"\"Converts and image to matrix (one pixel per line)\"\"\"\n return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))\n\n\ndef mat2im(X, shape):\n \"\"\"Converts back a matrix to an image\"\"\"\n return X.reshape(shape)\n\n\ndef minmax(I):\n return np.clip(I, 0, 1)" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "# Authors: Remi Flamary \n# Stanislas Chambon \n#\n# License: MIT License\n\nimport numpy as np\nfrom scipy import ndimage\nimport matplotlib.pylab as pl\nimport ot\n\nr = np.random.RandomState(42)\n\n\ndef im2mat(I):\n \"\"\"Converts and image to matrix (one pixel per line)\"\"\"\n return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))\n\n\ndef mat2im(X, shape):\n \"\"\"Converts back a matrix to an image\"\"\"\n return X.reshape(shape)\n\n\ndef minmax(I):\n return np.clip(I, 0, 1)" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Generate data\n-------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "# Loading images\nI1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256\nI2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256\n\n\nX1 = im2mat(I1)\nX2 = im2mat(I2)\n\n# training samples\nnb = 1000\nidx1 = r.randint(X1.shape[0], size=(nb,))\nidx2 = r.randint(X2.shape[0], size=(nb,))\n\nXs = X1[idx1, :]\nXt = X2[idx2, :]" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "# Loading images\nI1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256\nI2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256\n\n\nX1 = im2mat(I1)\nX2 = im2mat(I2)\n\n# training samples\nnb = 1000\nidx1 = r.randint(X1.shape[0], size=(nb,))\nidx2 = r.randint(X2.shape[0], size=(nb,))\n\nXs = X1[idx1, :]\nXt = X2[idx2, :]" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Domain adaptation for pixel distribution transfer\n-------------------------------------------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "# EMDTransport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\ntransp_Xs_emd = ot_emd.transform(Xs=X1)\nImage_emd = minmax(mat2im(transp_Xs_emd, I1.shape))\n\n# SinkhornTransport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\ntransp_Xs_sinkhorn = ot_emd.transform(Xs=X1)\nImage_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape))\n\not_mapping_linear = ot.da.MappingTransport(\n mu=1e0, eta=1e-8, bias=True, max_iter=20, verbose=True)\not_mapping_linear.fit(Xs=Xs, Xt=Xt)\n\nX1tl = ot_mapping_linear.transform(Xs=X1)\nImage_mapping_linear = minmax(mat2im(X1tl, I1.shape))\n\not_mapping_gaussian = ot.da.MappingTransport(\n mu=1e0, eta=1e-2, sigma=1, bias=False, max_iter=10, verbose=True)\not_mapping_gaussian.fit(Xs=Xs, Xt=Xt)\n\nX1tn = ot_mapping_gaussian.transform(Xs=X1) # use the estimated mapping\nImage_mapping_gaussian = minmax(mat2im(X1tn, I1.shape))" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "# EMDTransport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\ntransp_Xs_emd = ot_emd.transform(Xs=X1)\nImage_emd = minmax(mat2im(transp_Xs_emd, I1.shape))\n\n# SinkhornTransport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\ntransp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1)\nImage_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape))\n\not_mapping_linear = ot.da.MappingTransport(\n mu=1e0, eta=1e-8, bias=True, max_iter=20, verbose=True)\not_mapping_linear.fit(Xs=Xs, Xt=Xt)\n\nX1tl = ot_mapping_linear.transform(Xs=X1)\nImage_mapping_linear = minmax(mat2im(X1tl, I1.shape))\n\not_mapping_gaussian = ot.da.MappingTransport(\n mu=1e0, eta=1e-2, sigma=1, bias=False, max_iter=10, verbose=True)\not_mapping_gaussian.fit(Xs=Xs, Xt=Xt)\n\nX1tn = ot_mapping_gaussian.transform(Xs=X1) # use the estimated mapping\nImage_mapping_gaussian = minmax(mat2im(X1tn, I1.shape))" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Plot original images\n--------------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "pl.figure(1, figsize=(6.4, 3))\npl.subplot(1, 2, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')\npl.tight_layout()" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "pl.figure(1, figsize=(6.4, 3))\npl.subplot(1, 2, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')\npl.tight_layout()" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Plot pixel values distribution\n------------------------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "pl.figure(2, figsize=(6.4, 5))\n\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 2], c=Xs)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 2], c=Xt)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\npl.tight_layout()" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "pl.figure(2, figsize=(6.4, 5))\n\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 2], c=Xs)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 2], c=Xt)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\npl.tight_layout()" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "Plot transformed images\n-----------------------\n\n" - ], - "cell_type": "markdown", - "metadata": {} - }, + ] + }, { - "execution_count": null, - "cell_type": "code", - "source": [ - "pl.figure(2, figsize=(10, 5))\n\npl.subplot(2, 3, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Im. 1')\n\npl.subplot(2, 3, 4)\npl.imshow(I2)\npl.axis('off')\npl.title('Im. 2')\n\npl.subplot(2, 3, 2)\npl.imshow(Image_emd)\npl.axis('off')\npl.title('EmdTransport')\n\npl.subplot(2, 3, 5)\npl.imshow(Image_sinkhorn)\npl.axis('off')\npl.title('SinkhornTransport')\n\npl.subplot(2, 3, 3)\npl.imshow(Image_mapping_linear)\npl.axis('off')\npl.title('MappingTransport (linear)')\n\npl.subplot(2, 3, 6)\npl.imshow(Image_mapping_gaussian)\npl.axis('off')\npl.title('MappingTransport (gaussian)')\npl.tight_layout()\n\npl.show()" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } + }, + "outputs": [], + "source": [ + "pl.figure(2, figsize=(10, 5))\n\npl.subplot(2, 3, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Im. 1')\n\npl.subplot(2, 3, 4)\npl.imshow(I2)\npl.axis('off')\npl.title('Im. 2')\n\npl.subplot(2, 3, 2)\npl.imshow(Image_emd)\npl.axis('off')\npl.title('EmdTransport')\n\npl.subplot(2, 3, 5)\npl.imshow(Image_sinkhorn)\npl.axis('off')\npl.title('SinkhornTransport')\n\npl.subplot(2, 3, 3)\npl.imshow(Image_mapping_linear)\npl.axis('off')\npl.title('MappingTransport (linear)')\n\npl.subplot(2, 3, 6)\npl.imshow(Image_mapping_gaussian)\npl.axis('off')\npl.title('MappingTransport (gaussian)')\npl.tight_layout()\n\npl.show()" + ] } - ], + ], "metadata": { "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, "language_info": { - "mimetype": "text/x-python", - "nbconvert_exporter": "python", - "name": "python", - "file_extension": ".py", - "version": "2.7.12", - "pygments_lexer": "ipython2", "codemirror_mode": { - "version": 2, - "name": "ipython" - } + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.7" } - } + }, + "nbformat": 4, + "nbformat_minor": 0 } \ No newline at end of file diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.py b/docs/source/auto_examples/plot_otda_mapping_colors_images.py index 5f1e844..a20eca8 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.py +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.py @@ -77,7 +77,7 @@ Image_emd = minmax(mat2im(transp_Xs_emd, I1.shape)) # SinkhornTransport ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) ot_sinkhorn.fit(Xs=Xs, Xt=Xt) -transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) +transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1) Image_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) ot_mapping_linear = ot.da.MappingTransport( diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.rst b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst index 8394fb0..2afdc8a 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.rst +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst @@ -104,7 +104,7 @@ Domain adaptation for pixel distribution transfer # SinkhornTransport ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) ot_sinkhorn.fit(Xs=Xs, Xt=Xt) - transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) + transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1) Image_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) ot_mapping_linear = ot.da.MappingTransport( @@ -132,39 +132,39 @@ Domain adaptation for pixel distribution transfer It. |Loss |Delta loss -------------------------------- - 0|3.680518e+02|0.000000e+00 - 1|3.592439e+02|-2.393116e-02 - 2|3.590632e+02|-5.030248e-04 - 3|3.589698e+02|-2.601358e-04 - 4|3.589118e+02|-1.614977e-04 - 5|3.588724e+02|-1.097608e-04 - 6|3.588436e+02|-8.035205e-05 - 7|3.588215e+02|-6.141923e-05 - 8|3.588042e+02|-4.832627e-05 - 9|3.587902e+02|-3.909574e-05 - 10|3.587786e+02|-3.225418e-05 - 11|3.587688e+02|-2.712592e-05 - 12|3.587605e+02|-2.314041e-05 - 13|3.587534e+02|-1.991287e-05 - 14|3.587471e+02|-1.744348e-05 - 15|3.587416e+02|-1.544523e-05 - 16|3.587367e+02|-1.364654e-05 - 17|3.587323e+02|-1.230435e-05 - 18|3.587284e+02|-1.093370e-05 - 19|3.587276e+02|-2.052728e-06 + 0|3.680534e+02|0.000000e+00 + 1|3.592501e+02|-2.391854e-02 + 2|3.590682e+02|-5.061555e-04 + 3|3.589745e+02|-2.610227e-04 + 4|3.589167e+02|-1.611644e-04 + 5|3.588768e+02|-1.109242e-04 + 6|3.588482e+02|-7.972733e-05 + 7|3.588261e+02|-6.166174e-05 + 8|3.588086e+02|-4.871697e-05 + 9|3.587946e+02|-3.919056e-05 + 10|3.587830e+02|-3.228124e-05 + 11|3.587731e+02|-2.744744e-05 + 12|3.587648e+02|-2.334451e-05 + 13|3.587576e+02|-1.995629e-05 + 14|3.587513e+02|-1.761058e-05 + 15|3.587457e+02|-1.542568e-05 + 16|3.587408e+02|-1.366315e-05 + 17|3.587365e+02|-1.221732e-05 + 18|3.587325e+02|-1.102488e-05 + 19|3.587303e+02|-6.062107e-06 It. |Loss |Delta loss -------------------------------- - 0|3.784758e+02|0.000000e+00 - 1|3.646352e+02|-3.656911e-02 - 2|3.642861e+02|-9.574714e-04 - 3|3.641523e+02|-3.672061e-04 - 4|3.640788e+02|-2.020990e-04 - 5|3.640321e+02|-1.282701e-04 - 6|3.640002e+02|-8.751240e-05 - 7|3.639765e+02|-6.521203e-05 - 8|3.639582e+02|-5.007767e-05 - 9|3.639439e+02|-3.938917e-05 - 10|3.639323e+02|-3.187865e-05 + 0|3.784871e+02|0.000000e+00 + 1|3.646491e+02|-3.656142e-02 + 2|3.642975e+02|-9.642655e-04 + 3|3.641626e+02|-3.702413e-04 + 4|3.640888e+02|-2.026301e-04 + 5|3.640419e+02|-1.289607e-04 + 6|3.640097e+02|-8.831646e-05 + 7|3.639861e+02|-6.487612e-05 + 8|3.639679e+02|-4.994063e-05 + 9|3.639536e+02|-3.941436e-05 + 10|3.639419e+02|-3.209753e-05 Plot original images @@ -283,11 +283,13 @@ Plot transformed images -**Total running time of the script:** ( 2 minutes 52.212 seconds) +**Total running time of the script:** ( 3 minutes 14.206 seconds) -.. container:: sphx-glr-footer +.. only :: html + + .. container:: sphx-glr-footer .. container:: sphx-glr-download @@ -300,6 +302,9 @@ Plot transformed images :download:`Download Jupyter notebook: plot_otda_mapping_colors_images.ipynb ` -.. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery `_ +.. 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 index c6f0013..7f6ff3d 100644 --- a/docs/source/auto_examples/plot_stochastic.ipynb +++ b/docs/source/auto_examples/plot_stochastic.ipynb @@ -33,25 +33,7 @@ "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" + "COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM\n############################################################################\n############################################################################\n DISCRETE CASE:\n\n Sample two discrete measures for the discrete case\n ---------------------------------------------\n\n Define 2 discrete measures a and b, the points where are defined the source\n and the target measures and finally the cost matrix c.\n\n" ] }, { @@ -87,7 +69,7 @@ "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" + "SEMICONTINOUS CASE:\n\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" ] }, { @@ -202,25 +184,7 @@ "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" + "COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM\n############################################################################\n############################################################################\n SEMICONTINOUS CASE:\n\n Sample one general measure a, one discrete measures b for the semicontinous\n case\n ---------------------------------------------\n\n Define one general measure a, one discrete measures b, the points where\n are defined the source and the target measures and finally the cost matrix c.\n\n" ] }, { @@ -323,7 +287,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/docs/source/auto_examples/plot_stochastic.py b/docs/source/auto_examples/plot_stochastic.py index b9375d4..742f8d9 100644 --- a/docs/source/auto_examples/plot_stochastic.py +++ b/docs/source/auto_examples/plot_stochastic.py @@ -21,9 +21,9 @@ import ot.plot ############################################################################# # COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM ############################################################################# -print("------------SEMI-DUAL PROBLEM------------") ############################################################################# -# DISCRETE CASE +# DISCRETE CASE: +# # Sample two discrete measures for the discrete case # --------------------------------------------- # @@ -57,7 +57,8 @@ sag_pi = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method, print(sag_pi) ############################################################################# -# SEMICONTINOUS CASE +# SEMICONTINOUS CASE: +# # Sample one general measure a, one discrete measures b for the semicontinous # case # --------------------------------------------- @@ -139,9 +140,9 @@ pl.show() ############################################################################# # COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM ############################################################################# -print("------------DUAL PROBLEM------------") ############################################################################# -# SEMICONTINOUS CASE +# SEMICONTINOUS CASE: +# # Sample one general measure a, one discrete measures b for the semicontinous # case # --------------------------------------------- diff --git a/docs/source/auto_examples/plot_stochastic.rst b/docs/source/auto_examples/plot_stochastic.rst index a49bc05..d531045 100644 --- a/docs/source/auto_examples/plot_stochastic.rst +++ b/docs/source/auto_examples/plot_stochastic.rst @@ -34,29 +34,14 @@ algorithms for descrete and semicontinous measures from the POT library. COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM ############################################################################ +############################################################################ + DISCRETE CASE: + Sample two discrete measures for the discrete case + --------------------------------------------- - -.. 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. + Define 2 discrete measures a and b, the points where are defined the source + and the target measures and finally the cost matrix c. @@ -115,7 +100,8 @@ results. [4.15462212e-02 2.65987989e-02 7.23177216e-02 2.39440107e-03]] -SEMICONTINOUS CASE +SEMICONTINOUS CASE: + Sample one general measure a, one discrete measures b for the semicontinous case --------------------------------------------- @@ -174,15 +160,15 @@ results. 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]] + [3.98220325 7.76235856 3.97645524 2.72051681 1.23219313 3.07696856 + 2.84476972] [-2.65544161 -2.50838395 -0.9397765 6.10360206] + [[2.34528761e-02 1.00491956e-01 1.89058354e-02 6.47543413e-06] + [1.16616747e-01 1.32074516e-02 1.45653361e-03 1.15764107e-02] + [3.16154850e-03 7.42892944e-02 6.54061055e-02 1.94426150e-07] + [2.33152216e-02 3.27486992e-02 8.61986263e-02 5.94595747e-04] + [6.34131496e-03 5.31975896e-04 8.12724003e-03 1.27856612e-01] + [1.41744829e-02 6.49096245e-04 1.42704389e-03 1.26606520e-01] + [3.73127657e-02 2.62526499e-02 7.57727161e-02 3.51901117e-03]] Compare the results with the Sinkhorn algorithm @@ -288,30 +274,15 @@ Plot Sinkhorn results COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM ############################################################################ +############################################################################ + SEMICONTINOUS CASE: + Sample one general measure a, one discrete measures b for the semicontinous + case + --------------------------------------------- - -.. 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. + 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. @@ -365,15 +336,15 @@ Call ot.solve_dual_entropic and plot the results. 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]] + [0.92449986 2.75486107 1.07923806 0.02741145 0.61355413 1.81961594 + 0.12072562] [0.33831611 0.46806842 1.5640451 4.96947652] + [[2.20001105e-02 9.26497883e-02 1.08654588e-02 9.78995555e-08] + [1.55669974e-02 1.73279561e-03 1.19120878e-04 2.49058251e-05] + [3.48198483e-03 8.04151063e-02 4.41335396e-02 3.45115752e-09] + [3.14927954e-02 4.34760520e-02 7.13338154e-02 1.29442395e-05] + [6.81836550e-02 5.62182457e-03 5.35386584e-02 2.21568095e-02] + [8.04671052e-02 3.62163462e-03 4.96331605e-03 1.15837801e-02] + [4.88644009e-02 3.37903481e-02 6.07955004e-02 7.42743505e-05]] Compare the results with the Sinkhorn algorithm @@ -448,7 +419,7 @@ Plot Sinkhorn results -**Total running time of the script:** ( 0 minutes 22.857 seconds) +**Total running time of the script:** ( 0 minutes 20.889 seconds) -- cgit v1.2.3