| 1 |
|
| 2 |
""" |
| 3 |
========================== |
| 4 |
Gromov-Wasserstein example |
| 5 |
========================== |
| 6 |
This example is designed to show how to use the Gromov-Wasserstein distance |
| 7 |
computation in POT. |
| 8 |
We first compare 3 solvers to estimate the distance based on |
| 9 |
Conditional Gradient [24] or Sinkhorn projections [12, 51]. |
| 10 |
Then we compare 2 stochastic solvers to estimate the distance with a lower |
| 11 |
numerical cost [33]. |
| 12 |
|
| 13 |
[12] Gabriel Peyré, Marco Cuturi, and Justin Solomon (2016), |
| 14 |
"Gromov-Wasserstein averaging of kernel and distance matrices". |
| 15 |
International Conference on Machine Learning (ICML). |
| 16 |
|
| 17 |
[24] Vayer Titouan, Chapel Laetitia, Flamary Rémi, Tavenard Romain |
| 18 |
and Courty Nicolas |
| 19 |
"Optimal Transport for structured data with application on graphs" |
| 20 |
International Conference on Machine Learning (ICML). 2019. |
| 21 |
|
| 22 |
[33] Kerdoncuff T., Emonet R., Marc S. "Sampled Gromov Wasserstein", |
| 23 |
Machine Learning Journal (MJL), 2021. |
| 24 |
|
| 25 |
[51] Xu, H., Luo, D., Zha, H., & Duke, L. C. (2019). |
| 26 |
"Gromov-wasserstein learning for graph matching and node embedding". |
| 27 |
In International Conference on Machine Learning (ICML), 2019. |
| 28 |
|
| 29 |
""" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
import scipy as sp |
| 41 |
import numpy as np |
| 42 |
import matplotlib.pylab as pl |
| 43 |
from mpl_toolkits.mplot3d import Axes3D |
| 44 |
import ot |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
n_samples = 30 |
| 57 |
|
| 58 |
mu_s = np.array([0, 0]) |
| 59 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 60 |
|
| 61 |
mu_t = np.array([4, 4, 4]) |
| 62 |
cov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) |
| 63 |
|
| 64 |
np.random.seed(0) |
| 65 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s) |
| 66 |
P = sp.linalg.sqrtm(cov_t) |
| 67 |
xt = np.random.randn(n_samples, 3).dot(P) + mu_t |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
fig = pl.figure(1) |
| 76 |
ax1 = fig.add_subplot(121) |
| 77 |
ax1.plot(xs[:, 0], xs[:, 1], "+b", label="Source samples") |
| 78 |
ax2 = fig.add_subplot(122, projection="3d") |
| 79 |
ax2.scatter(xt[:, 0], xt[:, 1], xt[:, 2], color="r") |
| 80 |
pl.show() |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
C1 = sp.spatial.distance.cdist(xs, xs) |
| 89 |
C2 = sp.spatial.distance.cdist(xt, xt) |
| 90 |
|
| 91 |
C1 /= C1.max() |
| 92 |
C2 /= C2.max() |
| 93 |
|
| 94 |
pl.figure(2) |
| 95 |
pl.subplot(121) |
| 96 |
pl.imshow(C1) |
| 97 |
pl.title("C1") |
| 98 |
|
| 99 |
pl.subplot(122) |
| 100 |
pl.imshow(C2) |
| 101 |
pl.title("C2") |
| 102 |
|
| 103 |
pl.show() |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
p = ot.unif(n_samples) |
| 111 |
q = ot.unif(n_samples) |
| 112 |
|
| 113 |
|
| 114 |
gw0, log0 = ot.gromov.gromov_wasserstein( |
| 115 |
C1, C2, p, q, "square_loss", verbose=True, log=True |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
gw, log = ot.gromov.entropic_gromov_wasserstein( |
| 120 |
C1, C2, p, q, "square_loss", epsilon=5e-4, solver="PPA", log=True, verbose=True |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
gwe, loge = ot.gromov.entropic_gromov_wasserstein( |
| 125 |
C1, C2, p, q, "square_loss", epsilon=5e-4, solver="PGD", log=True, verbose=True |
| 126 |
) |
| 127 |
|
| 128 |
print( |
| 129 |
"Gromov-Wasserstein distance estimated with Conditional Gradient solver: " |
| 130 |
+ str(log0["gw_dist"]) |
| 131 |
) |
| 132 |
print( |
| 133 |
"Gromov-Wasserstein distance estimated with Proximal Point solver: " |
| 134 |
+ str(log["gw_dist"]) |
| 135 |
) |
| 136 |
print( |
| 137 |
"Entropic Gromov-Wasserstein distance estimated with Projected Gradient solver: " |
| 138 |
+ str(loge["gw_dist"]) |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
gw0_sparsity = 100 * (gw0 == 0.0).astype(np.float64).sum() / (n_samples**2) |
| 143 |
gw_sparsity = 100 * (gw == 0.0).astype(np.float64).sum() / (n_samples**2) |
| 144 |
gwe_sparsity = 100 * (gwe == 0.0).astype(np.float64).sum() / (n_samples**2) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
err0 = np.linalg.norm(gw0.sum(1) - p) + np.linalg.norm(gw0.sum(0) - q) |
| 150 |
err = np.linalg.norm(gw.sum(1) - p) + np.linalg.norm(gw.sum(0) - q) |
| 151 |
erre = np.linalg.norm(gwe.sum(1) - p) + np.linalg.norm(gwe.sum(0) - q) |
| 152 |
|
| 153 |
pl.figure(3, (10, 6)) |
| 154 |
cmap = "Blues" |
| 155 |
fontsize = 12 |
| 156 |
pl.subplot(131) |
| 157 |
pl.imshow(gw0, cmap=cmap) |
| 158 |
pl.title( |
| 159 |
"(CG algo) GW=%s \n \n OT sparsity=%s \n feasibility error=%s" |
| 160 |
% ( |
| 161 |
np.round(log0["gw_dist"], 4), |
| 162 |
str(np.round(gw0_sparsity, 2)) + " %", |
| 163 |
np.round(np.round(err0, 4)), |
| 164 |
), |
| 165 |
fontsize=fontsize, |
| 166 |
) |
| 167 |
|
| 168 |
pl.subplot(132) |
| 169 |
pl.imshow(gw, cmap=cmap) |
| 170 |
pl.title( |
| 171 |
"(PP algo) GW=%s \n \n OT sparsity=%s \nfeasibility error=%s" |
| 172 |
% ( |
| 173 |
np.round(log["gw_dist"], 4), |
| 174 |
str(np.round(gw_sparsity, 2)) + " %", |
| 175 |
np.round(err, 4), |
| 176 |
), |
| 177 |
fontsize=fontsize, |
| 178 |
) |
| 179 |
|
| 180 |
pl.subplot(133) |
| 181 |
pl.imshow(gwe, cmap=cmap) |
| 182 |
pl.title( |
| 183 |
"Entropic GW=%s \n \n OT sparsity=%s \nfeasibility error=%s" |
| 184 |
% ( |
| 185 |
np.round(loge["gw_dist"], 4), |
| 186 |
str(np.round(gwe_sparsity, 2)) + " %", |
| 187 |
np.round(erre, 4), |
| 188 |
), |
| 189 |
fontsize=fontsize, |
| 190 |
) |
| 191 |
|
| 192 |
pl.tight_layout() |
| 193 |
pl.show() |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
def loss(x, y): |
| 202 |
return np.abs(x - y) |
| 203 |
|
| 204 |
|
| 205 |
pgw, plog = ot.gromov.pointwise_gromov_wasserstein( |
| 206 |
C1, C2, p, q, loss, max_iter=100, log=True |
| 207 |
) |
| 208 |
|
| 209 |
sgw, slog = ot.gromov.sampled_gromov_wasserstein( |
| 210 |
C1, C2, p, q, loss, epsilon=0.1, max_iter=100, log=True |
| 211 |
) |
| 212 |
|
| 213 |
print( |
| 214 |
"Pointwise Gromov-Wasserstein distance estimated: " + str(plog["gw_dist_estimated"]) |
| 215 |
) |
| 216 |
print("Variance estimated: " + str(plog["gw_dist_std"])) |
| 217 |
print("Sampled Gromov-Wasserstein distance: " + str(slog["gw_dist_estimated"])) |
| 218 |
print("Variance estimated: " + str(slog["gw_dist_std"])) |
| 219 |
|
| 220 |
|
| 221 |
pl.figure(4, (10, 5)) |
| 222 |
|
| 223 |
pl.subplot(121) |
| 224 |
pl.imshow(pgw.toarray(), cmap=cmap) |
| 225 |
pl.title("Pointwise Gromov Wasserstein") |
| 226 |
|
| 227 |
pl.subplot(122) |
| 228 |
pl.imshow(sgw, cmap=cmap) |
| 229 |
pl.title("Sampled Gromov Wasserstein") |
| 230 |
|
| 231 |
pl.show() |