| 1 |
|
| 2 |
""" |
| 3 |
Bregman projections solvers for entropic Gromov-Wasserstein |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
> |
| 9 |
|
| 10 |
> |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
import numpy as np |
| 15 |
import warnings |
| 16 |
|
| 17 |
from ..bregman import sinkhorn |
| 18 |
from ..utils import dist, UndefinedParameter, list_to_array, check_random_state, unif |
| 19 |
from ..backend import get_backend |
| 20 |
|
| 21 |
from ._utils import init_matrix, gwloss, gwggrad |
| 22 |
from ._utils import update_barycenter_structure, update_barycenter_feature |
| 23 |
|
| 24 |
|
| 25 |
def entropic_gromov_wasserstein( |
| 26 |
C1, |
| 27 |
C2, |
| 28 |
p=None, |
| 29 |
q=None, |
| 30 |
loss_fun="square_loss", |
| 31 |
epsilon=0.1, |
| 32 |
symmetric=None, |
| 33 |
G0=None, |
| 34 |
max_iter=1000, |
| 35 |
tol=1e-9, |
| 36 |
solver="PGD", |
| 37 |
warmstart=False, |
| 38 |
verbose=False, |
| 39 |
log=False, |
| 40 |
**kwargs, |
| 41 |
): |
| 42 |
r""" |
| 43 |
Returns the Gromov-Wasserstein transport between :math:`(\mathbf{C_1}, \mathbf{p})` and :math:`(\mathbf{C_2}, \mathbf{q})` |
| 44 |
estimated using Sinkhorn projections. |
| 45 |
|
| 46 |
If `solver="PGD"`, the function solves the following entropic-regularized |
| 47 |
Gromov-Wasserstein optimization problem using Projected Gradient Descent [12]: |
| 48 |
|
| 49 |
.. math:: |
| 50 |
\mathbf{T}^* \in \mathop{\arg\min}_\mathbf{T} \quad \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} - \epsilon H(\mathbf{T}) |
| 51 |
|
| 52 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 53 |
|
| 54 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 55 |
|
| 56 |
\mathbf{T} &\geq 0 |
| 57 |
|
| 58 |
Else if `solver="PPA"`, the function solves the following Gromov-Wasserstein |
| 59 |
optimization problem using Proximal Point Algorithm [51]: |
| 60 |
|
| 61 |
.. math:: |
| 62 |
\mathbf{T}^* \in \mathop{\arg\min}_\mathbf{T} \quad \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} |
| 63 |
|
| 64 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 65 |
|
| 66 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 67 |
|
| 68 |
\mathbf{T} &\geq 0 |
| 69 |
Where : |
| 70 |
|
| 71 |
- :math:`\mathbf{C_1}`: Metric cost matrix in the source space |
| 72 |
- :math:`\mathbf{C_2}`: Metric cost matrix in the target space |
| 73 |
- :math:`\mathbf{p}`: distribution in the source space |
| 74 |
- :math:`\mathbf{q}`: distribution in the target space |
| 75 |
- `L`: loss function to account for the misfit between the similarity matrices |
| 76 |
- `H`: entropy |
| 77 |
|
| 78 |
.. note:: If the inner solver `ot.sinkhorn` did not convergence, the |
| 79 |
optimal coupling :math:`\mathbf{T}` returned by this function does not |
| 80 |
necessarily satisfy the marginal constraints |
| 81 |
:math:`\mathbf{T}\mathbf{1}=\mathbf{p}` and |
| 82 |
:math:`\mathbf{T}^T\mathbf{1}=\mathbf{q}`. So the returned |
| 83 |
Gromov-Wasserstein loss does not necessarily satisfy distance |
| 84 |
properties and may be negative. |
| 85 |
|
| 86 |
Parameters |
| 87 |
---------- |
| 88 |
C1 : array-like, shape (ns, ns) |
| 89 |
Metric cost matrix in the source space |
| 90 |
C2 : array-like, shape (nt, nt) |
| 91 |
Metric cost matrix in the target space |
| 92 |
p : array-like, shape (ns,), optional |
| 93 |
Distribution in the source space. |
| 94 |
If let to its default value None, uniform distribution is taken. |
| 95 |
q : array-like, shape (nt,), optional |
| 96 |
Distribution in the target space. |
| 97 |
If let to its default value None, uniform distribution is taken. |
| 98 |
loss_fun : string, optional (default='square_loss') |
| 99 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 100 |
epsilon : float, optional |
| 101 |
Regularization term >0 |
| 102 |
symmetric : bool, optional |
| 103 |
Either C1 and C2 are to be assumed symmetric or not. |
| 104 |
If let to its default None value, a symmetry test will be conducted. |
| 105 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 106 |
G0: array-like, shape (ns,nt), optional |
| 107 |
If None the initial transport plan of the solver is pq^T. |
| 108 |
Otherwise G0 will be used as initial transport of the solver. G0 is not |
| 109 |
required to satisfy marginal constraints but we strongly recommend it |
| 110 |
to correctly estimate the GW distance. |
| 111 |
max_iter : int, optional |
| 112 |
Max number of iterations |
| 113 |
tol : float, optional |
| 114 |
Stop threshold on error (>0) |
| 115 |
solver: string, optional |
| 116 |
Solver to use either 'PGD' for Projected Gradient Descent or 'PPA' |
| 117 |
for Proximal Point Algorithm. |
| 118 |
Default value is 'PGD'. |
| 119 |
warmstart: bool, optional |
| 120 |
Either to perform warmstart of dual potentials in the successive |
| 121 |
Sinkhorn projections. |
| 122 |
verbose : bool, optional |
| 123 |
Print information along iterations |
| 124 |
log : bool, optional |
| 125 |
Record log if True. |
| 126 |
**kwargs: dict |
| 127 |
parameters can be directly passed to the ot.sinkhorn solver. |
| 128 |
Such as `numItermax` and `stopThr` to control its estimation precision, |
| 129 |
e.g [51] suggests to use `numItermax=1`. |
| 130 |
Returns |
| 131 |
------- |
| 132 |
T : array-like, shape (`ns`, `nt`) |
| 133 |
Optimal coupling between the two spaces |
| 134 |
|
| 135 |
References |
| 136 |
---------- |
| 137 |
.. [12] Gabriel Peyré, Marco Cuturi, and Justin Solomon, |
| 138 |
"Gromov-Wasserstein averaging of kernel and distance matrices." |
| 139 |
International Conference on Machine Learning (ICML). 2016. |
| 140 |
|
| 141 |
.. [47] Chowdhury, S., & Mémoli, F. (2019). The gromov–wasserstein |
| 142 |
distance between networks and stable network invariants. |
| 143 |
Information and Inference: A Journal of the IMA, 8(4), 757-787. |
| 144 |
|
| 145 |
.. [51] Xu, H., Luo, D., Zha, H., & Duke, L. C. (2019). Gromov-wasserstein |
| 146 |
learning for graph matching and node embedding. In International |
| 147 |
Conference on Machine Learning (ICML), 2019. |
| 148 |
""" |
| 149 |
if solver not in ["PGD", "PPA"]: |
| 150 |
raise ValueError("Unknown solver '%s'. Pick one in ['PGD', 'PPA']." % solver) |
| 151 |
|
| 152 |
if loss_fun not in ("square_loss", "kl_loss"): |
| 153 |
raise ValueError( |
| 154 |
f"Unknown `loss_fun='{loss_fun}'`. Use one of: {'square_loss', 'kl_loss'}." |
| 155 |
) |
| 156 |
|
| 157 |
C1, C2 = list_to_array(C1, C2) |
| 158 |
arr = [C1, C2] |
| 159 |
if p is not None: |
| 160 |
arr.append(list_to_array(p)) |
| 161 |
else: |
| 162 |
p = unif(C1.shape[0], type_as=C1) |
| 163 |
if q is not None: |
| 164 |
arr.append(list_to_array(q)) |
| 165 |
else: |
| 166 |
q = unif(C2.shape[0], type_as=C2) |
| 167 |
|
| 168 |
if G0 is not None: |
| 169 |
arr.append(G0) |
| 170 |
|
| 171 |
nx = get_backend(*arr) |
| 172 |
|
| 173 |
if G0 is None: |
| 174 |
G0 = nx.outer(p, q) |
| 175 |
|
| 176 |
T = G0 |
| 177 |
constC, hC1, hC2 = init_matrix(C1, C2, p, q, loss_fun, nx) |
| 178 |
|
| 179 |
if symmetric is None: |
| 180 |
symmetric = nx.allclose(C1, C1.T, atol=1e-10) and nx.allclose( |
| 181 |
C2, C2.T, atol=1e-10 |
| 182 |
) |
| 183 |
if not symmetric: |
| 184 |
constCt, hC1t, hC2t = init_matrix(C1.T, C2.T, p, q, loss_fun, nx) |
| 185 |
|
| 186 |
cpt = 0 |
| 187 |
err = 1 |
| 188 |
|
| 189 |
if warmstart: |
| 190 |
|
| 191 |
N1, N2 = C1.shape[0], C2.shape[0] |
| 192 |
mu = nx.zeros(N1, type_as=C1) - np.log(N1) |
| 193 |
nu = nx.zeros(N2, type_as=C2) - np.log(N2) |
| 194 |
|
| 195 |
if log: |
| 196 |
log = {"err": []} |
| 197 |
|
| 198 |
while err > tol and cpt < max_iter: |
| 199 |
Tprev = T |
| 200 |
|
| 201 |
|
| 202 |
if symmetric: |
| 203 |
tens = gwggrad(constC, hC1, hC2, T, nx) |
| 204 |
else: |
| 205 |
tens = 0.5 * ( |
| 206 |
gwggrad(constC, hC1, hC2, T, nx) + gwggrad(constCt, hC1t, hC2t, T, nx) |
| 207 |
) |
| 208 |
|
| 209 |
if solver == "PPA": |
| 210 |
tens = tens - epsilon * nx.log(T) |
| 211 |
|
| 212 |
if warmstart: |
| 213 |
T, loginn = sinkhorn( |
| 214 |
p, |
| 215 |
q, |
| 216 |
tens, |
| 217 |
epsilon, |
| 218 |
method="sinkhorn", |
| 219 |
log=True, |
| 220 |
warmstart=(mu, nu), |
| 221 |
**kwargs, |
| 222 |
) |
| 223 |
mu = epsilon * nx.log(loginn["u"]) |
| 224 |
nu = epsilon * nx.log(loginn["v"]) |
| 225 |
|
| 226 |
else: |
| 227 |
T = sinkhorn(p, q, tens, epsilon, method="sinkhorn", **kwargs) |
| 228 |
|
| 229 |
if cpt % 10 == 0: |
| 230 |
|
| 231 |
|
| 232 |
err = nx.norm(T - Tprev) |
| 233 |
|
| 234 |
if log: |
| 235 |
log["err"].append(err) |
| 236 |
|
| 237 |
if verbose: |
| 238 |
if cpt % 200 == 0: |
| 239 |
print("{:5s}|{:12s}".format("It.", "Err") + "\n" + "-" * 19) |
| 240 |
print("{:5d}|{:8e}|".format(cpt, err)) |
| 241 |
|
| 242 |
cpt += 1 |
| 243 |
|
| 244 |
if abs(nx.sum(T) - 1) > 1e-5: |
| 245 |
warnings.warn( |
| 246 |
"Solver failed to produce a transport plan. You might " |
| 247 |
"want to increase the regularization parameter `epsilon`." |
| 248 |
) |
| 249 |
if log: |
| 250 |
log["gw_dist"] = gwloss(constC, hC1, hC2, T, nx) |
| 251 |
return T, log |
| 252 |
else: |
| 253 |
return T |
| 254 |
|
| 255 |
|
| 256 |
def entropic_gromov_wasserstein2( |
| 257 |
C1, |
| 258 |
C2, |
| 259 |
p=None, |
| 260 |
q=None, |
| 261 |
loss_fun="square_loss", |
| 262 |
epsilon=0.1, |
| 263 |
symmetric=None, |
| 264 |
G0=None, |
| 265 |
max_iter=1000, |
| 266 |
tol=1e-9, |
| 267 |
solver="PGD", |
| 268 |
warmstart=False, |
| 269 |
verbose=False, |
| 270 |
log=False, |
| 271 |
**kwargs, |
| 272 |
): |
| 273 |
r""" |
| 274 |
Returns the Gromov-Wasserstein loss :math:`\mathbf{GW}` between :math:`(\mathbf{C_1}, \mathbf{p})` and :math:`(\mathbf{C_2}, \mathbf{q})` |
| 275 |
estimated using Sinkhorn projections. To recover the Gromov-Wasserstein distance as defined in [13] compute :math:`d_{GW} = \frac{1}{2} \sqrt{\mathbf{GW}}`. |
| 276 |
|
| 277 |
If `solver="PGD"`, the function solves the following entropic-regularized |
| 278 |
Gromov-Wasserstein optimization problem using Projected Gradient Descent [12]: |
| 279 |
|
| 280 |
.. math:: |
| 281 |
\mathbf{GW} = \mathop{\min}_\mathbf{T} \quad \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} - \epsilon H(\mathbf{T}) |
| 282 |
|
| 283 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 284 |
|
| 285 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 286 |
|
| 287 |
\mathbf{T} &\geq 0 |
| 288 |
|
| 289 |
Else if `solver="PPA"`, the function solves the following Gromov-Wasserstein |
| 290 |
optimization problem using Proximal Point Algorithm [51]: |
| 291 |
|
| 292 |
.. math:: |
| 293 |
\mathbf{GW} = \mathop{\min}_\mathbf{T} \quad \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} |
| 294 |
|
| 295 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 296 |
|
| 297 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 298 |
|
| 299 |
\mathbf{T} &\geq 0 |
| 300 |
Where : |
| 301 |
|
| 302 |
- :math:`\mathbf{C_1}`: Metric cost matrix in the source space |
| 303 |
- :math:`\mathbf{C_2}`: Metric cost matrix in the target space |
| 304 |
- :math:`\mathbf{p}`: distribution in the source space |
| 305 |
- :math:`\mathbf{q}`: distribution in the target space |
| 306 |
- `L`: loss function to account for the misfit between the similarity matrices |
| 307 |
- `H`: entropy |
| 308 |
|
| 309 |
.. note:: If the inner solver `ot.sinkhorn` did not convergence, the |
| 310 |
optimal coupling :math:`\mathbf{T}` returned by this function does not |
| 311 |
necessarily satisfy the marginal constraints |
| 312 |
:math:`\mathbf{T}\mathbf{1}=\mathbf{p}` and |
| 313 |
:math:`\mathbf{T}^T\mathbf{1}=\mathbf{q}`. So the returned |
| 314 |
Gromov-Wasserstein loss does not necessarily satisfy distance |
| 315 |
properties and may be negative. |
| 316 |
|
| 317 |
Parameters |
| 318 |
---------- |
| 319 |
C1 : array-like, shape (ns, ns) |
| 320 |
Metric cost matrix in the source space |
| 321 |
C2 : array-like, shape (nt, nt) |
| 322 |
Metric cost matrix in the target space |
| 323 |
p : array-like, shape (ns,), optional |
| 324 |
Distribution in the source space. |
| 325 |
If let to its default value None, uniform distribution is taken. |
| 326 |
q : array-like, shape (nt,), optional |
| 327 |
Distribution in the target space. |
| 328 |
If let to its default value None, uniform distribution is taken. |
| 329 |
loss_fun : string, optional (default='square_loss') |
| 330 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 331 |
epsilon : float, optional |
| 332 |
Regularization term >0 |
| 333 |
symmetric : bool, optional |
| 334 |
Either C1 and C2 are to be assumed symmetric or not. |
| 335 |
If let to its default None value, a symmetry test will be conducted. |
| 336 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 337 |
G0: array-like, shape (ns,nt), optional |
| 338 |
If None the initial transport plan of the solver is pq^T. |
| 339 |
Otherwise G0 will be used as initial transport of the solver. G0 is not |
| 340 |
required to satisfy marginal constraints but we strongly recommend it |
| 341 |
to correctly estimate the GW distance. |
| 342 |
max_iter : int, optional |
| 343 |
Max number of iterations |
| 344 |
tol : float, optional |
| 345 |
Stop threshold on error (>0) |
| 346 |
solver: string, optional |
| 347 |
Solver to use either 'PGD' for Projected Gradient Descent or 'PPA' |
| 348 |
for Proximal Point Algorithm. |
| 349 |
Default value is 'PGD'. |
| 350 |
warmstart: bool, optional |
| 351 |
Either to perform warmstart of dual potentials in the successive |
| 352 |
Sinkhorn projections. |
| 353 |
verbose : bool, optional |
| 354 |
Print information along iterations |
| 355 |
log : bool, optional |
| 356 |
Record log if True. |
| 357 |
**kwargs: dict |
| 358 |
parameters can be directly passed to the ot.sinkhorn solver. |
| 359 |
Such as `numItermax` and `stopThr` to control its estimation precision, |
| 360 |
e.g [51] suggests to use `numItermax=1`. |
| 361 |
Returns |
| 362 |
------- |
| 363 |
gw_dist : float |
| 364 |
Gromov-Wasserstein distance |
| 365 |
|
| 366 |
References |
| 367 |
---------- |
| 368 |
.. [12] Gabriel Peyré, Marco Cuturi, and Justin Solomon, |
| 369 |
"Gromov-Wasserstein averaging of kernel and distance matrices." |
| 370 |
International Conference on Machine Learning (ICML). 2016. |
| 371 |
|
| 372 |
.. [51] Xu, H., Luo, D., Zha, H., & Duke, L. C. (2019). Gromov-wasserstein |
| 373 |
learning for graph matching and node embedding. In International |
| 374 |
Conference on Machine Learning (ICML), 2019. |
| 375 |
""" |
| 376 |
|
| 377 |
T, logv = entropic_gromov_wasserstein( |
| 378 |
C1, |
| 379 |
C2, |
| 380 |
p, |
| 381 |
q, |
| 382 |
loss_fun, |
| 383 |
epsilon, |
| 384 |
symmetric, |
| 385 |
G0, |
| 386 |
max_iter, |
| 387 |
tol, |
| 388 |
solver, |
| 389 |
warmstart, |
| 390 |
verbose, |
| 391 |
log=True, |
| 392 |
**kwargs, |
| 393 |
) |
| 394 |
|
| 395 |
logv["T"] = T |
| 396 |
|
| 397 |
if log: |
| 398 |
return logv["gw_dist"], logv |
| 399 |
else: |
| 400 |
return logv["gw_dist"] |
| 401 |
|
| 402 |
|
| 403 |
def BAPG_gromov_wasserstein( |
| 404 |
C1, |
| 405 |
C2, |
| 406 |
p=None, |
| 407 |
q=None, |
| 408 |
loss_fun="square_loss", |
| 409 |
epsilon=0.1, |
| 410 |
symmetric=None, |
| 411 |
G0=None, |
| 412 |
max_iter=1000, |
| 413 |
tol=1e-9, |
| 414 |
marginal_loss=False, |
| 415 |
verbose=False, |
| 416 |
log=False, |
| 417 |
): |
| 418 |
r""" |
| 419 |
Returns the Gromov-Wasserstein transport between :math:`(\mathbf{C_1}, \mathbf{p})` and :math:`(\mathbf{C_2}, \mathbf{q})` |
| 420 |
estimated using Bregman Alternated Projected Gradient method. |
| 421 |
|
| 422 |
If `marginal_loss=True`, the function solves the following Gromov-Wasserstein |
| 423 |
optimization problem : |
| 424 |
|
| 425 |
.. math:: |
| 426 |
\mathbf{T}^* \in \mathop{\arg\min}_\mathbf{T} \quad \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} |
| 427 |
|
| 428 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 429 |
|
| 430 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 431 |
|
| 432 |
\mathbf{T} &\geq 0 |
| 433 |
|
| 434 |
Else, the function solves an equivalent problem [63], where constant terms only |
| 435 |
depending on the marginals :math:`\mathbf{p}`: and :math:`\mathbf{q}`: are |
| 436 |
discarded while assuming that L decomposes as in Proposition 1 in [12]: |
| 437 |
|
| 438 |
.. math:: |
| 439 |
\mathbf{T}^* \in\mathop{\arg\min}_\mathbf{T} \quad - \langle h_1(\mathbf{C}_1) \mathbf{T} h_2(\mathbf{C_2})^\top , \mathbf{T} \rangle_F |
| 440 |
|
| 441 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 442 |
|
| 443 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 444 |
|
| 445 |
\mathbf{T} &\geq 0 |
| 446 |
Where : |
| 447 |
|
| 448 |
- :math:`\mathbf{C_1}`: Metric cost matrix in the source space |
| 449 |
- :math:`\mathbf{C_2}`: Metric cost matrix in the target space |
| 450 |
- :math:`\mathbf{p}`: distribution in the source space |
| 451 |
- :math:`\mathbf{q}`: distribution in the target space |
| 452 |
- `L`: loss function to account for the misfit between the similarity matrices |
| 453 |
satisfying :math:`L(a, b) = f_1(a) + f_2(b) - h_1(a) h_2(b)` |
| 454 |
|
| 455 |
.. note:: By algorithmic design the optimal coupling :math:`\mathbf{T}` |
| 456 |
returned by this function does not necessarily satisfy the marginal |
| 457 |
constraints :math:`\mathbf{T}\mathbf{1}=\mathbf{p}` and |
| 458 |
:math:`\mathbf{T}^T\mathbf{1}=\mathbf{q}`. So the returned |
| 459 |
Gromov-Wasserstein loss does not necessarily satisfy distance |
| 460 |
properties and may be negative. |
| 461 |
|
| 462 |
Parameters |
| 463 |
---------- |
| 464 |
C1 : array-like, shape (ns, ns) |
| 465 |
Metric cost matrix in the source space |
| 466 |
C2 : array-like, shape (nt, nt) |
| 467 |
Metric cost matrix in the target space |
| 468 |
p : array-like, shape (ns,), optional |
| 469 |
Distribution in the source space. |
| 470 |
If let to its default value None, uniform distribution is taken. |
| 471 |
q : array-like, shape (nt,), optional |
| 472 |
Distribution in the target space. |
| 473 |
If let to its default value None, uniform distribution is taken. |
| 474 |
loss_fun : string, optional (default='square_loss') |
| 475 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 476 |
epsilon : float, optional |
| 477 |
Regularization term >0 |
| 478 |
symmetric : bool, optional |
| 479 |
Either C1 and C2 are to be assumed symmetric or not. |
| 480 |
If let to its default None value, a symmetry test will be conducted. |
| 481 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 482 |
G0: array-like, shape (ns,nt), optional |
| 483 |
If None the initial transport plan of the solver is pq^T. |
| 484 |
Otherwise G0 will be used as initial transport of the solver. G0 is not |
| 485 |
required to satisfy marginal constraints but we strongly recommend it |
| 486 |
to correctly estimate the GW distance. |
| 487 |
max_iter : int, optional |
| 488 |
Max number of iterations |
| 489 |
tol : float, optional |
| 490 |
Stop threshold on error (>0) |
| 491 |
marginal_loss: bool, optional. Default is False. |
| 492 |
Include constant marginal terms or not in the objective function. |
| 493 |
verbose : bool, optional |
| 494 |
Print information along iterations |
| 495 |
log : bool, optional |
| 496 |
Record log if True. |
| 497 |
Returns |
| 498 |
------- |
| 499 |
T : array-like, shape (`ns`, `nt`) |
| 500 |
Optimal coupling between the two spaces |
| 501 |
|
| 502 |
References |
| 503 |
---------- |
| 504 |
.. [63] Li, J., Tang, J., Kong, L., Liu, H., Li, J., So, A. M. C., & Blanchet, J. |
| 505 |
"A Convergent Single-Loop Algorithm for Relaxation of Gromov-Wasserstein |
| 506 |
in Graph Data". International Conference on Learning Representations (ICLR), 2022. |
| 507 |
|
| 508 |
""" |
| 509 |
if loss_fun not in ("square_loss", "kl_loss"): |
| 510 |
raise ValueError( |
| 511 |
f"Unknown `loss_fun='{loss_fun}'`. Use one of: {'square_loss', 'kl_loss'}." |
| 512 |
) |
| 513 |
|
| 514 |
C1, C2 = list_to_array(C1, C2) |
| 515 |
arr = [C1, C2] |
| 516 |
if p is not None: |
| 517 |
arr.append(list_to_array(p)) |
| 518 |
else: |
| 519 |
p = unif(C1.shape[0], type_as=C1) |
| 520 |
if q is not None: |
| 521 |
arr.append(list_to_array(q)) |
| 522 |
else: |
| 523 |
q = unif(C2.shape[0], type_as=C2) |
| 524 |
|
| 525 |
if G0 is not None: |
| 526 |
arr.append(G0) |
| 527 |
|
| 528 |
nx = get_backend(*arr) |
| 529 |
|
| 530 |
if G0 is None: |
| 531 |
G0 = nx.outer(p, q) |
| 532 |
|
| 533 |
T = G0 |
| 534 |
constC, hC1, hC2 = init_matrix(C1, C2, p, q, loss_fun, nx) |
| 535 |
|
| 536 |
if symmetric is None: |
| 537 |
symmetric = nx.allclose(C1, C1.T, atol=1e-10) and nx.allclose( |
| 538 |
C2, C2.T, atol=1e-10 |
| 539 |
) |
| 540 |
if not symmetric: |
| 541 |
constCt, hC1t, hC2t = init_matrix(C1.T, C2.T, p, q, loss_fun, nx) |
| 542 |
|
| 543 |
if marginal_loss: |
| 544 |
if symmetric: |
| 545 |
|
| 546 |
def df(T): |
| 547 |
return gwggrad(constC, hC1, hC2, T, nx) |
| 548 |
else: |
| 549 |
|
| 550 |
def df(T): |
| 551 |
return 0.5 * ( |
| 552 |
gwggrad(constC, hC1, hC2, T, nx) |
| 553 |
+ gwggrad(constCt, hC1t, hC2t, T, nx) |
| 554 |
) |
| 555 |
|
| 556 |
else: |
| 557 |
if symmetric: |
| 558 |
|
| 559 |
def df(T): |
| 560 |
A = -nx.dot(nx.dot(hC1, T), hC2.T) |
| 561 |
return 2 * A |
| 562 |
else: |
| 563 |
|
| 564 |
def df(T): |
| 565 |
A = -nx.dot(nx.dot(hC1, T), hC2t) |
| 566 |
At = -nx.dot(nx.dot(hC1t, T), hC2) |
| 567 |
return A + At |
| 568 |
|
| 569 |
cpt = 0 |
| 570 |
err = 1e15 |
| 571 |
|
| 572 |
if log: |
| 573 |
log = {"err": []} |
| 574 |
|
| 575 |
while err > tol and cpt < max_iter: |
| 576 |
Tprev = T |
| 577 |
|
| 578 |
|
| 579 |
T = T * nx.exp(-df(T) / epsilon) |
| 580 |
row_scaling = p / nx.sum(T, 1) |
| 581 |
T = nx.reshape(row_scaling, (-1, 1)) * T |
| 582 |
|
| 583 |
|
| 584 |
T = T * nx.exp(-df(T) / epsilon) |
| 585 |
column_scaling = q / nx.sum(T, 0) |
| 586 |
T = nx.reshape(column_scaling, (1, -1)) * T |
| 587 |
|
| 588 |
if cpt % 10 == 0: |
| 589 |
|
| 590 |
|
| 591 |
err = nx.norm(T - Tprev) |
| 592 |
|
| 593 |
if log: |
| 594 |
log["err"].append(err) |
| 595 |
|
| 596 |
if verbose: |
| 597 |
if cpt % 200 == 0: |
| 598 |
print("{:5s}|{:12s}".format("It.", "Err") + "\n" + "-" * 19) |
| 599 |
print("{:5d}|{:8e}|".format(cpt, err)) |
| 600 |
|
| 601 |
cpt += 1 |
| 602 |
|
| 603 |
if nx.any(nx.isnan(T)): |
| 604 |
warnings.warn( |
| 605 |
"Solver failed to produce a transport plan. You might " |
| 606 |
"want to increase the regularization parameter `epsilon`.", |
| 607 |
UserWarning, |
| 608 |
) |
| 609 |
if log: |
| 610 |
log["gw_dist"] = gwloss(constC, hC1, hC2, T, nx) |
| 611 |
|
| 612 |
if not marginal_loss: |
| 613 |
log["loss"] = log["gw_dist"] - nx.sum(constC * T) |
| 614 |
|
| 615 |
return T, log |
| 616 |
else: |
| 617 |
return T |
| 618 |
|
| 619 |
|
| 620 |
def BAPG_gromov_wasserstein2( |
| 621 |
C1, |
| 622 |
C2, |
| 623 |
p=None, |
| 624 |
q=None, |
| 625 |
loss_fun="square_loss", |
| 626 |
epsilon=0.1, |
| 627 |
symmetric=None, |
| 628 |
G0=None, |
| 629 |
max_iter=1000, |
| 630 |
tol=1e-9, |
| 631 |
marginal_loss=False, |
| 632 |
verbose=False, |
| 633 |
log=False, |
| 634 |
): |
| 635 |
r""" |
| 636 |
Returns the Gromov-Wasserstein loss :math:`\mathbf{GW}` between :math:`(\mathbf{C_1}, \mathbf{p})` and :math:`(\mathbf{C_2}, \mathbf{q})` |
| 637 |
estimated using Bregman Alternated Projected Gradient method. |
| 638 |
|
| 639 |
If `marginal_loss=True`, the function solves the following Gromov-Wasserstein |
| 640 |
optimization problem : |
| 641 |
|
| 642 |
|
| 643 |
.. math:: |
| 644 |
\mathbf{GW} = \mathop{\min}_\mathbf{T} \quad \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} |
| 645 |
|
| 646 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 647 |
|
| 648 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 649 |
|
| 650 |
\mathbf{T} &\geq 0 |
| 651 |
|
| 652 |
Else, the function solves an equivalent problem [63, 64], where constant terms only |
| 653 |
depending on the marginals :math:`\mathbf{p}`: and :math:`\mathbf{q}`: are |
| 654 |
discarded while assuming that L decomposes as in Proposition 1 in [12]: |
| 655 |
|
| 656 |
.. math:: |
| 657 |
\mathop{\min}_\mathbf{T} \quad - \langle h_1(\mathbf{C}_1) \mathbf{T} h_2(\mathbf{C_2})^\top , \mathbf{T} \rangle_F |
| 658 |
|
| 659 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 660 |
|
| 661 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 662 |
|
| 663 |
\mathbf{T} &\geq 0 |
| 664 |
Where : |
| 665 |
|
| 666 |
- :math:`\mathbf{C_1}`: Metric cost matrix in the source space |
| 667 |
- :math:`\mathbf{C_2}`: Metric cost matrix in the target space |
| 668 |
- :math:`\mathbf{p}`: distribution in the source space |
| 669 |
- :math:`\mathbf{q}`: distribution in the target space |
| 670 |
- `L`: loss function to account for the misfit between the similarity matrices |
| 671 |
satisfying :math:`L(a, b) = f_1(a) + f_2(b) - h_1(a) h_2(b)` |
| 672 |
|
| 673 |
.. note:: By algorithmic design the optimal coupling :math:`\mathbf{T}` |
| 674 |
returned by this function does not necessarily satisfy the marginal |
| 675 |
constraints :math:`\mathbf{T}\mathbf{1}=\mathbf{p}` and |
| 676 |
:math:`\mathbf{T}^T\mathbf{1}=\mathbf{q}`. So the returned |
| 677 |
Gromov-Wasserstein loss does not necessarily satisfy distance |
| 678 |
properties and may be negative. |
| 679 |
|
| 680 |
|
| 681 |
Parameters |
| 682 |
---------- |
| 683 |
C1 : array-like, shape (ns, ns) |
| 684 |
Metric cost matrix in the source space |
| 685 |
C2 : array-like, shape (nt, nt) |
| 686 |
Metric cost matrix in the target space |
| 687 |
p : array-like, shape (ns,), optional |
| 688 |
Distribution in the source space. |
| 689 |
If let to its default value None, uniform distribution is taken. |
| 690 |
q : array-like, shape (nt,), optional |
| 691 |
Distribution in the target space. |
| 692 |
If let to its default value None, uniform distribution is taken. |
| 693 |
loss_fun : string, optional (default='square_loss') |
| 694 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 695 |
epsilon : float, optional |
| 696 |
Regularization term >0 |
| 697 |
symmetric : bool, optional |
| 698 |
Either C1 and C2 are to be assumed symmetric or not. |
| 699 |
If let to its default None value, a symmetry test will be conducted. |
| 700 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 701 |
G0: array-like, shape (ns,nt), optional |
| 702 |
If None the initial transport plan of the solver is pq^T. |
| 703 |
Otherwise G0 will be used as initial transport of the solver. G0 is not |
| 704 |
required to satisfy marginal constraints but we strongly recommend it |
| 705 |
to correctly estimate the GW distance. |
| 706 |
max_iter : int, optional |
| 707 |
Max number of iterations |
| 708 |
tol : float, optional |
| 709 |
Stop threshold on error (>0) |
| 710 |
marginal_loss: bool, optional. Default is False. |
| 711 |
Include constant marginal terms or not in the objective function. |
| 712 |
verbose : bool, optional |
| 713 |
Print information along iterations |
| 714 |
log : bool, optional |
| 715 |
Record log if True. |
| 716 |
|
| 717 |
Returns |
| 718 |
------- |
| 719 |
gw_dist : float |
| 720 |
Gromov-Wasserstein distance |
| 721 |
|
| 722 |
References |
| 723 |
---------- |
| 724 |
.. [63] Li, J., Tang, J., Kong, L., Liu, H., Li, J., So, A. M. C., & Blanchet, J. |
| 725 |
"A Convergent Single-Loop Algorithm for Relaxation of Gromov-Wasserstein |
| 726 |
in Graph Data". International Conference on Learning Representations (ICLR), 2023. |
| 727 |
|
| 728 |
""" |
| 729 |
|
| 730 |
T, logv = BAPG_gromov_wasserstein( |
| 731 |
C1, |
| 732 |
C2, |
| 733 |
p, |
| 734 |
q, |
| 735 |
loss_fun, |
| 736 |
epsilon, |
| 737 |
symmetric, |
| 738 |
G0, |
| 739 |
max_iter, |
| 740 |
tol, |
| 741 |
marginal_loss, |
| 742 |
verbose, |
| 743 |
log=True, |
| 744 |
) |
| 745 |
|
| 746 |
logv["T"] = T |
| 747 |
|
| 748 |
if log: |
| 749 |
return logv["gw_dist"], logv |
| 750 |
else: |
| 751 |
return logv["gw_dist"] |
| 752 |
|
| 753 |
|
| 754 |
def entropic_gromov_barycenters( |
| 755 |
N, |
| 756 |
Cs, |
| 757 |
ps=None, |
| 758 |
p=None, |
| 759 |
lambdas=None, |
| 760 |
loss_fun="square_loss", |
| 761 |
epsilon=0.1, |
| 762 |
symmetric=True, |
| 763 |
max_iter=1000, |
| 764 |
tol=1e-9, |
| 765 |
stop_criterion="barycenter", |
| 766 |
warmstartT=False, |
| 767 |
verbose=False, |
| 768 |
log=False, |
| 769 |
init_C=None, |
| 770 |
random_state=None, |
| 771 |
**kwargs, |
| 772 |
): |
| 773 |
r""" |
| 774 |
Returns the Gromov-Wasserstein barycenters of `S` measured similarity matrices :math:`(\mathbf{C}_s)_{1 \leq s \leq S}` |
| 775 |
estimated using Gromov-Wasserstein transports from Sinkhorn projections. |
| 776 |
|
| 777 |
The function solves the following optimization problem: |
| 778 |
|
| 779 |
.. math:: |
| 780 |
|
| 781 |
\mathbf{C}^* = \mathop{\arg \min}_{\mathbf{C}\in \mathbb{R}^{N \times N}} \quad \sum_s \lambda_s \mathrm{GW}(\mathbf{C}, \mathbf{C}_s, \mathbf{p}, \mathbf{p}_s) |
| 782 |
|
| 783 |
Where : |
| 784 |
|
| 785 |
- :math:`\mathbf{C}_s`: metric cost matrix |
| 786 |
- :math:`\mathbf{p}_s`: distribution |
| 787 |
|
| 788 |
Parameters |
| 789 |
---------- |
| 790 |
N : int |
| 791 |
Size of the targeted barycenter |
| 792 |
Cs : list of S array-like of shape (ns,ns) |
| 793 |
Metric cost matrices |
| 794 |
ps : list of S array-like of shape (ns,), optional |
| 795 |
Sample weights in the `S` spaces. |
| 796 |
If let to its default value None, uniform distributions are taken. |
| 797 |
p : array-like, shape (N,), optional |
| 798 |
Weights in the targeted barycenter. |
| 799 |
If let to its default value None, uniform distribution is taken. |
| 800 |
lambdas : list of float, optional |
| 801 |
List of the `S` spaces' weights. |
| 802 |
If let to its default value None, uniform weights are taken. |
| 803 |
loss_fun : string, optional (default='square_loss') |
| 804 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 805 |
epsilon : float, optional |
| 806 |
Regularization term >0 |
| 807 |
symmetric : bool, optional. |
| 808 |
Either structures are to be assumed symmetric or not. Default value is True. |
| 809 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 810 |
max_iter : int, optional |
| 811 |
Max number of iterations |
| 812 |
tol : float, optional |
| 813 |
Stop threshold on error (>0) |
| 814 |
stop_criterion : str, optional. Default is 'barycenter'. |
| 815 |
Convergence criterion taking values in ['barycenter', 'loss']. If set to 'barycenter' |
| 816 |
uses absolute norm variations of estimated barycenters. Else if set to 'loss' |
| 817 |
uses the relative variations of the loss. |
| 818 |
warmstartT: bool, optional |
| 819 |
Either to perform warmstart of transport plans in the successive |
| 820 |
gromov-wasserstein transport problems. |
| 821 |
verbose : bool, optional |
| 822 |
Print information along iterations. |
| 823 |
log : bool, optional |
| 824 |
Record log if True. |
| 825 |
init_C : bool | array-like, shape (N, N) |
| 826 |
Random initial value for the :math:`\mathbf{C}` matrix provided by user. |
| 827 |
random_state : int or RandomState instance, optional |
| 828 |
Fix the seed for reproducibility |
| 829 |
**kwargs: dict |
| 830 |
parameters can be directly passed to the `ot.entropic_gromov_wasserstein` solver. |
| 831 |
|
| 832 |
Returns |
| 833 |
------- |
| 834 |
C : array-like, shape (`N`, `N`) |
| 835 |
Similarity matrix in the barycenter space (permutated arbitrarily) |
| 836 |
log : dict |
| 837 |
Only returned when log=True. It contains the keys: |
| 838 |
|
| 839 |
- :math:`\mathbf{T}`: list of (`N`, `ns`) transport matrices |
| 840 |
- :math:`\mathbf{p}`: (`N`,) barycenter weights |
| 841 |
- values used in convergence evaluation. |
| 842 |
|
| 843 |
References |
| 844 |
---------- |
| 845 |
.. [12] Gabriel Peyré, Marco Cuturi, and Justin Solomon, |
| 846 |
"Gromov-Wasserstein averaging of kernel and distance matrices." |
| 847 |
International Conference on Machine Learning (ICML). 2016. |
| 848 |
""" |
| 849 |
if loss_fun not in ("square_loss", "kl_loss"): |
| 850 |
raise ValueError( |
| 851 |
f"Unknown `loss_fun='{loss_fun}'`. Use one of: {'square_loss', 'kl_loss'}." |
| 852 |
) |
| 853 |
|
| 854 |
if stop_criterion not in ["barycenter", "loss"]: |
| 855 |
raise ValueError( |
| 856 |
f"Unknown `stop_criterion='{stop_criterion}'`. Use one of: {'barycenter', 'loss'}." |
| 857 |
) |
| 858 |
|
| 859 |
if isinstance(Cs[0], list): |
| 860 |
raise ValueError( |
| 861 |
"Deprecated feature in POT 0.9.4: structures Cs[i] are lists and should be arrays from a supported backend (e.g numpy)." |
| 862 |
) |
| 863 |
|
| 864 |
arr = [*Cs] |
| 865 |
if ps is not None: |
| 866 |
if isinstance(ps[0], list): |
| 867 |
raise ValueError( |
| 868 |
"Deprecated feature in POT 0.9.4: weights ps[i] are lists and should be arrays from a supported backend (e.g numpy)." |
| 869 |
) |
| 870 |
|
| 871 |
arr += [*ps] |
| 872 |
else: |
| 873 |
ps = [unif(C.shape[0], type_as=C) for C in Cs] |
| 874 |
if p is not None: |
| 875 |
arr.append(list_to_array(p)) |
| 876 |
else: |
| 877 |
p = unif(N, type_as=Cs[0]) |
| 878 |
|
| 879 |
nx = get_backend(*arr) |
| 880 |
|
| 881 |
S = len(Cs) |
| 882 |
if lambdas is None: |
| 883 |
lambdas = [1.0 / S] * S |
| 884 |
|
| 885 |
|
| 886 |
if init_C is None: |
| 887 |
generator = check_random_state(random_state) |
| 888 |
xalea = generator.randn(N, 2) |
| 889 |
C = dist(xalea, xalea) |
| 890 |
C /= C.max() |
| 891 |
C = nx.from_numpy(C, type_as=p) |
| 892 |
else: |
| 893 |
C = init_C |
| 894 |
|
| 895 |
cpt = 0 |
| 896 |
err = 1e15 |
| 897 |
|
| 898 |
if warmstartT: |
| 899 |
T = [None] * S |
| 900 |
|
| 901 |
if stop_criterion == "barycenter": |
| 902 |
inner_log = False |
| 903 |
else: |
| 904 |
inner_log = True |
| 905 |
curr_loss = 1e15 |
| 906 |
|
| 907 |
if log: |
| 908 |
log_ = {} |
| 909 |
log_["err"] = [] |
| 910 |
if stop_criterion == "loss": |
| 911 |
log_["loss"] = [] |
| 912 |
|
| 913 |
while (err > tol) and (cpt < max_iter): |
| 914 |
if stop_criterion == "barycenter": |
| 915 |
Cprev = C |
| 916 |
else: |
| 917 |
prev_loss = curr_loss |
| 918 |
|
| 919 |
|
| 920 |
if warmstartT: |
| 921 |
res = [ |
| 922 |
entropic_gromov_wasserstein( |
| 923 |
C, |
| 924 |
Cs[s], |
| 925 |
p, |
| 926 |
ps[s], |
| 927 |
loss_fun, |
| 928 |
epsilon, |
| 929 |
symmetric, |
| 930 |
T[s], |
| 931 |
max_iter, |
| 932 |
1e-4, |
| 933 |
verbose=verbose, |
| 934 |
log=inner_log, |
| 935 |
**kwargs, |
| 936 |
) |
| 937 |
for s in range(S) |
| 938 |
] |
| 939 |
else: |
| 940 |
res = [ |
| 941 |
entropic_gromov_wasserstein( |
| 942 |
C, |
| 943 |
Cs[s], |
| 944 |
p, |
| 945 |
ps[s], |
| 946 |
loss_fun, |
| 947 |
epsilon, |
| 948 |
symmetric, |
| 949 |
None, |
| 950 |
max_iter, |
| 951 |
1e-4, |
| 952 |
verbose=verbose, |
| 953 |
log=inner_log, |
| 954 |
**kwargs, |
| 955 |
) |
| 956 |
for s in range(S) |
| 957 |
] |
| 958 |
if stop_criterion == "barycenter": |
| 959 |
T = res |
| 960 |
else: |
| 961 |
T = [output[0] for output in res] |
| 962 |
curr_loss = np.sum([output[1]["gw_dist"] for output in res]) |
| 963 |
|
| 964 |
|
| 965 |
C = update_barycenter_structure( |
| 966 |
T, Cs, lambdas, p, loss_fun, target=False, check_zeros=False, nx=nx |
| 967 |
) |
| 968 |
|
| 969 |
|
| 970 |
if stop_criterion == "barycenter": |
| 971 |
err = nx.norm(C - Cprev) |
| 972 |
if log: |
| 973 |
log_["err"].append(err) |
| 974 |
|
| 975 |
else: |
| 976 |
err = abs(curr_loss - prev_loss) / prev_loss if prev_loss != 0.0 else np.nan |
| 977 |
if log: |
| 978 |
log_["loss"].append(curr_loss) |
| 979 |
log_["err"].append(err) |
| 980 |
|
| 981 |
if verbose: |
| 982 |
if cpt % 200 == 0: |
| 983 |
print("{:5s}|{:12s}".format("It.", "Err") + "\n" + "-" * 19) |
| 984 |
print("{:5d}|{:8e}|".format(cpt, err)) |
| 985 |
|
| 986 |
cpt += 1 |
| 987 |
|
| 988 |
if log: |
| 989 |
log_["T"] = T |
| 990 |
log_["p"] = p |
| 991 |
|
| 992 |
return C, log_ |
| 993 |
else: |
| 994 |
return C |
| 995 |
|
| 996 |
|
| 997 |
def entropic_fused_gromov_wasserstein( |
| 998 |
M, |
| 999 |
C1, |
| 1000 |
C2, |
| 1001 |
p=None, |
| 1002 |
q=None, |
| 1003 |
loss_fun="square_loss", |
| 1004 |
epsilon=0.1, |
| 1005 |
symmetric=None, |
| 1006 |
alpha=0.5, |
| 1007 |
G0=None, |
| 1008 |
max_iter=1000, |
| 1009 |
tol=1e-9, |
| 1010 |
solver="PGD", |
| 1011 |
warmstart=False, |
| 1012 |
verbose=False, |
| 1013 |
log=False, |
| 1014 |
**kwargs, |
| 1015 |
): |
| 1016 |
r""" |
| 1017 |
Returns the Fused Gromov-Wasserstein transport between :math:`(\mathbf{C_1}, \mathbf{Y_1}, \mathbf{p})` and :math:`(\mathbf{C_2}, \mathbf{Y_2}, \mathbf{q})` |
| 1018 |
with pairwise distance matrix :math:`\mathbf{M}` between node feature matrices :math:`\mathbf{Y_1}` and :math:`\mathbf{Y_2}`, |
| 1019 |
estimated using Sinkhorn projections. |
| 1020 |
|
| 1021 |
If `solver="PGD"`, the function solves the following entropic-regularized |
| 1022 |
Fused Gromov-Wasserstein optimization problem using Projected Gradient Descent [12]: |
| 1023 |
|
| 1024 |
.. math:: |
| 1025 |
\mathbf{T}^* \in \mathop{\arg\min}_\mathbf{T} \quad (1 - \alpha) \langle \mathbf{T}, \mathbf{M} \rangle_F + |
| 1026 |
\alpha \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} - \epsilon H(\mathbf{T}) |
| 1027 |
|
| 1028 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 1029 |
|
| 1030 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 1031 |
|
| 1032 |
\mathbf{T} &\geq 0 |
| 1033 |
|
| 1034 |
Else if `solver="PPA"`, the function solves the following Fused Gromov-Wasserstein |
| 1035 |
optimization problem using Proximal Point Algorithm [51]: |
| 1036 |
|
| 1037 |
.. math:: |
| 1038 |
\mathbf{T}^* \in\mathop{\arg\min}_\mathbf{T} \quad (1 - \alpha) \langle \mathbf{T}, \mathbf{M} \rangle_F + |
| 1039 |
\alpha \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} |
| 1040 |
|
| 1041 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 1042 |
|
| 1043 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 1044 |
|
| 1045 |
\mathbf{T} &\geq 0 |
| 1046 |
Where : |
| 1047 |
|
| 1048 |
- :math:`\mathbf{M}`: metric cost matrix between features across domains |
| 1049 |
- :math:`\mathbf{C_1}`: Metric cost matrix in the source space |
| 1050 |
- :math:`\mathbf{C_2}`: Metric cost matrix in the target space |
| 1051 |
- :math:`\mathbf{p}`: distribution in the source space |
| 1052 |
- :math:`\mathbf{q}`: distribution in the target space |
| 1053 |
- `L`: loss function to account for the misfit between the similarity and feature matrices |
| 1054 |
- `H`: entropy |
| 1055 |
- :math:`\alpha`: trade-off parameter |
| 1056 |
|
| 1057 |
.. note:: If the inner solver `ot.sinkhorn` did not convergence, the |
| 1058 |
optimal coupling :math:`\mathbf{T}` returned by this function does not |
| 1059 |
necessarily satisfy the marginal constraints |
| 1060 |
:math:`\mathbf{T}\mathbf{1}=\mathbf{p}` and |
| 1061 |
:math:`\mathbf{T}^T\mathbf{1}=\mathbf{q}`. So the returned |
| 1062 |
Fused Gromov-Wasserstein loss does not necessarily satisfy distance |
| 1063 |
properties and may be negative. |
| 1064 |
|
| 1065 |
Parameters |
| 1066 |
---------- |
| 1067 |
M : array-like, shape (ns, nt) |
| 1068 |
Metric cost matrix between features across domains |
| 1069 |
C1 : array-like, shape (ns, ns) |
| 1070 |
Metric cost matrix in the source space |
| 1071 |
C2 : array-like, shape (nt, nt) |
| 1072 |
Metric cost matrix in the target space |
| 1073 |
p : array-like, shape (ns,), optional |
| 1074 |
Distribution in the source space. |
| 1075 |
If let to its default value None, uniform distribution is taken. |
| 1076 |
q : array-like, shape (nt,), optional |
| 1077 |
Distribution in the target space. |
| 1078 |
If let to its default value None, uniform distribution is taken. |
| 1079 |
loss_fun : string, optional (default='square_loss') |
| 1080 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 1081 |
epsilon : float, optional |
| 1082 |
Regularization term >0 |
| 1083 |
symmetric : bool, optional |
| 1084 |
Either C1 and C2 are to be assumed symmetric or not. |
| 1085 |
If let to its default None value, a symmetry test will be conducted. |
| 1086 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 1087 |
alpha : float, optional |
| 1088 |
Trade-off parameter (0 < alpha < 1) |
| 1089 |
G0: array-like, shape (ns,nt), optional |
| 1090 |
If None the initial transport plan of the solver is pq^T. |
| 1091 |
Otherwise G0 will be used as initial transport of the solver. G0 is not |
| 1092 |
required to satisfy marginal constraints but we strongly recommend it |
| 1093 |
to correctly estimate the GW distance. |
| 1094 |
max_iter : int, optional |
| 1095 |
Max number of iterations |
| 1096 |
tol : float, optional |
| 1097 |
Stop threshold on error (>0) |
| 1098 |
solver: string, optional |
| 1099 |
Solver to use either 'PGD' for Projected Gradient Descent or 'PPA' |
| 1100 |
for Proximal Point Algorithm. |
| 1101 |
Default value is 'PGD'. |
| 1102 |
warmstart: bool, optional |
| 1103 |
Either to perform warmstart of dual potentials in the successive |
| 1104 |
Sinkhorn projections. |
| 1105 |
verbose : bool, optional |
| 1106 |
Print information along iterations |
| 1107 |
log : bool, optional |
| 1108 |
Record log if True. |
| 1109 |
**kwargs: dict |
| 1110 |
parameters can be directly passed to the ot.sinkhorn solver. |
| 1111 |
Such as `numItermax` and `stopThr` to control its estimation precision, |
| 1112 |
e.g [51] suggests to use `numItermax=1`. |
| 1113 |
Returns |
| 1114 |
------- |
| 1115 |
T : array-like, shape (`ns`, `nt`) |
| 1116 |
Optimal coupling between the two joint spaces |
| 1117 |
|
| 1118 |
References |
| 1119 |
---------- |
| 1120 |
.. [12] Gabriel Peyré, Marco Cuturi, and Justin Solomon, |
| 1121 |
"Gromov-Wasserstein averaging of kernel and distance matrices." |
| 1122 |
International Conference on Machine Learning (ICML). 2016. |
| 1123 |
|
| 1124 |
.. [47] Chowdhury, S., & Mémoli, F. (2019). The gromov–wasserstein |
| 1125 |
distance between networks and stable network invariants. |
| 1126 |
Information and Inference: A Journal of the IMA, 8(4), 757-787. |
| 1127 |
|
| 1128 |
.. [51] Xu, H., Luo, D., Zha, H., & Duke, L. C. (2019). Gromov-wasserstein |
| 1129 |
learning for graph matching and node embedding. In International |
| 1130 |
Conference on Machine Learning (ICML), 2019. |
| 1131 |
|
| 1132 |
.. [24] Vayer Titouan, Chapel Laetitia, Flamary Rémi, Tavenard Romain |
| 1133 |
and Courty Nicolas "Optimal Transport for structured data with |
| 1134 |
application on graphs", International Conference on Machine Learning |
| 1135 |
(ICML). 2019. |
| 1136 |
""" |
| 1137 |
if solver not in ["PGD", "PPA"]: |
| 1138 |
raise ValueError("Unknown solver '%s'. Pick one in ['PGD', 'PPA']." % solver) |
| 1139 |
|
| 1140 |
if loss_fun not in ("square_loss", "kl_loss"): |
| 1141 |
raise ValueError( |
| 1142 |
f"Unknown `loss_fun='{loss_fun}'`. Use one of: {'square_loss', 'kl_loss'}." |
| 1143 |
) |
| 1144 |
|
| 1145 |
M, C1, C2 = list_to_array(M, C1, C2) |
| 1146 |
arr = [M, C1, C2] |
| 1147 |
if p is not None: |
| 1148 |
arr.append(list_to_array(p)) |
| 1149 |
else: |
| 1150 |
p = unif(C1.shape[0], type_as=C1) |
| 1151 |
if q is not None: |
| 1152 |
arr.append(list_to_array(q)) |
| 1153 |
else: |
| 1154 |
q = unif(C2.shape[0], type_as=C2) |
| 1155 |
|
| 1156 |
if G0 is not None: |
| 1157 |
arr.append(G0) |
| 1158 |
|
| 1159 |
nx = get_backend(*arr) |
| 1160 |
|
| 1161 |
if G0 is None: |
| 1162 |
G0 = nx.outer(p, q) |
| 1163 |
|
| 1164 |
T = G0 |
| 1165 |
constC, hC1, hC2 = init_matrix(C1, C2, p, q, loss_fun, nx) |
| 1166 |
if symmetric is None: |
| 1167 |
symmetric = nx.allclose(C1, C1.T, atol=1e-10) and nx.allclose( |
| 1168 |
C2, C2.T, atol=1e-10 |
| 1169 |
) |
| 1170 |
if not symmetric: |
| 1171 |
constCt, hC1t, hC2t = init_matrix(C1.T, C2.T, p, q, loss_fun, nx) |
| 1172 |
cpt = 0 |
| 1173 |
err = 1 |
| 1174 |
|
| 1175 |
if warmstart: |
| 1176 |
|
| 1177 |
N1, N2 = C1.shape[0], C2.shape[0] |
| 1178 |
mu = nx.zeros(N1, type_as=C1) - np.log(N1) |
| 1179 |
nu = nx.zeros(N2, type_as=C2) - np.log(N2) |
| 1180 |
|
| 1181 |
if log: |
| 1182 |
log = {"err": []} |
| 1183 |
|
| 1184 |
while err > tol and cpt < max_iter: |
| 1185 |
Tprev = T |
| 1186 |
|
| 1187 |
|
| 1188 |
if symmetric: |
| 1189 |
tens = alpha * gwggrad(constC, hC1, hC2, T, nx) + (1 - alpha) * M |
| 1190 |
else: |
| 1191 |
tens = (alpha * 0.5) * ( |
| 1192 |
gwggrad(constC, hC1, hC2, T, nx) + gwggrad(constCt, hC1t, hC2t, T, nx) |
| 1193 |
) + (1 - alpha) * M |
| 1194 |
|
| 1195 |
if solver == "PPA": |
| 1196 |
tens = tens - epsilon * nx.log(T) |
| 1197 |
|
| 1198 |
if warmstart: |
| 1199 |
T, loginn = sinkhorn( |
| 1200 |
p, |
| 1201 |
q, |
| 1202 |
tens, |
| 1203 |
epsilon, |
| 1204 |
method="sinkhorn", |
| 1205 |
log=True, |
| 1206 |
warmstart=(mu, nu), |
| 1207 |
**kwargs, |
| 1208 |
) |
| 1209 |
mu = epsilon * nx.log(loginn["u"]) |
| 1210 |
nu = epsilon * nx.log(loginn["v"]) |
| 1211 |
|
| 1212 |
else: |
| 1213 |
T = sinkhorn(p, q, tens, epsilon, method="sinkhorn", **kwargs) |
| 1214 |
|
| 1215 |
if cpt % 10 == 0: |
| 1216 |
|
| 1217 |
|
| 1218 |
err = nx.norm(T - Tprev) |
| 1219 |
|
| 1220 |
if log: |
| 1221 |
log["err"].append(err) |
| 1222 |
|
| 1223 |
if verbose: |
| 1224 |
if cpt % 200 == 0: |
| 1225 |
print("{:5s}|{:12s}".format("It.", "Err") + "\n" + "-" * 19) |
| 1226 |
print("{:5d}|{:8e}|".format(cpt, err)) |
| 1227 |
|
| 1228 |
cpt += 1 |
| 1229 |
|
| 1230 |
if abs(nx.sum(T) - 1) > 1e-5: |
| 1231 |
warnings.warn( |
| 1232 |
"Solver failed to produce a transport plan. You might " |
| 1233 |
"want to increase the regularization parameter `epsilon`." |
| 1234 |
) |
| 1235 |
if log: |
| 1236 |
log["fgw_dist"] = (1 - alpha) * nx.sum(M * T) + alpha * gwloss( |
| 1237 |
constC, hC1, hC2, T, nx |
| 1238 |
) |
| 1239 |
return T, log |
| 1240 |
else: |
| 1241 |
return T |
| 1242 |
|
| 1243 |
|
| 1244 |
def entropic_fused_gromov_wasserstein2( |
| 1245 |
M, |
| 1246 |
C1, |
| 1247 |
C2, |
| 1248 |
p=None, |
| 1249 |
q=None, |
| 1250 |
loss_fun="square_loss", |
| 1251 |
epsilon=0.1, |
| 1252 |
symmetric=None, |
| 1253 |
alpha=0.5, |
| 1254 |
G0=None, |
| 1255 |
max_iter=1000, |
| 1256 |
tol=1e-9, |
| 1257 |
solver="PGD", |
| 1258 |
warmstart=False, |
| 1259 |
verbose=False, |
| 1260 |
log=False, |
| 1261 |
**kwargs, |
| 1262 |
): |
| 1263 |
r""" |
| 1264 |
Returns the Fused Gromov-Wasserstein distance between :math:`(\mathbf{C_1}, \mathbf{Y_1}, \mathbf{p})` and :math:`(\mathbf{C_2}, \mathbf{Y_2}, \mathbf{q})` |
| 1265 |
with pairwise distance matrix :math:`\mathbf{M}` between node feature matrices :math:`\mathbf{Y_1}` and :math:`\mathbf{Y_2}`, |
| 1266 |
estimated using Sinkhorn projections. |
| 1267 |
|
| 1268 |
If `solver="PGD"`, the function solves the following entropic-regularized |
| 1269 |
Fused Gromov-Wasserstein optimization problem using Projected Gradient Descent [12]: |
| 1270 |
|
| 1271 |
.. math:: |
| 1272 |
\mathbf{FGW} = \mathop{\min}_\mathbf{T} \quad (1 - \alpha) \langle \mathbf{T}, \mathbf{M} \rangle_F + |
| 1273 |
\alpha \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} - \epsilon H(\mathbf{T}) |
| 1274 |
|
| 1275 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 1276 |
|
| 1277 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 1278 |
|
| 1279 |
\mathbf{T} &\geq 0 |
| 1280 |
|
| 1281 |
Else if `solver="PPA"`, the function solves the following Fused Gromov-Wasserstein |
| 1282 |
optimization problem using Proximal Point Algorithm [51]: |
| 1283 |
|
| 1284 |
.. math:: |
| 1285 |
\mathbf{FGW} = \mathop{\min}_\mathbf{T} \quad (1 - \alpha) \langle \mathbf{T}, \mathbf{M} \rangle_F + |
| 1286 |
\alpha \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} |
| 1287 |
|
| 1288 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 1289 |
|
| 1290 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 1291 |
|
| 1292 |
\mathbf{T} &\geq 0 |
| 1293 |
Where : |
| 1294 |
|
| 1295 |
- :math:`\mathbf{M}`: metric cost matrix between features across domains |
| 1296 |
- :math:`\mathbf{C_1}`: Metric cost matrix in the source space |
| 1297 |
- :math:`\mathbf{C_2}`: Metric cost matrix in the target space |
| 1298 |
- :math:`\mathbf{p}`: distribution in the source space |
| 1299 |
- :math:`\mathbf{q}`: distribution in the target space |
| 1300 |
- `L`: loss function to account for the misfit between the similarity and feature matrices |
| 1301 |
- `H`: entropy |
| 1302 |
- :math:`\alpha`: trade-off parameter |
| 1303 |
|
| 1304 |
.. note:: If the inner solver `ot.sinkhorn` did not convergence, the |
| 1305 |
optimal coupling :math:`\mathbf{T}` returned by this function does not |
| 1306 |
necessarily satisfy the marginal constraints |
| 1307 |
:math:`\mathbf{T}\mathbf{1}=\mathbf{p}` and |
| 1308 |
:math:`\mathbf{T}^T\mathbf{1}=\mathbf{q}`. So the returned |
| 1309 |
Fused Gromov-Wasserstein loss does not necessarily satisfy distance |
| 1310 |
properties and may be negative. |
| 1311 |
|
| 1312 |
Parameters |
| 1313 |
---------- |
| 1314 |
M : array-like, shape (ns, nt) |
| 1315 |
Metric cost matrix between features across domains |
| 1316 |
C1 : array-like, shape (ns, ns) |
| 1317 |
Metric cost matrix in the source space |
| 1318 |
C2 : array-like, shape (nt, nt) |
| 1319 |
Metric cost matrix in the target space |
| 1320 |
p : array-like, shape (ns,), optional |
| 1321 |
Distribution in the source space. |
| 1322 |
If let to its default value None, uniform distribution is taken. |
| 1323 |
q : array-like, shape (nt,), optional |
| 1324 |
Distribution in the target space. |
| 1325 |
If let to its default value None, uniform distribution is taken. |
| 1326 |
loss_fun : string, optional (default='square_loss') |
| 1327 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 1328 |
epsilon : float, optional |
| 1329 |
Regularization term >0 |
| 1330 |
symmetric : bool, optional |
| 1331 |
Either C1 and C2 are to be assumed symmetric or not. |
| 1332 |
If let to its default None value, a symmetry test will be conducted. |
| 1333 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 1334 |
alpha : float, optional |
| 1335 |
Trade-off parameter (0 < alpha < 1) |
| 1336 |
G0: array-like, shape (ns,nt), optional |
| 1337 |
If None the initial transport plan of the solver is pq^T. |
| 1338 |
Otherwise G0 will be used as initial transport of the solver. G0 is not |
| 1339 |
required to satisfy marginal constraints but we strongly recommend it |
| 1340 |
to correctly estimate the GW distance. |
| 1341 |
max_iter : int, optional |
| 1342 |
Max number of iterations |
| 1343 |
tol : float, optional |
| 1344 |
Stop threshold on error (>0) |
| 1345 |
verbose : bool, optional |
| 1346 |
Print information along iterations |
| 1347 |
log : bool, optional |
| 1348 |
Record log if True. |
| 1349 |
|
| 1350 |
Returns |
| 1351 |
------- |
| 1352 |
fgw_dist : float |
| 1353 |
Fused Gromov-Wasserstein distance |
| 1354 |
|
| 1355 |
References |
| 1356 |
---------- |
| 1357 |
.. [12] Gabriel Peyré, Marco Cuturi, and Justin Solomon, |
| 1358 |
"Gromov-Wasserstein averaging of kernel and distance matrices." |
| 1359 |
International Conference on Machine Learning (ICML). 2016. |
| 1360 |
|
| 1361 |
.. [51] Xu, H., Luo, D., Zha, H., & Duke, L. C. (2019). Gromov-wasserstein |
| 1362 |
learning for graph matching and node embedding. In International |
| 1363 |
Conference on Machine Learning (ICML), 2019. |
| 1364 |
|
| 1365 |
.. [24] Vayer Titouan, Chapel Laetitia, Flamary Rémi, Tavenard Romain |
| 1366 |
and Courty Nicolas "Optimal Transport for structured data with |
| 1367 |
application on graphs", International Conference on Machine Learning |
| 1368 |
(ICML). 2019. |
| 1369 |
|
| 1370 |
""" |
| 1371 |
|
| 1372 |
nx = get_backend(M, C1, C2) |
| 1373 |
|
| 1374 |
T, logv = entropic_fused_gromov_wasserstein( |
| 1375 |
M, |
| 1376 |
C1, |
| 1377 |
C2, |
| 1378 |
p, |
| 1379 |
q, |
| 1380 |
loss_fun, |
| 1381 |
epsilon, |
| 1382 |
symmetric, |
| 1383 |
alpha, |
| 1384 |
G0, |
| 1385 |
max_iter, |
| 1386 |
tol, |
| 1387 |
solver, |
| 1388 |
warmstart, |
| 1389 |
verbose, |
| 1390 |
log=True, |
| 1391 |
**kwargs, |
| 1392 |
) |
| 1393 |
|
| 1394 |
logv["T"] = T |
| 1395 |
|
| 1396 |
lin_term = nx.sum(T * M) |
| 1397 |
logv["quad_loss"] = logv["fgw_dist"] - (1 - alpha) * lin_term |
| 1398 |
logv["lin_loss"] = lin_term * (1 - alpha) |
| 1399 |
|
| 1400 |
if log: |
| 1401 |
return logv["fgw_dist"], logv |
| 1402 |
else: |
| 1403 |
return logv["fgw_dist"] |
| 1404 |
|
| 1405 |
|
| 1406 |
def BAPG_fused_gromov_wasserstein( |
| 1407 |
M, |
| 1408 |
C1, |
| 1409 |
C2, |
| 1410 |
p=None, |
| 1411 |
q=None, |
| 1412 |
loss_fun="square_loss", |
| 1413 |
epsilon=0.1, |
| 1414 |
symmetric=None, |
| 1415 |
alpha=0.5, |
| 1416 |
G0=None, |
| 1417 |
max_iter=1000, |
| 1418 |
tol=1e-9, |
| 1419 |
marginal_loss=False, |
| 1420 |
verbose=False, |
| 1421 |
log=False, |
| 1422 |
): |
| 1423 |
r""" |
| 1424 |
Returns the Fused Gromov-Wasserstein transport between :math:`(\mathbf{C_1}, \mathbf{Y_1}, \mathbf{p})` and :math:`(\mathbf{C_2}, \mathbf{Y_2}, \mathbf{q})` |
| 1425 |
with pairwise distance matrix :math:`\mathbf{M}` between node feature matrices :math:`\mathbf{Y_1}` and :math:`\mathbf{Y_2}`, |
| 1426 |
estimated using Bregman Alternated Projected Gradient method. |
| 1427 |
|
| 1428 |
If `marginal_loss=True`, the function solves the following Fused Gromov-Wasserstein |
| 1429 |
optimization problem : |
| 1430 |
|
| 1431 |
.. math:: |
| 1432 |
\mathbf{T}^* \in\mathop{\arg\min}_\mathbf{T} \quad (1 - \alpha) \langle \mathbf{T}, \mathbf{M} \rangle_F + |
| 1433 |
\alpha \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} |
| 1434 |
|
| 1435 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 1436 |
|
| 1437 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 1438 |
|
| 1439 |
\mathbf{T} &\geq 0 |
| 1440 |
|
| 1441 |
Else, the function solves an equivalent problem [63, 64], where constant terms only |
| 1442 |
depending on the marginals :math:`\mathbf{p}`: and :math:`\mathbf{q}`: are |
| 1443 |
discarded while assuming that L decomposes as in Proposition 1 in [12]: |
| 1444 |
|
| 1445 |
.. math:: |
| 1446 |
\mathbf{T}^* \in\mathop{\arg\min}_\mathbf{T} \quad (1 - \alpha) \langle \mathbf{T}, \mathbf{M} \rangle_F - |
| 1447 |
\alpha \langle h_1(\mathbf{C}_1) \mathbf{T} h_2(\mathbf{C_2})^\top , \mathbf{T} \rangle_F |
| 1448 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 1449 |
|
| 1450 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 1451 |
|
| 1452 |
\mathbf{T} &\geq 0 |
| 1453 |
|
| 1454 |
Where : |
| 1455 |
|
| 1456 |
- :math:`\mathbf{M}`: pairwise relation matrix between features across domains |
| 1457 |
- :math:`\mathbf{C_1}`: Metric cost matrix in the source space |
| 1458 |
- :math:`\mathbf{C_2}`: Metric cost matrix in the target space |
| 1459 |
- :math:`\mathbf{p}`: distribution in the source space |
| 1460 |
- :math:`\mathbf{q}`: distribution in the target space |
| 1461 |
- `L`: loss function to account for the misfit between the similarity and feature matrices |
| 1462 |
satisfying :math:`L(a, b) = f_1(a) + f_2(b) - h_1(a) h_2(b)` |
| 1463 |
- :math:`\alpha`: trade-off parameter |
| 1464 |
|
| 1465 |
.. note:: By algorithmic design the optimal coupling :math:`\mathbf{T}` |
| 1466 |
returned by this function does not necessarily satisfy the marginal |
| 1467 |
constraints :math:`\mathbf{T}\mathbf{1}=\mathbf{p}` and |
| 1468 |
:math:`\mathbf{T}^T\mathbf{1}=\mathbf{q}`. So the returned Fused |
| 1469 |
Gromov-Wasserstein loss does not necessarily satisfy distance |
| 1470 |
properties and may be negative. |
| 1471 |
|
| 1472 |
Parameters |
| 1473 |
---------- |
| 1474 |
M : array-like, shape (ns, nt) |
| 1475 |
Pairwise relation matrix between features across domains |
| 1476 |
C1 : array-like, shape (ns, ns) |
| 1477 |
Metric cost matrix in the source space |
| 1478 |
C2 : array-like, shape (nt, nt) |
| 1479 |
Metric cost matrix in the target space |
| 1480 |
p : array-like, shape (ns,), optional |
| 1481 |
Distribution in the source space. |
| 1482 |
If let to its default value None, uniform distribution is taken. |
| 1483 |
q : array-like, shape (nt,), optional |
| 1484 |
Distribution in the target space. |
| 1485 |
If let to its default value None, uniform distribution is taken. |
| 1486 |
loss_fun : string, optional (default='square_loss') |
| 1487 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 1488 |
epsilon : float, optional |
| 1489 |
Regularization term >0 |
| 1490 |
symmetric : bool, optional |
| 1491 |
Either C1 and C2 are to be assumed symmetric or not. |
| 1492 |
If let to its default None value, a symmetry test will be conducted. |
| 1493 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 1494 |
alpha : float, optional |
| 1495 |
Trade-off parameter (0 < alpha < 1) |
| 1496 |
G0: array-like, shape (ns,nt), optional |
| 1497 |
If None the initial transport plan of the solver is pq^T. |
| 1498 |
Otherwise G0 will be used as initial transport of the solver. G0 is not |
| 1499 |
required to satisfy marginal constraints but we strongly recommend it |
| 1500 |
to correctly estimate the GW distance. |
| 1501 |
max_iter : int, optional |
| 1502 |
Max number of iterations |
| 1503 |
tol : float, optional |
| 1504 |
Stop threshold on error (>0) |
| 1505 |
marginal_loss: bool, optional. Default is False. |
| 1506 |
Include constant marginal terms or not in the objective function. |
| 1507 |
verbose : bool, optional |
| 1508 |
Print information along iterations |
| 1509 |
log : bool, optional |
| 1510 |
Record log if True. |
| 1511 |
Returns |
| 1512 |
------- |
| 1513 |
T : array-like, shape (`ns`, `nt`) |
| 1514 |
Optimal coupling between the two joint spaces |
| 1515 |
|
| 1516 |
References |
| 1517 |
---------- |
| 1518 |
.. [63] Li, J., Tang, J., Kong, L., Liu, H., Li, J., So, A. M. C., & Blanchet, J. |
| 1519 |
"A Convergent Single-Loop Algorithm for Relaxation of Gromov-Wasserstein |
| 1520 |
in Graph Data". International Conference on Learning Representations (ICLR), 2023. |
| 1521 |
|
| 1522 |
.. [64] Ma, X., Chu, X., Wang, Y., Lin, Y., Zhao, J., Ma, L., & Zhu, W. |
| 1523 |
"Fused Gromov-Wasserstein Graph Mixup for Graph-level Classifications". |
| 1524 |
In Thirty-seventh Conference on Neural Information Processing Systems. |
| 1525 |
""" |
| 1526 |
if loss_fun not in ("square_loss", "kl_loss"): |
| 1527 |
raise ValueError( |
| 1528 |
f"Unknown `loss_fun='{loss_fun}'`. Use one of: {'square_loss', 'kl_loss'}." |
| 1529 |
) |
| 1530 |
|
| 1531 |
M, C1, C2 = list_to_array(M, C1, C2) |
| 1532 |
arr = [M, C1, C2] |
| 1533 |
if p is not None: |
| 1534 |
arr.append(list_to_array(p)) |
| 1535 |
else: |
| 1536 |
p = unif(C1.shape[0], type_as=C1) |
| 1537 |
if q is not None: |
| 1538 |
arr.append(list_to_array(q)) |
| 1539 |
else: |
| 1540 |
q = unif(C2.shape[0], type_as=C2) |
| 1541 |
|
| 1542 |
if G0 is not None: |
| 1543 |
arr.append(G0) |
| 1544 |
|
| 1545 |
nx = get_backend(*arr) |
| 1546 |
|
| 1547 |
if G0 is None: |
| 1548 |
G0 = nx.outer(p, q) |
| 1549 |
|
| 1550 |
T = G0 |
| 1551 |
constC, hC1, hC2 = init_matrix(C1, C2, p, q, loss_fun, nx) |
| 1552 |
if symmetric is None: |
| 1553 |
symmetric = nx.allclose(C1, C1.T, atol=1e-10) and nx.allclose( |
| 1554 |
C2, C2.T, atol=1e-10 |
| 1555 |
) |
| 1556 |
if not symmetric: |
| 1557 |
constCt, hC1t, hC2t = init_matrix(C1.T, C2.T, p, q, loss_fun, nx) |
| 1558 |
|
| 1559 |
|
| 1560 |
if marginal_loss: |
| 1561 |
if symmetric: |
| 1562 |
|
| 1563 |
def df(T): |
| 1564 |
return alpha * gwggrad(constC, hC1, hC2, T, nx) + (1 - alpha) * M |
| 1565 |
else: |
| 1566 |
|
| 1567 |
def df(T): |
| 1568 |
return (alpha * 0.5) * ( |
| 1569 |
gwggrad(constC, hC1, hC2, T, nx) |
| 1570 |
+ gwggrad(constCt, hC1t, hC2t, T, nx) |
| 1571 |
) + (1 - alpha) * M |
| 1572 |
|
| 1573 |
else: |
| 1574 |
if symmetric: |
| 1575 |
|
| 1576 |
def df(T): |
| 1577 |
A = -nx.dot(nx.dot(hC1, T), hC2.T) |
| 1578 |
return 2 * alpha * A + (1 - alpha) * M |
| 1579 |
else: |
| 1580 |
|
| 1581 |
def df(T): |
| 1582 |
A = -nx.dot(nx.dot(hC1, T), hC2t) |
| 1583 |
At = -nx.dot(nx.dot(hC1t, T), hC2) |
| 1584 |
return alpha * (A + At) + (1 - alpha) * M |
| 1585 |
|
| 1586 |
cpt = 0 |
| 1587 |
err = 1e15 |
| 1588 |
|
| 1589 |
if log: |
| 1590 |
log = {"err": []} |
| 1591 |
|
| 1592 |
while err > tol and cpt < max_iter: |
| 1593 |
Tprev = T |
| 1594 |
|
| 1595 |
|
| 1596 |
T = T * nx.exp(-df(T) / epsilon) |
| 1597 |
row_scaling = p / nx.sum(T, 1) |
| 1598 |
T = nx.reshape(row_scaling, (-1, 1)) * T |
| 1599 |
|
| 1600 |
|
| 1601 |
T = T * nx.exp(-df(T) / epsilon) |
| 1602 |
column_scaling = q / nx.sum(T, 0) |
| 1603 |
T = nx.reshape(column_scaling, (1, -1)) * T |
| 1604 |
|
| 1605 |
if cpt % 10 == 0: |
| 1606 |
|
| 1607 |
|
| 1608 |
err = nx.norm(T - Tprev) |
| 1609 |
|
| 1610 |
if log: |
| 1611 |
log["err"].append(err) |
| 1612 |
|
| 1613 |
if verbose: |
| 1614 |
if cpt % 200 == 0: |
| 1615 |
print("{:5s}|{:12s}".format("It.", "Err") + "\n" + "-" * 19) |
| 1616 |
print("{:5d}|{:8e}|".format(cpt, err)) |
| 1617 |
|
| 1618 |
cpt += 1 |
| 1619 |
|
| 1620 |
if nx.any(nx.isnan(T)): |
| 1621 |
warnings.warn( |
| 1622 |
"Solver failed to produce a transport plan. You might " |
| 1623 |
"want to increase the regularization parameter `epsilon`.", |
| 1624 |
UserWarning, |
| 1625 |
) |
| 1626 |
if log: |
| 1627 |
log["fgw_dist"] = (1 - alpha) * nx.sum(M * T) + alpha * gwloss( |
| 1628 |
constC, hC1, hC2, T, nx |
| 1629 |
) |
| 1630 |
|
| 1631 |
if not marginal_loss: |
| 1632 |
log["loss"] = log["fgw_dist"] - alpha * nx.sum(constC * T) |
| 1633 |
|
| 1634 |
return T, log |
| 1635 |
else: |
| 1636 |
return T |
| 1637 |
|
| 1638 |
|
| 1639 |
def BAPG_fused_gromov_wasserstein2( |
| 1640 |
M, |
| 1641 |
C1, |
| 1642 |
C2, |
| 1643 |
p=None, |
| 1644 |
q=None, |
| 1645 |
loss_fun="square_loss", |
| 1646 |
epsilon=0.1, |
| 1647 |
symmetric=None, |
| 1648 |
alpha=0.5, |
| 1649 |
G0=None, |
| 1650 |
max_iter=1000, |
| 1651 |
tol=1e-9, |
| 1652 |
marginal_loss=False, |
| 1653 |
verbose=False, |
| 1654 |
log=False, |
| 1655 |
): |
| 1656 |
r""" |
| 1657 |
Returns the Fused Gromov-Wasserstein loss between :math:`(\mathbf{C_1}, \mathbf{Y_1}, \mathbf{p})` and :math:`(\mathbf{C_2}, \mathbf{Y_2}, \mathbf{q})` |
| 1658 |
with pairwise distance matrix :math:`\mathbf{M}` between node feature matrices :math:`\mathbf{Y_1}` and :math:`\mathbf{Y_2}`, |
| 1659 |
estimated using Bregman Alternated Projected Gradient method. |
| 1660 |
|
| 1661 |
If `marginal_loss=True`, the function solves the following Fused Gromov-Wasserstein |
| 1662 |
optimization problem : |
| 1663 |
|
| 1664 |
.. math:: |
| 1665 |
\mathbf{FGW} = \mathop{\min}_\mathbf{T} \quad (1 - \alpha) \langle \mathbf{T}, \mathbf{M} \rangle_F + |
| 1666 |
\alpha \sum_{i,j,k,l} L(\mathbf{C_1}_{i,k}, \mathbf{C_2}_{j,l}) \mathbf{T}_{i,j} \mathbf{T}_{k,l} |
| 1667 |
|
| 1668 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 1669 |
|
| 1670 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 1671 |
|
| 1672 |
\mathbf{T} &\geq 0 |
| 1673 |
|
| 1674 |
Else, the function solves an equivalent problem [63, 64], where constant terms only |
| 1675 |
depending on the marginals :math:`\mathbf{p}`: and :math:`\mathbf{q}`: are |
| 1676 |
discarded while assuming that L decomposes as in Proposition 1 in [12]: |
| 1677 |
|
| 1678 |
.. math:: |
| 1679 |
\mathop{\min}_\mathbf{T} \quad (1 - \alpha) \langle \mathbf{T}, \mathbf{M} \rangle_F - |
| 1680 |
\alpha \langle h_1(\mathbf{C}_1) \mathbf{T} h_2(\mathbf{C_2})^\top , \mathbf{T} \rangle_F |
| 1681 |
s.t. \ \mathbf{T} \mathbf{1} &= \mathbf{p} |
| 1682 |
|
| 1683 |
\mathbf{T}^T \mathbf{1} &= \mathbf{q} |
| 1684 |
|
| 1685 |
\mathbf{T} &\geq 0 |
| 1686 |
Where : |
| 1687 |
|
| 1688 |
- :math:`\mathbf{M}`: metric cost matrix between features across domains |
| 1689 |
- :math:`\mathbf{C_1}`: Metric cost matrix in the source space |
| 1690 |
- :math:`\mathbf{C_2}`: Metric cost matrix in the target space |
| 1691 |
- :math:`\mathbf{p}`: distribution in the source space |
| 1692 |
- :math:`\mathbf{q}`: distribution in the target space |
| 1693 |
- `L`: loss function to account for the misfit between the similarity and feature matrices |
| 1694 |
satisfying :math:`L(a, b) = f_1(a) + f_2(b) - h_1(a) h_2(b)` |
| 1695 |
- :math:`\alpha`: trade-off parameter |
| 1696 |
|
| 1697 |
.. note:: By algorithmic design the optimal coupling :math:`\mathbf{T}` |
| 1698 |
returned by this function does not necessarily satisfy the marginal |
| 1699 |
constraints :math:`\mathbf{T}\mathbf{1}=\mathbf{p}` and |
| 1700 |
:math:`\mathbf{T}^T\mathbf{1}=\mathbf{q}`. So the returned Fused |
| 1701 |
Gromov-Wasserstein loss does not necessarily satisfy distance |
| 1702 |
properties and may be negative. |
| 1703 |
|
| 1704 |
Parameters |
| 1705 |
---------- |
| 1706 |
M : array-like, shape (ns, nt) |
| 1707 |
Metric cost matrix between features across domains |
| 1708 |
C1 : array-like, shape (ns, ns) |
| 1709 |
Metric cost matrix in the source space |
| 1710 |
C2 : array-like, shape (nt, nt) |
| 1711 |
Metric cost matrix in the target space |
| 1712 |
p : array-like, shape (ns,), optional |
| 1713 |
Distribution in the source space. |
| 1714 |
If let to its default value None, uniform distribution is taken. |
| 1715 |
q : array-like, shape (nt,), optional |
| 1716 |
Distribution in the target space. |
| 1717 |
If let to its default value None, uniform distribution is taken. |
| 1718 |
loss_fun : string, optional (default='square_loss') |
| 1719 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 1720 |
epsilon : float, optional |
| 1721 |
Regularization term >0 |
| 1722 |
symmetric : bool, optional |
| 1723 |
Either C1 and C2 are to be assumed symmetric or not. |
| 1724 |
If let to its default None value, a symmetry test will be conducted. |
| 1725 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 1726 |
alpha : float, optional |
| 1727 |
Trade-off parameter (0 < alpha < 1) |
| 1728 |
G0: array-like, shape (ns,nt), optional |
| 1729 |
If None the initial transport plan of the solver is pq^T. |
| 1730 |
Otherwise G0 will be used as initial transport of the solver. G0 is not |
| 1731 |
required to satisfy marginal constraints but we strongly recommend it |
| 1732 |
to correctly estimate the GW distance. |
| 1733 |
max_iter : int, optional |
| 1734 |
Max number of iterations |
| 1735 |
tol : float, optional |
| 1736 |
Stop threshold on error (>0) |
| 1737 |
marginal_loss: bool, optional. Default is False. |
| 1738 |
Include constant marginal terms or not in the objective function. |
| 1739 |
verbose : bool, optional |
| 1740 |
Print information along iterations |
| 1741 |
log : bool, optional |
| 1742 |
Record log if True. |
| 1743 |
Returns |
| 1744 |
------- |
| 1745 |
T : array-like, shape (`ns`, `nt`) |
| 1746 |
Optimal coupling between the two joint spaces |
| 1747 |
|
| 1748 |
References |
| 1749 |
---------- |
| 1750 |
.. [63] Li, J., Tang, J., Kong, L., Liu, H., Li, J., So, A. M. C., & Blanchet, J. |
| 1751 |
"A Convergent Single-Loop Algorithm for Relaxation of Gromov-Wasserstein |
| 1752 |
in Graph Data". International Conference on Learning Representations (ICLR), 2023. |
| 1753 |
|
| 1754 |
.. [64] Ma, X., Chu, X., Wang, Y., Lin, Y., Zhao, J., Ma, L., & Zhu, W. |
| 1755 |
"Fused Gromov-Wasserstein Graph Mixup for Graph-level Classifications". |
| 1756 |
In Thirty-seventh Conference on Neural Information Processing Systems. |
| 1757 |
""" |
| 1758 |
nx = get_backend(M, C1, C2) |
| 1759 |
|
| 1760 |
T, logv = BAPG_fused_gromov_wasserstein( |
| 1761 |
M, |
| 1762 |
C1, |
| 1763 |
C2, |
| 1764 |
p, |
| 1765 |
q, |
| 1766 |
loss_fun, |
| 1767 |
epsilon, |
| 1768 |
symmetric, |
| 1769 |
alpha, |
| 1770 |
G0, |
| 1771 |
max_iter, |
| 1772 |
tol, |
| 1773 |
marginal_loss, |
| 1774 |
verbose, |
| 1775 |
log=True, |
| 1776 |
) |
| 1777 |
|
| 1778 |
logv["T"] = T |
| 1779 |
|
| 1780 |
lin_term = nx.sum(T * M) |
| 1781 |
logv["quad_loss"] = logv["fgw_dist"] - (1 - alpha) * lin_term |
| 1782 |
logv["lin_loss"] = lin_term * (1 - alpha) |
| 1783 |
|
| 1784 |
if log: |
| 1785 |
return logv["fgw_dist"], logv |
| 1786 |
else: |
| 1787 |
return logv["fgw_dist"] |
| 1788 |
|
| 1789 |
|
| 1790 |
def entropic_fused_gromov_barycenters( |
| 1791 |
N, |
| 1792 |
Ys, |
| 1793 |
Cs, |
| 1794 |
ps=None, |
| 1795 |
p=None, |
| 1796 |
lambdas=None, |
| 1797 |
loss_fun="square_loss", |
| 1798 |
epsilon=0.1, |
| 1799 |
symmetric=True, |
| 1800 |
alpha=0.5, |
| 1801 |
max_iter=1000, |
| 1802 |
tol=1e-9, |
| 1803 |
stop_criterion="barycenter", |
| 1804 |
warmstartT=False, |
| 1805 |
verbose=False, |
| 1806 |
log=False, |
| 1807 |
init_C=None, |
| 1808 |
init_Y=None, |
| 1809 |
fixed_structure=False, |
| 1810 |
fixed_features=False, |
| 1811 |
random_state=None, |
| 1812 |
**kwargs, |
| 1813 |
): |
| 1814 |
r""" |
| 1815 |
Returns the Fused Gromov-Wasserstein barycenters of `S` measurable networks with node features :math:`(\mathbf{C}_s, \mathbf{Y}_s, \mathbf{p}_s)_{1 \leq s \leq S}` |
| 1816 |
estimated using Fused Gromov-Wasserstein transports from Sinkhorn projections. |
| 1817 |
|
| 1818 |
The function solves the following optimization problem: |
| 1819 |
|
| 1820 |
.. math:: |
| 1821 |
|
| 1822 |
\mathbf{C}^*, \mathbf{Y}^* = \mathop{\arg \min}_{\mathbf{C}\in \mathbb{R}^{N \times N}, \mathbf{Y}\in \mathbb{Y}^{N \times d}} \quad \sum_s \lambda_s \mathrm{FGW}_{\alpha}(\mathbf{C}, \mathbf{C}_s, \mathbf{Y}, \mathbf{Y}_s, \mathbf{p}, \mathbf{p}_s) |
| 1823 |
|
| 1824 |
Where : |
| 1825 |
|
| 1826 |
- :math:`\mathbf{Y}_s`: feature matrix |
| 1827 |
- :math:`\mathbf{C}_s`: metric cost matrix |
| 1828 |
- :math:`\mathbf{p}_s`: distribution |
| 1829 |
|
| 1830 |
Parameters |
| 1831 |
---------- |
| 1832 |
N : int |
| 1833 |
Size of the targeted barycenter |
| 1834 |
Ys: list of array-like, each element has shape (ns,d) |
| 1835 |
Features of all samples |
| 1836 |
Cs : list of S array-like of shape (ns,ns) |
| 1837 |
Metric cost matrices |
| 1838 |
ps : list of S array-like of shape (ns,), optional |
| 1839 |
Sample weights in the `S` spaces. |
| 1840 |
If let to its default value None, uniform distributions are taken. |
| 1841 |
p : array-like, shape (N,), optional |
| 1842 |
Weights in the targeted barycenter. |
| 1843 |
If let to its default value None, uniform distribution is taken. |
| 1844 |
lambdas : list of float, optional |
| 1845 |
List of the `S` spaces' weights. |
| 1846 |
If let to its default value None, uniform weights are taken. |
| 1847 |
loss_fun : string, optional (default='square_loss') |
| 1848 |
Loss function used for the solver either 'square_loss' or 'kl_loss' |
| 1849 |
epsilon : float, optional |
| 1850 |
Regularization term >0 |
| 1851 |
symmetric : bool, optional. |
| 1852 |
Either structures are to be assumed symmetric or not. Default value is True. |
| 1853 |
Else if set to True (resp. False), C1 and C2 will be assumed symmetric (resp. asymmetric). |
| 1854 |
alpha : float, optional |
| 1855 |
Trade-off parameter (0 < alpha < 1) |
| 1856 |
max_iter : int, optional |
| 1857 |
Max number of iterations |
| 1858 |
tol : float, optional |
| 1859 |
Stop threshold on error (>0) |
| 1860 |
stop_criterion : str, optional. Default is 'barycenter'. |
| 1861 |
Stop criterion taking values in ['barycenter', 'loss']. If set to 'barycenter' |
| 1862 |
uses absolute norm variations of estimated barycenters. Else if set to 'loss' |
| 1863 |
uses the relative variations of the loss. |
| 1864 |
warmstartT: bool, optional |
| 1865 |
Either to perform warmstart of transport plans in the successive |
| 1866 |
fused gromov-wasserstein transport problems. |
| 1867 |
verbose : bool, optional |
| 1868 |
Print information along iterations. |
| 1869 |
log : bool, optional |
| 1870 |
Record log if True. |
| 1871 |
init_C : bool | array-like, shape (N, N) |
| 1872 |
Random initial value for the :math:`\mathbf{C}` matrix provided by user. |
| 1873 |
init_Y : array-like, shape (N,d), optional |
| 1874 |
Initialization for the barycenters' features. If not set a |
| 1875 |
random init is used. |
| 1876 |
fixed_structure : bool, optional |
| 1877 |
Whether to fix the structure of the barycenter during the updates. |
| 1878 |
fixed_features : bool, optional |
| 1879 |
Whether to fix the feature of the barycenter during the updates |
| 1880 |
random_state : int or RandomState instance, optional |
| 1881 |
Fix the seed for reproducibility |
| 1882 |
**kwargs: dict |
| 1883 |
parameters can be directly passed to the `ot.entropic_fused_gromov_wasserstein` solver. |
| 1884 |
|
| 1885 |
Returns |
| 1886 |
------- |
| 1887 |
Y : array-like, shape (`N`, `d`) |
| 1888 |
Feature matrix in the barycenter space (permutated arbitrarily) |
| 1889 |
C : array-like, shape (`N`, `N`) |
| 1890 |
Similarity matrix in the barycenter space (permutated as Y's rows) |
| 1891 |
log : dict |
| 1892 |
Only returned when log=True. It contains the keys: |
| 1893 |
|
| 1894 |
- :math:`\mathbf{T}`: list of (`N`, `ns`) transport matrices |
| 1895 |
- :math:`\mathbf{p}`: (`N`,) barycenter weights |
| 1896 |
- :math:`(\mathbf{M}_s)_s`: all distance matrices between the feature of the barycenter and the other features :math:`(dist(\mathbf{X}, \mathbf{Y}_s))_s` shape (`N`, `ns`) |
| 1897 |
- values used in convergence evaluation. |
| 1898 |
|
| 1899 |
References |
| 1900 |
---------- |
| 1901 |
.. [12] Gabriel Peyré, Marco Cuturi, and Justin Solomon, |
| 1902 |
"Gromov-Wasserstein averaging of kernel and distance matrices." |
| 1903 |
International Conference on Machine Learning (ICML). 2016. |
| 1904 |
|
| 1905 |
.. [24] Vayer Titouan, Chapel Laetitia, Flamary Rémi, Tavenard Romain |
| 1906 |
and Courty Nicolas |
| 1907 |
"Optimal Transport for structured data with application on graphs" |
| 1908 |
International Conference on Machine Learning (ICML). 2019. |
| 1909 |
""" |
| 1910 |
if loss_fun not in ("square_loss", "kl_loss"): |
| 1911 |
raise ValueError( |
| 1912 |
f"Unknown `loss_fun='{loss_fun}'`. Use one of: {'square_loss', 'kl_loss'}." |
| 1913 |
) |
| 1914 |
|
| 1915 |
if stop_criterion not in ["barycenter", "loss"]: |
| 1916 |
raise ValueError( |
| 1917 |
f"Unknown `stop_criterion='{stop_criterion}'`. Use one of: {'barycenter', 'loss'}." |
| 1918 |
) |
| 1919 |
|
| 1920 |
if isinstance(Cs[0], list) or isinstance(Ys[0], list): |
| 1921 |
raise ValueError( |
| 1922 |
"Deprecated feature in POT 0.9.4: structures Cs[i] and/or features Ys[i] are lists and should be arrays from a supported backend (e.g numpy)." |
| 1923 |
) |
| 1924 |
|
| 1925 |
arr = [*Cs, *Ys] |
| 1926 |
if ps is not None: |
| 1927 |
if isinstance(ps[0], list): |
| 1928 |
raise ValueError( |
| 1929 |
"Deprecated feature in POT 0.9.4: weights ps[i] are lists and should be arrays from a supported backend (e.g numpy)." |
| 1930 |
) |
| 1931 |
|
| 1932 |
arr += [*ps] |
| 1933 |
else: |
| 1934 |
ps = [unif(C.shape[0], type_as=C) for C in Cs] |
| 1935 |
if p is not None: |
| 1936 |
arr.append(list_to_array(p)) |
| 1937 |
else: |
| 1938 |
p = unif(N, type_as=Cs[0]) |
| 1939 |
|
| 1940 |
nx = get_backend(*arr) |
| 1941 |
S = len(Cs) |
| 1942 |
if lambdas is None: |
| 1943 |
lambdas = [1.0 / S] * S |
| 1944 |
|
| 1945 |
d = Ys[0].shape[1] |
| 1946 |
|
| 1947 |
|
| 1948 |
if fixed_structure: |
| 1949 |
if init_C is None: |
| 1950 |
raise UndefinedParameter("If C is fixed it must be provided in init_C") |
| 1951 |
else: |
| 1952 |
C = init_C |
| 1953 |
else: |
| 1954 |
if init_C is None: |
| 1955 |
rng = check_random_state(random_state) |
| 1956 |
xalea = rng.randn(N, 2) |
| 1957 |
C = dist(xalea, xalea) |
| 1958 |
C = nx.from_numpy(C, type_as=ps[0]) |
| 1959 |
else: |
| 1960 |
C = init_C |
| 1961 |
|
| 1962 |
|
| 1963 |
if fixed_features: |
| 1964 |
if init_Y is None: |
| 1965 |
raise UndefinedParameter("If Y is fixed it must be provided in init_Y") |
| 1966 |
else: |
| 1967 |
Y = init_Y |
| 1968 |
else: |
| 1969 |
if init_Y is None: |
| 1970 |
Y = nx.zeros((N, d), type_as=ps[0]) |
| 1971 |
|
| 1972 |
else: |
| 1973 |
Y = init_Y |
| 1974 |
|
| 1975 |
Ms = [dist(Y, Ys[s]) for s in range(len(Ys))] |
| 1976 |
|
| 1977 |
if warmstartT: |
| 1978 |
T = [None] * S |
| 1979 |
|
| 1980 |
if stop_criterion == "barycenter": |
| 1981 |
inner_log = False |
| 1982 |
|
| 1983 |
else: |
| 1984 |
inner_log = True |
| 1985 |
curr_loss = 1e15 |
| 1986 |
|
| 1987 |
if log: |
| 1988 |
log_ = {} |
| 1989 |
if stop_criterion == "barycenter": |
| 1990 |
log_["err_feature"] = [] |
| 1991 |
log_["err_structure"] = [] |
| 1992 |
log_["Ts_iter"] = [] |
| 1993 |
else: |
| 1994 |
log_["loss"] = [] |
| 1995 |
log_["err_rel_loss"] = [] |
| 1996 |
|
| 1997 |
for cpt in range(max_iter): |
| 1998 |
if stop_criterion == "barycenter": |
| 1999 |
Cprev = C |
| 2000 |
Yprev = Y |
| 2001 |
else: |
| 2002 |
prev_loss = curr_loss |
| 2003 |
|
| 2004 |
|
| 2005 |
if warmstartT: |
| 2006 |
res = [ |
| 2007 |
entropic_fused_gromov_wasserstein( |
| 2008 |
Ms[s], |
| 2009 |
C, |
| 2010 |
Cs[s], |
| 2011 |
p, |
| 2012 |
ps[s], |
| 2013 |
loss_fun, |
| 2014 |
epsilon, |
| 2015 |
symmetric, |
| 2016 |
alpha, |
| 2017 |
T[s], |
| 2018 |
max_iter, |
| 2019 |
1e-4, |
| 2020 |
verbose=verbose, |
| 2021 |
log=inner_log, |
| 2022 |
**kwargs, |
| 2023 |
) |
| 2024 |
for s in range(S) |
| 2025 |
] |
| 2026 |
|
| 2027 |
else: |
| 2028 |
res = [ |
| 2029 |
entropic_fused_gromov_wasserstein( |
| 2030 |
Ms[s], |
| 2031 |
C, |
| 2032 |
Cs[s], |
| 2033 |
p, |
| 2034 |
ps[s], |
| 2035 |
loss_fun, |
| 2036 |
epsilon, |
| 2037 |
symmetric, |
| 2038 |
alpha, |
| 2039 |
None, |
| 2040 |
max_iter, |
| 2041 |
1e-4, |
| 2042 |
verbose=verbose, |
| 2043 |
log=inner_log, |
| 2044 |
**kwargs, |
| 2045 |
) |
| 2046 |
for s in range(S) |
| 2047 |
] |
| 2048 |
|
| 2049 |
if stop_criterion == "barycenter": |
| 2050 |
T = res |
| 2051 |
else: |
| 2052 |
T = [output[0] for output in res] |
| 2053 |
curr_loss = np.sum([output[1]["fgw_dist"] for output in res]) |
| 2054 |
|
| 2055 |
|
| 2056 |
if not fixed_features: |
| 2057 |
X = update_barycenter_feature( |
| 2058 |
T, Ys, lambdas, p, target=False, check_zeros=False, nx=nx |
| 2059 |
) |
| 2060 |
|
| 2061 |
Ms = [dist(X, Ys[s]) for s in range(len(Ys))] |
| 2062 |
|
| 2063 |
if not fixed_structure: |
| 2064 |
C = update_barycenter_structure( |
| 2065 |
T, Cs, lambdas, p, loss_fun, target=False, check_zeros=False, nx=nx |
| 2066 |
) |
| 2067 |
|
| 2068 |
|
| 2069 |
if stop_criterion == "barycenter": |
| 2070 |
err_feature, err_structure = 0.0, 0.0 |
| 2071 |
if not fixed_features: |
| 2072 |
err_feature = nx.norm(Y - Yprev) |
| 2073 |
if not fixed_structure: |
| 2074 |
err_structure = nx.norm(C - Cprev) |
| 2075 |
if log: |
| 2076 |
log_["err_feature"].append(err_feature) |
| 2077 |
log_["err_structure"].append(err_structure) |
| 2078 |
log_["Ts_iter"].append(T) |
| 2079 |
|
| 2080 |
if verbose: |
| 2081 |
if cpt % 200 == 0: |
| 2082 |
print("{:5s}|{:12s}".format("It.", "Err") + "\n" + "-" * 19) |
| 2083 |
print("{:5d}|{:8e}|".format(cpt, err_structure)) |
| 2084 |
print("{:5d}|{:8e}|".format(cpt, err_feature)) |
| 2085 |
|
| 2086 |
if (err_feature <= tol) or (err_structure <= tol): |
| 2087 |
break |
| 2088 |
else: |
| 2089 |
err_rel_loss = ( |
| 2090 |
abs(curr_loss - prev_loss) / prev_loss if prev_loss != 0.0 else np.nan |
| 2091 |
) |
| 2092 |
if log: |
| 2093 |
log_["loss"].append(curr_loss) |
| 2094 |
log_["err_rel_loss"].append(err_rel_loss) |
| 2095 |
|
| 2096 |
if verbose: |
| 2097 |
if cpt % 200 == 0: |
| 2098 |
print("{:5s}|{:12s}".format("It.", "Err") + "\n" + "-" * 19) |
| 2099 |
print("{:5d}|{:8e}|".format(cpt, err_rel_loss)) |
| 2100 |
|
| 2101 |
if err_rel_loss <= tol: |
| 2102 |
break |
| 2103 |
|
| 2104 |
if log: |
| 2105 |
log_["T"] = T |
| 2106 |
log_["p"] = p |
| 2107 |
log_["Ms"] = Ms |
| 2108 |
|
| 2109 |
return Y, C, log_ |
| 2110 |
else: |
| 2111 |
return Y, C |