|
| 1 | +""" |
| 2 | +Factored OT solvers (low rank, cost or OT plan) |
| 3 | +""" |
| 4 | + |
| 5 | +# Author: Remi Flamary <remi.flamary@polytehnique.edu> |
| 6 | +# |
| 7 | +# License: MIT License |
| 8 | + |
| 9 | +from .backend import get_backend |
| 10 | +from .utils import dist |
| 11 | +from .lp import emd |
| 12 | +from .bregman import sinkhorn |
| 13 | + |
| 14 | +__all__ = ['factored_optimal_transport'] |
| 15 | + |
| 16 | + |
| 17 | +def factored_optimal_transport(Xa, Xb, a=None, b=None, reg=0.0, r=100, X0=None, stopThr=1e-7, numItermax=100, verbose=False, log=False, **kwargs): |
| 18 | + r"""Solves factored OT problem and return OT plans and intermediate distribution |
| 19 | +
|
| 20 | + This function solve the following OT problem [40]_ |
| 21 | +
|
| 22 | + .. math:: |
| 23 | + \mathop{\arg \min}_\mu \quad W_2^2(\mu_a,\mu)+ W_2^2(\mu,\mu_b) |
| 24 | +
|
| 25 | + where : |
| 26 | +
|
| 27 | + - :math:`\mu_a` and :math:`\mu_b` are empirical distributions. |
| 28 | + - :math:`\mu` is an empirical distribution with r samples |
| 29 | +
|
| 30 | + And returns the two OT plans between |
| 31 | +
|
| 32 | + .. note:: This function is backend-compatible and will work on arrays |
| 33 | + from all compatible backends. But the algorithm uses the C++ CPU backend |
| 34 | + which can lead to copy overhead on GPU arrays. |
| 35 | +
|
| 36 | + Uses the conditional gradient algorithm to solve the problem proposed in |
| 37 | + :ref:`[39] <references-weak>`. |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + Xa : (ns,d) array-like, float |
| 42 | + Source samples |
| 43 | + Xb : (nt,d) array-like, float |
| 44 | + Target samples |
| 45 | + a : (ns,) array-like, float |
| 46 | + Source histogram (uniform weight if empty list) |
| 47 | + b : (nt,) array-like, float |
| 48 | + Target histogram (uniform weight if empty list)) |
| 49 | + numItermax : int, optional |
| 50 | + Max number of iterations |
| 51 | + stopThr : float, optional |
| 52 | + Stop threshold on the relative variation (>0) |
| 53 | + verbose : bool, optional |
| 54 | + Print information along iterations |
| 55 | + log : bool, optional |
| 56 | + record log if True |
| 57 | +
|
| 58 | +
|
| 59 | + Returns |
| 60 | + ------- |
| 61 | + Ga: array-like, shape (ns, r) |
| 62 | + Optimal transportation matrix between source and the intermediate |
| 63 | + distribution |
| 64 | + Gb: array-like, shape (r, nt) |
| 65 | + Optimal transportation matrix between the intermediate and target |
| 66 | + distribution |
| 67 | + X: array-like, shape (r, d) |
| 68 | + Support of the intermediate distribution |
| 69 | + log: dict, optional |
| 70 | + If input log is true, a dictionary containing the cost and dual |
| 71 | + variables and exit status |
| 72 | +
|
| 73 | +
|
| 74 | + .. _references-factored: |
| 75 | + References |
| 76 | + ---------- |
| 77 | + .. [40] Forrow, A., Hütter, J. C., Nitzan, M., Rigollet, P., Schiebinger, |
| 78 | + G., & Weed, J. (2019, April). Statistical optimal transport via factored |
| 79 | + couplings. In The 22nd International Conference on Artificial |
| 80 | + Intelligence and Statistics (pp. 2454-2465). PMLR. |
| 81 | +
|
| 82 | + See Also |
| 83 | + -------- |
| 84 | + ot.bregman.sinkhorn : Entropic regularized OT ot.optim.cg : General |
| 85 | + regularized OT |
| 86 | + """ |
| 87 | + |
| 88 | + nx = get_backend(Xa, Xb) |
| 89 | + |
| 90 | + n_a = Xa.shape[0] |
| 91 | + n_b = Xb.shape[0] |
| 92 | + d = Xa.shape[1] |
| 93 | + |
| 94 | + if a is None: |
| 95 | + a = nx.ones((n_a), type_as=Xa) / n_a |
| 96 | + if b is None: |
| 97 | + b = nx.ones((n_b), type_as=Xb) / n_b |
| 98 | + |
| 99 | + if X0 is None: |
| 100 | + X = nx.randn(r, d, type_as=Xa) |
| 101 | + else: |
| 102 | + X = X0 |
| 103 | + |
| 104 | + w = nx.ones(r, type_as=Xa) / r |
| 105 | + |
| 106 | + def solve_ot(X1, X2, w1, w2): |
| 107 | + M = dist(X1, X2) |
| 108 | + if reg > 0: |
| 109 | + G, log = sinkhorn(w1, w2, M, reg, log=True, **kwargs) |
| 110 | + log['cost'] = nx.sum(G * M) |
| 111 | + return G, log |
| 112 | + else: |
| 113 | + return emd(w1, w2, M, log=True, **kwargs) |
| 114 | + |
| 115 | + norm_delta = [] |
| 116 | + |
| 117 | + # solve the barycenter |
| 118 | + for i in range(numItermax): |
| 119 | + |
| 120 | + old_X = X |
| 121 | + |
| 122 | + # solve OT with template |
| 123 | + Ga, loga = solve_ot(Xa, X, a, w) |
| 124 | + Gb, logb = solve_ot(X, Xb, w, b) |
| 125 | + |
| 126 | + X = 0.5 * (nx.dot(Ga.T, Xa) + nx.dot(Gb, Xb)) * r |
| 127 | + |
| 128 | + delta = nx.norm(X - old_X) |
| 129 | + if delta < stopThr: |
| 130 | + break |
| 131 | + if log: |
| 132 | + norm_delta.append(delta) |
| 133 | + |
| 134 | + if log: |
| 135 | + log_dic = {'delta_iter': norm_delta, |
| 136 | + 'ua': loga['u'], |
| 137 | + 'va': loga['v'], |
| 138 | + 'ub': logb['u'], |
| 139 | + 'vb': logb['v'], |
| 140 | + 'costa': loga['cost'], |
| 141 | + 'costb': logb['cost'], |
| 142 | + } |
| 143 | + return Ga, Gb, X, log_dic |
| 144 | + |
| 145 | + return Ga, Gb, X |
0 commit comments