|
| 1 | +.. only:: html |
| 2 | + |
| 3 | + .. note:: |
| 4 | + :class: sphx-glr-download-link-note |
| 5 | + |
| 6 | + Click :ref:`here <sphx_glr_download_auto_examples_plot_partial_wass_and_gromov.py>` to download the full example code |
| 7 | + .. rst-class:: sphx-glr-example-title |
| 8 | + |
| 9 | + .. _sphx_glr_auto_examples_plot_partial_wass_and_gromov.py: |
| 10 | + |
| 11 | + |
| 12 | +========================== |
| 13 | +Partial Wasserstein and Gromov-Wasserstein example |
| 14 | +========================== |
| 15 | + |
| 16 | +This example is designed to show how to use the Partial (Gromov-)Wassertsein |
| 17 | +distance computation in POT. |
| 18 | + |
| 19 | + |
| 20 | +.. code-block:: default |
| 21 | +
|
| 22 | +
|
| 23 | + # Author: Laetitia Chapel <[email protected]> |
| 24 | + # License: MIT License |
| 25 | +
|
| 26 | + # necessary for 3d plot even if not used |
| 27 | + from mpl_toolkits.mplot3d import Axes3D # noqa |
| 28 | + import scipy as sp |
| 29 | + import numpy as np |
| 30 | + import matplotlib.pylab as pl |
| 31 | + import ot |
| 32 | +
|
| 33 | +
|
| 34 | +
|
| 35 | +
|
| 36 | +
|
| 37 | +
|
| 38 | +
|
| 39 | +
|
| 40 | +
|
| 41 | +Sample two 2D Gaussian distributions and plot them |
| 42 | +-------------------------------------------------- |
| 43 | + |
| 44 | +For demonstration purpose, we sample two Gaussian distributions in 2-d |
| 45 | +spaces and add some random noise. |
| 46 | + |
| 47 | + |
| 48 | +.. code-block:: default |
| 49 | +
|
| 50 | +
|
| 51 | +
|
| 52 | + n_samples = 20 # nb samples (gaussian) |
| 53 | + n_noise = 20 # nb of samples (noise) |
| 54 | +
|
| 55 | + mu = np.array([0, 0]) |
| 56 | + cov = np.array([[1, 0], [0, 2]]) |
| 57 | +
|
| 58 | + xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov) |
| 59 | + xs = np.append(xs, (np.random.rand(n_noise, 2) + 1) * 4).reshape((-1, 2)) |
| 60 | + xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov) |
| 61 | + xt = np.append(xt, (np.random.rand(n_noise, 2) + 1) * -3).reshape((-1, 2)) |
| 62 | +
|
| 63 | + M = sp.spatial.distance.cdist(xs, xt) |
| 64 | +
|
| 65 | + fig = pl.figure() |
| 66 | + ax1 = fig.add_subplot(131) |
| 67 | + ax1.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') |
| 68 | + ax2 = fig.add_subplot(132) |
| 69 | + ax2.scatter(xt[:, 0], xt[:, 1], color='r') |
| 70 | + ax3 = fig.add_subplot(133) |
| 71 | + ax3.imshow(M) |
| 72 | + pl.show() |
| 73 | +
|
| 74 | +
|
| 75 | +
|
| 76 | +
|
| 77 | +.. image:: /auto_examples/images/sphx_glr_plot_partial_wass_and_gromov_001.png |
| 78 | + :class: sphx-glr-single-img |
| 79 | + |
| 80 | + |
| 81 | +.. rst-class:: sphx-glr-script-out |
| 82 | + |
| 83 | + Out: |
| 84 | + |
| 85 | + .. code-block:: none |
| 86 | +
|
| 87 | + /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:51: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. |
| 88 | + pl.show() |
| 89 | +
|
| 90 | +
|
| 91 | +
|
| 92 | +
|
| 93 | +Compute partial Wasserstein plans and distance, |
| 94 | +by transporting 50% of the mass |
| 95 | +---------------------------------------------- |
| 96 | + |
| 97 | + |
| 98 | +.. code-block:: default |
| 99 | +
|
| 100 | +
|
| 101 | + p = ot.unif(n_samples + n_noise) |
| 102 | + q = ot.unif(n_samples + n_noise) |
| 103 | +
|
| 104 | + w0, log0 = ot.partial.partial_wasserstein(p, q, M, m=0.5, log=True) |
| 105 | + w, log = ot.partial.entropic_partial_wasserstein(p, q, M, reg=0.1, m=0.5, |
| 106 | + log=True) |
| 107 | +
|
| 108 | + print('Partial Wasserstein distance (m = 0.5): ' + str(log0['partial_w_dist'])) |
| 109 | + print('Entropic partial Wasserstein distance (m = 0.5): ' + |
| 110 | + str(log['partial_w_dist'])) |
| 111 | +
|
| 112 | + pl.figure(1, (10, 5)) |
| 113 | + pl.subplot(1, 2, 1) |
| 114 | + pl.imshow(w0, cmap='jet') |
| 115 | + pl.title('Partial Wasserstein') |
| 116 | + pl.subplot(1, 2, 2) |
| 117 | + pl.imshow(w, cmap='jet') |
| 118 | + pl.title('Entropic partial Wasserstein') |
| 119 | + pl.show() |
| 120 | +
|
| 121 | +
|
| 122 | +
|
| 123 | +
|
| 124 | +
|
| 125 | +.. image:: /auto_examples/images/sphx_glr_plot_partial_wass_and_gromov_002.png |
| 126 | + :class: sphx-glr-single-img |
| 127 | + |
| 128 | + |
| 129 | +.. rst-class:: sphx-glr-script-out |
| 130 | + |
| 131 | + Out: |
| 132 | + |
| 133 | + .. code-block:: none |
| 134 | +
|
| 135 | + Partial Wasserstein distance (m = 0.5): 0.29721185147886475 |
| 136 | + Entropic partial Wasserstein distance (m = 0.5): 0.31204119793315976 |
| 137 | + /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:77: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. |
| 138 | + pl.show() |
| 139 | +
|
| 140 | +
|
| 141 | +
|
| 142 | +
|
| 143 | +Sample one 2D and 3D Gaussian distributions and plot them |
| 144 | +--------------------------------------------------------- |
| 145 | + |
| 146 | +The Gromov-Wasserstein distance allows to compute distances with samples that |
| 147 | +do not belong to the same metric space. For demonstration purpose, we sample |
| 148 | +two Gaussian distributions in 2- and 3-dimensional spaces. |
| 149 | + |
| 150 | + |
| 151 | +.. code-block:: default |
| 152 | +
|
| 153 | +
|
| 154 | + n_samples = 20 # nb samples |
| 155 | + n_noise = 10 # nb of samples (noise) |
| 156 | +
|
| 157 | + p = ot.unif(n_samples + n_noise) |
| 158 | + q = ot.unif(n_samples + n_noise) |
| 159 | +
|
| 160 | + mu_s = np.array([0, 0]) |
| 161 | + cov_s = np.array([[1, 0], [0, 1]]) |
| 162 | +
|
| 163 | + mu_t = np.array([0, 0, 0]) |
| 164 | + cov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) |
| 165 | +
|
| 166 | +
|
| 167 | + xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s) |
| 168 | + xs = np.concatenate((xs, ((np.random.rand(n_noise, 2) + 1) * 4)), axis=0) |
| 169 | + P = sp.linalg.sqrtm(cov_t) |
| 170 | + xt = np.random.randn(n_samples, 3).dot(P) + mu_t |
| 171 | + xt = np.concatenate((xt, ((np.random.rand(n_noise, 3) + 1) * 10)), axis=0) |
| 172 | +
|
| 173 | + fig = pl.figure() |
| 174 | + ax1 = fig.add_subplot(121) |
| 175 | + ax1.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') |
| 176 | + ax2 = fig.add_subplot(122, projection='3d') |
| 177 | + ax2.scatter(xt[:, 0], xt[:, 1], xt[:, 2], color='r') |
| 178 | + pl.show() |
| 179 | +
|
| 180 | +
|
| 181 | +
|
| 182 | +
|
| 183 | +
|
| 184 | +.. image:: /auto_examples/images/sphx_glr_plot_partial_wass_and_gromov_003.png |
| 185 | + :class: sphx-glr-single-img |
| 186 | + |
| 187 | + |
| 188 | +.. rst-class:: sphx-glr-script-out |
| 189 | + |
| 190 | + Out: |
| 191 | + |
| 192 | + .. code-block:: none |
| 193 | +
|
| 194 | + /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:113: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. |
| 195 | + pl.show() |
| 196 | +
|
| 197 | +
|
| 198 | +
|
| 199 | +
|
| 200 | +Compute partial Gromov-Wasserstein plans and distance, |
| 201 | +by transporting 100% and 2/3 of the mass |
| 202 | +----------------------------------------------------- |
| 203 | + |
| 204 | + |
| 205 | +.. code-block:: default |
| 206 | +
|
| 207 | +
|
| 208 | + C1 = sp.spatial.distance.cdist(xs, xs) |
| 209 | + C2 = sp.spatial.distance.cdist(xt, xt) |
| 210 | +
|
| 211 | + print('-----m = 1') |
| 212 | + m = 1 |
| 213 | + res0, log0 = ot.partial.partial_gromov_wasserstein(C1, C2, p, q, m=m, |
| 214 | + log=True) |
| 215 | + res, log = ot.partial.entropic_partial_gromov_wasserstein(C1, C2, p, q, 10, |
| 216 | + m=m, log=True) |
| 217 | +
|
| 218 | + print('Partial Wasserstein distance (m = 1): ' + str(log0['partial_gw_dist'])) |
| 219 | + print('Entropic partial Wasserstein distance (m = 1): ' + |
| 220 | + str(log['partial_gw_dist'])) |
| 221 | +
|
| 222 | + pl.figure(1, (10, 5)) |
| 223 | + pl.title("mass to be transported m = 1") |
| 224 | + pl.subplot(1, 2, 1) |
| 225 | + pl.imshow(res0, cmap='jet') |
| 226 | + pl.title('Partial Wasserstein') |
| 227 | + pl.subplot(1, 2, 2) |
| 228 | + pl.imshow(res, cmap='jet') |
| 229 | + pl.title('Entropic partial Wasserstein') |
| 230 | + pl.show() |
| 231 | +
|
| 232 | + print('-----m = 2/3') |
| 233 | + m = 2 / 3 |
| 234 | + res0, log0 = ot.partial.partial_gromov_wasserstein(C1, C2, p, q, m=m, log=True) |
| 235 | + res, log = ot.partial.entropic_partial_gromov_wasserstein(C1, C2, p, q, 10, |
| 236 | + m=m, log=True) |
| 237 | +
|
| 238 | + print('Partial Wasserstein distance (m = 2/3): ' + |
| 239 | + str(log0['partial_gw_dist'])) |
| 240 | + print('Entropic partial Wasserstein distance (m = 2/3): ' + |
| 241 | + str(log['partial_gw_dist'])) |
| 242 | +
|
| 243 | + pl.figure(1, (10, 5)) |
| 244 | + pl.title("mass to be transported m = 2/3") |
| 245 | + pl.subplot(1, 2, 1) |
| 246 | + pl.imshow(res0, cmap='jet') |
| 247 | + pl.title('Partial Wasserstein') |
| 248 | + pl.subplot(1, 2, 2) |
| 249 | + pl.imshow(res, cmap='jet') |
| 250 | + pl.title('Entropic partial Wasserstein') |
| 251 | + pl.show() |
| 252 | +
|
| 253 | +
|
| 254 | +
|
| 255 | +.. image:: /auto_examples/images/sphx_glr_plot_partial_wass_and_gromov_004.png |
| 256 | + :class: sphx-glr-single-img |
| 257 | + |
| 258 | + |
| 259 | +.. rst-class:: sphx-glr-script-out |
| 260 | + |
| 261 | + Out: |
| 262 | + |
| 263 | + .. code-block:: none |
| 264 | +
|
| 265 | + -----m = 1 |
| 266 | + Partial Wasserstein distance (m = 1): 56.18870587756925 |
| 267 | + Entropic partial Wasserstein distance (m = 1): 57.63642536818668 |
| 268 | + /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:144: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. |
| 269 | + pl.show() |
| 270 | + -----m = 2/3 |
| 271 | + Partial Wasserstein distance (m = 2/3): 0.18550643334550976 |
| 272 | + Entropic partial Wasserstein distance (m = 2/3): 1.0781947761552997 |
| 273 | + /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:159: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. |
| 274 | + pl.subplot(1, 2, 1) |
| 275 | + /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:162: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. |
| 276 | + pl.subplot(1, 2, 2) |
| 277 | + /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:165: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. |
| 278 | + pl.show() |
| 279 | +
|
| 280 | +
|
| 281 | +
|
| 282 | +
|
| 283 | +
|
| 284 | +.. rst-class:: sphx-glr-timing |
| 285 | + |
| 286 | + **Total running time of the script:** ( 0 minutes 1.656 seconds) |
| 287 | + |
| 288 | + |
| 289 | +.. _sphx_glr_download_auto_examples_plot_partial_wass_and_gromov.py: |
| 290 | + |
| 291 | + |
| 292 | +.. only :: html |
| 293 | +
|
| 294 | + .. container:: sphx-glr-footer |
| 295 | + :class: sphx-glr-footer-example |
| 296 | +
|
| 297 | +
|
| 298 | +
|
| 299 | + .. container:: sphx-glr-download sphx-glr-download-python |
| 300 | +
|
| 301 | + :download:`Download Python source code: plot_partial_wass_and_gromov.py <plot_partial_wass_and_gromov.py>` |
| 302 | +
|
| 303 | +
|
| 304 | +
|
| 305 | + .. container:: sphx-glr-download sphx-glr-download-jupyter |
| 306 | +
|
| 307 | + :download:`Download Jupyter notebook: plot_partial_wass_and_gromov.ipynb <plot_partial_wass_and_gromov.ipynb>` |
| 308 | +
|
| 309 | +
|
| 310 | +.. only:: html |
| 311 | + |
| 312 | + .. rst-class:: sphx-glr-signature |
| 313 | + |
| 314 | + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_ |
0 commit comments