Debian Codegraph @

examples/backends/plot_optim_gromov_pytorch.py

raw symbols SCIP
1 r"""
2 =======================================================
3 Optimizing the Gromov-Wasserstein distance with PyTorch
4 =======================================================
5
6 In this example, we use the pytorch backend to optimize the Gromov-Wasserstein
7 (GW) loss between two graphs expressed as empirical distribution.
8
9 In the first part, we optimize the weights on the node of a simple template
10 graph so that it minimizes the GW with a given Stochastic Block Model graph.
11 We can see that this actually recovers the proportion of classes in the SBM
12 and allows for an accurate clustering of the nodes using the GW optimal plan.
13
14 In the second part, we optimize simultaneously the weights and the structure of
15 the template graph which allows us to perform graph compression and to recover
16 other properties of the SBM.
17
18 The backend actually uses the gradients expressed in [38] to optimize the
19 weights.
20
21 [38] C. Vincent-Cuaz, T. Vayer, R. Flamary, M. Corneli, N. Courty, Online Graph
22 Dictionary Learning, International Conference on Machine Learning (ICML), 2021.
23
24 """
25 # Author: Rémi Flamary <[email protected]>
26 #
27 # License: MIT License
28
29 # sphinx_gallery_thumbnail_number = 3
30
31 from sklearn.manifold import MDS
32 import numpy as np
33 import matplotlib.pylab as pl
34 import torch
35
36 import ot
37 from ot.gromov import gromov_wasserstein2
38
39 # %%
40 # Graph generation
41 # ----------------
42
43 rng = np.random.RandomState(42)
44
45
46 def get_sbm(n, nc, ratio, P):
47 nbpc = np.round(n * ratio).astype(int)
48 n = np.sum(nbpc)
49 C = np.zeros((n, n))
50 for c1 in range(nc):
51 for c2 in range(c1 + 1):
52 if c1 == c2:
53 for i in range(np.sum(nbpc[:c1]), np.sum(nbpc[: c1 + 1])):
54 for j in range(np.sum(nbpc[:c2]), i):
55 if rng.rand() <= P[c1, c2]:
56 C[i, j] = 1
57 else:
58 for i in range(np.sum(nbpc[:c1]), np.sum(nbpc[: c1 + 1])):
59 for j in range(np.sum(nbpc[:c2]), np.sum(nbpc[: c2 + 1])):
60 if rng.rand() <= P[c1, c2]:
61 C[i, j] = 1
62
63 return C + C.T
64
65
66 n = 100
67 nc = 3
68 ratio = np.array([0.5, 0.3, 0.2])
69 P = np.array(0.6 * np.eye(3) + 0.05 * np.ones((3, 3)))
70 C1 = get_sbm(n, nc, ratio, P)
71
72 # get 2d position for nodes
73 x1 = MDS(dissimilarity="precomputed", random_state=0).fit_transform(1 - C1)
74
75
76 def plot_graph(x, C, color="C0", s=None):
77 for j in range(C.shape[0]):
78 for i in range(j):
79 if C[i, j] > 0:
80 pl.plot([x[i, 0], x[j, 0]], [x[i, 1], x[j, 1]], alpha=0.2, color="k")
81 pl.scatter(
82 x[:, 0], x[:, 1], c=color, s=s, zorder=10, edgecolors="k", cmap="tab10", vmax=9
83 )
84
85
86 pl.figure(1, (10, 5))
87 pl.clf()
88 pl.subplot(1, 2, 1)
89 plot_graph(x1, C1, color="C0")
90 pl.title("SBM Graph")
91 pl.axis("off")
92 pl.subplot(1, 2, 2)
93 pl.imshow(C1, interpolation="nearest")
94 pl.title("Adjacency matrix")
95 pl.axis("off")
96
97
98 # %%
99 # Optimizing GW w.r.t. the weights on a template structure
100 # --------------------------------------------------------
101 # The adjacency matrix C1 is block diagonal with 3 blocks. We want to
102 # optimize the weights of a simple template C0=eye(3) and see if we can
103 # recover the proportion of classes from the SBM (up to a permutation).
104
105 C0 = np.eye(3)
106
107
108 def min_weight_gw(C1, C2, a2, nb_iter_max=100, lr=1e-2):
109 """solve min_a GW(C1,C2,a, a2) by gradient descent"""
110
111 # use pyTorch for our data
112 C1_torch = torch.tensor(C1)
113 C2_torch = torch.tensor(C2)
114
115 a0 = rng.rand(C1.shape[0]) # random_init
116 a0 /= a0.sum() # on simplex
117 a1_torch = torch.tensor(a0).requires_grad_(True)
118 a2_torch = torch.tensor(a2)
119
120 loss_iter = []
121
122 for i in range(nb_iter_max):
123 loss = gromov_wasserstein2(C1_torch, C2_torch, a1_torch, a2_torch)
124
125 loss_iter.append(loss.clone().detach().cpu().numpy())
126 loss.backward()
127
128 # print("{:03d} | {}".format(i, loss_iter[-1]))
129
130 # performs a step of projected gradient descent
131 with torch.no_grad():
132 grad = a1_torch.grad
133 a1_torch -= grad * lr # step
134 a1_torch.grad.zero_()
135 a1_torch.data = ot.utils.proj_simplex(a1_torch)
136
137 a1 = a1_torch.clone().detach().cpu().numpy()
138
139 return a1, loss_iter
140
141
142 a0_est, loss_iter0 = min_weight_gw(C0, C1, ot.unif(n), nb_iter_max=100, lr=1e-2)
143
144 pl.figure(2)
145 pl.plot(loss_iter0)
146 pl.title("Loss along iterations")
147
148 print("Estimated weights : ", a0_est)
149 print("True proportions : ", ratio)
150
151
152 # %%
153 # It is clear that the optimization has converged and that we recover the
154 # ratio of the different classes in the SBM graph up to a permutation.
155
156
157 # %%
158 # Community clustering with uniform and estimated weights
159 # -------------------------------------------------------
160 # The GW OT plan can be used to perform a clustering of the nodes of a graph
161 # when computing the GW with a simple template like C0 by labeling nodes in
162 # the original graph using by the index of the node in the template receiving
163 # the most mass.
164 #
165 # We show here the result of such a clustering when using uniform weights on
166 # the template C0 and when using the optimal weights previously estimated.
167
168
169 T_unif = ot.gromov_wasserstein(C1, C0, ot.unif(n), ot.unif(3))
170 label_unif = T_unif.argmax(1)
171
172 T_est = ot.gromov_wasserstein(C1, C0, ot.unif(n), a0_est)
173 label_est = T_est.argmax(1)
174
175 pl.figure(3, (10, 5))
176 pl.clf()
177 pl.subplot(1, 2, 1)
178 plot_graph(x1, C1, color=label_unif)
179 pl.title("Graph clustering unif. weights")
180 pl.axis("off")
181 pl.subplot(1, 2, 2)
182 plot_graph(x1, C1, color=label_est)
183 pl.title("Graph clustering est. weights")
184 pl.axis("off")
185
186
187 # %%
188 # Graph compression with GW
189 # -------------------------
190 # Now we optimize both the weights and structure of a small graph that
191 # minimize the GW distance wrt our data graph. This can be seen as graph
192 # compression but can also recover important properties of an SBM such
193 # as its class proportion but also its matrix of probability of links between
194 # classes
195
196
197 def graph_compression_gw(nb_nodes, C2, a2, nb_iter_max=100, lr=1e-2):
198 """solve min_a GW(C1,C2,a, a2) by gradient descent"""
199
200 # use pyTorch for our data
201
202 C2_torch = torch.tensor(C2)
203 a2_torch = torch.tensor(a2)
204
205 a0 = rng.rand(nb_nodes) # random_init
206 a0 /= a0.sum() # on simplex
207 a1_torch = torch.tensor(a0).requires_grad_(True)
208 C0 = np.eye(nb_nodes)
209 C1_torch = torch.tensor(C0).requires_grad_(True)
210
211 loss_iter = []
212
213 for i in range(nb_iter_max):
214 loss = gromov_wasserstein2(C1_torch, C2_torch, a1_torch, a2_torch)
215
216 loss_iter.append(loss.clone().detach().cpu().numpy())
217 loss.backward()
218
219 # print("{:03d} | {}".format(i, loss_iter[-1]))
220
221 # performs a step of projected gradient descent
222 with torch.no_grad():
223 grad = a1_torch.grad
224 a1_torch -= grad * lr # step
225 a1_torch.grad.zero_()
226 a1_torch.data = ot.utils.proj_simplex(a1_torch)
227
228 grad = C1_torch.grad
229 C1_torch -= grad * lr # step
230 C1_torch.grad.zero_()
231 C1_torch.data = torch.clamp(C1_torch, 0, 1)
232
233 a1 = a1_torch.clone().detach().cpu().numpy()
234 C1 = C1_torch.clone().detach().cpu().numpy()
235
236 return a1, C1, loss_iter
237
238
239 nb_nodes = 3
240 a0_est2, C0_est2, loss_iter2 = graph_compression_gw(
241 nb_nodes, C1, ot.unif(n), nb_iter_max=100, lr=5e-2
242 )
243
244 pl.figure(4)
245 pl.plot(loss_iter2)
246 pl.title("Loss along iterations")
247
248
249 print("Estimated weights : ", a0_est2)
250 print("True proportions : ", ratio)
251
252 pl.figure(6, (10, 3.5))
253 pl.clf()
254 pl.subplot(1, 2, 1)
255 pl.imshow(P, vmin=0, vmax=1)
256 pl.title("True SBM P matrix")
257 pl.subplot(1, 2, 2)
258 pl.imshow(C0_est2, vmin=0, vmax=1)
259 pl.title("Estimated C0 matrix")
260 pl.colorbar()