.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/lowrank/plot_nystroem_approximation.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_lowrank_plot_nystroem_approximation.py: ============================ Nyström approximation for OT ============================ Shows how to use Nyström kernel approximation for approximating the Sinkhorn algorithm in linear time. .. GENERATED FROM PYTHON SOURCE LINES 11-26 .. code-block:: Python # Author: Titouan Vayer # # License: MIT License # sphinx_gallery_thumbnail_number = 2 import numpy as np from ot.lowrank import kernel_nystroem, sinkhorn_low_rank_kernel from ot.bregman import empirical_sinkhorn_nystroem import math import ot import matplotlib.pyplot as plt from matplotlib.colors import LogNorm .. GENERATED FROM PYTHON SOURCE LINES 27-29 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 31-61 .. code-block:: Python offset = 1 n_samples_per_blob = 500 # We use 2D ''blobs'' data random_state = 42 std = 0.2 # standard deviation np.random.seed(random_state) centers = np.array( [ [-offset, -offset], # Class 0 - blob 1 [-offset, offset], # Class 0 - blob 2 [offset, -offset], # Class 1 - blob 1 [offset, offset], # Class 1 - blob 2 ] ) X_list = [] y_list = [] for i, center in enumerate(centers): blob_points = np.random.randn(n_samples_per_blob, 2) * std + center label = 0 if i < 2 else 1 X_list.append(blob_points) y_list.append(np.full(n_samples_per_blob, label)) X = np.vstack(X_list) y = np.concatenate(y_list) Xs = X[y == 0] # source data Xt = X[y == 1] # target data .. GENERATED FROM PYTHON SOURCE LINES 62-64 Plot data --------- .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: Python plt.scatter(Xs[:, 0], Xs[:, 1], label="Source") plt.scatter(Xt[:, 0], Xt[:, 1], label="Target") plt.legend() .. image-sg:: /auto_examples/lowrank/images/sphx_glr_plot_nystroem_approximation_001.png :alt: plot nystroem approximation :srcset: /auto_examples/lowrank/images/sphx_glr_plot_nystroem_approximation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 71-73 Compute the Nyström approximation of the Gaussian kernel -------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 75-83 .. code-block:: Python reg = 5.0 # proportional to the std of the Gaussian kernel anchors = 10 # number of anchor points for the Nyström approximation ot.tic() left_factor, right_factor = kernel_nystroem( Xs, Xt, anchors=anchors, sigma=math.sqrt(reg / 2.0), random_state=random_state ) ot.toc() .. rst-class:: sphx-glr-script-out .. code-block:: none Elapsed time : 0.0008536669993191026 s 0.0008536669993191026 .. GENERATED FROM PYTHON SOURCE LINES 84-88 Use this approximation in a Sinkhorn algorithm with low rank kernel. Each matrix/vector product in the Sinkhorn is accelerated since :math:`Kv = K_1 (K_2^\top v)` can be computed in :math:`O(nr)` time instead of :math:`O(n^2)` .. GENERATED FROM PYTHON SOURCE LINES 90-110 .. code-block:: Python numItermax = 1000 stopThr = 1e-7 verbose = True a, b = None, None warn = True warmstart = None ot.tic() u, v, dict_log = sinkhorn_low_rank_kernel( K1=left_factor, K2=right_factor, a=a, b=b, numItermax=numItermax, stopThr=stopThr, verbose=verbose, log=True, warn=warn, warmstart=warmstart, ) ot.toc() .. rst-class:: sphx-glr-script-out .. code-block:: none It. |Err ------------------- 0|7.482235e-05| Elapsed time : 0.0010054979993583402 s 0.0010054979993583402 .. GENERATED FROM PYTHON SOURCE LINES 111-113 Compare with Sinkhorn --------------------- .. GENERATED FROM PYTHON SOURCE LINES 115-130 .. code-block:: Python M = ot.dist(Xs, Xt) ot.tic() G, log_ = ot.sinkhorn( a=[], b=[], M=M, reg=reg, numItermax=numItermax, verbose=verbose, log=True, warn=warn, warmstart=warmstart, ) ot.toc() .. rst-class:: sphx-glr-script-out .. code-block:: none It. |Err ------------------- 0|7.517180e-05| Elapsed time : 0.012269569999261876 s 0.012269569999261876 .. GENERATED FROM PYTHON SOURCE LINES 131-133 Use directly ot.bregman.empirical_sinkhorn_nystroem -------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 135-146 .. code-block:: Python ot.tic() G_nys = empirical_sinkhorn_nystroem( Xs, Xt, anchors=anchors, reg=reg, numItermax=numItermax, verbose=True, random_state=random_state, )[:] ot.toc() .. rst-class:: sphx-glr-script-out .. code-block:: none It. |Err ------------------- 0|7.482235e-05| Elapsed time : 0.0033195709984283894 s 0.0033195709984283894 .. GENERATED FROM PYTHON SOURCE LINES 147-153 .. code-block:: Python ot.tic() G_sinkh = ot.bregman.empirical_sinkhorn( Xs, Xt, reg=reg, numIterMax=numItermax, verbose=True ) ot.toc() .. rst-class:: sphx-glr-script-out .. code-block:: none It. |Err ------------------- 0|7.517180e-05| Elapsed time : 0.01705986200067855 s 0.01705986200067855 .. GENERATED FROM PYTHON SOURCE LINES 154-156 Compare OT plans ---------------- .. GENERATED FROM PYTHON SOURCE LINES 156-170 .. code-block:: Python fig, ax = plt.subplots(1, 2, figsize=(10, 4), constrained_layout=True) vmin = min(G_sinkh.min(), G_nys.min()) vmax = max(G_sinkh.max(), G_nys.max()) norm = LogNorm(vmin=vmin, vmax=vmax) im0 = ax[0].imshow(G_sinkh, norm=norm, cmap="coolwarm") im1 = ax[1].imshow(G_nys, norm=norm, cmap="coolwarm") cbar = fig.colorbar(im1, ax=ax, orientation="vertical", fraction=0.046, pad=0.04) ax[0].set_title("OT plan Sinkhorn") ax[1].set_title("OT plan Nyström Sinkhorn") for a in ax: a.set_xticks([]) a.set_yticks([]) plt.show() .. image-sg:: /auto_examples/lowrank/images/sphx_glr_plot_nystroem_approximation_002.png :alt: OT plan Sinkhorn, OT plan Nyström Sinkhorn :srcset: /auto_examples/lowrank/images/sphx_glr_plot_nystroem_approximation_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.941 seconds) .. _sphx_glr_download_auto_examples_lowrank_plot_nystroem_approximation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_nystroem_approximation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_nystroem_approximation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_nystroem_approximation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_