1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
{
"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": [],
"metadata": {
"collapsed": false
}
},
{
"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
}
},
{
"source": [
"Compute distance kernels, normalize them and then display\r\n---------------------------------------------------------\r\n\n"
],
"cell_type": "markdown",
"metadata": {}
},
{
"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": [],
"metadata": {
"collapsed": false
}
},
{
"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
}
}
],
"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",
"codemirror_mode": {
"version": 2,
"name": "ipython"
}
}
}
}
|