| 1 |
|
| 2 |
""" |
| 3 |
Screening Sinkhorn Algorithms for Regularized Optimal Transport |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
import warnings |
| 12 |
|
| 13 |
import numpy as np |
| 14 |
from scipy.optimize import fmin_l_bfgs_b |
| 15 |
|
| 16 |
from ..utils import list_to_array |
| 17 |
from ..backend import get_backend |
| 18 |
|
| 19 |
|
| 20 |
def screenkhorn( |
| 21 |
a, |
| 22 |
b, |
| 23 |
M, |
| 24 |
reg, |
| 25 |
ns_budget=None, |
| 26 |
nt_budget=None, |
| 27 |
uniform=False, |
| 28 |
restricted=True, |
| 29 |
maxiter=10000, |
| 30 |
maxfun=10000, |
| 31 |
pgtol=1e-09, |
| 32 |
verbose=False, |
| 33 |
log=False, |
| 34 |
): |
| 35 |
r""" |
| 36 |
Screening Sinkhorn Algorithm for Regularized Optimal Transport |
| 37 |
|
| 38 |
The function solves an approximate dual of Sinkhorn divergence :ref:`[2] |
| 39 |
<references-screenkhorn>` which is written as the following optimization problem: |
| 40 |
|
| 41 |
.. math:: |
| 42 |
|
| 43 |
(\mathbf{u}, \mathbf{v}) = \mathop{\arg \min}_{\mathbf{u}, \mathbf{v}} \quad |
| 44 |
\mathbf{1}_{ns}^T \mathbf{B}(\mathbf{u}, \mathbf{v}) \mathbf{1}_{nt} - |
| 45 |
\langle \kappa \mathbf{u}, \mathbf{a} \rangle - |
| 46 |
\langle \frac{1}{\kappa} \mathbf{v}, \mathbf{b} \rangle |
| 47 |
|
| 48 |
where: |
| 49 |
|
| 50 |
.. math:: |
| 51 |
|
| 52 |
\mathbf{B}(\mathbf{u}, \mathbf{v}) = \mathrm{diag}(e^\mathbf{u}) \mathbf{K} \mathrm{diag}(e^\mathbf{v}) \text{, with } \mathbf{K} = e^{-\mathbf{M} / \mathrm{reg}} \text{ and} |
| 53 |
|
| 54 |
.. math:: |
| 55 |
|
| 56 |
s.t. \ e^{u_i} &\geq \epsilon / \kappa, \forall i \in \{1, \ldots, ns\} |
| 57 |
|
| 58 |
e^{v_j} &\geq \epsilon \kappa, \forall j \in \{1, \ldots, nt\} |
| 59 |
|
| 60 |
The parameters `kappa` and `epsilon` are determined w.r.t the couple number |
| 61 |
budget of points (`ns_budget`, `nt_budget`), see Equation (5) |
| 62 |
in :ref:`[26] <references-screenkhorn>` |
| 63 |
|
| 64 |
|
| 65 |
Parameters |
| 66 |
---------- |
| 67 |
a: array-like, shape=(ns,) |
| 68 |
samples weights in the source domain |
| 69 |
b: array-like, shape=(nt,) |
| 70 |
samples weights in the target domain |
| 71 |
M: array-like, shape=(ns, nt) |
| 72 |
Cost matrix |
| 73 |
reg: `float` |
| 74 |
Level of the entropy regularisation |
| 75 |
ns_budget: `int`, default=None |
| 76 |
Number budget of points to be kept in the source domain. |
| 77 |
If it is None then 50% of the source sample points will be kept |
| 78 |
nt_budget: `int`, default=None |
| 79 |
Number budget of points to be kept in the target domain. |
| 80 |
If it is None then 50% of the target sample points will be kept |
| 81 |
uniform: `bool`, default=False |
| 82 |
If `True`, the source and target distribution are supposed to be uniform, |
| 83 |
i.e., :math:`a_i = 1 / ns` and :math:`b_j = 1 / nt` |
| 84 |
restricted : `bool`, default=True |
| 85 |
If `True`, a warm-start initialization for the L-BFGS-B solver |
| 86 |
using a restricted Sinkhorn algorithm with at most 5 iterations |
| 87 |
maxiter: `int`, default=10000 |
| 88 |
Maximum number of iterations in LBFGS solver |
| 89 |
maxfun: `int`, default=10000 |
| 90 |
Maximum number of function evaluations in LBFGS solver |
| 91 |
pgtol: `float`, default=1e-09 |
| 92 |
Final objective function accuracy in LBFGS solver |
| 93 |
verbose: `bool`, default=False |
| 94 |
If `True`, display information about the cardinals of the active sets |
| 95 |
and the parameters kappa and epsilon |
| 96 |
|
| 97 |
|
| 98 |
.. admonition:: Dependency |
| 99 |
|
| 100 |
To gain more efficiency, :py:func:`ot.bregman.screenkhorn` needs to call the "Bottleneck" |
| 101 |
package (https://pypi.org/project/Bottleneck/) in the screening pre-processing step. |
| 102 |
|
| 103 |
If Bottleneck isn't installed, the following error message appears: |
| 104 |
|
| 105 |
"Bottleneck module doesn't exist. Install it from https://pypi.org/project/Bottleneck/" |
| 106 |
|
| 107 |
|
| 108 |
Returns |
| 109 |
------- |
| 110 |
gamma : array-like, shape=(ns, nt) |
| 111 |
Screened optimal transportation matrix for the given parameters |
| 112 |
|
| 113 |
log : `dict`, default=False |
| 114 |
Log dictionary return only if log==True in parameters |
| 115 |
|
| 116 |
|
| 117 |
.. _references-screenkhorn: |
| 118 |
References |
| 119 |
----------- |
| 120 |
|
| 121 |
.. [2] M. Cuturi, Sinkhorn Distances : Lightspeed Computation of Optimal Transport, |
| 122 |
Advances in Neural Information Processing Systems (NIPS) 26, 2013 |
| 123 |
|
| 124 |
.. [26] Alaya M. Z., Bérar M., Gasso G., Rakotomamonjy A. (2019). |
| 125 |
Screening Sinkhorn Algorithm for Regularized Optimal Transport (NIPS) 33, 2019 |
| 126 |
|
| 127 |
""" |
| 128 |
|
| 129 |
try: |
| 130 |
import bottleneck |
| 131 |
except ImportError: |
| 132 |
warnings.warn( |
| 133 |
"Bottleneck module is not installed. Install it from" |
| 134 |
" https://pypi.org/project/Bottleneck/ for better performance." |
| 135 |
) |
| 136 |
bottleneck = np |
| 137 |
|
| 138 |
a, b, M = list_to_array(a, b, M) |
| 139 |
|
| 140 |
nx = get_backend(M, a, b) |
| 141 |
if nx.__name__ in ("jax", "tf"): |
| 142 |
raise TypeError( |
| 143 |
"JAX or TF arrays have been received but screenkhorn is not " |
| 144 |
"compatible with neither JAX nor TF." |
| 145 |
) |
| 146 |
|
| 147 |
ns, nt = M.shape |
| 148 |
|
| 149 |
|
| 150 |
if ns_budget is None: |
| 151 |
ns_budget = int(np.floor(0.5 * ns)) |
| 152 |
if nt_budget is None: |
| 153 |
nt_budget = int(np.floor(0.5 * nt)) |
| 154 |
|
| 155 |
|
| 156 |
K = nx.exp(-M / reg) |
| 157 |
|
| 158 |
def projection(u, epsilon): |
| 159 |
u = nx.maximum(u, epsilon) |
| 160 |
return u |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
if ns_budget == ns and nt_budget == nt: |
| 167 |
|
| 168 |
Isel = nx.from_numpy(np.ones(ns, dtype=bool)) |
| 169 |
Jsel = nx.from_numpy(np.ones(nt, dtype=bool)) |
| 170 |
epsilon = 0.0 |
| 171 |
kappa = 1.0 |
| 172 |
|
| 173 |
cst_u = 0.0 |
| 174 |
cst_v = 0.0 |
| 175 |
|
| 176 |
bounds_u = [(0.0, np.inf)] * ns |
| 177 |
bounds_v = [(0.0, np.inf)] * nt |
| 178 |
|
| 179 |
a_I = a |
| 180 |
b_J = b |
| 181 |
K_IJ = K |
| 182 |
K_IJc = [] |
| 183 |
K_IcJ = [] |
| 184 |
|
| 185 |
vec_eps_IJc = nx.zeros((nt,), type_as=M) |
| 186 |
vec_eps_IcJ = nx.zeros((ns,), type_as=M) |
| 187 |
|
| 188 |
else: |
| 189 |
|
| 190 |
K_sum_cols = nx.sum(K, axis=1) |
| 191 |
K_sum_rows = nx.sum(K, axis=0) |
| 192 |
|
| 193 |
if uniform: |
| 194 |
if ns / ns_budget < 4: |
| 195 |
aK_sort = nx.sort(K_sum_cols) |
| 196 |
epsilon_u_square = a[0] / aK_sort[ns_budget - 1] |
| 197 |
else: |
| 198 |
aK_sort = nx.from_numpy( |
| 199 |
bottleneck.partition(nx.to_numpy(K_sum_cols), ns_budget - 1)[ |
| 200 |
ns_budget - 1 |
| 201 |
], |
| 202 |
type_as=M, |
| 203 |
) |
| 204 |
epsilon_u_square = a[0] / aK_sort |
| 205 |
|
| 206 |
if nt / nt_budget < 4: |
| 207 |
bK_sort = nx.sort(K_sum_rows) |
| 208 |
epsilon_v_square = b[0] / bK_sort[nt_budget - 1] |
| 209 |
else: |
| 210 |
bK_sort = nx.from_numpy( |
| 211 |
bottleneck.partition(nx.to_numpy(K_sum_rows), nt_budget - 1)[ |
| 212 |
nt_budget - 1 |
| 213 |
], |
| 214 |
type_as=M, |
| 215 |
) |
| 216 |
epsilon_v_square = b[0] / bK_sort |
| 217 |
else: |
| 218 |
aK = a / K_sum_cols |
| 219 |
bK = b / K_sum_rows |
| 220 |
|
| 221 |
aK_sort = nx.flip(nx.sort(aK), axis=0) |
| 222 |
epsilon_u_square = aK_sort[ns_budget - 1] |
| 223 |
|
| 224 |
bK_sort = nx.flip(nx.sort(bK), axis=0) |
| 225 |
epsilon_v_square = bK_sort[nt_budget - 1] |
| 226 |
|
| 227 |
|
| 228 |
Isel = a >= epsilon_u_square * K_sum_cols |
| 229 |
Jsel = b >= epsilon_v_square * K_sum_rows |
| 230 |
|
| 231 |
if nx.sum(Isel) != ns_budget: |
| 232 |
if uniform: |
| 233 |
aK = a / K_sum_cols |
| 234 |
aK_sort = nx.flip(nx.sort(aK), axis=0) |
| 235 |
epsilon_u_square = nx.mean(aK_sort[ns_budget - 1 : ns_budget + 1]) |
| 236 |
Isel = a >= epsilon_u_square * K_sum_cols |
| 237 |
ns_budget = nx.sum(Isel) |
| 238 |
|
| 239 |
if nx.sum(Jsel) != nt_budget: |
| 240 |
if uniform: |
| 241 |
bK = b / K_sum_rows |
| 242 |
bK_sort = nx.flip(nx.sort(bK), axis=0) |
| 243 |
epsilon_v_square = nx.mean(bK_sort[nt_budget - 1 : nt_budget + 1]) |
| 244 |
Jsel = b >= epsilon_v_square * K_sum_rows |
| 245 |
nt_budget = nx.sum(Jsel) |
| 246 |
|
| 247 |
epsilon = (epsilon_u_square * epsilon_v_square) ** (1 / 4) |
| 248 |
kappa = (epsilon_v_square / epsilon_u_square) ** (1 / 2) |
| 249 |
|
| 250 |
if verbose: |
| 251 |
print("epsilon = %s\n" % epsilon) |
| 252 |
print("kappa = %s\n" % kappa) |
| 253 |
print( |
| 254 |
"Cardinality of selected points: |Isel| = %s \t |Jsel| = %s \n" |
| 255 |
% (sum(Isel), sum(Jsel)) |
| 256 |
) |
| 257 |
|
| 258 |
|
| 259 |
Ic = ~Isel |
| 260 |
Jc = ~Jsel |
| 261 |
|
| 262 |
K_IJ = K[np.ix_(Isel, Jsel)] |
| 263 |
K_IcJ = K[np.ix_(Ic, Jsel)] |
| 264 |
K_IJc = K[np.ix_(Isel, Jc)] |
| 265 |
|
| 266 |
K_min = nx.min(K_IJ) |
| 267 |
if K_min == 0: |
| 268 |
K_min = float(np.finfo(float).tiny) |
| 269 |
|
| 270 |
|
| 271 |
a_I = a[Isel] |
| 272 |
b_J = b[Jsel] |
| 273 |
if not uniform: |
| 274 |
a_I_min = nx.min(a_I) |
| 275 |
a_I_max = nx.max(a_I) |
| 276 |
b_J_max = nx.max(b_J) |
| 277 |
b_J_min = nx.min(b_J) |
| 278 |
else: |
| 279 |
a_I_min = a_I[0] |
| 280 |
a_I_max = a_I[0] |
| 281 |
b_J_max = b_J[0] |
| 282 |
b_J_min = b_J[0] |
| 283 |
|
| 284 |
|
| 285 |
bounds_u = [ |
| 286 |
( |
| 287 |
max( |
| 288 |
a_I_min |
| 289 |
/ ( |
| 290 |
(nt - nt_budget) * epsilon |
| 291 |
+ nt_budget * (b_J_max / (ns * epsilon * kappa * K_min)) |
| 292 |
), |
| 293 |
epsilon / kappa, |
| 294 |
), |
| 295 |
a_I_max / (nt * epsilon * K_min), |
| 296 |
) |
| 297 |
] * ns_budget |
| 298 |
|
| 299 |
bounds_v = [ |
| 300 |
( |
| 301 |
max( |
| 302 |
b_J_min |
| 303 |
/ ( |
| 304 |
(ns - ns_budget) * epsilon |
| 305 |
+ ns_budget * (kappa * a_I_max / (nt * epsilon * K_min)) |
| 306 |
), |
| 307 |
epsilon * kappa, |
| 308 |
), |
| 309 |
b_J_max / (ns * epsilon * K_min), |
| 310 |
) |
| 311 |
] * nt_budget |
| 312 |
|
| 313 |
|
| 314 |
vec_eps_IJc = ( |
| 315 |
epsilon |
| 316 |
* kappa |
| 317 |
* nx.sum(K_IJc * nx.ones((nt - nt_budget,), type_as=M)[None, :], axis=1) |
| 318 |
) |
| 319 |
vec_eps_IcJ = (epsilon / kappa) * nx.sum( |
| 320 |
nx.ones((ns - ns_budget,), type_as=M)[:, None] * K_IcJ, axis=0 |
| 321 |
) |
| 322 |
|
| 323 |
|
| 324 |
u0 = nx.full((ns_budget,), 1.0 / ns_budget + epsilon / kappa, type_as=M) |
| 325 |
v0 = nx.full((nt_budget,), 1.0 / nt_budget + epsilon * kappa, type_as=M) |
| 326 |
|
| 327 |
|
| 328 |
if restricted: |
| 329 |
if ns_budget != ns or nt_budget != nt: |
| 330 |
cst_u = kappa * epsilon * nx.sum(K_IJc, axis=1) |
| 331 |
cst_v = epsilon * nx.sum(K_IcJ, axis=0) / kappa |
| 332 |
|
| 333 |
for _ in range(5): |
| 334 |
K_IJ_v = nx.dot(K_IJ.T, u0) + cst_v |
| 335 |
v0 = b_J / (kappa * K_IJ_v) |
| 336 |
KIJ_u = nx.dot(K_IJ, v0) + cst_u |
| 337 |
u0 = (kappa * a_I) / KIJ_u |
| 338 |
|
| 339 |
u0 = projection(u0, epsilon / kappa) |
| 340 |
v0 = projection(v0, epsilon * kappa) |
| 341 |
|
| 342 |
else: |
| 343 |
u0 = u0 |
| 344 |
v0 = v0 |
| 345 |
|
| 346 |
def restricted_sinkhorn(usc, vsc, max_iter=5): |
| 347 |
""" |
| 348 |
Restricted Sinkhorn Algorithm as a warm-start initialized pointfor L-BFGS-B) |
| 349 |
""" |
| 350 |
for _ in range(max_iter): |
| 351 |
K_IJ_v = nx.dot(K_IJ.T, usc) + cst_v |
| 352 |
vsc = b_J / (kappa * K_IJ_v) |
| 353 |
KIJ_u = nx.dot(K_IJ, vsc) + cst_u |
| 354 |
usc = (kappa * a_I) / KIJ_u |
| 355 |
|
| 356 |
usc = projection(usc, epsilon / kappa) |
| 357 |
vsc = projection(vsc, epsilon * kappa) |
| 358 |
|
| 359 |
return usc, vsc |
| 360 |
|
| 361 |
def screened_obj(usc, vsc): |
| 362 |
part_IJ = ( |
| 363 |
nx.dot(nx.dot(usc, K_IJ), vsc) |
| 364 |
- kappa * nx.dot(a_I, nx.log(usc)) |
| 365 |
- (1.0 / kappa) * nx.dot(b_J, nx.log(vsc)) |
| 366 |
) |
| 367 |
part_IJc = nx.dot(usc, vec_eps_IJc) |
| 368 |
part_IcJ = nx.dot(vec_eps_IcJ, vsc) |
| 369 |
psi_epsilon = part_IJ + part_IJc + part_IcJ |
| 370 |
return psi_epsilon |
| 371 |
|
| 372 |
def screened_grad(usc, vsc): |
| 373 |
|
| 374 |
grad_u = nx.dot(K_IJ, vsc) + vec_eps_IJc - kappa * a_I / usc |
| 375 |
grad_v = nx.dot(K_IJ.T, usc) + vec_eps_IcJ - (1.0 / kappa) * b_J / vsc |
| 376 |
return grad_u, grad_v |
| 377 |
|
| 378 |
def bfgspost(theta): |
| 379 |
u = theta[:ns_budget] |
| 380 |
v = theta[ns_budget:] |
| 381 |
|
| 382 |
f = screened_obj(u, v) |
| 383 |
|
| 384 |
g_u, g_v = screened_grad(u, v) |
| 385 |
g = nx.concatenate([g_u, g_v], axis=0) |
| 386 |
return nx.to_numpy(f), nx.to_numpy(g) |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
u0, v0 = restricted_sinkhorn(u0, v0) |
| 393 |
theta0 = nx.concatenate([u0, v0], axis=0) |
| 394 |
|
| 395 |
bounds = bounds_u + bounds_v |
| 396 |
|
| 397 |
def obj(theta): |
| 398 |
return bfgspost(nx.from_numpy(theta, type_as=M)) |
| 399 |
|
| 400 |
theta, _, _ = fmin_l_bfgs_b( |
| 401 |
func=obj, x0=theta0, bounds=bounds, maxfun=maxfun, pgtol=pgtol, maxiter=maxiter |
| 402 |
) |
| 403 |
theta = nx.from_numpy(theta, type_as=M) |
| 404 |
|
| 405 |
usc = theta[:ns_budget] |
| 406 |
vsc = theta[ns_budget:] |
| 407 |
|
| 408 |
usc_full = nx.full((ns,), epsilon / kappa, type_as=M) |
| 409 |
vsc_full = nx.full((nt,), epsilon * kappa, type_as=M) |
| 410 |
usc_full[Isel] = usc |
| 411 |
vsc_full[Jsel] = vsc |
| 412 |
|
| 413 |
if log: |
| 414 |
log = {} |
| 415 |
log["u"] = usc_full |
| 416 |
log["v"] = vsc_full |
| 417 |
log["Isel"] = Isel |
| 418 |
log["Jsel"] = Jsel |
| 419 |
|
| 420 |
gamma = usc_full[:, None] * K * vsc_full[None, :] |
| 421 |
gamma = gamma / nx.sum(gamma) |
| 422 |
|
| 423 |
if log: |
| 424 |
return gamma, log |
| 425 |
else: |
| 426 |
return gamma |