| 1 |
""" |
| 2 |
Weak optimal ransport solvers |
| 3 |
""" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from .backend import get_backend |
| 10 |
from .optim import cg |
| 11 |
import numpy as np |
| 12 |
|
| 13 |
__all__ = ["weak_optimal_transport"] |
| 14 |
|
| 15 |
|
| 16 |
def weak_optimal_transport( |
| 17 |
Xa, Xb, a=None, b=None, verbose=False, log=False, G0=None, **kwargs |
| 18 |
): |
| 19 |
r"""Solves the weak optimal transport problem between two empirical distributions |
| 20 |
|
| 21 |
|
| 22 |
.. math:: |
| 23 |
\gamma = \mathop{\arg \min}_\gamma \quad \sum_i \mathbf{a}_i \left(\mathbf{X^a}_i - \frac{1}{\mathbf{a}_i} \sum_j \gamma_{ij} \mathbf{X^b}_j \right)^2 |
| 24 |
|
| 25 |
s.t. \ \gamma \mathbf{1} = \mathbf{a} |
| 26 |
|
| 27 |
\gamma^T \mathbf{1} = \mathbf{b} |
| 28 |
|
| 29 |
\gamma \geq 0 |
| 30 |
|
| 31 |
where : |
| 32 |
|
| 33 |
- :math:`X^a` and :math:`X^b` are the sample matrices. |
| 34 |
- :math:`\mathbf{a}` and :math:`\mathbf{b}` are the sample weights |
| 35 |
|
| 36 |
|
| 37 |
.. note:: This function is backend-compatible and will work on arrays |
| 38 |
from all compatible backends. But the algorithm uses the C++ CPU backend |
| 39 |
which can lead to copy overhead on GPU arrays. |
| 40 |
|
| 41 |
Uses the conditional gradient algorithm to solve the problem proposed |
| 42 |
in :ref:`[39] <references-weak>`. |
| 43 |
|
| 44 |
Parameters |
| 45 |
---------- |
| 46 |
Xa : (ns,d) array-like, float |
| 47 |
Source samples |
| 48 |
Xb : (nt,d) array-like, float |
| 49 |
Target samples |
| 50 |
a : (ns,) array-like, float |
| 51 |
Source histogram (uniform weight if empty list) |
| 52 |
b : (nt,) array-like, float |
| 53 |
Target histogram (uniform weight if empty list)) |
| 54 |
G0 : (ns,nt) array-like, float |
| 55 |
initial guess (default is indep joint density) |
| 56 |
numItermax : int, optional |
| 57 |
Max number of iterations |
| 58 |
numItermaxEmd : int, optional |
| 59 |
Max number of iterations for emd |
| 60 |
stopThr : float, optional |
| 61 |
Stop threshold on the relative variation (>0) |
| 62 |
stopThr2 : float, optional |
| 63 |
Stop threshold on the absolute variation (>0) |
| 64 |
verbose : bool, optional |
| 65 |
Print information along iterations |
| 66 |
log : bool, optional |
| 67 |
record log if True |
| 68 |
|
| 69 |
|
| 70 |
Returns |
| 71 |
------- |
| 72 |
gamma: array-like, shape (ns, nt) |
| 73 |
Optimal transportation matrix for the given |
| 74 |
parameters |
| 75 |
log: dict, optional |
| 76 |
If input log is true, a dictionary containing the |
| 77 |
cost and dual variables and exit status |
| 78 |
|
| 79 |
|
| 80 |
.. _references-weak: |
| 81 |
References |
| 82 |
---------- |
| 83 |
.. [39] Gozlan, N., Roberto, C., Samson, P. M., & Tetali, P. (2017). |
| 84 |
Kantorovich duality for general transport costs and applications. |
| 85 |
Journal of Functional Analysis, 273(11), 3327-3405. |
| 86 |
|
| 87 |
See Also |
| 88 |
-------- |
| 89 |
ot.bregman.sinkhorn : Entropic regularized OT |
| 90 |
ot.optim.cg : General regularized OT |
| 91 |
""" |
| 92 |
|
| 93 |
nx = get_backend(Xa, Xb) |
| 94 |
|
| 95 |
Xa2 = nx.to_numpy(Xa) |
| 96 |
Xb2 = nx.to_numpy(Xb) |
| 97 |
|
| 98 |
if a is None: |
| 99 |
a2 = np.ones((Xa.shape[0])) / Xa.shape[0] |
| 100 |
else: |
| 101 |
a2 = nx.to_numpy(a) |
| 102 |
if b is None: |
| 103 |
b2 = np.ones((Xb.shape[0])) / Xb.shape[0] |
| 104 |
else: |
| 105 |
b2 = nx.to_numpy(b) |
| 106 |
|
| 107 |
|
| 108 |
if G0 is None: |
| 109 |
T0 = a2[:, None] * b2[None, :] |
| 110 |
else: |
| 111 |
T0 = nx.to_numpy(G0) |
| 112 |
|
| 113 |
|
| 114 |
def f(T): |
| 115 |
return np.dot(a2, np.sum((Xa2 - np.dot(T, Xb2) / a2[:, None]) ** 2, 1)) |
| 116 |
|
| 117 |
|
| 118 |
def df(T): |
| 119 |
return -2 * np.dot(Xa2 - np.dot(T, Xb2) / a2[:, None], Xb2.T) |
| 120 |
|
| 121 |
|
| 122 |
if log: |
| 123 |
res, log = cg(a2, b2, 0, 1, f, df, T0, log=log, verbose=verbose, **kwargs) |
| 124 |
log["u"] = nx.from_numpy(log["u"], type_as=Xa) |
| 125 |
log["v"] = nx.from_numpy(log["v"], type_as=Xb) |
| 126 |
return nx.from_numpy(res, type_as=Xa), log |
| 127 |
else: |
| 128 |
return nx.from_numpy( |
| 129 |
cg(a2, b2, 0, 1, f, df, T0, log=log, verbose=verbose, **kwargs), type_as=Xa |
| 130 |
) |