| 1 |
|
| 2 |
""" |
| 3 |
================================================================ |
| 4 |
Regularization path of l2-penalized unbalanced optimal transport |
| 5 |
================================================================ |
| 6 |
This example illustrate the regularization path for 2D unbalanced |
| 7 |
optimal transport. We present here both the fully relaxed case |
| 8 |
and the semi-relaxed case. |
| 9 |
|
| 10 |
[Chapel et al., 2021] Chapel, L., Flamary, R., Wu, H., Févotte, C., |
| 11 |
and Gasso, G. (2021). Unbalanced optimal transport through non-negative |
| 12 |
penalized linear regression. |
| 13 |
""" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import numpy as np |
| 21 |
import matplotlib.pylab as pl |
| 22 |
import ot |
| 23 |
import matplotlib.animation as animation |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
n = 20 |
| 31 |
|
| 32 |
mu_s = np.array([-1, -1]) |
| 33 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 34 |
|
| 35 |
mu_t = np.array([4, 4]) |
| 36 |
cov_t = np.array([[1, -0.8], [-0.8, 1]]) |
| 37 |
|
| 38 |
np.random.seed(0) |
| 39 |
xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s) |
| 40 |
xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t) |
| 41 |
|
| 42 |
a, b = np.ones((n,)) / n, np.ones((n,)) / n |
| 43 |
|
| 44 |
|
| 45 |
M = ot.dist(xs, xt) |
| 46 |
M /= M.max() |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
pl.figure(1) |
| 55 |
pl.scatter(xs[:, 0], xs[:, 1], c="C0", label="Source") |
| 56 |
pl.scatter(xt[:, 0], xt[:, 1], c="C1", label="Target") |
| 57 |
pl.legend(loc=2) |
| 58 |
pl.title("Source and target distributions") |
| 59 |
pl.show() |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
final_gamma = 1e-6 |
| 67 |
t, t_list, g_list = ot.regpath.regularization_path( |
| 68 |
a, b, M, reg=final_gamma, semi_relaxed=False |
| 69 |
) |
| 70 |
t2, t_list2, g_list2 = ot.regpath.regularization_path( |
| 71 |
a, b, M, reg=final_gamma, semi_relaxed=True |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
pl.figure(2) |
| 85 |
selected_gamma = [2e-1, 1e-1, 5e-2, 1e-3] |
| 86 |
for p in range(4): |
| 87 |
tp = ot.regpath.compute_transport_plan(selected_gamma[p], g_list, t_list) |
| 88 |
P = tp.reshape((n, n)) |
| 89 |
pl.subplot(2, 2, p + 1) |
| 90 |
if P.sum() > 0: |
| 91 |
P = P / P.max() |
| 92 |
for i in range(n): |
| 93 |
for j in range(n): |
| 94 |
if P[i, j] > 0: |
| 95 |
pl.plot( |
| 96 |
[xs[i, 0], xt[j, 0]], |
| 97 |
[xs[i, 1], xt[j, 1]], |
| 98 |
color="C2", |
| 99 |
alpha=P[i, j] * 0.3, |
| 100 |
) |
| 101 |
pl.scatter(xs[:, 0], xs[:, 1], c="C0", alpha=0.2) |
| 102 |
pl.scatter(xt[:, 0], xt[:, 1], c="C1", alpha=0.2) |
| 103 |
pl.scatter( |
| 104 |
xs[:, 0], |
| 105 |
xs[:, 1], |
| 106 |
c="C0", |
| 107 |
s=P.sum(1).ravel() * (1 + p) * 2, |
| 108 |
label="Re-weighted source", |
| 109 |
alpha=1, |
| 110 |
) |
| 111 |
pl.scatter( |
| 112 |
xt[:, 0], |
| 113 |
xt[:, 1], |
| 114 |
c="C1", |
| 115 |
s=P.sum(0).ravel() * (1 + p) * 2, |
| 116 |
label="Re-weighted target", |
| 117 |
alpha=1, |
| 118 |
) |
| 119 |
pl.plot([], [], color="C2", alpha=0.8, label="OT plan") |
| 120 |
pl.title(r"$\ell_2$ UOT $\gamma$={}".format(selected_gamma[p]), fontsize=11) |
| 121 |
if p < 2: |
| 122 |
pl.xticks(()) |
| 123 |
pl.show() |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
nv = 50 |
| 131 |
g_list_v = np.logspace(-0.5, -2.5, nv) |
| 132 |
|
| 133 |
pl.figure(3) |
| 134 |
|
| 135 |
|
| 136 |
def _update_plot(iv): |
| 137 |
pl.clf() |
| 138 |
tp = ot.regpath.compute_transport_plan(g_list_v[iv], g_list, t_list) |
| 139 |
P = tp.reshape((n, n)) |
| 140 |
if P.sum() > 0: |
| 141 |
P = P / P.max() |
| 142 |
for i in range(n): |
| 143 |
for j in range(n): |
| 144 |
if P[i, j] > 0: |
| 145 |
pl.plot( |
| 146 |
[xs[i, 0], xt[j, 0]], |
| 147 |
[xs[i, 1], xt[j, 1]], |
| 148 |
color="C2", |
| 149 |
alpha=P[i, j] * 0.5, |
| 150 |
) |
| 151 |
pl.scatter(xs[:, 0], xs[:, 1], c="C0", alpha=0.2) |
| 152 |
pl.scatter(xt[:, 0], xt[:, 1], c="C1", alpha=0.2) |
| 153 |
pl.scatter( |
| 154 |
xs[:, 0], |
| 155 |
xs[:, 1], |
| 156 |
c="C0", |
| 157 |
s=P.sum(1).ravel() * (1 + p) * 4, |
| 158 |
label="Re-weighted source", |
| 159 |
alpha=1, |
| 160 |
) |
| 161 |
pl.scatter( |
| 162 |
xt[:, 0], |
| 163 |
xt[:, 1], |
| 164 |
c="C1", |
| 165 |
s=P.sum(0).ravel() * (1 + p) * 4, |
| 166 |
label="Re-weighted target", |
| 167 |
alpha=1, |
| 168 |
) |
| 169 |
pl.plot([], [], color="C2", alpha=0.8, label="OT plan") |
| 170 |
pl.title(r"$\ell_2$ UOT $\gamma$={:1.3f}".format(g_list_v[iv]), fontsize=11) |
| 171 |
return 1 |
| 172 |
|
| 173 |
|
| 174 |
i = 0 |
| 175 |
_update_plot(i) |
| 176 |
|
| 177 |
ani = animation.FuncAnimation( |
| 178 |
pl.gcf(), _update_plot, nv, interval=100, repeat_delay=2000 |
| 179 |
) |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
pl.figure(4) |
| 189 |
selected_gamma = [10, 1, 1e-1, 1e-2] |
| 190 |
for p in range(4): |
| 191 |
tp = ot.regpath.compute_transport_plan(selected_gamma[p], g_list2, t_list2) |
| 192 |
P = tp.reshape((n, n)) |
| 193 |
pl.subplot(2, 2, p + 1) |
| 194 |
if P.sum() > 0: |
| 195 |
P = P / P.max() |
| 196 |
for i in range(n): |
| 197 |
for j in range(n): |
| 198 |
if P[i, j] > 0: |
| 199 |
pl.plot( |
| 200 |
[xs[i, 0], xt[j, 0]], |
| 201 |
[xs[i, 1], xt[j, 1]], |
| 202 |
color="C2", |
| 203 |
alpha=P[i, j] * 0.3, |
| 204 |
) |
| 205 |
pl.scatter(xs[:, 0], xs[:, 1], c="C0", alpha=0.2) |
| 206 |
pl.scatter(xt[:, 0], xt[:, 1], c="C1", alpha=1, label="Target marginal") |
| 207 |
pl.scatter( |
| 208 |
xs[:, 0], |
| 209 |
xs[:, 1], |
| 210 |
c="C0", |
| 211 |
s=P.sum(1).ravel() * 2 * (1 + p), |
| 212 |
label="Source marginal", |
| 213 |
alpha=1, |
| 214 |
) |
| 215 |
pl.plot([], [], color="C2", alpha=0.8, label="OT plan") |
| 216 |
pl.title( |
| 217 |
r"Semi-relaxed $l_2$ UOT $\gamma$={}".format(selected_gamma[p]), fontsize=11 |
| 218 |
) |
| 219 |
if p < 2: |
| 220 |
pl.xticks(()) |
| 221 |
pl.show() |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
nv = 50 |
| 229 |
g_list_v = np.logspace(2, -2, nv) |
| 230 |
|
| 231 |
pl.figure(5) |
| 232 |
|
| 233 |
|
| 234 |
def _update_plot(iv): |
| 235 |
pl.clf() |
| 236 |
tp = ot.regpath.compute_transport_plan(g_list_v[iv], g_list2, t_list2) |
| 237 |
P = tp.reshape((n, n)) |
| 238 |
if P.sum() > 0: |
| 239 |
P = P / P.max() |
| 240 |
for i in range(n): |
| 241 |
for j in range(n): |
| 242 |
if P[i, j] > 0: |
| 243 |
pl.plot( |
| 244 |
[xs[i, 0], xt[j, 0]], |
| 245 |
[xs[i, 1], xt[j, 1]], |
| 246 |
color="C2", |
| 247 |
alpha=P[i, j] * 0.5, |
| 248 |
) |
| 249 |
pl.scatter(xs[:, 0], xs[:, 1], c="C0", alpha=0.2) |
| 250 |
pl.scatter(xt[:, 0], xt[:, 1], c="C1", alpha=0.2) |
| 251 |
pl.scatter( |
| 252 |
xs[:, 0], |
| 253 |
xs[:, 1], |
| 254 |
c="C0", |
| 255 |
s=P.sum(1).ravel() * (1 + p) * 4, |
| 256 |
label="Re-weighted source", |
| 257 |
alpha=1, |
| 258 |
) |
| 259 |
pl.scatter( |
| 260 |
xt[:, 0], |
| 261 |
xt[:, 1], |
| 262 |
c="C1", |
| 263 |
s=P.sum(0).ravel() * (1 + p) * 4, |
| 264 |
label="Re-weighted target", |
| 265 |
alpha=1, |
| 266 |
) |
| 267 |
pl.plot([], [], color="C2", alpha=0.8, label="OT plan") |
| 268 |
pl.title( |
| 269 |
r"Semi-relaxed $\ell_2$ UOT $\gamma$={:1.3f}".format(g_list_v[iv]), fontsize=11 |
| 270 |
) |
| 271 |
return 1 |
| 272 |
|
| 273 |
|
| 274 |
i = 0 |
| 275 |
_update_plot(i) |
| 276 |
|
| 277 |
ani = animation.FuncAnimation( |
| 278 |
pl.gcf(), _update_plot, nv, interval=100, repeat_delay=2000 |
| 279 |
) |