summaryrefslogtreecommitdiff
path: root/docs/source/auto_examples/plot_gromov.ipynb
diff options
context:
space:
mode:
authorRémi Flamary <remi.flamary@gmail.com>2018-02-16 15:04:04 +0100
committerRémi Flamary <remi.flamary@gmail.com>2018-02-16 15:04:04 +0100
commitee19d423adc85a960c9a46e4f81c370196805dbf (patch)
tree1c0bc21a605d0097616c26cfdb846fc744ed43a0 /docs/source/auto_examples/plot_gromov.ipynb
parentefdbf9e4fe9295fb1bec893e8aaa9102537cb7f5 (diff)
update notebooks
Diffstat (limited to 'docs/source/auto_examples/plot_gromov.ipynb')
-rw-r--r--docs/source/auto_examples/plot_gromov.ipynb144
1 files changed, 36 insertions, 108 deletions
diff --git a/docs/source/auto_examples/plot_gromov.ipynb b/docs/source/auto_examples/plot_gromov.ipynb
index 865848e..6d6b522 100644
--- a/docs/source/auto_examples/plot_gromov.ipynb
+++ b/docs/source/auto_examples/plot_gromov.ipynb
@@ -1,126 +1,54 @@
{
- "nbformat_minor": 0,
- "nbformat": 4,
"cells": [
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "%matplotlib inline"
- ],
- "outputs": [],
- "metadata": {
- "collapsed": false
- }
- },
- {
- "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"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
- {
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "# Author: Erwan Vautier <erwan.vautier@gmail.com>\r\n# Nicolas Courty <ncourty@irisa.fr>\r\n#\r\n# License: MIT License\r\n\r\nimport scipy as sp\r\nimport numpy as np\r\nimport matplotlib.pylab as pl\r\nfrom mpl_toolkits.mplot3d import Axes3D # noqa\r\nimport ot"
- ],
- "outputs": [],
- "metadata": {
- "collapsed": false
- }
- },
- {
- "source": [
- "Sample two Gaussian distributions (2D and 3D)\r\n ---------------------------------------------\r\n\r\n The Gromov-Wasserstein distance allows to compute distances with samples that\r\n do not belong to the same metric space. For demonstration purpose, we sample\r\n two Gaussian distributions in 2- and 3-dimensional spaces.\r\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
- {
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "n_samples = 30 # nb samples\r\n\r\nmu_s = np.array([0, 0])\r\ncov_s = np.array([[1, 0], [0, 1]])\r\n\r\nmu_t = np.array([4, 4, 4])\r\ncov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])\r\n\r\n\r\nxs = ot.datasets.get_2D_samples_gauss(n_samples, mu_s, cov_s)\r\nP = sp.linalg.sqrtm(cov_t)\r\nxt = np.random.randn(n_samples, 3).dot(P) + mu_t"
- ],
- "outputs": [],
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
- {
+ },
+ "outputs": [],
"source": [
- "Plotting the distributions\r\n--------------------------\r\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
- {
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "fig = pl.figure()\r\nax1 = fig.add_subplot(121)\r\nax1.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\r\nax2 = fig.add_subplot(122, projection='3d')\r\nax2.scatter(xt[:, 0], xt[:, 1], xt[:, 2], color='r')\r\npl.show()"
- ],
- "outputs": [],
- "metadata": {
- "collapsed": false
- }
- },
+ "%matplotlib inline"
+ ],
+ "cell_type": "code"
+ },
{
+ "metadata": {},
"source": [
- "Compute distance kernels, normalize them and then display\r\n---------------------------------------------------------\r\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ "\n# Gromov-Wasserstein example\n\n\nThis example is designed to show how to use the Gromov-Wassertsein distance\ncomputation in POT.\n\n"
+ ],
+ "cell_type": "markdown"
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "C1 = sp.spatial.distance.cdist(xs, xs)\r\nC2 = sp.spatial.distance.cdist(xt, xt)\r\n\r\nC1 /= C1.max()\r\nC2 /= C2.max()\r\n\r\npl.figure()\r\npl.subplot(121)\r\npl.imshow(C1)\r\npl.subplot(122)\r\npl.imshow(C2)\r\npl.show()"
- ],
- "outputs": [],
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
- {
+ },
+ "outputs": [],
"source": [
- "Compute Gromov-Wasserstein plans and distance\r\n---------------------------------------------\r\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
- {
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "p = ot.unif(n_samples)\r\nq = ot.unif(n_samples)\r\n\r\ngw = ot.gromov_wasserstein(C1, C2, p, q, 'square_loss', epsilon=5e-4)\r\ngw_dist = ot.gromov_wasserstein2(C1, C2, p, q, 'square_loss', epsilon=5e-4)\r\n\r\nprint('Gromov-Wasserstein distances between the distribution: ' + str(gw_dist))\r\n\r\npl.figure()\r\npl.imshow(gw, cmap='jet')\r\npl.colorbar()\r\npl.show()"
- ],
- "outputs": [],
- "metadata": {
- "collapsed": false
- }
+ "# 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()"
+ ],
+ "cell_type": "code"
}
- ],
+ ],
"metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "name": "python2",
- "language": "python"
- },
"language_info": {
- "mimetype": "text/x-python",
- "nbconvert_exporter": "python",
- "name": "python",
- "file_extension": ".py",
- "version": "2.7.12",
- "pygments_lexer": "ipython2",
+ "name": "python",
"codemirror_mode": {
- "version": 2,
- "name": "ipython"
- }
+ "name": "ipython",
+ "version": 3
+ },
+ "nbconvert_exporter": "python",
+ "version": "3.5.2",
+ "pygments_lexer": "ipython3",
+ "file_extension": ".py",
+ "mimetype": "text/x-python"
+ },
+ "kernelspec": {
+ "display_name": "Python 3",
+ "name": "python3",
+ "language": "python"
}
- }
+ },
+ "nbformat_minor": 0,
+ "nbformat": 4
} \ No newline at end of file