.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_solve_variants.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_solve_variants.py: ====================================== Optimal Transport solvers comparison ====================================== This example illustrates the solutions returns for different variants of exact, regularized and unbalanced OT solvers. .. GENERATED FROM PYTHON SOURCE LINES 10-16 .. code-block:: Python # Author: Remi Flamary # # License: MIT License # sphinx_gallery_thumbnail_number = 3 .. GENERATED FROM PYTHON SOURCE LINES 17-24 .. code-block:: Python import numpy as np import matplotlib.pylab as pl import ot import ot.plot from ot.datasets import make_1D_gauss as gauss .. GENERATED FROM PYTHON SOURCE LINES 25-27 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 30-45 .. code-block:: Python n = 50 # nb bins # bin positions x = np.arange(n, dtype=np.float64) # Gaussian distributions a = 0.6 * gauss(n, m=15, s=5) + 0.4 * gauss(n, m=35, s=5) # m= mean, s= std b = gauss(n, m=25, s=5) # loss matrix M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) M /= M.max() .. GENERATED FROM PYTHON SOURCE LINES 46-48 Plot distributions and loss matrix ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 50-56 .. code-block:: Python pl.figure(1, figsize=(6.4, 3)) pl.plot(x, a, "b", label="Source distribution") pl.plot(x, b, "r", label="Target distribution") pl.legend() .. image-sg:: /auto_examples/images/sphx_glr_plot_solve_variants_001.png :alt: plot solve variants :srcset: /auto_examples/images/sphx_glr_plot_solve_variants_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python pl.figure(2, figsize=(5, 5)) ot.plot.plot1D_mat(a, b, M, "Cost matrix M") .. image-sg:: /auto_examples/images/sphx_glr_plot_solve_variants_002.png :alt: plot solve variants :srcset: /auto_examples/images/sphx_glr_plot_solve_variants_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (, , ) .. GENERATED FROM PYTHON SOURCE LINES 62-65 Define Group lasso regularization and gradient ------------------------------------------------ The groups are the first and second half of the columns of G .. GENERATED FROM PYTHON SOURCE LINES 65-85 .. code-block:: Python def reg_gl(G): # group lasso + small l2 reg G1 = G[: n // 2, :] ** 2 G2 = G[n // 2 :, :] ** 2 gl1 = np.sum(np.sqrt(np.sum(G1, 0))) gl2 = np.sum(np.sqrt(np.sum(G2, 0))) return gl1 + gl2 + 0.1 * np.sum(G**2) def grad_gl(G): # gradient of group lasso + small l2 reg G1 = G[: n // 2, :] G2 = G[n // 2 :, :] gl1 = G1 / np.sqrt(np.sum(G1**2, 0, keepdims=True) + 1e-8) gl2 = G2 / np.sqrt(np.sum(G2**2, 0, keepdims=True) + 1e-8) return np.concatenate((gl1, gl2), axis=0) + 0.2 * G reg_type_gl = (reg_gl, grad_gl) .. GENERATED FROM PYTHON SOURCE LINES 86-88 Set up parameters for solvers and solve --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 88-152 .. code-block:: Python lst_regs = ["No Reg.", "Entropic", "L2", "Group Lasso + L2"] lst_unbalanced = [ "Balanced", "Unbalanced KL", "Unbalanced L2", "Unb. TV (Partial)", ] # ["Balanced", "Unb. KL", "Unb. L2", "Unb L1 (partial)"] lst_solvers = [ # name, param for ot.solve function # balanced OT ("Exact OT", dict()), ("Entropic Reg. OT", dict(reg=0.005)), ("L2 Reg OT", dict(reg=1, reg_type="l2")), ("Group Lasso Reg. OT", dict(reg=0.1, reg_type=reg_type_gl)), # unbalanced OT KL ("Unbalanced KL No Reg.", dict(unbalanced=0.005)), ( "Unbalanced KL with KL Reg.", dict(reg=0.0005, unbalanced=0.005, unbalanced_type="kl", reg_type="kl"), ), ( "Unbalanced KL with L2 Reg.", dict(reg=0.5, reg_type="l2", unbalanced=0.005, unbalanced_type="kl"), ), ( "Unbalanced KL with Group Lasso Reg.", dict(reg=0.1, reg_type=reg_type_gl, unbalanced=0.05, unbalanced_type="kl"), ), # unbalanced OT L2 ("Unbalanced L2 No Reg.", dict(unbalanced=0.5, unbalanced_type="l2")), ( "Unbalanced L2 with KL Reg.", dict(reg=0.001, unbalanced=0.2, unbalanced_type="l2"), ), ( "Unbalanced L2 with L2 Reg.", dict(reg=0.1, reg_type="l2", unbalanced=0.2, unbalanced_type="l2"), ), ( "Unbalanced L2 with Group Lasso Reg.", dict(reg=0.05, reg_type=reg_type_gl, unbalanced=0.7, unbalanced_type="l2"), ), # unbalanced OT TV ("Unbalanced TV No Reg.", dict(unbalanced=0.1, unbalanced_type="tv")), ( "Unbalanced TV with KL Reg.", dict(reg=0.001, unbalanced=0.01, unbalanced_type="tv"), ), ( "Unbalanced TV with L2 Reg.", dict(reg=0.1, reg_type="l2", unbalanced=0.01, unbalanced_type="tv"), ), ( "Unbalanced TV with Group Lasso Reg.", dict(reg=0.02, reg_type=reg_type_gl, unbalanced=0.01, unbalanced_type="tv"), ), ] lst_plans = [] for name, param in lst_solvers: G = ot.solve(M, a, b, **param).plan lst_plans.append(G) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/circleci/project/ot/unbalanced/_lbfgs.py:277: UserWarning: The callable functions should be able to handle numpy arrays, wrapper ar added to handle this which comes with overhead warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 153-155 Plot plans ---------- .. GENERATED FROM PYTHON SOURCE LINES 155-179 .. code-block:: Python pl.figure(3, figsize=(9, 9)) for i, bname in enumerate(lst_unbalanced): for j, rname in enumerate(lst_regs): pl.subplot(len(lst_unbalanced), len(lst_regs), i * len(lst_regs) + j + 1) plan = lst_plans[i * len(lst_regs) + j] m2 = plan.sum(0) m1 = plan.sum(1) m1, m2 = m1 / a.max(), m2 / b.max() pl.imshow(plan, cmap="Greys") pl.plot(x, m2 * 10, "r") pl.plot(m1 * 10, x, "b") pl.plot(x, b / b.max() * 10, "r", alpha=0.3) pl.plot(a / a.max() * 10, x, "b", alpha=0.3) # pl.axis('off') pl.tick_params( left=False, right=False, labelleft=False, labelbottom=False, bottom=False ) if i == 0: pl.title(rname) if j == 0: pl.ylabel(bname, fontsize=14) .. image-sg:: /auto_examples/images/sphx_glr_plot_solve_variants_003.png :alt: No Reg., Entropic, L2, Group Lasso + L2 :srcset: /auto_examples/images/sphx_glr_plot_solve_variants_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.315 seconds) .. _sphx_glr_download_auto_examples_plot_solve_variants.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_solve_variants.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_solve_variants.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_solve_variants.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_