| 1 |
|
| 2 |
""" |
| 3 |
=============================================================== |
| 4 |
Translation Invariant Sinkhorn for Unbalanced Optimal Transport |
| 5 |
=============================================================== |
| 6 |
|
| 7 |
This examples illustrates the better convergence of the translation |
| 8 |
invariance Sinkhorn algorithm proposed in [73] compared to the classical |
| 9 |
Sinkhorn algorithm. |
| 10 |
|
| 11 |
[73] Séjourné, T., Vialard, F. X., & Peyré, G. (2022). |
| 12 |
Faster unbalanced optimal transport: Translation invariant sinkhorn and 1-d frank-wolfe. |
| 13 |
In International Conference on Artificial Intelligence and Statistics (pp. 4995-5021). PMLR. |
| 14 |
|
| 15 |
""" |
| 16 |
|
| 17 |
> |
| 18 |
|
| 19 |
|
| 20 |
import numpy as np |
| 21 |
import matplotlib.pylab as pl |
| 22 |
import ot |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
n_iter = 50 |
| 31 |
n = 40 |
| 32 |
|
| 33 |
num_iter_max = 100 |
| 34 |
n_noise = 10 |
| 35 |
|
| 36 |
reg = 0.005 |
| 37 |
reg_m_kl = 0.05 |
| 38 |
|
| 39 |
mu_s = np.array([-1, -1]) |
| 40 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 41 |
|
| 42 |
mu_t = np.array([4, 4]) |
| 43 |
cov_t = np.array([[1, -0.8], [-0.8, 1]]) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
err_sinkhorn_uot = np.empty((n_iter, num_iter_max)) |
| 51 |
err_sinkhorn_uot_ti = np.empty((n_iter, num_iter_max)) |
| 52 |
|
| 53 |
|
| 54 |
for seed in range(n_iter): |
| 55 |
np.random.seed(seed) |
| 56 |
xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s) |
| 57 |
xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t) |
| 58 |
|
| 59 |
xs = np.concatenate((xs, (np.random.rand(n_noise, 2) - 4)), axis=0) |
| 60 |
xt = np.concatenate((xt, (np.random.rand(n_noise, 2) + 6)), axis=0) |
| 61 |
|
| 62 |
n = n + n_noise |
| 63 |
|
| 64 |
a, b = np.ones((n,)) / n, np.ones((n,)) / n |
| 65 |
|
| 66 |
|
| 67 |
M = ot.dist(xs, xt) |
| 68 |
M /= M.max() |
| 69 |
|
| 70 |
entropic_kl_uot, log_uot = ot.unbalanced.sinkhorn_unbalanced( |
| 71 |
a, |
| 72 |
b, |
| 73 |
M, |
| 74 |
reg, |
| 75 |
reg_m_kl, |
| 76 |
reg_type="kl", |
| 77 |
log=True, |
| 78 |
numItermax=num_iter_max, |
| 79 |
stopThr=0, |
| 80 |
) |
| 81 |
entropic_kl_uot_ti, log_uot_ti = ot.unbalanced.sinkhorn_unbalanced( |
| 82 |
a, |
| 83 |
b, |
| 84 |
M, |
| 85 |
reg, |
| 86 |
reg_m_kl, |
| 87 |
reg_type="kl", |
| 88 |
method="sinkhorn_translation_invariant", |
| 89 |
log=True, |
| 90 |
numItermax=num_iter_max, |
| 91 |
stopThr=0, |
| 92 |
) |
| 93 |
|
| 94 |
err_sinkhorn_uot[seed] = log_uot["err"] |
| 95 |
err_sinkhorn_uot_ti[seed] = log_uot_ti["err"] |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
mean_sinkh = np.mean(err_sinkhorn_uot, axis=0) |
| 102 |
std_sinkh = np.std(err_sinkhorn_uot, axis=0) |
| 103 |
|
| 104 |
mean_sinkh_ti = np.mean(err_sinkhorn_uot_ti, axis=0) |
| 105 |
std_sinkh_ti = np.std(err_sinkhorn_uot_ti, axis=0) |
| 106 |
|
| 107 |
absc = list(range(num_iter_max)) |
| 108 |
|
| 109 |
pl.plot(absc, mean_sinkh, label="Sinkhorn") |
| 110 |
pl.fill_between(absc, mean_sinkh - 2 * std_sinkh, mean_sinkh + 2 * std_sinkh, alpha=0.5) |
| 111 |
|
| 112 |
pl.plot(absc, mean_sinkh_ti, label="Translation Invariant Sinkhorn") |
| 113 |
pl.fill_between( |
| 114 |
absc, mean_sinkh_ti - 2 * std_sinkh_ti, mean_sinkh_ti + 2 * std_sinkh_ti, alpha=0.5 |
| 115 |
) |
| 116 |
|
| 117 |
pl.yscale("log") |
| 118 |
pl.legend() |
| 119 |
pl.xlabel("Number of Iterations") |
| 120 |
pl.ylabel(r"$\|u-v\|_\infty$") |
| 121 |
pl.grid(True) |
| 122 |
pl.show() |