| 1 |
|
| 2 |
|
| 3 |
r""" |
| 4 |
===================================================== |
| 5 |
Semi-relaxed (Fused) Gromov-Wasserstein Barycenter as Dictionary Learning |
| 6 |
===================================================== |
| 7 |
|
| 8 |
In this example, we illustrate how to learn a semi-relaxed Gromov-Wasserstein |
| 9 |
(srGW) barycenter using a Block-Coordinate Descent algorithm, on a dataset of |
| 10 |
structured data such as graphs, denoted :math:`\{ \mathbf{C_s} \}_{s \in [S]}` |
| 11 |
where every nodes have uniform weights :math:`\{ \mathbf{p_s} \}_{s \in [S]}`. |
| 12 |
Given a barycenter structure matrix :math:`\mathbf{C}` with N nodes, |
| 13 |
each graph :math:`(\mathbf{C_s}, \mathbf{p_s})` is modeled as a reweighed subgraph |
| 14 |
with structure :math:`\mathbf{C}` and weights :math:`\mathbf{w_s} \in \Sigma_N` |
| 15 |
where each :math:`\mathbf{w_s}` corresponds to the second marginal of the OT |
| 16 |
:math:`\mathbf{T_s}` (s.t :math:`\mathbf{w_s} = \mathbf{T_s}^\top \mathbf{1}`) |
| 17 |
minimizing the srGW loss between the s^{th} input and the barycenter. |
| 18 |
|
| 19 |
|
| 20 |
First, we consider a dataset composed of graphs generated by Stochastic Block models |
| 21 |
with variable sizes taken in :math:`\{30, ... , 50\}` and number of clusters |
| 22 |
varying in :math:`\{ 1, 2, 3\}` with random proportions. We learn a srGW barycenter |
| 23 |
with 3 nodes and visualize the learned structure and the embeddings for some inputs. |
| 24 |
|
| 25 |
Second, we illustrate the extension of this framework to graphs endowed |
| 26 |
with node features by using the semi-relaxed Fused Gromov-Wasserstein |
| 27 |
divergence (srFGW). Starting from the aforementioned dataset of unattributed graphs, we |
| 28 |
add discrete labels uniformly depending on the number of clusters. Then conduct |
| 29 |
the analog analysis. |
| 30 |
|
| 31 |
|
| 32 |
[48] Cédric Vincent-Cuaz, Rémi Flamary, Marco Corneli, Titouan Vayer, Nicolas Courty. |
| 33 |
"Semi-relaxed Gromov-Wasserstein divergence and applications on graphs". |
| 34 |
International Conference on Learning Representations (ICLR), 2022. |
| 35 |
|
| 36 |
""" |
| 37 |
> |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
import numpy as np |
| 44 |
import matplotlib.pylab as pl |
| 45 |
from sklearn.manifold import MDS |
| 46 |
from ot.gromov import semirelaxed_gromov_barycenters, semirelaxed_fgw_barycenters |
| 47 |
import ot |
| 48 |
import networkx |
| 49 |
from networkx.generators.community import stochastic_block_model as sbm |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
np.random.seed(42) |
| 57 |
|
| 58 |
n_samples = 60 |
| 59 |
|
| 60 |
|
| 61 |
clusters = [1, 2, 3] |
| 62 |
Nc = n_samples // len(clusters) |
| 63 |
nlabels = len(clusters) |
| 64 |
dataset = [] |
| 65 |
node_labels = [] |
| 66 |
labels = [] |
| 67 |
|
| 68 |
p_inter = 0.1 |
| 69 |
p_intra = 0.9 |
| 70 |
for n_cluster in clusters: |
| 71 |
for i in range(Nc): |
| 72 |
n_nodes = int(np.random.uniform(low=30, high=50)) |
| 73 |
|
| 74 |
if n_cluster > 1: |
| 75 |
P = p_inter * np.ones((n_cluster, n_cluster)) |
| 76 |
np.fill_diagonal(P, p_intra) |
| 77 |
props = np.random.uniform(0.2, 1, size=(n_cluster,)) |
| 78 |
props /= props.sum() |
| 79 |
sizes = np.round(n_nodes * props).astype(np.int32) |
| 80 |
else: |
| 81 |
P = p_intra * np.eye(1) |
| 82 |
sizes = [n_nodes] |
| 83 |
|
| 84 |
G = sbm(sizes, P, seed=i, directed=False) |
| 85 |
part = np.array([G.nodes[i]["block"] for i in range(np.sum(sizes))]) |
| 86 |
C = networkx.to_numpy_array(G) |
| 87 |
dataset.append(C) |
| 88 |
node_labels.append(part) |
| 89 |
labels.append(n_cluster) |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
def plot_graph(x, C, binary=True, color="C0", s=None): |
| 96 |
for j in range(C.shape[0]): |
| 97 |
for i in range(j): |
| 98 |
if binary: |
| 99 |
if C[i, j] > 0: |
| 100 |
pl.plot( |
| 101 |
[x[i, 0], x[j, 0]], [x[i, 1], x[j, 1]], alpha=0.2, color="k" |
| 102 |
) |
| 103 |
else: |
| 104 |
pl.plot( |
| 105 |
[x[i, 0], x[j, 0]], [x[i, 1], x[j, 1]], alpha=C[i, j], color="k" |
| 106 |
) |
| 107 |
|
| 108 |
pl.scatter( |
| 109 |
x[:, 0], x[:, 1], c=color, s=s, zorder=10, edgecolors="k", cmap="tab10", vmax=9 |
| 110 |
) |
| 111 |
|
| 112 |
|
| 113 |
pl.figure(1, (12, 8)) |
| 114 |
pl.clf() |
| 115 |
for idx_c, c in enumerate(clusters): |
| 116 |
C = dataset[(c - 1) * Nc] |
| 117 |
|
| 118 |
x = MDS(dissimilarity="precomputed", random_state=0).fit_transform(1 - C) |
| 119 |
pl.subplot(2, nlabels, c) |
| 120 |
pl.title("(graph) sample from label " + str(c), fontsize=14) |
| 121 |
plot_graph(x, C, binary=True, color="C0", s=50.0) |
| 122 |
pl.axis("off") |
| 123 |
pl.subplot(2, nlabels, nlabels + c) |
| 124 |
pl.title("(matrix) sample from label %s \n" % c, fontsize=14) |
| 125 |
pl.imshow(C, interpolation="nearest") |
| 126 |
pl.axis("off") |
| 127 |
pl.tight_layout() |
| 128 |
pl.show() |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
np.random.seed(0) |
| 137 |
ps = [ot.unif(C.shape[0]) for C in dataset] |
| 138 |
lambdas = [1.0 / n_samples for _ in range(n_samples)] |
| 139 |
N = 3 |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
init_plan = "fluid" |
| 147 |
warmstartT = True |
| 148 |
|
| 149 |
C, log = semirelaxed_gromov_barycenters( |
| 150 |
N=N, |
| 151 |
Cs=dataset, |
| 152 |
ps=ps, |
| 153 |
lambdas=lambdas, |
| 154 |
loss_fun="square_loss", |
| 155 |
tol=1e-6, |
| 156 |
stop_criterion="loss", |
| 157 |
warmstartT=warmstartT, |
| 158 |
log=True, |
| 159 |
G0=init_plan, |
| 160 |
verbose=False, |
| 161 |
) |
| 162 |
|
| 163 |
print("barycenter structure:", C) |
| 164 |
|
| 165 |
unmixings = log["p"] |
| 166 |
|
| 167 |
unmixings2D = np.zeros(shape=(n_samples, 2)) |
| 168 |
for i, w in enumerate(unmixings): |
| 169 |
unmixings2D[i, 0] = (2.0 * w[1] + w[2]) / 2.0 |
| 170 |
unmixings2D[i, 1] = (np.sqrt(3.0) * w[2]) / 2.0 |
| 171 |
x = [0.0, 0.0] |
| 172 |
y = [1.0, 0.0] |
| 173 |
z = [0.5, np.sqrt(3) / 2.0] |
| 174 |
extremities = np.stack([x, y, z]) |
| 175 |
|
| 176 |
pl.figure(2, (4, 4)) |
| 177 |
pl.clf() |
| 178 |
pl.title("Embedding space", fontsize=14) |
| 179 |
for cluster in range(nlabels): |
| 180 |
start, end = Nc * cluster, Nc * (cluster + 1) |
| 181 |
if cluster == 0: |
| 182 |
pl.scatter( |
| 183 |
unmixings2D[start:end, 0], |
| 184 |
unmixings2D[start:end, 1], |
| 185 |
c="C" + str(cluster), |
| 186 |
marker="o", |
| 187 |
s=80.0, |
| 188 |
label="1 cluster", |
| 189 |
) |
| 190 |
else: |
| 191 |
pl.scatter( |
| 192 |
unmixings2D[start:end, 0], |
| 193 |
unmixings2D[start:end, 1], |
| 194 |
c="C" + str(cluster), |
| 195 |
marker="o", |
| 196 |
s=80.0, |
| 197 |
label="%s clusters" % (cluster + 1), |
| 198 |
) |
| 199 |
pl.scatter( |
| 200 |
extremities[:, 0], |
| 201 |
extremities[:, 1], |
| 202 |
c="black", |
| 203 |
marker="x", |
| 204 |
s=100.0, |
| 205 |
label="bary. nodes", |
| 206 |
) |
| 207 |
pl.plot([x[0], y[0]], [x[1], y[1]], color="black", linewidth=2.0) |
| 208 |
pl.plot([x[0], z[0]], [x[1], z[1]], color="black", linewidth=2.0) |
| 209 |
pl.plot([y[0], z[0]], [y[1], z[1]], color="black", linewidth=2.0) |
| 210 |
pl.axis("off") |
| 211 |
pl.legend(fontsize=11) |
| 212 |
pl.tight_layout() |
| 213 |
pl.show() |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
dataset_features = [] |
| 223 |
for i in range(len(dataset)): |
| 224 |
n = dataset[i].shape[0] |
| 225 |
F = np.zeros((n, 3)) |
| 226 |
F[np.arange(n), node_labels[i]] = 1.0 |
| 227 |
dataset_features.append(F) |
| 228 |
|
| 229 |
pl.figure(3, (12, 8)) |
| 230 |
pl.clf() |
| 231 |
for idx_c, c in enumerate(clusters): |
| 232 |
C = dataset[(c - 1) * Nc] |
| 233 |
F = dataset_features[(c - 1) * Nc] |
| 234 |
colors = [f"C{labels[i]}" for i in range(F.shape[0])] |
| 235 |
|
| 236 |
x = MDS(dissimilarity="precomputed", random_state=0).fit_transform(1 - C) |
| 237 |
pl.subplot(2, nlabels, c) |
| 238 |
pl.title("(graph) sample from label " + str(c), fontsize=14) |
| 239 |
plot_graph(x, C, binary=True, color=colors, s=50) |
| 240 |
pl.axis("off") |
| 241 |
pl.subplot(2, nlabels, nlabels + c) |
| 242 |
pl.title("(matrix) sample from label %s \n" % c, fontsize=14) |
| 243 |
pl.imshow(C, interpolation="nearest") |
| 244 |
pl.axis("off") |
| 245 |
pl.tight_layout() |
| 246 |
pl.show() |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
list_alphas = [0.0001, 0.5, 0.9999] |
| 258 |
list_unmixings2D = [] |
| 259 |
|
| 260 |
for ialpha, alpha in enumerate(list_alphas): |
| 261 |
print("--- alpha:", alpha) |
| 262 |
C, F, log = semirelaxed_fgw_barycenters( |
| 263 |
N=N, |
| 264 |
Ys=dataset_features, |
| 265 |
Cs=dataset, |
| 266 |
ps=ps, |
| 267 |
lambdas=lambdas, |
| 268 |
alpha=alpha, |
| 269 |
loss_fun="square_loss", |
| 270 |
tol=1e-6, |
| 271 |
stop_criterion="loss", |
| 272 |
warmstartT=warmstartT, |
| 273 |
log=True, |
| 274 |
G0=init_plan, |
| 275 |
) |
| 276 |
|
| 277 |
print("barycenter structure:", C) |
| 278 |
print("barycenter features:", F) |
| 279 |
|
| 280 |
unmixings = log["p"] |
| 281 |
|
| 282 |
unmixings2D = np.zeros(shape=(n_samples, 2)) |
| 283 |
for i, w in enumerate(unmixings): |
| 284 |
unmixings2D[i, 0] = (2.0 * w[1] + w[2]) / 2.0 |
| 285 |
unmixings2D[i, 1] = (np.sqrt(3.0) * w[2]) / 2.0 |
| 286 |
list_unmixings2D.append(unmixings2D.copy()) |
| 287 |
|
| 288 |
x = [0.0, 0.0] |
| 289 |
y = [1.0, 0.0] |
| 290 |
z = [0.5, np.sqrt(3) / 2.0] |
| 291 |
extremities = np.stack([x, y, z]) |
| 292 |
|
| 293 |
pl.figure(4, (12, 4)) |
| 294 |
pl.clf() |
| 295 |
pl.suptitle("Embedding spaces", fontsize=14) |
| 296 |
for ialpha, alpha in enumerate(list_alphas): |
| 297 |
pl.subplot(1, len(list_alphas), ialpha + 1) |
| 298 |
pl.title(f"alpha = {alpha}", fontsize=14) |
| 299 |
for cluster in range(nlabels): |
| 300 |
start, end = Nc * cluster, Nc * (cluster + 1) |
| 301 |
if cluster == 0: |
| 302 |
pl.scatter( |
| 303 |
list_unmixings2D[ialpha][start:end, 0], |
| 304 |
list_unmixings2D[ialpha][start:end, 1], |
| 305 |
c="C" + str(cluster), |
| 306 |
marker="o", |
| 307 |
s=80.0, |
| 308 |
label="1 cluster", |
| 309 |
) |
| 310 |
else: |
| 311 |
pl.scatter( |
| 312 |
list_unmixings2D[ialpha][start:end, 0], |
| 313 |
list_unmixings2D[ialpha][start:end, 1], |
| 314 |
c="C" + str(cluster), |
| 315 |
marker="o", |
| 316 |
s=80.0, |
| 317 |
label="%s clusters" % (cluster + 1), |
| 318 |
) |
| 319 |
pl.scatter( |
| 320 |
extremities[:, 0], |
| 321 |
extremities[:, 1], |
| 322 |
c="black", |
| 323 |
marker="x", |
| 324 |
s=100.0, |
| 325 |
label="bary. nodes", |
| 326 |
) |
| 327 |
pl.plot([x[0], y[0]], [x[1], y[1]], color="black", linewidth=2.0) |
| 328 |
pl.plot([x[0], z[0]], [x[1], z[1]], color="black", linewidth=2.0) |
| 329 |
pl.plot([y[0], z[0]], [y[1], z[1]], color="black", linewidth=2.0) |
| 330 |
pl.axis("off") |
| 331 |
pl.legend(fontsize=11) |
| 332 |
pl.tight_layout() |
| 333 |
pl.show() |