11{
2+ "nbformat_minor" : 0 ,
3+ "nbformat" : 4 ,
4+ "metadata" : {
5+ "language_info" : {
6+ "file_extension" : " .py" ,
7+ "codemirror_mode" : {
8+ "version" : 3 ,
9+ "name" : " ipython"
10+ },
11+ "nbconvert_exporter" : " python" ,
12+ "mimetype" : " text/x-python" ,
13+ "version" : " 3.5.2" ,
14+ "name" : " python" ,
15+ "pygments_lexer" : " ipython3"
16+ },
17+ "kernelspec" : {
18+ "display_name" : " Python 3" ,
19+ "name" : " python3" ,
20+ "language" : " python"
21+ }
22+ },
223 "cells" : [
324 {
25+ "outputs" : [],
26+ "source" : [
27+ " %matplotlib inline"
28+ ],
429 "execution_count" : null ,
530 "metadata" : {
631 "collapsed" : false
732 },
33+ "cell_type" : " code"
34+ },
35+ {
36+ "source" : [
37+ " \n # Gromov-Wasserstein example\n\n\n This example is designed to show how to use the Gromov-Wassertsein distance\n computation in POT.\n\n "
38+ ],
39+ "metadata" : {},
40+ "cell_type" : " markdown"
41+ },
42+ {
843 "outputs" : [],
944 "source" : [
10- " % matplotlib inline "
45+ " # Author: Erwan Vautier <erwan.vautier@gmail.com> \n # Nicolas Courty <ncourty@irisa.fr> \n # \n # License: MIT License \n\n import scipy as sp \n import numpy as np \n import matplotlib.pylab as pl \n from mpl_toolkits.mplot3d import Axes3D # noqa \n import ot "
1146 ],
47+ "execution_count" : null ,
48+ "metadata" : {
49+ "collapsed" : false
50+ },
1251 "cell_type" : " code"
1352 },
1453 {
15- "metadata" : {},
1654 "source" : [
17- " \n # Gromov-Wasserstein example \n \n\n This example is designed to show how to use the Gromov-Wassertsein distance \n computation in POT .\n\n "
55+ " 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 "
1856 ],
57+ "metadata" : {},
1958 "cell_type" : " markdown"
2059 },
2160 {
61+ "outputs" : [],
62+ "source" : [
63+ " n_samples = 30 # nb samples\n\n mu_s = np.array([0, 0])\n cov_s = np.array([[1, 0], [0, 1]])\n\n mu_t = np.array([4, 4, 4])\n cov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])\n\n\n xs = ot.datasets.get_2D_samples_gauss(n_samples, mu_s, cov_s)\n P = sp.linalg.sqrtm(cov_t)\n xt = np.random.randn(n_samples, 3).dot(P) + mu_t"
64+ ],
2265 "execution_count" : null ,
2366 "metadata" : {
2467 "collapsed" : false
2568 },
69+ "cell_type" : " code"
70+ },
71+ {
72+ "source" : [
73+ " Plotting the distributions\n --------------------------\n\n "
74+ ],
75+ "metadata" : {},
76+ "cell_type" : " markdown"
77+ },
78+ {
2679 "outputs" : [],
2780 "source" : [
28- "# 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()"
81+ " fig = pl.figure()\n ax1 = fig.add_subplot(121)\n ax1.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\n ax2 = fig.add_subplot(122, projection='3d')\n ax2.scatter(xt[:, 0], xt[:, 1], xt[:, 2], color='r')\n pl.show()"
2982 ],
83+ "execution_count" : null ,
84+ "metadata" : {
85+ "collapsed" : false
86+ },
3087 "cell_type" : " code"
31- }
32- ],
33- "metadata" : {
34- "language_info" : {
35- "name" : " python" ,
36- "codemirror_mode" : {
37- "name" : " ipython" ,
38- "version" : 3
88+ },
89+ {
90+ "source" : [
91+ " Compute distance kernels, normalize them and then display\n ---------------------------------------------------------\n\n "
92+ ],
93+ "metadata" : {},
94+ "cell_type" : " markdown"
95+ },
96+ {
97+ "outputs" : [],
98+ "source" : [
99+ " C1 = sp.spatial.distance.cdist(xs, xs)\n C2 = sp.spatial.distance.cdist(xt, xt)\n\n C1 /= C1.max()\n C2 /= C2.max()\n\n pl.figure()\n pl.subplot(121)\n pl.imshow(C1)\n pl.subplot(122)\n pl.imshow(C2)\n pl.show()"
100+ ],
101+ "execution_count" : null ,
102+ "metadata" : {
103+ "collapsed" : false
39104 },
40- "nbconvert_exporter" : " python" ,
41- "version" : " 3.5.2" ,
42- "pygments_lexer" : " ipython3" ,
43- "file_extension" : " .py" ,
44- "mimetype" : " text/x-python"
105+ "cell_type" : " code"
45106 },
46- "kernelspec" : {
47- "display_name" : " Python 3" ,
48- "name" : " python3" ,
49- "language" : " python"
107+ {
108+ "source" : [
109+ " Compute Gromov-Wasserstein plans and distance\n ---------------------------------------------\n\n "
110+ ],
111+ "metadata" : {},
112+ "cell_type" : " markdown"
113+ },
114+ {
115+ "outputs" : [],
116+ "source" : [
117+ " p = ot.unif(n_samples)\n q = ot.unif(n_samples)\n\n gw0, log0 = ot.gromov.gromov_wasserstein(\n C1, C2, p, q, 'square_loss', verbose=True, log=True)\n\n gw, log = ot.gromov.entropic_gromov_wasserstein(\n C1, C2, p, q, 'square_loss', epsilon=5e-4, log=True, verbose=True)\n\n\n print('Gromov-Wasserstein distances: ' + str(log0['gw_dist']))\n print('Entropic Gromov-Wasserstein distances: ' + str(log['gw_dist']))\n\n\n pl.figure(1, (10, 5))\n\n pl.subplot(1, 2, 1)\n pl.imshow(gw0, cmap='jet')\n pl.title('Gromov Wasserstein')\n\n pl.subplot(1, 2, 2)\n pl.imshow(gw, cmap='jet')\n pl.title('Entropic Gromov Wasserstein')\n\n pl.show()"
118+ ],
119+ "execution_count" : null ,
120+ "metadata" : {
121+ "collapsed" : false
122+ },
123+ "cell_type" : " code"
50124 }
51- },
52- "nbformat_minor" : 0 ,
53- "nbformat" : 4
125+ ]
54126}
0 commit comments