.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/unbalanced-partial/plot_regpath.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_unbalanced-partial_plot_regpath.py: ================================================================ Regularization path of l2-penalized unbalanced optimal transport ================================================================ This example illustrate the regularization path for 2D unbalanced optimal transport. We present here both the fully relaxed case and the semi-relaxed case. [Chapel et al., 2021] Chapel, L., Flamary, R., Wu, H., FĂ©votte, C., and Gasso, G. (2021). Unbalanced optimal transport through non-negative penalized linear regression. .. GENERATED FROM PYTHON SOURCE LINES 14-24 .. code-block:: Python # Author: Haoran Wu # License: MIT License # sphinx_gallery_thumbnail_number = 2 import numpy as np import matplotlib.pylab as pl import ot import matplotlib.animation as animation .. GENERATED FROM PYTHON SOURCE LINES 25-27 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 29-48 .. code-block:: Python n = 20 # nb samples mu_s = np.array([-1, -1]) cov_s = np.array([[1, 0], [0, 1]]) mu_t = np.array([4, 4]) cov_t = np.array([[1, -.8], [-.8, 1]]) np.random.seed(0) xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s) xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t) a, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples # loss matrix M = ot.dist(xs, xt) M /= M.max() .. GENERATED FROM PYTHON SOURCE LINES 49-51 Plot data --------- .. GENERATED FROM PYTHON SOURCE LINES 53-61 .. code-block:: Python pl.figure(1) pl.scatter(xs[:, 0], xs[:, 1], c='C0', label='Source') pl.scatter(xt[:, 0], xt[:, 1], c='C1', label='Target') pl.legend(loc=2) pl.title('Source and target distributions') pl.show() .. image-sg:: /auto_examples/unbalanced-partial/images/sphx_glr_plot_regpath_001.png :alt: Source and target distributions :srcset: /auto_examples/unbalanced-partial/images/sphx_glr_plot_regpath_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 62-64 Compute semi-relaxed and fully relaxed regularization paths ----------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 66-73 .. code-block:: Python final_gamma = 1e-6 t, t_list, g_list = ot.regpath.regularization_path(a, b, M, reg=final_gamma, semi_relaxed=False) t2, t_list2, g_list2 = ot.regpath.regularization_path(a, b, M, reg=final_gamma, semi_relaxed=True) .. GENERATED FROM PYTHON SOURCE LINES 74-79 Plot the regularization path ---------------------------- The OT plan is plotted as a function of $\gamma$ that is the inverse of the weight on the marginal relaxations. .. GENERATED FROM PYTHON SOURCE LINES 81-110 .. code-block:: Python pl.figure(2) selected_gamma = [2e-1, 1e-1, 5e-2, 1e-3] for p in range(4): tp = ot.regpath.compute_transport_plan(selected_gamma[p], g_list, t_list) P = tp.reshape((n, n)) pl.subplot(2, 2, p + 1) if P.sum() > 0: P = P / P.max() for i in range(n): for j in range(n): if P[i, j] > 0: pl.plot([xs[i, 0], xt[j, 0]], [xs[i, 1], xt[j, 1]], color='C2', alpha=P[i, j] * 0.3) pl.scatter(xs[:, 0], xs[:, 1], c='C0', alpha=0.2) pl.scatter(xt[:, 0], xt[:, 1], c='C1', alpha=0.2) pl.scatter(xs[:, 0], xs[:, 1], c='C0', s=P.sum(1).ravel() * (1 + p) * 2, label='Re-weighted source', alpha=1) pl.scatter(xt[:, 0], xt[:, 1], c='C1', s=P.sum(0).ravel() * (1 + p) * 2, label='Re-weighted target', alpha=1) pl.plot([], [], color='C2', alpha=0.8, label='OT plan') pl.title(r'$\ell_2$ UOT $\gamma$={}'.format(selected_gamma[p]), fontsize=11) if p < 2: pl.xticks(()) pl.show() .. image-sg:: /auto_examples/unbalanced-partial/images/sphx_glr_plot_regpath_002.png :alt: $\ell_2$ UOT $\gamma$=0.2, $\ell_2$ UOT $\gamma$=0.1, $\ell_2$ UOT $\gamma$=0.05, $\ell_2$ UOT $\gamma$=0.001 :srcset: /auto_examples/unbalanced-partial/images/sphx_glr_plot_regpath_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 111-113 Animation of the regpath for UOT l2 ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 113-150 .. code-block:: Python nv = 50 g_list_v = np.logspace(-.5, -2.5, nv) pl.figure(3) def _update_plot(iv): pl.clf() tp = ot.regpath.compute_transport_plan(g_list_v[iv], g_list, t_list) P = tp.reshape((n, n)) if P.sum() > 0: P = P / P.max() for i in range(n): for j in range(n): if P[i, j] > 0: pl.plot([xs[i, 0], xt[j, 0]], [xs[i, 1], xt[j, 1]], color='C2', alpha=P[i, j] * 0.5) pl.scatter(xs[:, 0], xs[:, 1], c='C0', alpha=0.2) pl.scatter(xt[:, 0], xt[:, 1], c='C1', alpha=0.2) pl.scatter(xs[:, 0], xs[:, 1], c='C0', s=P.sum(1).ravel() * (1 + p) * 4, label='Re-weighted source', alpha=1) pl.scatter(xt[:, 0], xt[:, 1], c='C1', s=P.sum(0).ravel() * (1 + p) * 4, label='Re-weighted target', alpha=1) pl.plot([], [], color='C2', alpha=0.8, label='OT plan') pl.title(r'$\ell_2$ UOT $\gamma$={:1.3f}'.format(g_list_v[iv]), fontsize=11) return 1 i = 0 _update_plot(i) ani = animation.FuncAnimation(pl.gcf(), _update_plot, nv, interval=100, repeat_delay=2000) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 151-153 Plot the semi-relaxed regularization path ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 155-182 .. code-block:: Python pl.figure(4) selected_gamma = [10, 1, 1e-1, 1e-2] for p in range(4): tp = ot.regpath.compute_transport_plan(selected_gamma[p], g_list2, t_list2) P = tp.reshape((n, n)) pl.subplot(2, 2, p + 1) if P.sum() > 0: P = P / P.max() for i in range(n): for j in range(n): if P[i, j] > 0: pl.plot([xs[i, 0], xt[j, 0]], [xs[i, 1], xt[j, 1]], color='C2', alpha=P[i, j] * 0.3) pl.scatter(xs[:, 0], xs[:, 1], c='C0', alpha=0.2) pl.scatter(xt[:, 0], xt[:, 1], c='C1', alpha=1, label='Target marginal') pl.scatter(xs[:, 0], xs[:, 1], c='C0', s=P.sum(1).ravel() * 2 * (1 + p), label='Source marginal', alpha=1) pl.plot([], [], color='C2', alpha=0.8, label='OT plan') pl.title(r'Semi-relaxed $l_2$ UOT $\gamma$={}'.format(selected_gamma[p]), fontsize=11) if p < 2: pl.xticks(()) pl.show() .. image-sg:: /auto_examples/unbalanced-partial/images/sphx_glr_plot_regpath_004.png :alt: Semi-relaxed $l_2$ UOT $\gamma$=10, Semi-relaxed $l_2$ UOT $\gamma$=1, Semi-relaxed $l_2$ UOT $\gamma$=0.1, Semi-relaxed $l_2$ UOT $\gamma$=0.01 :srcset: /auto_examples/unbalanced-partial/images/sphx_glr_plot_regpath_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 183-185 Animation of the regpath for semi-relaxed UOT l2 ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 185-220 .. code-block:: Python nv = 50 g_list_v = np.logspace(2, -2, nv) pl.figure(5) def _update_plot(iv): pl.clf() tp = ot.regpath.compute_transport_plan(g_list_v[iv], g_list2, t_list2) P = tp.reshape((n, n)) if P.sum() > 0: P = P / P.max() for i in range(n): for j in range(n): if P[i, j] > 0: pl.plot([xs[i, 0], xt[j, 0]], [xs[i, 1], xt[j, 1]], color='C2', alpha=P[i, j] * 0.5) pl.scatter(xs[:, 0], xs[:, 1], c='C0', alpha=0.2) pl.scatter(xt[:, 0], xt[:, 1], c='C1', alpha=0.2) pl.scatter(xs[:, 0], xs[:, 1], c='C0', s=P.sum(1).ravel() * (1 + p) * 4, label='Re-weighted source', alpha=1) pl.scatter(xt[:, 0], xt[:, 1], c='C1', s=P.sum(0).ravel() * (1 + p) * 4, label='Re-weighted target', alpha=1) pl.plot([], [], color='C2', alpha=0.8, label='OT plan') pl.title(r'Semi-relaxed $\ell_2$ UOT $\gamma$={:1.3f}'.format(g_list_v[iv]), fontsize=11) return 1 i = 0 _update_plot(i) ani = animation.FuncAnimation(pl.gcf(), _update_plot, nv, interval=100, repeat_delay=2000) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 38.343 seconds) .. _sphx_glr_download_auto_examples_unbalanced-partial_plot_regpath.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_regpath.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_regpath.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_