| 1 |
|
| 2 |
""" |
| 3 |
================================= |
| 4 |
Debiased Sinkhorn barycenter demo |
| 5 |
================================= |
| 6 |
|
| 7 |
This example illustrates the computation of the debiased Sinkhorn barycenter |
| 8 |
as proposed in [37]_. |
| 9 |
|
| 10 |
|
| 11 |
.. [37] Janati, H., Cuturi, M., Gramfort, A. Proceedings of the 37th |
| 12 |
International Conference on Machine Learning, PMLR 119:4692-4701, 2020 |
| 13 |
""" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import os |
| 21 |
from pathlib import Path |
| 22 |
|
| 23 |
import numpy as np |
| 24 |
import matplotlib.pyplot as plt |
| 25 |
|
| 26 |
import ot |
| 27 |
from ot.bregman import ( |
| 28 |
barycenter, |
| 29 |
barycenter_debiased, |
| 30 |
convolutional_barycenter2d, |
| 31 |
convolutional_barycenter2d_debiased, |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
n = 100 |
| 41 |
|
| 42 |
|
| 43 |
x = np.arange(n, dtype=np.float64) |
| 44 |
|
| 45 |
|
| 46 |
a1 = ot.datasets.make_1D_gauss(n, m=20, s=5) |
| 47 |
a2 = ot.datasets.make_1D_gauss(n, m=60, s=8) |
| 48 |
|
| 49 |
|
| 50 |
A = np.vstack((a1, a2)).T |
| 51 |
n_distributions = A.shape[1] |
| 52 |
|
| 53 |
|
| 54 |
M = ot.utils.dist0(n) |
| 55 |
M /= M.max() |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
alpha = 0.2 |
| 60 |
weights = np.array([1 - alpha, alpha]) |
| 61 |
|
| 62 |
epsilons = [5e-3, 1e-2, 5e-2] |
| 63 |
|
| 64 |
|
| 65 |
bars = [barycenter(A, M, reg, weights) for reg in epsilons] |
| 66 |
bars_debiased = [barycenter_debiased(A, M, reg, weights) for reg in epsilons] |
| 67 |
labels = ["Sinkhorn barycenter", "Debiased barycenter"] |
| 68 |
colors = ["indianred", "gold"] |
| 69 |
|
| 70 |
f, axes = plt.subplots( |
| 71 |
1, len(epsilons), tight_layout=True, sharey=True, figsize=(12, 4), num=1 |
| 72 |
) |
| 73 |
for ax, eps, bar, bar_debiased in zip(axes, epsilons, bars, bars_debiased): |
| 74 |
ax.plot(A[:, 0], color="k", ls="--", label="Input data", alpha=0.3) |
| 75 |
ax.plot(A[:, 1], color="k", ls="--", alpha=0.3) |
| 76 |
for data, label, color in zip([bar, bar_debiased], labels, colors): |
| 77 |
ax.plot(data, color=color, label=label, lw=2) |
| 78 |
ax.set_title(r"$\varepsilon = %.3f$" % eps) |
| 79 |
plt.legend() |
| 80 |
plt.show() |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
this_file = os.path.realpath("__file__") |
| 87 |
data_path = os.path.join(Path(this_file).parent.parent.parent, "data") |
| 88 |
f1 = 1 - plt.imread(os.path.join(data_path, "heart.png"))[:, :, 2] |
| 89 |
f2 = 1 - plt.imread(os.path.join(data_path, "duck.png"))[:, :, 2] |
| 90 |
|
| 91 |
A = np.asarray([f1, f2]) + 1e-2 |
| 92 |
A /= A.sum(axis=(1, 2))[:, None, None] |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
fig, axes = plt.subplots(1, 2, figsize=(7, 4), num=2) |
| 98 |
for ax, img in zip(axes, A): |
| 99 |
ax.imshow(img, cmap="Greys") |
| 100 |
ax.axis("off") |
| 101 |
fig.tight_layout() |
| 102 |
plt.show() |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
bars_sinkhorn, bars_debiased = [], [] |
| 111 |
epsilons = [5e-3, 7e-3, 1e-2] |
| 112 |
for eps in epsilons: |
| 113 |
bar = convolutional_barycenter2d(A, eps) |
| 114 |
bar_debiased, log = convolutional_barycenter2d_debiased(A, eps, log=True) |
| 115 |
bars_sinkhorn.append(bar) |
| 116 |
bars_debiased.append(bar_debiased) |
| 117 |
|
| 118 |
titles = ["Sinkhorn", "Debiased"] |
| 119 |
all_bars = [bars_sinkhorn, bars_debiased] |
| 120 |
fig, axes = plt.subplots(2, 3, figsize=(8, 6), num=3) |
| 121 |
for jj, (method, ax_row, bars) in enumerate(zip(titles, axes, all_bars)): |
| 122 |
for ii, (ax, img, eps) in enumerate(zip(ax_row, bars, epsilons)): |
| 123 |
ax.imshow(img, cmap="Greys") |
| 124 |
if jj == 0: |
| 125 |
ax.set_title(r"$\varepsilon = %.3f$" % eps, fontsize=13) |
| 126 |
ax.set_xticks([]) |
| 127 |
ax.set_yticks([]) |
| 128 |
ax.spines["top"].set_visible(False) |
| 129 |
ax.spines["right"].set_visible(False) |
| 130 |
ax.spines["bottom"].set_visible(False) |
| 131 |
ax.spines["left"].set_visible(False) |
| 132 |
if ii == 0: |
| 133 |
ax.set_ylabel(method, fontsize=15) |
| 134 |
fig.tight_layout() |
| 135 |
plt.show() |