| 1 |
|
| 2 |
""" |
| 3 |
================================================== |
| 4 |
Partial Wasserstein and Gromov-Wasserstein example |
| 5 |
================================================== |
| 6 |
|
| 7 |
This example is designed to show how to use the Partial (Gromov-)Wasserstein |
| 8 |
distance computation in POT. |
| 9 |
""" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
from mpl_toolkits.mplot3d import Axes3D |
| 18 |
import scipy as sp |
| 19 |
import numpy as np |
| 20 |
import matplotlib.pylab as pl |
| 21 |
import ot |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
n_samples = 20 |
| 34 |
n_noise = 20 |
| 35 |
|
| 36 |
mu = np.array([0, 0]) |
| 37 |
cov = np.array([[1, 0], [0, 2]]) |
| 38 |
|
| 39 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov) |
| 40 |
xs = np.append(xs, (np.random.rand(n_noise, 2) + 1) * 4).reshape((-1, 2)) |
| 41 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov) |
| 42 |
xt = np.append(xt, (np.random.rand(n_noise, 2) + 1) * -3).reshape((-1, 2)) |
| 43 |
|
| 44 |
M = sp.spatial.distance.cdist(xs, xt) |
| 45 |
|
| 46 |
fig = pl.figure() |
| 47 |
ax1 = fig.add_subplot(131) |
| 48 |
ax1.plot(xs[:, 0], xs[:, 1], "+b", label="Source samples") |
| 49 |
ax2 = fig.add_subplot(132) |
| 50 |
ax2.scatter(xt[:, 0], xt[:, 1], color="r") |
| 51 |
ax3 = fig.add_subplot(133) |
| 52 |
ax3.imshow(M) |
| 53 |
pl.show() |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
p = ot.unif(n_samples + n_noise) |
| 61 |
q = ot.unif(n_samples + n_noise) |
| 62 |
|
| 63 |
w0, log0 = ot.partial.partial_wasserstein(p, q, M, m=0.5, log=True) |
| 64 |
w, log = ot.partial.entropic_partial_wasserstein(p, q, M, reg=0.1, m=0.5, log=True) |
| 65 |
|
| 66 |
print("Partial Wasserstein distance (m = 0.5): " + str(log0["partial_w_dist"])) |
| 67 |
print("Entropic partial Wasserstein distance (m = 0.5): " + str(log["partial_w_dist"])) |
| 68 |
|
| 69 |
pl.figure(1, (10, 5)) |
| 70 |
pl.subplot(1, 2, 1) |
| 71 |
pl.imshow(w0, cmap="jet") |
| 72 |
pl.title("Partial Wasserstein") |
| 73 |
pl.subplot(1, 2, 2) |
| 74 |
pl.imshow(w, cmap="jet") |
| 75 |
pl.title("Entropic partial Wasserstein") |
| 76 |
pl.show() |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
n_samples = 20 |
| 89 |
n_noise = 10 |
| 90 |
|
| 91 |
p = ot.unif(n_samples + n_noise) |
| 92 |
q = ot.unif(n_samples + n_noise) |
| 93 |
|
| 94 |
mu_s = np.array([0, 0]) |
| 95 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 96 |
|
| 97 |
mu_t = np.array([0, 0, 0]) |
| 98 |
cov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) |
| 99 |
|
| 100 |
|
| 101 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s) |
| 102 |
xs = np.concatenate((xs, ((np.random.rand(n_noise, 2) + 1) * 4)), axis=0) |
| 103 |
P = sp.linalg.sqrtm(cov_t) |
| 104 |
xt = np.random.randn(n_samples, 3).dot(P) + mu_t |
| 105 |
xt = np.concatenate((xt, ((np.random.rand(n_noise, 3) + 1) * 10)), axis=0) |
| 106 |
|
| 107 |
fig = pl.figure() |
| 108 |
ax1 = fig.add_subplot(121) |
| 109 |
ax1.plot(xs[:, 0], xs[:, 1], "+b", label="Source samples") |
| 110 |
ax2 = fig.add_subplot(122, projection="3d") |
| 111 |
ax2.scatter(xt[:, 0], xt[:, 1], xt[:, 2], color="r") |
| 112 |
pl.show() |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
C1 = sp.spatial.distance.cdist(xs, xs) |
| 121 |
C2 = sp.spatial.distance.cdist(xt, xt) |
| 122 |
|
| 123 |
|
| 124 |
print("------m = 1") |
| 125 |
m = 1 |
| 126 |
res0, log0 = ot.gromov.partial_gromov_wasserstein(C1, C2, p, q, m=m, log=True) |
| 127 |
res, log = ot.gromov.entropic_partial_gromov_wasserstein( |
| 128 |
C1, C2, p, q, 10, m=m, log=True, verbose=True |
| 129 |
) |
| 130 |
|
| 131 |
print("Wasserstein distance (m = 1): " + str(log0["partial_gw_dist"])) |
| 132 |
print("Entropic Wasserstein distance (m = 1): " + str(log["partial_gw_dist"])) |
| 133 |
|
| 134 |
pl.figure(1, (10, 5)) |
| 135 |
pl.title("mass to be transported m = 1") |
| 136 |
pl.subplot(1, 2, 1) |
| 137 |
pl.imshow(res0, cmap="jet") |
| 138 |
pl.title("Gromov-Wasserstein") |
| 139 |
pl.subplot(1, 2, 2) |
| 140 |
pl.imshow(res, cmap="jet") |
| 141 |
pl.title("Entropic Gromov-Wasserstein") |
| 142 |
pl.show() |
| 143 |
|
| 144 |
|
| 145 |
print("------m = 2/3") |
| 146 |
m = 2 / 3 |
| 147 |
res0, log0 = ot.gromov.partial_gromov_wasserstein( |
| 148 |
C1, C2, p, q, m=m, log=True, verbose=True |
| 149 |
) |
| 150 |
res, log = ot.gromov.entropic_partial_gromov_wasserstein( |
| 151 |
C1, C2, p, q, 10, m=m, log=True, verbose=True |
| 152 |
) |
| 153 |
|
| 154 |
print("Partial Wasserstein distance (m = 2/3): " + str(log0["partial_gw_dist"])) |
| 155 |
print("Entropic partial Wasserstein distance (m = 2/3): " + str(log["partial_gw_dist"])) |
| 156 |
|
| 157 |
pl.figure(1, (10, 5)) |
| 158 |
pl.title("mass to be transported m = 2/3") |
| 159 |
pl.subplot(1, 2, 1) |
| 160 |
pl.imshow(res0, cmap="jet") |
| 161 |
pl.title("Partial Gromov-Wasserstein") |
| 162 |
pl.subplot(1, 2, 2) |
| 163 |
pl.imshow(res, cmap="jet") |
| 164 |
pl.title("Entropic partial Gromov-Wasserstein") |
| 165 |
pl.show() |