|
9 | 9 | """ |
10 | 10 |
|
11 | 11 | # Author: Hicham Janati <hicham.janati@inria.fr> |
| 12 | +# Clément Bonet <clement.bonet.mapp@polytechnique.edu> |
12 | 13 | # |
13 | 14 | # License: MIT License |
14 | 15 |
|
|
19 | 20 | import ot |
20 | 21 | import ot.plot |
21 | 22 | from ot.datasets import make_1D_gauss as gauss |
| 23 | +import torch |
22 | 24 |
|
23 | 25 | ############################################################################## |
24 | 26 | # Generate data |
|
41 | 43 |
|
42 | 44 | # loss matrix |
43 | 45 | M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) |
44 | | -M /= M.max() |
45 | 46 |
|
46 | 47 |
|
47 | 48 | ############################################################################## |
|
62 | 63 |
|
63 | 64 |
|
64 | 65 | ############################################################################## |
65 | | -# Solve Unbalanced Sinkhorn |
66 | | -# ------------------------- |
| 66 | +# Solve Unbalanced OT with MM Unbalanced |
| 67 | +# ----------------------------------- |
67 | 68 |
|
68 | | -# Sinkhorn |
| 69 | +# %% MM Unbalanced |
69 | 70 |
|
70 | | -epsilon = 0.1 # entropy parameter |
71 | 71 | alpha = 1.0 # Unbalanced KL relaxation parameter |
72 | | -Gs = ot.unbalanced.sinkhorn_unbalanced(a, b, M, epsilon, alpha, verbose=True) |
| 72 | + |
| 73 | +Gs, log = ot.unbalanced.mm_unbalanced(a, b, M / M.max(), alpha, verbose=False, log=True) |
73 | 74 |
|
74 | 75 | pl.figure(3, figsize=(5, 5)) |
75 | | -ot.plot.plot1D_mat(a, b, Gs, "UOT matrix Sinkhorn") |
| 76 | +ot.plot.plot1D_mat(a, b, Gs, "UOT plan") |
| 77 | +pl.show() |
76 | 78 |
|
| 79 | +pl.figure(4, figsize=(6.4, 3)) |
| 80 | +pl.plot(x, a, "b", label="Source distribution") |
| 81 | +pl.plot(x, b, "r", label="Target distribution") |
| 82 | +pl.fill(x, Gs.sum(1), "b", alpha=0.5, label="Transported source") |
| 83 | +pl.fill(x, Gs.sum(0), "r", alpha=0.5, label="Transported target") |
| 84 | +pl.legend(loc="upper right") |
| 85 | +pl.title("Distributions and transported mass for UOT") |
77 | 86 | pl.show() |
78 | 87 |
|
| 88 | +print("Mass of reweighted marginals:", Gs.sum()) |
| 89 | +print("Unbalanced OT loss:", log["total_cost"] * M.max()) |
| 90 | + |
| 91 | + |
| 92 | +############################################################################## |
| 93 | +# Solve 1D UOT with Frank-Wolfe |
| 94 | +# ----------------------------- |
| 95 | + |
79 | 96 |
|
80 | | -# %% |
81 | | -# plot the transported mass |
| 97 | +# %% 1D UOT with FW |
| 98 | + |
| 99 | + |
| 100 | +alpha = M.max() # Unbalanced KL relaxation parameter |
| 101 | + |
| 102 | +a_reweighted, b_reweighted, loss = ot.unbalanced.uot_1d( |
| 103 | + torch.tensor(x, dtype=torch.float64), |
| 104 | + torch.tensor(x, dtype=torch.float64), |
| 105 | + alpha, |
| 106 | + u_weights=torch.tensor(a, dtype=torch.float64), |
| 107 | + v_weights=torch.tensor(b, dtype=torch.float64), |
| 108 | + p=2, |
| 109 | + returnCost="total", |
| 110 | +) |
| 111 | + |
| 112 | +pl.figure(4, figsize=(6.4, 3)) |
| 113 | +pl.plot(x, a, "b", label="Source distribution") |
| 114 | +pl.plot(x, b, "r", label="Target distribution") |
| 115 | +pl.fill(x, a_reweighted, "b", alpha=0.5, label="Transported source") |
| 116 | +pl.fill(x, b_reweighted, "r", alpha=0.5, label="Transported target") |
| 117 | +pl.legend(loc="upper right") |
| 118 | +pl.title("Distributions and transported mass for UOT") |
| 119 | +pl.show() |
| 120 | + |
| 121 | +print("Mass of reweighted marginals:", a_reweighted.sum().item()) |
| 122 | +print("Unbalanced OT loss:", loss.item()) |
| 123 | + |
| 124 | + |
| 125 | +############################################################################## |
| 126 | +# Solve Unbalanced Sinkhorn |
82 | 127 | # ------------------------- |
83 | 128 |
|
| 129 | +# %% Sinkhorn UOT |
| 130 | + |
| 131 | +# Sinkhorn |
| 132 | + |
| 133 | +epsilon = 0.1 # entropy parameter |
| 134 | +alpha = 1.0 # Unbalanced KL relaxation parameter |
| 135 | +Gs = ot.unbalanced.sinkhorn_unbalanced(a, b, M / M.max(), epsilon, alpha, verbose=True) |
| 136 | + |
| 137 | +pl.figure(3, figsize=(5, 5)) |
| 138 | +ot.plot.plot1D_mat(a, b, Gs, "Entropic UOT plan") |
| 139 | +pl.show() |
| 140 | + |
84 | 141 | pl.figure(4, figsize=(6.4, 3)) |
85 | 142 | pl.plot(x, a, "b", label="Source distribution") |
86 | 143 | pl.plot(x, b, "r", label="Target distribution") |
87 | 144 | pl.fill(x, Gs.sum(1), "b", alpha=0.5, label="Transported source") |
88 | 145 | pl.fill(x, Gs.sum(0), "r", alpha=0.5, label="Transported target") |
89 | 146 | pl.legend(loc="upper right") |
90 | 147 | pl.title("Distributions and transported mass for UOT") |
| 148 | +pl.show() |
| 149 | + |
| 150 | +print("Mass of reweighted marginals:", Gs.sum()) |
0 commit comments