Debian Codegraph @

examples/gromov/plot_fgw.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 ==============================
4 Plot Fused-Gromov-Wasserstein
5 ==============================
6
7 This example first illustrates the computation of FGW for 1D measures estimated
8 using a Conditional Gradient solver [24].
9
10 [24] Vayer Titouan, Chapel Laetitia, Flamary Rémi, Tavenard Romain
11 and Courty Nicolas
12 "Optimal Transport for structured data with application on graphs"
13 International Conference on Machine Learning (ICML). 2019.
14 """
15
16 # Author: Titouan Vayer <[email protected]>
17 #
18 # License: MIT License
19
20 # sphinx_gallery_thumbnail_number = 3
21
22 import matplotlib.pyplot as pl
23 import numpy as np
24 import ot
25 from ot.gromov import gromov_wasserstein, fused_gromov_wasserstein
26
27
28 ##############################################################################
29 # Generate data
30 # -------------
31
32 # parameters
33
34 # We create two 1D random measures
35 n = 20 # number of points in the first distribution
36 n2 = 30 # number of points in the second distribution
37 sig = 1 # std of first distribution
38 sig2 = 0.1 # std of second distribution
39
40 np.random.seed(0)
41
42 phi = np.arange(n)[:, None]
43 xs = phi + sig * np.random.randn(n, 1)
44 ys = np.vstack(
45 (np.ones((n // 2, 1)), 0 * np.ones((n // 2, 1)))
46 ) + sig2 * np.random.randn(n, 1)
47
48 phi2 = np.arange(n2)[:, None]
49 xt = phi2 + sig * np.random.randn(n2, 1)
50 yt = np.vstack(
51 (np.ones((n2 // 2, 1)), 0 * np.ones((n2 // 2, 1)))
52 ) + sig2 * np.random.randn(n2, 1)
53 yt = yt[::-1, :]
54
55 p = ot.unif(n)
56 q = ot.unif(n2)
57
58 ##############################################################################
59 # Plot data
60 # ---------
61
62 # plot the distributions
63
64 pl.figure(1, (7, 7))
65
66 pl.subplot(2, 1, 1)
67
68 pl.scatter(ys, xs, c=phi, s=70)
69 pl.ylabel("Feature value a", fontsize=20)
70 pl.title("$\mu=\sum_i \delta_{x_i,a_i}$", fontsize=25, y=1)
71 pl.xticks(())
72 pl.yticks(())
73 pl.subplot(2, 1, 2)
74 pl.scatter(yt, xt, c=phi2, s=70)
75 pl.xlabel("coordinates x/y", fontsize=25)
76 pl.ylabel("Feature value b", fontsize=20)
77 pl.title("$\\nu=\sum_j \delta_{y_j,b_j}$", fontsize=25, y=1)
78 pl.yticks(())
79 pl.tight_layout()
80 pl.show()
81
82 ##############################################################################
83 # Create structure matrices and across-feature distance matrix
84 # ------------------------------------------------------------
85
86 # Structure matrices and across-features distance matrix
87 C1 = ot.dist(xs)
88 C2 = ot.dist(xt)
89 M = ot.dist(ys, yt)
90 w1 = ot.unif(C1.shape[0])
91 w2 = ot.unif(C2.shape[0])
92 Got = ot.emd([], [], M)
93
94 ##############################################################################
95 # Plot matrices
96 # -------------
97
98 cmap = "Reds"
99
100 pl.figure(2, (5, 5))
101 fs = 15
102 l_x = [0, 5, 10, 15]
103 l_y = [0, 5, 10, 15, 20, 25]
104 gs = pl.GridSpec(5, 5)
105
106 ax1 = pl.subplot(gs[3:, :2])
107
108 pl.imshow(C1, cmap=cmap, interpolation="nearest")
109 pl.title("$C_1$", fontsize=fs)
110 pl.xlabel("$k$", fontsize=fs)
111 pl.ylabel("$i$", fontsize=fs)
112 pl.xticks(l_x)
113 pl.yticks(l_x)
114
115 ax2 = pl.subplot(gs[:3, 2:])
116
117 pl.imshow(C2, cmap=cmap, interpolation="nearest")
118 pl.title("$C_2$", fontsize=fs)
119 pl.ylabel("$l$", fontsize=fs)
120 pl.xticks(())
121 pl.yticks(l_y)
122 ax2.set_aspect("auto")
123
124 ax3 = pl.subplot(gs[3:, 2:], sharex=ax2, sharey=ax1)
125 pl.imshow(M, cmap=cmap, interpolation="nearest")
126 pl.yticks(l_x)
127 pl.xticks(l_y)
128 pl.ylabel("$i$", fontsize=fs)
129 pl.title("$M_{AB}$", fontsize=fs)
130 pl.xlabel("$j$", fontsize=fs)
131 pl.tight_layout()
132 ax3.set_aspect("auto")
133 pl.show()
134
135 ##############################################################################
136 # Compute FGW/GW
137 # --------------
138
139 # Computing FGW and GW
140 alpha = 1e-3
141
142 ot.tic()
143 Gwg, logw = fused_gromov_wasserstein(
144 M, C1, C2, p, q, loss_fun="square_loss", alpha=alpha, verbose=True, log=True
145 )
146 ot.toc()
147
148 # reload_ext WGW
149 Gg, log = gromov_wasserstein(
150 C1, C2, p, q, loss_fun="square_loss", verbose=True, log=True
151 )
152
153 ##############################################################################
154 # Visualize transport matrices
155 # ----------------------------
156
157 # visu OT matrix
158 cmap = "Blues"
159 fs = 15
160 pl.figure(3, (13, 5))
161 pl.clf()
162 pl.subplot(1, 3, 1)
163 pl.imshow(Got, cmap=cmap, interpolation="nearest")
164 pl.ylabel("$i$", fontsize=fs)
165 pl.xticks(())
166
167 pl.title("Wasserstein ($M$ only)")
168
169 pl.subplot(1, 3, 2)
170 pl.imshow(Gg, cmap=cmap, interpolation="nearest")
171 pl.title("Gromov ($C_1,C_2$ only)")
172 pl.xticks(())
173 pl.subplot(1, 3, 3)
174 pl.imshow(Gwg, cmap=cmap, interpolation="nearest")
175 pl.title("FGW ($M+C_1,C_2$)")
176
177 pl.xlabel("$j$", fontsize=fs)
178 pl.ylabel("$i$", fontsize=fs)
179
180 pl.tight_layout()
181 pl.show()