Debian Codegraph @

ot/factored.py

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