| 1 |
|
| 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 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
n = 20 |
| 36 |
n2 = 30 |
| 37 |
sig = 1 |
| 38 |
sig2 = 0.1 |
| 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 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 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 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 149 |
Gg, log = gromov_wasserstein( |
| 150 |
C1, C2, p, q, loss_fun="square_loss", verbose=True, log=True |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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() |