r"""Solves the Earth Movers distance problem and returns the loss .. math:: \min_\gamma \quad \langle \gamma, \mathbf{M} \rangle_F s.t. \ \gamma \mathbf{1} = \mathbf{a} \gamma^T \mathbf{1} = \mathbf{b} \gamma \geq 0 where : - :math:`\mathbf
(
a,
b,
M,
processes=1,
numItermax=100000,
log=False,
return_matrix=False,
center_dual=True,
numThreads=1,
check_marginals=True,
)
| 350 | |
| 351 | |
| 352 | def emd2( |
| 353 | a, |
| 354 | b, |
| 355 | M, |
| 356 | processes=1, |
| 357 | numItermax=100000, |
| 358 | log=False, |
| 359 | return_matrix=False, |
| 360 | center_dual=True, |
| 361 | numThreads=1, |
| 362 | check_marginals=True, |
| 363 | ): |
| 364 | r"""Solves the Earth Movers distance problem and returns the loss |
| 365 | |
| 366 | .. math:: |
| 367 | \min_\gamma \quad \langle \gamma, \mathbf{M} \rangle_F |
| 368 | |
| 369 | s.t. \ \gamma \mathbf{1} = \mathbf{a} |
| 370 | |
| 371 | \gamma^T \mathbf{1} = \mathbf{b} |
| 372 | |
| 373 | \gamma \geq 0 |
| 374 | |
| 375 | where : |
| 376 | |
| 377 | - :math:`\mathbf{M}` is the metric cost matrix |
| 378 | - :math:`\mathbf{a}` and :math:`\mathbf{b}` are the sample weights |
| 379 | |
| 380 | .. note:: This function is backend-compatible and will work on arrays |
| 381 | from all compatible backends. But the algorithm uses the C++ CPU backend |
| 382 | which can lead to copy overhead on GPU arrays. |
| 383 | |
| 384 | .. note:: This function will cast the computed transport plan and |
| 385 | transportation loss to the data type of the provided input with the |
| 386 | following priority: :math:`\mathbf{a}`, then :math:`\mathbf{b}`, |
| 387 | then :math:`\mathbf{M}` if marginals are not provided. |
| 388 | Casting to an integer tensor might result in a loss of precision. |
| 389 | If this behaviour is unwanted, please make sure to provide a |
| 390 | floating point input. |
| 391 | |
| 392 | .. note:: An error will be raised if the vectors :math:`\mathbf{a}` and :math:`\mathbf{b}` do not sum to the same value. |
| 393 | |
| 394 | Uses the algorithm proposed in :ref:`[1] <references-emd2>`. |
| 395 | |
| 396 | Parameters |
| 397 | ---------- |
| 398 | a : (ns,) array-like, float64 |
| 399 | Source histogram (uniform weight if empty list) |
| 400 | b : (nt,) array-like, float64 |
| 401 | Target histogram (uniform weight if empty list) |
| 402 | M : (ns,nt) array-like, float64 |
| 403 | Loss matrix (for numpy c-order array with type float64) |
| 404 | processes : int, optional (default=1) |
| 405 | Nb of processes used for multiple emd computation (deprecated) |
| 406 | numItermax : int, optional (default=100000) |
| 407 | The maximum number of iterations before stopping the optimization |
| 408 | algorithm if it has not converged. |
| 409 | log: boolean, optional (default=False) |
no test coverage detected