diff options
Diffstat (limited to 'docs/source/auto_examples/plot_gromov.ipynb')
-rw-r--r-- | docs/source/auto_examples/plot_gromov.ipynb | 120 |
1 files changed, 96 insertions, 24 deletions
diff --git a/docs/source/auto_examples/plot_gromov.ipynb b/docs/source/auto_examples/plot_gromov.ipynb index 6d6b522..57d6a4a 100644 --- a/docs/source/auto_examples/plot_gromov.ipynb +++ b/docs/source/auto_examples/plot_gromov.ipynb @@ -1,54 +1,126 @@ { + "nbformat_minor": 0, + "nbformat": 4, + "metadata": { + "language_info": { + "file_extension": ".py", + "codemirror_mode": { + "version": 3, + "name": "ipython" + }, + "nbconvert_exporter": "python", + "mimetype": "text/x-python", + "version": "3.5.2", + "name": "python", + "pygments_lexer": "ipython3" + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3", + "language": "python" + } + }, "cells": [ { + "outputs": [], + "source": [ + "%matplotlib inline" + ], "execution_count": null, "metadata": { "collapsed": false }, + "cell_type": "code" + }, + { + "source": [ + "\n# Gromov-Wasserstein example\n\n\nThis example is designed to show how to use the Gromov-Wassertsein distance\ncomputation in POT.\n\n" + ], + "metadata": {}, + "cell_type": "markdown" + }, + { "outputs": [], "source": [ - "%matplotlib inline" + "# Author: Erwan Vautier <erwan.vautier@gmail.com>\n# Nicolas Courty <ncourty@irisa.fr>\n#\n# License: MIT License\n\nimport scipy as sp\nimport numpy as np\nimport matplotlib.pylab as pl\nfrom mpl_toolkits.mplot3d import Axes3D # noqa\nimport ot" ], + "execution_count": null, + "metadata": { + "collapsed": false + }, "cell_type": "code" }, { - "metadata": {}, "source": [ - "\n# Gromov-Wasserstein example\n\n\nThis example is designed to show how to use the Gromov-Wassertsein distance\ncomputation in POT.\n\n" + "Sample two Gaussian distributions (2D and 3D)\n---------------------------------------------\n\nThe Gromov-Wasserstein distance allows to compute distances with samples that\ndo not belong to the same metric space. For demonstration purpose, we sample\ntwo Gaussian distributions in 2- and 3-dimensional spaces.\n\n" ], + "metadata": {}, "cell_type": "markdown" }, { + "outputs": [], + "source": [ + "n_samples = 30 # nb samples\n\nmu_s = np.array([0, 0])\ncov_s = np.array([[1, 0], [0, 1]])\n\nmu_t = np.array([4, 4, 4])\ncov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])\n\n\nxs = ot.datasets.get_2D_samples_gauss(n_samples, mu_s, cov_s)\nP = sp.linalg.sqrtm(cov_t)\nxt = np.random.randn(n_samples, 3).dot(P) + mu_t" + ], "execution_count": null, "metadata": { "collapsed": false }, + "cell_type": "code" + }, + { + "source": [ + "Plotting the distributions\n--------------------------\n\n" + ], + "metadata": {}, + "cell_type": "markdown" + }, + { "outputs": [], "source": [ - "# Author: Erwan Vautier <erwan.vautier@gmail.com>\n# Nicolas Courty <ncourty@irisa.fr>\n#\n# License: MIT License\n\nimport scipy as sp\nimport numpy as np\nimport matplotlib.pylab as pl\nfrom mpl_toolkits.mplot3d import Axes3D # noqa\nimport ot\n\n\n#\n# Sample two Gaussian distributions (2D and 3D)\n# ---------------------------------------------\n#\n# The Gromov-Wasserstein distance allows to compute distances with samples that\n# do not belong to the same metric space. For demonstration purpose, we sample\n# two Gaussian distributions in 2- and 3-dimensional spaces.\n\n\nn_samples = 30 # nb samples\n\nmu_s = np.array([0, 0])\ncov_s = np.array([[1, 0], [0, 1]])\n\nmu_t = np.array([4, 4, 4])\ncov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])\n\n\nxs = ot.datasets.get_2D_samples_gauss(n_samples, mu_s, cov_s)\nP = sp.linalg.sqrtm(cov_t)\nxt = np.random.randn(n_samples, 3).dot(P) + mu_t\n\n\n#\n# Plotting the distributions\n# --------------------------\n\n\nfig = pl.figure()\nax1 = fig.add_subplot(121)\nax1.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\nax2 = fig.add_subplot(122, projection='3d')\nax2.scatter(xt[:, 0], xt[:, 1], xt[:, 2], color='r')\npl.show()\n\n\n#\n# Compute distance kernels, normalize them and then display\n# ---------------------------------------------------------\n\n\nC1 = sp.spatial.distance.cdist(xs, xs)\nC2 = sp.spatial.distance.cdist(xt, xt)\n\nC1 /= C1.max()\nC2 /= C2.max()\n\npl.figure()\npl.subplot(121)\npl.imshow(C1)\npl.subplot(122)\npl.imshow(C2)\npl.show()\n\n#\n# Compute Gromov-Wasserstein plans and distance\n# ---------------------------------------------\n\np = ot.unif(n_samples)\nq = ot.unif(n_samples)\n\ngw0, log0 = ot.gromov.gromov_wasserstein(\n C1, C2, p, q, 'square_loss', verbose=True, log=True)\n\ngw, log = ot.gromov.entropic_gromov_wasserstein(\n C1, C2, p, q, 'square_loss', epsilon=5e-4, log=True, verbose=True)\n\n\nprint('Gromov-Wasserstein distances: ' + str(log0['gw_dist']))\nprint('Entropic Gromov-Wasserstein distances: ' + str(log['gw_dist']))\n\n\npl.figure(1, (10, 5))\n\npl.subplot(1, 2, 1)\npl.imshow(gw0, cmap='jet')\npl.title('Gromov Wasserstein')\n\npl.subplot(1, 2, 2)\npl.imshow(gw, cmap='jet')\npl.title('Entropic Gromov Wasserstein')\n\npl.show()" + "fig = pl.figure()\nax1 = fig.add_subplot(121)\nax1.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\nax2 = fig.add_subplot(122, projection='3d')\nax2.scatter(xt[:, 0], xt[:, 1], xt[:, 2], color='r')\npl.show()" ], + "execution_count": null, + "metadata": { + "collapsed": false + }, "cell_type": "code" - } - ], - "metadata": { - "language_info": { - "name": "python", - "codemirror_mode": { - "name": "ipython", - "version": 3 + }, + { + "source": [ + "Compute distance kernels, normalize them and then display\n---------------------------------------------------------\n\n" + ], + "metadata": {}, + "cell_type": "markdown" + }, + { + "outputs": [], + "source": [ + "C1 = sp.spatial.distance.cdist(xs, xs)\nC2 = sp.spatial.distance.cdist(xt, xt)\n\nC1 /= C1.max()\nC2 /= C2.max()\n\npl.figure()\npl.subplot(121)\npl.imshow(C1)\npl.subplot(122)\npl.imshow(C2)\npl.show()" + ], + "execution_count": null, + "metadata": { + "collapsed": false }, - "nbconvert_exporter": "python", - "version": "3.5.2", - "pygments_lexer": "ipython3", - "file_extension": ".py", - "mimetype": "text/x-python" + "cell_type": "code" }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3", - "language": "python" + { + "source": [ + "Compute Gromov-Wasserstein plans and distance\n---------------------------------------------\n\n" + ], + "metadata": {}, + "cell_type": "markdown" + }, + { + "outputs": [], + "source": [ + "p = ot.unif(n_samples)\nq = ot.unif(n_samples)\n\ngw0, log0 = ot.gromov.gromov_wasserstein(\n C1, C2, p, q, 'square_loss', verbose=True, log=True)\n\ngw, log = ot.gromov.entropic_gromov_wasserstein(\n C1, C2, p, q, 'square_loss', epsilon=5e-4, log=True, verbose=True)\n\n\nprint('Gromov-Wasserstein distances: ' + str(log0['gw_dist']))\nprint('Entropic Gromov-Wasserstein distances: ' + str(log['gw_dist']))\n\n\npl.figure(1, (10, 5))\n\npl.subplot(1, 2, 1)\npl.imshow(gw0, cmap='jet')\npl.title('Gromov Wasserstein')\n\npl.subplot(1, 2, 2)\npl.imshow(gw, cmap='jet')\npl.title('Entropic Gromov Wasserstein')\n\npl.show()" + ], + "execution_count": null, + "metadata": { + "collapsed": false + }, + "cell_type": "code" } - }, - "nbformat_minor": 0, - "nbformat": 4 + ] }
\ No newline at end of file |