| 1 |
|
| 2 |
r""" |
| 3 |
====================================================================== |
| 4 |
Continuous OT plan estimation with Pytorch |
| 5 |
====================================================================== |
| 6 |
|
| 7 |
|
| 8 |
""" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import numpy as np |
| 17 |
import matplotlib.pyplot as pl |
| 18 |
import torch |
| 19 |
from torch import nn |
| 20 |
import ot |
| 21 |
import ot.plot |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
torch.manual_seed(42) |
| 28 |
np.random.seed(42) |
| 29 |
|
| 30 |
n_source_samples = 1000 |
| 31 |
n_target_samples = 1000 |
| 32 |
theta = 2 * np.pi / 20 |
| 33 |
noise_level = 0.1 |
| 34 |
|
| 35 |
Xs = np.random.randn(n_source_samples, 2) * 0.5 |
| 36 |
Xt = np.random.randn(n_target_samples, 2) * 2 |
| 37 |
|
| 38 |
|
| 39 |
Xt = Xt + 4 |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
nvisu = 300 |
| 46 |
pl.figure(1, (5, 5)) |
| 47 |
pl.clf() |
| 48 |
pl.scatter(Xs[:nvisu, 0], Xs[:nvisu, 1], marker="+", label="Source samples", alpha=0.5) |
| 49 |
pl.scatter(Xt[:nvisu, 0], Xt[:nvisu, 1], marker="o", label="Target samples", alpha=0.5) |
| 50 |
pl.legend(loc=0) |
| 51 |
ax_bounds = pl.axis() |
| 52 |
pl.title("Source and target distributions") |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
xs = torch.tensor(Xs) |
| 59 |
xt = torch.tensor(Xt) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
torch.manual_seed(42) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
class Potential(torch.nn.Module): |
| 71 |
def __init__(self): |
| 72 |
super(Potential, self).__init__() |
| 73 |
self.fc1 = nn.Linear(2, 200) |
| 74 |
self.fc2 = nn.Linear(200, 1) |
| 75 |
self.relu = torch.nn.ReLU() |
| 76 |
|
| 77 |
def forward(self, x): |
| 78 |
output = self.fc1(x) |
| 79 |
output = self.relu(output) |
| 80 |
output = self.fc2(output) |
| 81 |
return output.ravel() |
| 82 |
|
| 83 |
|
| 84 |
u = Potential().double() |
| 85 |
v = Potential().double() |
| 86 |
|
| 87 |
reg = 1 |
| 88 |
|
| 89 |
optimizer = torch.optim.Adam(list(u.parameters()) + list(v.parameters()), lr=0.005) |
| 90 |
|
| 91 |
|
| 92 |
n_iter = 500 |
| 93 |
n_batch = 500 |
| 94 |
|
| 95 |
|
| 96 |
losses = [] |
| 97 |
|
| 98 |
for i in range(n_iter): |
| 99 |
|
| 100 |
|
| 101 |
iperms = torch.randint(0, n_source_samples, (n_batch,)) |
| 102 |
ipermt = torch.randint(0, n_target_samples, (n_batch,)) |
| 103 |
|
| 104 |
xsi = xs[iperms] |
| 105 |
xti = xt[ipermt] |
| 106 |
|
| 107 |
|
| 108 |
loss = -ot.stochastic.loss_dual_entropic(u(xsi), v(xti), xsi, xti, reg=reg) |
| 109 |
losses.append(float(loss.detach())) |
| 110 |
|
| 111 |
if i % 10 == 0: |
| 112 |
print("Iter: {:3d}, loss={}".format(i, losses[-1])) |
| 113 |
|
| 114 |
loss.backward() |
| 115 |
optimizer.step() |
| 116 |
optimizer.zero_grad() |
| 117 |
|
| 118 |
|
| 119 |
pl.figure(2) |
| 120 |
pl.plot(losses) |
| 121 |
pl.grid() |
| 122 |
pl.title("Dual objective (negative)") |
| 123 |
pl.xlabel("Iterations") |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
nv = 100 |
| 132 |
xl = np.linspace(ax_bounds[0], ax_bounds[1], nv) |
| 133 |
yl = np.linspace(ax_bounds[2], ax_bounds[3], nv) |
| 134 |
|
| 135 |
XX, YY = np.meshgrid(xl, yl) |
| 136 |
|
| 137 |
xg = np.concatenate((XX.ravel()[:, None], YY.ravel()[:, None]), axis=1) |
| 138 |
|
| 139 |
wxg = np.exp(-((xg[:, 0] - 4) ** 2 + (xg[:, 1] - 4) ** 2) / (2 * 2)) |
| 140 |
wxg = wxg / np.sum(wxg) |
| 141 |
|
| 142 |
xg = torch.tensor(xg) |
| 143 |
wxg = torch.tensor(wxg) |
| 144 |
|
| 145 |
|
| 146 |
pl.figure(4, (12, 4)) |
| 147 |
pl.clf() |
| 148 |
pl.subplot(1, 3, 1) |
| 149 |
|
| 150 |
iv = 2 |
| 151 |
Gg = ot.stochastic.plan_dual_entropic( |
| 152 |
u(xs[iv : iv + 1, :]), v(xg), xs[iv : iv + 1, :], xg, reg=reg, wt=wxg |
| 153 |
) |
| 154 |
Gg = Gg.reshape((nv, nv)).detach().numpy() |
| 155 |
|
| 156 |
pl.scatter(Xs[:nvisu, 0], Xs[:nvisu, 1], marker="+", zorder=2, alpha=0.05) |
| 157 |
pl.scatter(Xt[:nvisu, 0], Xt[:nvisu, 1], marker="o", zorder=2, alpha=0.05) |
| 158 |
pl.scatter( |
| 159 |
Xs[iv : iv + 1, 0], |
| 160 |
Xs[iv : iv + 1, 1], |
| 161 |
s=100, |
| 162 |
marker="+", |
| 163 |
label="Source sample", |
| 164 |
zorder=2, |
| 165 |
alpha=1, |
| 166 |
color="C0", |
| 167 |
) |
| 168 |
pl.pcolormesh(XX, YY, Gg, cmap="Greens", label="Density of transported source sample") |
| 169 |
pl.legend(loc=0) |
| 170 |
ax_bounds = pl.axis() |
| 171 |
pl.title("Density of transported source sample") |
| 172 |
|
| 173 |
pl.subplot(1, 3, 2) |
| 174 |
|
| 175 |
iv = 3 |
| 176 |
Gg = ot.stochastic.plan_dual_entropic( |
| 177 |
u(xs[iv : iv + 1, :]), v(xg), xs[iv : iv + 1, :], xg, reg=reg, wt=wxg |
| 178 |
) |
| 179 |
Gg = Gg.reshape((nv, nv)).detach().numpy() |
| 180 |
|
| 181 |
pl.scatter(Xs[:nvisu, 0], Xs[:nvisu, 1], marker="+", zorder=2, alpha=0.05) |
| 182 |
pl.scatter(Xt[:nvisu, 0], Xt[:nvisu, 1], marker="o", zorder=2, alpha=0.05) |
| 183 |
pl.scatter( |
| 184 |
Xs[iv : iv + 1, 0], |
| 185 |
Xs[iv : iv + 1, 1], |
| 186 |
s=100, |
| 187 |
marker="+", |
| 188 |
label="Source sample", |
| 189 |
zorder=2, |
| 190 |
alpha=1, |
| 191 |
color="C0", |
| 192 |
) |
| 193 |
pl.pcolormesh(XX, YY, Gg, cmap="Greens", label="Density of transported source sample") |
| 194 |
pl.legend(loc=0) |
| 195 |
ax_bounds = pl.axis() |
| 196 |
pl.title("Density of transported source sample") |
| 197 |
|
| 198 |
pl.subplot(1, 3, 3) |
| 199 |
|
| 200 |
iv = 6 |
| 201 |
Gg = ot.stochastic.plan_dual_entropic( |
| 202 |
u(xs[iv : iv + 1, :]), v(xg), xs[iv : iv + 1, :], xg, reg=reg, wt=wxg |
| 203 |
) |
| 204 |
Gg = Gg.reshape((nv, nv)).detach().numpy() |
| 205 |
|
| 206 |
pl.scatter(Xs[:nvisu, 0], Xs[:nvisu, 1], marker="+", zorder=2, alpha=0.05) |
| 207 |
pl.scatter(Xt[:nvisu, 0], Xt[:nvisu, 1], marker="o", zorder=2, alpha=0.05) |
| 208 |
pl.scatter( |
| 209 |
Xs[iv : iv + 1, 0], |
| 210 |
Xs[iv : iv + 1, 1], |
| 211 |
s=100, |
| 212 |
marker="+", |
| 213 |
label="Source sample", |
| 214 |
zorder=2, |
| 215 |
alpha=1, |
| 216 |
color="C0", |
| 217 |
) |
| 218 |
pl.pcolormesh(XX, YY, Gg, cmap="Greens", label="Density of transported source sample") |
| 219 |
pl.legend(loc=0) |
| 220 |
ax_bounds = pl.axis() |
| 221 |
pl.title("Density of transported source sample") |