| 1 |
|
| 2 |
""" |
| 3 |
============================================================== |
| 4 |
2D examples of exact and entropic unbalanced optimal transport |
| 5 |
============================================================== |
| 6 |
This example is designed to show how to compute unbalanced and |
| 7 |
partial OT in POT. |
| 8 |
|
| 9 |
UOT aims at solving the following optimization problem: |
| 10 |
|
| 11 |
.. math:: |
| 12 |
W = \min_{\gamma} <\gamma, \mathbf{M}>_F + |
| 13 |
\mathrm{reg}\cdot\Omega(\gamma) + |
| 14 |
\mathrm{reg_m} \cdot \mathrm{div}(\gamma \mathbf{1}, \mathbf{a}) + |
| 15 |
\mathrm{reg_m} \cdot \mathrm{div}(\gamma^T \mathbf{1}, \mathbf{b}) |
| 16 |
|
| 17 |
s.t. |
| 18 |
\gamma \geq 0 |
| 19 |
|
| 20 |
where :math:`\mathrm{div}` is a divergence. |
| 21 |
When using the entropic UOT, :math:`\mathrm{reg}>0` and :math:`\mathrm{div}` |
| 22 |
should be the Kullback-Leibler divergence. |
| 23 |
When solving exact UOT, :math:`\mathrm{reg}=0` and :math:`\mathrm{div}` |
| 24 |
can be either the Kullback-Leibler or the quadratic divergence. |
| 25 |
Using :math:`\ell_1` norm gives the so-called partial OT. |
| 26 |
""" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
import numpy as np |
| 32 |
import matplotlib.pylab as pl |
| 33 |
import ot |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
n = 40 |
| 42 |
|
| 43 |
mu_s = np.array([-1, -1]) |
| 44 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 45 |
|
| 46 |
mu_t = np.array([4, 4]) |
| 47 |
cov_t = np.array([[1, -0.8], [-0.8, 1]]) |
| 48 |
|
| 49 |
np.random.seed(0) |
| 50 |
xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s) |
| 51 |
xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t) |
| 52 |
|
| 53 |
n_noise = 10 |
| 54 |
|
| 55 |
xs = np.concatenate((xs, (np.random.rand(n_noise, 2) - 4)), axis=0) |
| 56 |
xt = np.concatenate((xt, (np.random.rand(n_noise, 2) + 6)), axis=0) |
| 57 |
|
| 58 |
n = n + n_noise |
| 59 |
|
| 60 |
a, b = np.ones((n,)) / n, np.ones((n,)) / n |
| 61 |
|
| 62 |
|
| 63 |
M = ot.dist(xs, xt) |
| 64 |
M /= M.max() |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
reg = 0.005 |
| 72 |
reg_m_kl = 0.05 |
| 73 |
reg_m_l2 = 5 |
| 74 |
mass = 0.7 |
| 75 |
|
| 76 |
entropic_kl_uot = ot.unbalanced.sinkhorn_unbalanced(a, b, M, reg, reg_m_kl) |
| 77 |
kl_uot = ot.unbalanced.mm_unbalanced(a, b, M, reg_m_kl, div="kl") |
| 78 |
l2_uot = ot.unbalanced.mm_unbalanced(a, b, M, reg_m_l2, div="l2") |
| 79 |
partial_ot = ot.partial.partial_wasserstein(a, b, M, m=mass) |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
pl.figure(2) |
| 86 |
transp = [partial_ot, l2_uot, kl_uot, entropic_kl_uot] |
| 87 |
title = [ |
| 88 |
"partial OT \n m=" + str(mass), |
| 89 |
"$\ell_2$-UOT \n $\mathrm{reg_m}$=" + str(reg_m_l2), |
| 90 |
"kl-UOT \n $\mathrm{reg_m}$=" + str(reg_m_kl), |
| 91 |
"entropic kl-UOT \n $\mathrm{reg_m}$=" + str(reg_m_kl), |
| 92 |
] |
| 93 |
|
| 94 |
for p in range(4): |
| 95 |
pl.subplot(2, 4, p + 1) |
| 96 |
P = transp[p] |
| 97 |
if P.sum() > 0: |
| 98 |
P = P / P.max() |
| 99 |
for i in range(n): |
| 100 |
for j in range(n): |
| 101 |
if P[i, j] > 0: |
| 102 |
pl.plot( |
| 103 |
[xs[i, 0], xt[j, 0]], |
| 104 |
[xs[i, 1], xt[j, 1]], |
| 105 |
color="C2", |
| 106 |
alpha=P[i, j] * 0.3, |
| 107 |
) |
| 108 |
pl.scatter(xs[:, 0], xs[:, 1], c="C0", alpha=0.2) |
| 109 |
pl.scatter(xt[:, 0], xt[:, 1], c="C1", alpha=0.2) |
| 110 |
pl.scatter(xs[:, 0], xs[:, 1], c="C0", s=P.sum(1).ravel() * (1 + p) * 2) |
| 111 |
pl.scatter(xt[:, 0], xt[:, 1], c="C1", s=P.sum(0).ravel() * (1 + p) * 2) |
| 112 |
pl.title(title[p]) |
| 113 |
pl.yticks(()) |
| 114 |
pl.xticks(()) |
| 115 |
if p < 1: |
| 116 |
pl.ylabel("mappings") |
| 117 |
pl.subplot(2, 4, p + 5) |
| 118 |
pl.imshow(P, cmap="jet") |
| 119 |
pl.yticks(()) |
| 120 |
pl.xticks(()) |
| 121 |
if p < 1: |
| 122 |
pl.ylabel("transport plans") |
| 123 |
pl.show() |