| 1 |
|
| 2 |
""" |
| 3 |
Bregman projections solvers for entropic regularized OT for empirical distributions |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
import warnings |
| 13 |
|
| 14 |
from ..utils import dist, list_to_array, unif, LazyTensor |
| 15 |
from ..backend import get_backend |
| 16 |
|
| 17 |
from ._sinkhorn import sinkhorn, sinkhorn2 |
| 18 |
|
| 19 |
|
| 20 |
def get_sinkhorn_lazytensor(X_a, X_b, f, g, metric="sqeuclidean", reg=1e-1, nx=None): |
| 21 |
r"""Get a LazyTensor of Sinkhorn solution from the dual potentials |
| 22 |
|
| 23 |
The returned LazyTensor is |
| 24 |
:math:`\mathbf{T} = exp( \mathbf{f} \mathbf{1}_b^\top + \mathbf{1}_a \mathbf{g}^\top - \mathbf{C}/reg)`, where :math:`\mathbf{C}` is the pairwise metric matrix between samples :math:`\mathbf{X}_a` and :math:`\mathbf{X}_b`. |
| 25 |
|
| 26 |
Parameters |
| 27 |
---------- |
| 28 |
X_a : array-like, shape (n_samples_a, dim) |
| 29 |
samples in the source domain |
| 30 |
X_b : array-like, shape (n_samples_b, dim) |
| 31 |
samples in the target domain |
| 32 |
f : array-like, shape (n_samples_a,) |
| 33 |
First dual potentials (log space) |
| 34 |
g : array-like, shape (n_samples_b,) |
| 35 |
Second dual potentials (log space) |
| 36 |
metric : str, default='sqeuclidean' |
| 37 |
Metric used for the cost matrix computation |
| 38 |
reg : float, default=1e-1 |
| 39 |
Regularization term >0 |
| 40 |
nx : Backend(), default=None |
| 41 |
Numerical backend used |
| 42 |
|
| 43 |
|
| 44 |
Returns |
| 45 |
------- |
| 46 |
T : LazyTensor |
| 47 |
Sinkhorn solution tensor |
| 48 |
""" |
| 49 |
|
| 50 |
if nx is None: |
| 51 |
nx = get_backend(X_a, X_b, f, g) |
| 52 |
|
| 53 |
shape = (X_a.shape[0], X_b.shape[0]) |
| 54 |
|
| 55 |
def func(i, j, X_a, X_b, f, g, metric, reg): |
| 56 |
C = dist(X_a[i], X_b[j], metric=metric) |
| 57 |
return nx.exp(f[i, None] + g[None, j] - C / reg) |
| 58 |
|
| 59 |
T = LazyTensor(shape, func, X_a=X_a, X_b=X_b, f=f, g=g, metric=metric, reg=reg) |
| 60 |
|
| 61 |
return T |
| 62 |
|
| 63 |
|
| 64 |
def empirical_sinkhorn( |
| 65 |
X_s, |
| 66 |
X_t, |
| 67 |
reg, |
| 68 |
a=None, |
| 69 |
b=None, |
| 70 |
metric="sqeuclidean", |
| 71 |
numIterMax=10000, |
| 72 |
stopThr=1e-9, |
| 73 |
isLazy=False, |
| 74 |
batchSize=100, |
| 75 |
verbose=False, |
| 76 |
log=False, |
| 77 |
warn=True, |
| 78 |
warmstart=None, |
| 79 |
**kwargs, |
| 80 |
): |
| 81 |
r""" |
| 82 |
Solve the entropic regularization optimal transport problem and return the |
| 83 |
OT matrix from empirical data |
| 84 |
|
| 85 |
The function solves the following optimization problem: |
| 86 |
|
| 87 |
.. math:: |
| 88 |
\gamma = \mathop{\arg \min}_\gamma \quad \langle \gamma, \mathbf{M} \rangle_F + |
| 89 |
\mathrm{reg} \cdot\Omega(\gamma) |
| 90 |
|
| 91 |
s.t. \ \gamma \mathbf{1} &= \mathbf{a} |
| 92 |
|
| 93 |
\gamma^T \mathbf{1} &= \mathbf{b} |
| 94 |
|
| 95 |
\gamma &\geq 0 |
| 96 |
|
| 97 |
where : |
| 98 |
|
| 99 |
- :math:`\mathbf{M}` is the (`n_samples_a`, `n_samples_b`) metric cost matrix |
| 100 |
- :math:`\Omega` is the entropic regularization term |
| 101 |
:math:`\Omega(\gamma)=\sum_{i,j} \gamma_{i,j}\log(\gamma_{i,j})` |
| 102 |
- :math:`\mathbf{a}` and :math:`\mathbf{b}` are source and target weights (sum to 1) |
| 103 |
|
| 104 |
|
| 105 |
Parameters |
| 106 |
---------- |
| 107 |
X_s : array-like, shape (n_samples_a, dim) |
| 108 |
samples in the source domain |
| 109 |
X_t : array-like, shape (n_samples_b, dim) |
| 110 |
samples in the target domain |
| 111 |
reg : float |
| 112 |
Regularization term >0 |
| 113 |
a : array-like, shape (n_samples_a,) |
| 114 |
samples weights in the source domain |
| 115 |
b : array-like, shape (n_samples_b,) |
| 116 |
samples weights in the target domain |
| 117 |
numItermax : int, optional |
| 118 |
Max number of iterations |
| 119 |
stopThr : float, optional |
| 120 |
Stop threshold on error (>0) |
| 121 |
isLazy: boolean, optional |
| 122 |
If True, then only calculate the cost matrix by block and return |
| 123 |
the dual potentials only (to save memory). If False, calculate full |
| 124 |
cost matrix and return outputs of sinkhorn function. |
| 125 |
batchSize: int or tuple of 2 int, optional |
| 126 |
Size of the batches used to compute the sinkhorn update without memory overhead. |
| 127 |
When a tuple is provided it sets the size of the left/right batches. |
| 128 |
verbose : bool, optional |
| 129 |
Print information along iterations |
| 130 |
log : bool, optional |
| 131 |
record log if True |
| 132 |
warn : bool, optional |
| 133 |
if True, raises a warning if the algorithm doesn't convergence. |
| 134 |
warmstart: tuple of arrays, shape (dim_a, dim_b), optional |
| 135 |
Initialization of dual potentials. If provided, the dual potentials should be given |
| 136 |
(that is the logarithm of the u,v sinkhorn scaling vectors) |
| 137 |
|
| 138 |
|
| 139 |
Returns |
| 140 |
------- |
| 141 |
gamma : array-like, shape (n_samples_a, n_samples_b) |
| 142 |
Regularized optimal transportation matrix for the given parameters |
| 143 |
log : dict |
| 144 |
log dictionary return only if log==True in parameters |
| 145 |
|
| 146 |
Examples |
| 147 |
-------- |
| 148 |
|
| 149 |
>>> import numpy as np |
| 150 |
>>> n_samples_a = 2 |
| 151 |
>>> n_samples_b = 2 |
| 152 |
>>> reg = 0.1 |
| 153 |
>>> X_s = np.reshape(np.arange(n_samples_a, dtype=np.float64), (n_samples_a, 1)) |
| 154 |
>>> X_t = np.reshape(np.arange(0, n_samples_b, dtype=np.float64), (n_samples_b, 1)) |
| 155 |
>>> empirical_sinkhorn(X_s, X_t, reg=reg, verbose=False) # doctest: +NORMALIZE_WHITESPACE |
| 156 |
array([[4.99977301e-01, 2.26989344e-05], |
| 157 |
[2.26989344e-05, 4.99977301e-01]]) |
| 158 |
|
| 159 |
|
| 160 |
References |
| 161 |
---------- |
| 162 |
|
| 163 |
.. [2] M. Cuturi, Sinkhorn Distances : Lightspeed Computation of Optimal |
| 164 |
Transport, Advances in Neural Information Processing Systems (NIPS) 26, 2013 |
| 165 |
|
| 166 |
.. [9] Schmitzer, B. (2016). Stabilized Sparse Scaling Algorithms for |
| 167 |
Entropy Regularized Transport Problems. arXiv preprint arXiv:1610.06519. |
| 168 |
|
| 169 |
.. [10] Chizat, L., Peyré, G., Schmitzer, B., & Vialard, F. X. (2016). |
| 170 |
Scaling algorithms for unbalanced transport problems. arXiv preprint arXiv:1607.05816. |
| 171 |
""" |
| 172 |
|
| 173 |
X_s, X_t = list_to_array(X_s, X_t) |
| 174 |
|
| 175 |
nx = get_backend(X_s, X_t) |
| 176 |
|
| 177 |
ns, nt = X_s.shape[0], X_t.shape[0] |
| 178 |
if a is None: |
| 179 |
a = nx.from_numpy(unif(ns), type_as=X_s) |
| 180 |
if b is None: |
| 181 |
b = nx.from_numpy(unif(nt), type_as=X_s) |
| 182 |
|
| 183 |
if isLazy: |
| 184 |
if log: |
| 185 |
dict_log = {"err": []} |
| 186 |
|
| 187 |
log_a, log_b = nx.log(a), nx.log(b) |
| 188 |
if warmstart is None: |
| 189 |
f, g = nx.zeros((ns,), type_as=a), nx.zeros((nt,), type_as=a) |
| 190 |
else: |
| 191 |
f, g = warmstart |
| 192 |
|
| 193 |
if isinstance(batchSize, int): |
| 194 |
bs, bt = batchSize, batchSize |
| 195 |
elif isinstance(batchSize, tuple) and len(batchSize) == 2: |
| 196 |
bs, bt = batchSize[0], batchSize[1] |
| 197 |
else: |
| 198 |
raise ValueError("Batch size must be in integer or a tuple of two integers") |
| 199 |
|
| 200 |
range_s, range_t = range(0, ns, bs), range(0, nt, bt) |
| 201 |
|
| 202 |
lse_f = nx.zeros((ns,), type_as=a) |
| 203 |
lse_g = nx.zeros((nt,), type_as=a) |
| 204 |
|
| 205 |
X_s_np = nx.to_numpy(X_s) |
| 206 |
X_t_np = nx.to_numpy(X_t) |
| 207 |
|
| 208 |
for i_ot in range(numIterMax): |
| 209 |
lse_f_cols = [] |
| 210 |
for i in range_s: |
| 211 |
M = dist(X_s_np[i : i + bs, :], X_t_np, metric=metric) |
| 212 |
M = nx.from_numpy(M, type_as=a) |
| 213 |
lse_f_cols.append(nx.logsumexp(g[None, :] - M / reg, axis=1)) |
| 214 |
lse_f = nx.concatenate(lse_f_cols, axis=0) |
| 215 |
f = log_a - lse_f |
| 216 |
|
| 217 |
lse_g_cols = [] |
| 218 |
for j in range_t: |
| 219 |
M = dist(X_s_np, X_t_np[j : j + bt, :], metric=metric) |
| 220 |
M = nx.from_numpy(M, type_as=a) |
| 221 |
lse_g_cols.append(nx.logsumexp(f[:, None] - M / reg, axis=0)) |
| 222 |
lse_g = nx.concatenate(lse_g_cols, axis=0) |
| 223 |
g = log_b - lse_g |
| 224 |
|
| 225 |
if (i_ot + 1) % 10 == 0: |
| 226 |
m1_cols = [] |
| 227 |
for i in range_s: |
| 228 |
M = dist(X_s_np[i : i + bs, :], X_t_np, metric=metric) |
| 229 |
M = nx.from_numpy(M, type_as=a) |
| 230 |
m1_cols.append( |
| 231 |
nx.sum( |
| 232 |
nx.exp(f[i : i + bs, None] + g[None, :] - M / reg), axis=1 |
| 233 |
) |
| 234 |
) |
| 235 |
m1 = nx.concatenate(m1_cols, axis=0) |
| 236 |
err = nx.sum(nx.abs(m1 - a)) |
| 237 |
if log: |
| 238 |
dict_log["err"].append(err) |
| 239 |
|
| 240 |
if verbose and (i_ot + 1) % 100 == 0: |
| 241 |
print( |
| 242 |
"Error in marginal at iteration {} = {}".format(i_ot + 1, err) |
| 243 |
) |
| 244 |
|
| 245 |
if err <= stopThr: |
| 246 |
break |
| 247 |
else: |
| 248 |
if warn: |
| 249 |
warnings.warn( |
| 250 |
"Sinkhorn did not converge. You might want to " |
| 251 |
"increase the number of iterations `numItermax` " |
| 252 |
"or the regularization parameter `reg`." |
| 253 |
) |
| 254 |
if log: |
| 255 |
dict_log["u"] = f |
| 256 |
dict_log["v"] = g |
| 257 |
dict_log["niter"] = i_ot |
| 258 |
dict_log["lazy_plan"] = get_sinkhorn_lazytensor(X_s, X_t, f, g, metric, reg) |
| 259 |
return (f, g, dict_log) |
| 260 |
else: |
| 261 |
return (f, g) |
| 262 |
|
| 263 |
else: |
| 264 |
M = dist(X_s, X_t, metric=metric) |
| 265 |
if log: |
| 266 |
pi, log = sinkhorn( |
| 267 |
a, |
| 268 |
b, |
| 269 |
M, |
| 270 |
reg, |
| 271 |
numItermax=numIterMax, |
| 272 |
stopThr=stopThr, |
| 273 |
verbose=verbose, |
| 274 |
log=True, |
| 275 |
warmstart=warmstart, |
| 276 |
**kwargs, |
| 277 |
) |
| 278 |
return pi, log |
| 279 |
else: |
| 280 |
pi = sinkhorn( |
| 281 |
a, |
| 282 |
b, |
| 283 |
M, |
| 284 |
reg, |
| 285 |
numItermax=numIterMax, |
| 286 |
stopThr=stopThr, |
| 287 |
verbose=verbose, |
| 288 |
log=False, |
| 289 |
warmstart=warmstart, |
| 290 |
**kwargs, |
| 291 |
) |
| 292 |
return pi |
| 293 |
|
| 294 |
|
| 295 |
def empirical_sinkhorn2( |
| 296 |
X_s, |
| 297 |
X_t, |
| 298 |
reg, |
| 299 |
a=None, |
| 300 |
b=None, |
| 301 |
metric="sqeuclidean", |
| 302 |
numIterMax=10000, |
| 303 |
stopThr=1e-9, |
| 304 |
isLazy=False, |
| 305 |
batchSize=100, |
| 306 |
verbose=False, |
| 307 |
log=False, |
| 308 |
warn=True, |
| 309 |
warmstart=None, |
| 310 |
**kwargs, |
| 311 |
): |
| 312 |
r""" |
| 313 |
Solve the entropic regularization optimal transport problem from empirical |
| 314 |
data and return the OT loss |
| 315 |
|
| 316 |
|
| 317 |
The function solves the following optimization problem: |
| 318 |
|
| 319 |
.. math:: |
| 320 |
W = \min_\gamma \quad \langle \gamma, \mathbf{M} \rangle_F + |
| 321 |
\mathrm{reg} \cdot\Omega(\gamma) |
| 322 |
|
| 323 |
s.t. \ \gamma \mathbf{1} &= \mathbf{a} |
| 324 |
|
| 325 |
\gamma^T \mathbf{1} &= \mathbf{b} |
| 326 |
|
| 327 |
\gamma &\geq 0 |
| 328 |
|
| 329 |
where : |
| 330 |
|
| 331 |
- :math:`\mathbf{M}` is the (`n_samples_a`, `n_samples_b`) metric cost matrix |
| 332 |
- :math:`\Omega` is the entropic regularization term |
| 333 |
:math:`\Omega(\gamma)=\sum_{i,j} \gamma_{i,j}\log(\gamma_{i,j})` |
| 334 |
- :math:`\mathbf{a}` and :math:`\mathbf{b}` are source and target weights (sum to 1) |
| 335 |
|
| 336 |
and returns :math:`\langle \gamma^*, \mathbf{M} \rangle_F` (without |
| 337 |
the entropic contribution). |
| 338 |
|
| 339 |
|
| 340 |
Parameters |
| 341 |
---------- |
| 342 |
X_s : array-like, shape (n_samples_a, dim) |
| 343 |
samples in the source domain |
| 344 |
X_t : array-like, shape (n_samples_b, dim) |
| 345 |
samples in the target domain |
| 346 |
reg : float |
| 347 |
Regularization term >0 |
| 348 |
a : array-like, shape (n_samples_a,) |
| 349 |
samples weights in the source domain |
| 350 |
b : array-like, shape (n_samples_b,) |
| 351 |
samples weights in the target domain |
| 352 |
numItermax : int, optional |
| 353 |
Max number of iterations |
| 354 |
stopThr : float, optional |
| 355 |
Stop threshold on error (>0) |
| 356 |
isLazy: boolean, optional |
| 357 |
If True, then only calculate the cost matrix by block and return |
| 358 |
the dual potentials only (to save memory). If False, calculate |
| 359 |
full cost matrix and return outputs of sinkhorn function. |
| 360 |
batchSize: int or tuple of 2 int, optional |
| 361 |
Size of the batches used to compute the sinkhorn update without memory overhead. |
| 362 |
When a tuple is provided it sets the size of the left/right batches. |
| 363 |
verbose : bool, optional |
| 364 |
Print information along iterations |
| 365 |
log : bool, optional |
| 366 |
record log if True |
| 367 |
warn : bool, optional |
| 368 |
if True, raises a warning if the algorithm doesn't convergence. |
| 369 |
warmstart: tuple of arrays, shape (dim_a, dim_b), optional |
| 370 |
Initialization of dual potentials. If provided, the dual potentials should be given |
| 371 |
(that is the logarithm of the u,v sinkhorn scaling vectors) |
| 372 |
|
| 373 |
Returns |
| 374 |
------- |
| 375 |
W : (n_hists) array-like or float |
| 376 |
Optimal transportation loss for the given parameters |
| 377 |
log : dict |
| 378 |
log dictionary return only if log==True in parameters |
| 379 |
|
| 380 |
Examples |
| 381 |
-------- |
| 382 |
|
| 383 |
>>> import numpy as np |
| 384 |
>>> n_samples_a = 2 |
| 385 |
>>> n_samples_b = 2 |
| 386 |
>>> reg = 0.1 |
| 387 |
>>> X_s = np.reshape(np.arange(n_samples_a, dtype=np.float64), (n_samples_a, 1)) |
| 388 |
>>> X_t = np.reshape(np.arange(0, n_samples_b, dtype=np.float64), (n_samples_b, 1)) |
| 389 |
>>> b = np.full((n_samples_b, 3), 1/n_samples_b) |
| 390 |
>>> empirical_sinkhorn2(X_s, X_t, b=b, reg=reg, verbose=False) |
| 391 |
array([4.53978687e-05, 4.53978687e-05, 4.53978687e-05]) |
| 392 |
|
| 393 |
|
| 394 |
References |
| 395 |
---------- |
| 396 |
|
| 397 |
.. [2] M. Cuturi, Sinkhorn Distances : Lightspeed Computation |
| 398 |
of Optimal Transport, Advances in Neural Information |
| 399 |
Processing Systems (NIPS) 26, 2013 |
| 400 |
|
| 401 |
.. [9] Schmitzer, B. (2016). Stabilized Sparse Scaling |
| 402 |
Algorithms for Entropy Regularized Transport Problems. |
| 403 |
arXiv preprint arXiv:1610.06519. |
| 404 |
|
| 405 |
.. [10] Chizat, L., Peyré, G., Schmitzer, B., & Vialard, F. X. (2016). |
| 406 |
Scaling algorithms for unbalanced transport problems. |
| 407 |
arXiv preprint arXiv:1607.05816. |
| 408 |
""" |
| 409 |
|
| 410 |
X_s, X_t = list_to_array(X_s, X_t) |
| 411 |
|
| 412 |
nx = get_backend(X_s, X_t) |
| 413 |
|
| 414 |
ns, nt = X_s.shape[0], X_t.shape[0] |
| 415 |
if a is None: |
| 416 |
a = nx.from_numpy(unif(ns), type_as=X_s) |
| 417 |
if b is None: |
| 418 |
b = nx.from_numpy(unif(nt), type_as=X_s) |
| 419 |
|
| 420 |
if isLazy: |
| 421 |
if log: |
| 422 |
f, g, dict_log = empirical_sinkhorn( |
| 423 |
X_s, |
| 424 |
X_t, |
| 425 |
reg, |
| 426 |
a, |
| 427 |
b, |
| 428 |
metric, |
| 429 |
numIterMax=numIterMax, |
| 430 |
stopThr=stopThr, |
| 431 |
isLazy=isLazy, |
| 432 |
batchSize=batchSize, |
| 433 |
verbose=verbose, |
| 434 |
log=log, |
| 435 |
warn=warn, |
| 436 |
warmstart=warmstart, |
| 437 |
) |
| 438 |
else: |
| 439 |
f, g = empirical_sinkhorn( |
| 440 |
X_s, |
| 441 |
X_t, |
| 442 |
reg, |
| 443 |
a, |
| 444 |
b, |
| 445 |
metric, |
| 446 |
numIterMax=numIterMax, |
| 447 |
stopThr=stopThr, |
| 448 |
isLazy=isLazy, |
| 449 |
batchSize=batchSize, |
| 450 |
verbose=verbose, |
| 451 |
log=log, |
| 452 |
warn=warn, |
| 453 |
warmstart=warmstart, |
| 454 |
) |
| 455 |
|
| 456 |
bs = batchSize if isinstance(batchSize, int) else batchSize[0] |
| 457 |
range_s = range(0, ns, bs) |
| 458 |
|
| 459 |
loss = 0 |
| 460 |
|
| 461 |
X_s_np = nx.to_numpy(X_s) |
| 462 |
X_t_np = nx.to_numpy(X_t) |
| 463 |
|
| 464 |
for i in range_s: |
| 465 |
M_block = dist(X_s_np[i : i + bs, :], X_t_np, metric=metric) |
| 466 |
M_block = nx.from_numpy(M_block, type_as=a) |
| 467 |
pi_block = nx.exp(f[i : i + bs, None] + g[None, :] - M_block / reg) |
| 468 |
loss += nx.sum(M_block * pi_block) |
| 469 |
|
| 470 |
if log: |
| 471 |
return loss, dict_log |
| 472 |
else: |
| 473 |
return loss |
| 474 |
|
| 475 |
else: |
| 476 |
M = dist(X_s, X_t, metric=metric) |
| 477 |
|
| 478 |
if log: |
| 479 |
sinkhorn_loss, log = sinkhorn2( |
| 480 |
a, |
| 481 |
b, |
| 482 |
M, |
| 483 |
reg, |
| 484 |
numItermax=numIterMax, |
| 485 |
stopThr=stopThr, |
| 486 |
verbose=verbose, |
| 487 |
log=log, |
| 488 |
warn=warn, |
| 489 |
warmstart=warmstart, |
| 490 |
**kwargs, |
| 491 |
) |
| 492 |
return sinkhorn_loss, log |
| 493 |
else: |
| 494 |
sinkhorn_loss = sinkhorn2( |
| 495 |
a, |
| 496 |
b, |
| 497 |
M, |
| 498 |
reg, |
| 499 |
numItermax=numIterMax, |
| 500 |
stopThr=stopThr, |
| 501 |
verbose=verbose, |
| 502 |
log=log, |
| 503 |
warn=warn, |
| 504 |
warmstart=warmstart, |
| 505 |
**kwargs, |
| 506 |
) |
| 507 |
return sinkhorn_loss |
| 508 |
|
| 509 |
|
| 510 |
def empirical_sinkhorn_divergence( |
| 511 |
X_s, |
| 512 |
X_t, |
| 513 |
reg, |
| 514 |
a=None, |
| 515 |
b=None, |
| 516 |
metric="sqeuclidean", |
| 517 |
numIterMax=10000, |
| 518 |
stopThr=1e-9, |
| 519 |
verbose=False, |
| 520 |
log=False, |
| 521 |
warn=True, |
| 522 |
warmstart=None, |
| 523 |
**kwargs, |
| 524 |
): |
| 525 |
r""" |
| 526 |
Compute the sinkhorn divergence loss from empirical data |
| 527 |
|
| 528 |
The function solves the following optimization problems and return the |
| 529 |
sinkhorn divergence :math:`S`: |
| 530 |
|
| 531 |
.. math:: |
| 532 |
W &= \min_\gamma \quad \langle \gamma, \mathbf{M} \rangle_F + |
| 533 |
\mathrm{reg} \cdot\Omega(\gamma) |
| 534 |
|
| 535 |
W_a &= \min_{\gamma_a} \quad \langle \gamma_a, \mathbf{M_a} \rangle_F + |
| 536 |
\mathrm{reg} \cdot\Omega(\gamma_a) |
| 537 |
|
| 538 |
W_b &= \min_{\gamma_b} \quad \langle \gamma_b, \mathbf{M_b} \rangle_F + |
| 539 |
\mathrm{reg} \cdot\Omega(\gamma_b) |
| 540 |
|
| 541 |
S &= W - \frac{W_a + W_b}{2} |
| 542 |
|
| 543 |
.. math:: |
| 544 |
s.t. \ \gamma \mathbf{1} &= \mathbf{a} |
| 545 |
|
| 546 |
\gamma^T \mathbf{1} &= \mathbf{b} |
| 547 |
|
| 548 |
\gamma &\geq 0 |
| 549 |
|
| 550 |
\gamma_a \mathbf{1} &= \mathbf{a} |
| 551 |
|
| 552 |
\gamma_a^T \mathbf{1} &= \mathbf{a} |
| 553 |
|
| 554 |
\gamma_a &\geq 0 |
| 555 |
|
| 556 |
\gamma_b \mathbf{1} &= \mathbf{b} |
| 557 |
|
| 558 |
\gamma_b^T \mathbf{1} &= \mathbf{b} |
| 559 |
|
| 560 |
\gamma_b &\geq 0 |
| 561 |
|
| 562 |
where : |
| 563 |
|
| 564 |
- :math:`\mathbf{M}` (resp. :math:`\mathbf{M_a}`, :math:`\mathbf{M_b}`) |
| 565 |
is the (`n_samples_a`, `n_samples_b`) metric cost matrix |
| 566 |
(resp (`n_samples_a, n_samples_a`) and (`n_samples_b`, `n_samples_b`)) |
| 567 |
- :math:`\Omega` is the entropic regularization term |
| 568 |
:math:`\Omega(\gamma)=\sum_{i,j} \gamma_{i,j}\log(\gamma_{i,j})` |
| 569 |
- :math:`\mathbf{a}` and :math:`\mathbf{b}` are source and target weights (sum to 1) |
| 570 |
|
| 571 |
and returns :math:`\langle \gamma^*, \mathbf{M} \rangle_F -(\langle \gamma^*_a, \mathbf{M_a} \rangle_F + \langle |
| 572 |
\gamma^*_b , \mathbf{M_b} \rangle_F)/2`. |
| 573 |
|
| 574 |
.. note: The current implementation does not account for the entropic contributions and thus differs from the |
| 575 |
Sinkhorn divergence as introduced in the literature. The possibility to account for the entropic contributions |
| 576 |
will be provided in a future release. |
| 577 |
|
| 578 |
|
| 579 |
Parameters |
| 580 |
---------- |
| 581 |
X_s : array-like, shape (n_samples_a, dim) |
| 582 |
samples in the source domain |
| 583 |
X_t : array-like, shape (n_samples_b, dim) |
| 584 |
samples in the target domain |
| 585 |
reg : float |
| 586 |
Regularization term >0 |
| 587 |
a : array-like, shape (n_samples_a,) |
| 588 |
samples weights in the source domain |
| 589 |
b : array-like, shape (n_samples_b,) |
| 590 |
samples weights in the target domain |
| 591 |
numItermax : int, optional |
| 592 |
Max number of iterations |
| 593 |
stopThr : float, optional |
| 594 |
Stop threshold on error (>0) |
| 595 |
verbose : bool, optional |
| 596 |
Print information along iterations |
| 597 |
log : bool, optional |
| 598 |
record log if True |
| 599 |
warn : bool, optional |
| 600 |
if True, raises a warning if the algorithm doesn't convergence. |
| 601 |
warmstart: tuple of arrays, shape (dim_a, dim_b), optional |
| 602 |
Initialization of dual potentials. If provided, the dual potentials should be given |
| 603 |
(that is the logarithm of the u,v sinkhorn scaling vectors) |
| 604 |
|
| 605 |
Returns |
| 606 |
------- |
| 607 |
W : (1,) array-like |
| 608 |
Optimal transportation symmetrized loss for the given parameters |
| 609 |
log : dict |
| 610 |
log dictionary return only if log==True in parameters |
| 611 |
|
| 612 |
Examples |
| 613 |
-------- |
| 614 |
|
| 615 |
>>> import numpy as np |
| 616 |
>>> n_samples_a = 2 |
| 617 |
>>> n_samples_b = 4 |
| 618 |
>>> reg = 0.1 |
| 619 |
>>> X_s = np.reshape(np.arange(n_samples_a, dtype=np.float64), (n_samples_a, 1)) |
| 620 |
>>> X_t = np.reshape(np.arange(0, n_samples_b, dtype=np.float64), (n_samples_b, 1)) |
| 621 |
>>> empirical_sinkhorn_divergence(X_s, X_t, reg) # doctest: +ELLIPSIS |
| 622 |
1.499887176049052 |
| 623 |
|
| 624 |
|
| 625 |
References |
| 626 |
---------- |
| 627 |
.. [23] Aude Genevay, Gabriel Peyré, Marco Cuturi, Learning Generative |
| 628 |
Models with Sinkhorn Divergences, Proceedings of the Twenty-First |
| 629 |
International Conference on Artificial Intelligence and Statistics, |
| 630 |
(AISTATS) 21, 2018 |
| 631 |
""" |
| 632 |
X_s, X_t = list_to_array(X_s, X_t) |
| 633 |
|
| 634 |
nx = get_backend(X_s, X_t) |
| 635 |
if warmstart is None: |
| 636 |
warmstart_a, warmstart_b = None, None |
| 637 |
else: |
| 638 |
u, v = warmstart |
| 639 |
warmstart_a = (u, u) |
| 640 |
warmstart_b = (v, v) |
| 641 |
|
| 642 |
if log: |
| 643 |
sinkhorn_loss_ab, log_ab = empirical_sinkhorn2( |
| 644 |
X_s, |
| 645 |
X_t, |
| 646 |
reg, |
| 647 |
a, |
| 648 |
b, |
| 649 |
metric=metric, |
| 650 |
numIterMax=numIterMax, |
| 651 |
stopThr=stopThr, |
| 652 |
verbose=verbose, |
| 653 |
log=log, |
| 654 |
warn=warn, |
| 655 |
warmstart=warmstart, |
| 656 |
**kwargs, |
| 657 |
) |
| 658 |
|
| 659 |
sinkhorn_loss_a, log_a = empirical_sinkhorn2( |
| 660 |
X_s, |
| 661 |
X_s, |
| 662 |
reg, |
| 663 |
a, |
| 664 |
a, |
| 665 |
metric=metric, |
| 666 |
numIterMax=numIterMax, |
| 667 |
stopThr=stopThr, |
| 668 |
verbose=verbose, |
| 669 |
log=log, |
| 670 |
warn=warn, |
| 671 |
warmstart=warmstart_a, |
| 672 |
**kwargs, |
| 673 |
) |
| 674 |
|
| 675 |
sinkhorn_loss_b, log_b = empirical_sinkhorn2( |
| 676 |
X_t, |
| 677 |
X_t, |
| 678 |
reg, |
| 679 |
b, |
| 680 |
b, |
| 681 |
metric=metric, |
| 682 |
numIterMax=numIterMax, |
| 683 |
stopThr=stopThr, |
| 684 |
verbose=verbose, |
| 685 |
log=log, |
| 686 |
warn=warn, |
| 687 |
warmstart=warmstart_b, |
| 688 |
**kwargs, |
| 689 |
) |
| 690 |
|
| 691 |
sinkhorn_div = sinkhorn_loss_ab - 0.5 * (sinkhorn_loss_a + sinkhorn_loss_b) |
| 692 |
|
| 693 |
log = {} |
| 694 |
log["sinkhorn_loss_ab"] = sinkhorn_loss_ab |
| 695 |
log["sinkhorn_loss_a"] = sinkhorn_loss_a |
| 696 |
log["sinkhorn_loss_b"] = sinkhorn_loss_b |
| 697 |
log["log_sinkhorn_ab"] = log_ab |
| 698 |
log["log_sinkhorn_a"] = log_a |
| 699 |
log["log_sinkhorn_b"] = log_b |
| 700 |
|
| 701 |
return nx.maximum(0, sinkhorn_div), log |
| 702 |
|
| 703 |
else: |
| 704 |
sinkhorn_loss_ab = empirical_sinkhorn2( |
| 705 |
X_s, |
| 706 |
X_t, |
| 707 |
reg, |
| 708 |
a, |
| 709 |
b, |
| 710 |
metric=metric, |
| 711 |
numIterMax=numIterMax, |
| 712 |
stopThr=stopThr, |
| 713 |
verbose=verbose, |
| 714 |
log=log, |
| 715 |
warn=warn, |
| 716 |
warmstart=warmstart, |
| 717 |
**kwargs, |
| 718 |
) |
| 719 |
|
| 720 |
sinkhorn_loss_a = empirical_sinkhorn2( |
| 721 |
X_s, |
| 722 |
X_s, |
| 723 |
reg, |
| 724 |
a, |
| 725 |
a, |
| 726 |
metric=metric, |
| 727 |
numIterMax=numIterMax, |
| 728 |
stopThr=stopThr, |
| 729 |
verbose=verbose, |
| 730 |
log=log, |
| 731 |
warn=warn, |
| 732 |
warmstart=warmstart_a, |
| 733 |
**kwargs, |
| 734 |
) |
| 735 |
|
| 736 |
sinkhorn_loss_b = empirical_sinkhorn2( |
| 737 |
X_t, |
| 738 |
X_t, |
| 739 |
reg, |
| 740 |
b, |
| 741 |
b, |
| 742 |
metric=metric, |
| 743 |
numIterMax=numIterMax, |
| 744 |
stopThr=stopThr, |
| 745 |
verbose=verbose, |
| 746 |
log=log, |
| 747 |
warn=warn, |
| 748 |
warmstart=warmstart_b, |
| 749 |
**kwargs, |
| 750 |
) |
| 751 |
|
| 752 |
sinkhorn_div = sinkhorn_loss_ab - 0.5 * (sinkhorn_loss_a + sinkhorn_loss_b) |
| 753 |
return nx.maximum(0, sinkhorn_div) |