| 1 |
r""" |
| 2 |
============================================================ |
| 3 |
Sliced Wasserstein barycenter and gradient flow with PyTorch |
| 4 |
============================================================ |
| 5 |
|
| 6 |
In this example we use the pytorch backend to optimize the sliced Wasserstein |
| 7 |
loss between two empirical distributions [31]. |
| 8 |
|
| 9 |
In the first example one we perform a |
| 10 |
gradient flow on the support of a distribution that minimize the sliced |
| 11 |
Wasserstein distance as proposed in [36]. |
| 12 |
|
| 13 |
In the second example we optimize with a gradient descent the sliced |
| 14 |
Wasserstein barycenter between two distributions as in [31]. |
| 15 |
|
| 16 |
[31] Bonneel, Nicolas, et al. "Sliced and radon wasserstein barycenters of |
| 17 |
measures." Journal of Mathematical Imaging and Vision 51.1 (2015): 22-45 |
| 18 |
|
| 19 |
[36] Liutkus, A., Simsekli, U., Majewski, S., Durmus, A., & Stöter, F. R. |
| 20 |
(2019, May). Sliced-Wasserstein flows: Nonparametric generative modeling |
| 21 |
via optimal transport and diffusions. In International Conference on |
| 22 |
Machine Learning (pp. 4104-4113). PMLR. |
| 23 |
|
| 24 |
|
| 25 |
""" |
| 26 |
> |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
import numpy as np |
| 37 |
import matplotlib.pylab as pl |
| 38 |
import torch |
| 39 |
import ot |
| 40 |
import matplotlib.animation as animation |
| 41 |
|
| 42 |
I1 = pl.imread("../../data/redcross.png").astype(np.float64)[::5, ::5, 2] |
| 43 |
I2 = pl.imread("../../data/tooth.png").astype(np.float64)[::5, ::5, 2] |
| 44 |
|
| 45 |
sz = I2.shape[0] |
| 46 |
XX, YY = np.meshgrid(np.arange(sz), np.arange(sz)) |
| 47 |
|
| 48 |
x1 = np.stack((XX[I1 == 0], YY[I1 == 0]), 1) * 1.0 |
| 49 |
x2 = np.stack((XX[I2 == 0] + 60, -YY[I2 == 0] + 32), 1) * 1.0 |
| 50 |
x3 = np.stack((XX[I2 == 0], -YY[I2 == 0] + 32), 1) * 1.0 |
| 51 |
|
| 52 |
pl.figure(1, (8, 4)) |
| 53 |
pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5) |
| 54 |
pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
device = "cuda" if torch.cuda.is_available() else "cpu" |
| 62 |
|
| 63 |
|
| 64 |
x1_torch = torch.tensor(x1).to(device=device).requires_grad_(True) |
| 65 |
x2_torch = torch.tensor(x2).to(device=device) |
| 66 |
|
| 67 |
|
| 68 |
lr = 1e3 |
| 69 |
nb_iter_max = 50 |
| 70 |
|
| 71 |
x_all = np.zeros((nb_iter_max, x1.shape[0], 2)) |
| 72 |
|
| 73 |
loss_iter = [] |
| 74 |
|
| 75 |
|
| 76 |
gen = torch.Generator(device=device) |
| 77 |
gen.manual_seed(42) |
| 78 |
|
| 79 |
for i in range(nb_iter_max): |
| 80 |
loss = ot.sliced_wasserstein_distance( |
| 81 |
x1_torch, x2_torch, n_projections=20, seed=gen |
| 82 |
) |
| 83 |
|
| 84 |
loss_iter.append(loss.clone().detach().cpu().numpy()) |
| 85 |
loss.backward() |
| 86 |
|
| 87 |
|
| 88 |
with torch.no_grad(): |
| 89 |
grad = x1_torch.grad |
| 90 |
x1_torch -= grad * lr / (1 + i / 5e1) |
| 91 |
x1_torch.grad.zero_() |
| 92 |
x_all[i, :, :] = x1_torch.clone().detach().cpu().numpy() |
| 93 |
|
| 94 |
xb = x1_torch.clone().detach().cpu().numpy() |
| 95 |
|
| 96 |
pl.figure(2, (8, 4)) |
| 97 |
pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5, label="$\mu^{(0)}$") |
| 98 |
pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5, label=r"$\nu$") |
| 99 |
pl.scatter(xb[:, 0], xb[:, 1], alpha=0.5, label="$\mu^{(100)}$") |
| 100 |
pl.title("Sliced Wasserstein gradient flow") |
| 101 |
pl.legend() |
| 102 |
ax = pl.axis() |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
pl.figure(3, (8, 4)) |
| 109 |
|
| 110 |
|
| 111 |
def _update_plot(i): |
| 112 |
pl.clf() |
| 113 |
pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5, label="$\mu^{(0)}$") |
| 114 |
pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5, label=r"$\nu$") |
| 115 |
pl.scatter(x_all[i, :, 0], x_all[i, :, 1], alpha=0.5, label="$\mu^{(100)}$") |
| 116 |
pl.title("Sliced Wasserstein gradient flow Iter. {}".format(i)) |
| 117 |
pl.axis(ax) |
| 118 |
return 1 |
| 119 |
|
| 120 |
|
| 121 |
ani = animation.FuncAnimation( |
| 122 |
pl.gcf(), _update_plot, nb_iter_max, interval=100, repeat_delay=2000 |
| 123 |
) |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
x1_torch = torch.tensor(x1).to(device=device) |
| 129 |
x3_torch = torch.tensor(x3).to(device=device) |
| 130 |
xbinit = np.random.randn(500, 2) * 10 + 16 |
| 131 |
xbary_torch = torch.tensor(xbinit).to(device=device).requires_grad_(True) |
| 132 |
|
| 133 |
lr = 1e3 |
| 134 |
nb_iter_max = 50 |
| 135 |
|
| 136 |
x_all = np.zeros((nb_iter_max, xbary_torch.shape[0], 2)) |
| 137 |
|
| 138 |
loss_iter = [] |
| 139 |
|
| 140 |
|
| 141 |
gen = torch.Generator(device=device) |
| 142 |
gen.manual_seed(42) |
| 143 |
|
| 144 |
alpha = 0.5 |
| 145 |
|
| 146 |
for i in range(nb_iter_max): |
| 147 |
loss = alpha * ot.sliced_wasserstein_distance( |
| 148 |
xbary_torch, x3_torch, n_projections=50, seed=gen |
| 149 |
) + (1 - alpha) * ot.sliced_wasserstein_distance( |
| 150 |
xbary_torch, x1_torch, n_projections=50, seed=gen |
| 151 |
) |
| 152 |
|
| 153 |
loss_iter.append(loss.clone().detach().cpu().numpy()) |
| 154 |
loss.backward() |
| 155 |
|
| 156 |
|
| 157 |
with torch.no_grad(): |
| 158 |
grad = xbary_torch.grad |
| 159 |
xbary_torch -= grad * lr |
| 160 |
xbary_torch.grad.zero_() |
| 161 |
x_all[i, :, :] = xbary_torch.clone().detach().cpu().numpy() |
| 162 |
|
| 163 |
xb = xbary_torch.clone().detach().cpu().numpy() |
| 164 |
|
| 165 |
pl.figure(4, (8, 4)) |
| 166 |
pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5, label="$\mu$") |
| 167 |
pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5, label=r"$\nu$") |
| 168 |
pl.scatter(xb[:, 0] + 30, xb[:, 1], alpha=0.5, label="Barycenter") |
| 169 |
pl.title("Sliced Wasserstein barycenter") |
| 170 |
pl.legend() |
| 171 |
ax = pl.axis() |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
pl.figure(5, (8, 4)) |
| 179 |
|
| 180 |
|
| 181 |
def _update_plot(i): |
| 182 |
pl.clf() |
| 183 |
pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5, label="$\mu^{(0)}$") |
| 184 |
pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5, label=r"$\nu$") |
| 185 |
pl.scatter(x_all[i, :, 0] + 30, x_all[i, :, 1], alpha=0.5, label="$\mu^{(100)}$") |
| 186 |
pl.title("Sliced Wasserstein barycenter Iter. {}".format(i)) |
| 187 |
pl.axis(ax) |
| 188 |
return 1 |
| 189 |
|
| 190 |
|
| 191 |
ani = animation.FuncAnimation( |
| 192 |
pl.gcf(), _update_plot, nb_iter_max, interval=100, repeat_delay=2000 |
| 193 |
) |