MCPcopy Create free account
hub / github.com/PythonOT/POT / batch_grad_dual

Function batch_grad_dual

ot/stochastic.py:378–460  ·  view source on GitHub ↗

r""" Computes the partial gradient of the dual optimal transport problem. For each :math:`(i,j)` in a batch of coordinates, the partial gradients are : .. math:: \partial_{\mathbf{u}_i} F = \frac{b_s}{l_v} \mathbf{u}_i - \sum_{j \in B_v} \mathbf{a}_i \mathbf{b}_j

(a, b, M, reg, alpha, beta, batch_size, batch_alpha, batch_beta)

Source from the content-addressed store, hash-verified

376
377
378def batch_grad_dual(a, b, M, reg, alpha, beta, batch_size, batch_alpha, batch_beta):
379 r"""
380 Computes the partial gradient of the dual optimal transport problem.
381
382 For each :math:`(i,j)` in a batch of coordinates, the partial gradients are :
383
384 .. math::
385 \partial_{\mathbf{u}_i} F = \frac{b_s}{l_v} \mathbf{u}_i -
386 \sum_{j \in B_v} \mathbf{a}_i \mathbf{b}_j
387 \exp\left( \frac{\mathbf{u}_i + \mathbf{v}_j - \mathbf{M}_{i,j}}{\mathrm{reg}} \right)
388
389 \partial_{\mathbf{v}_j} F = \frac{b_s}{l_u} \mathbf{v}_j -
390 \sum_{i \in B_u} \mathbf{a}_i \mathbf{b}_j
391 \exp\left( \frac{\mathbf{u}_i + \mathbf{v}_j - \mathbf{M}_{i,j}}{\mathrm{reg}} \right)
392
393 Where :
394
395 - :math:`\mathbf{M}` is the (`ns`, `nt`) metric cost matrix
396 - :math:`\mathbf{u}`, :math:`\mathbf{v}` are dual variables in :math:`\mathbb{R}^{ns} \times \mathbb{R}^{nt}`
397 - reg is the regularization term
398 - :math:`B_u` and :math:`B_v` are lists of index
399 - :math:`b_s` is the size of the batches :math:`B_u` and :math:`B_v`
400 - :math:`l_u` and :math:`l_v` are the lengths of :math:`B_u` and :math:`B_v`
401 - :math:`\mathbf{a}` and :math:`\mathbf{b}` are source and target weights (sum to 1)
402
403
404 The algorithm used for solving the dual problem is the SGD algorithm
405 as proposed in :ref:`[19] <references-batch-grad-dual>` [alg.1]
406
407
408 Parameters
409 ----------
410 a : ndarray, shape (ns,)
411 source measure
412 b : ndarray, shape (nt,)
413 target measure
414 M : ndarray, shape (ns, nt)
415 cost matrix
416 reg : float
417 Regularization term > 0
418 alpha : ndarray, shape (ns,)
419 dual variable
420 beta : ndarray, shape (nt,)
421 dual variable
422 batch_size : int
423 size of the batch
424 batch_alpha : ndarray, shape (bs,)
425 batch of index of alpha
426 batch_beta : ndarray, shape (bs,)
427 batch of index of beta
428
429 Returns
430 -------
431 grad : ndarray, shape (`ns`,)
432 partial grad F
433
434
435 .. _references-batch-grad-dual:

Callers 1

Calls 3

expMethod · 0.45
zerosMethod · 0.45
sumMethod · 0.45

Tested by

no test coverage detected