|
| 1 | +""" |
| 2 | +Sparsity-constrained optimal transport solvers. |
| 3 | +
|
| 4 | +Implementation of : |
| 5 | +Sparsity-Constrained Optimal Transport. |
| 6 | +Tianlin Liu, Joan Puigcerver, Mathieu Blondel. |
| 7 | +In Proc. of AISTATS 2018. |
| 8 | +https://arxiv.org/abs/1710.06276 |
| 9 | +
|
| 10 | +[50] Liu, T., Puigcerver, J., & Blondel, M. (2023). |
| 11 | +Sparsity-constrained optimal transport. |
| 12 | +Proceedings of the Eleventh International Conference on |
| 13 | +Learning Representations (ICLR). |
| 14 | +""" |
| 15 | + |
| 16 | +# Author: Tianlin Liu <t.liu@unibas.ch> |
| 17 | +# |
| 18 | +# License: MIT License |
| 19 | + |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +import ot |
| 23 | +from .backend import get_backend |
| 24 | + |
| 25 | + |
| 26 | +class SparsityConstrained(ot.smooth.Regularization): |
| 27 | + """ Squared L2 regularization with sparsity constraints """ |
| 28 | + |
| 29 | + def __init__(self, max_nz, gamma=1.0): |
| 30 | + self.max_nz = max_nz |
| 31 | + self.gamma = gamma |
| 32 | + |
| 33 | + def delta_Omega(self, X): |
| 34 | + # For each column of X, find entries that are not among the top max_nz. |
| 35 | + non_top_indices = np.argpartition( |
| 36 | + -X, self.max_nz, axis=0)[self.max_nz:] |
| 37 | + # Set these entries to -inf. |
| 38 | + X[non_top_indices, np.arange(X.shape[1])] = -np.inf |
| 39 | + max_X = np.maximum(X, 0) |
| 40 | + val = np.sum(max_X ** 2, axis=0) / (2 * self.gamma) |
| 41 | + G = max_X / self.gamma |
| 42 | + return val, G |
| 43 | + |
| 44 | + def max_Omega(self, X, b): |
| 45 | + # For each column of X, find top max_nz values and |
| 46 | + # their corresponding indices. |
| 47 | + max_nz_indices = np.argpartition( |
| 48 | + X, |
| 49 | + kth=-self.max_nz, |
| 50 | + axis=0)[-self.max_nz:] |
| 51 | + max_nz_values = X[max_nz_indices, np.arange(X.shape[1])] |
| 52 | + |
| 53 | + # Project the top max_nz values onto the simplex. |
| 54 | + G_nz_values = ot.smooth.projection_simplex( |
| 55 | + max_nz_values / (b * self.gamma), axis=0) |
| 56 | + |
| 57 | + # Put the projection of max_nz_values to their original indices |
| 58 | + # and set all other values zero. |
| 59 | + G = np.zeros_like(X) |
| 60 | + G[max_nz_indices, np.arange(X.shape[1])] = G_nz_values |
| 61 | + val = np.sum(X * G, axis=0) |
| 62 | + val -= 0.5 * self.gamma * b * np.sum(G * G, axis=0) |
| 63 | + return val, G |
| 64 | + |
| 65 | + def Omega(self, T): |
| 66 | + return 0.5 * self.gamma * np.sum(T ** 2) |
| 67 | + |
| 68 | + |
| 69 | +def sparsity_constrained_ot_dual( |
| 70 | + a, b, M, reg, max_nz, |
| 71 | + method="L-BFGS-B", stopThr=1e-9, |
| 72 | + numItermax=500, verbose=False, log=False): |
| 73 | + r""" |
| 74 | + Solve the sparsity-constrained OT problem in the dual and return the OT matrix. |
| 75 | +
|
| 76 | + The function solves the sparsity-contrained OT in dual formulation in |
| 77 | + :ref:`[50] <references-sparsity-constrained-ot-dual>`. |
| 78 | +
|
| 79 | +
|
| 80 | + Parameters |
| 81 | + ---------- |
| 82 | + a : np.ndarray (ns,) |
| 83 | + samples weights in the source domain |
| 84 | + b : np.ndarray (nt,) or np.ndarray (nt,nbb) |
| 85 | + samples in the target domain, compute sinkhorn with multiple targets |
| 86 | + and fixed :math:`\mathbf{M}` if :math:`\mathbf{b}` is a matrix |
| 87 | + (return OT loss + dual variables in log) |
| 88 | + M : np.ndarray (ns,nt) |
| 89 | + loss matrix |
| 90 | + reg : float |
| 91 | + Regularization term >0 |
| 92 | + max_nz: int |
| 93 | + Maximum number of non-zero entries permitted in each column of the |
| 94 | + optimal transport matrix. |
| 95 | + method : str |
| 96 | + Solver to use for scipy.optimize.minimize |
| 97 | + numItermax : int, optional |
| 98 | + Max number of iterations |
| 99 | + stopThr : float, optional |
| 100 | + Stop threshold on error (>0) |
| 101 | + verbose : bool, optional |
| 102 | + Print information along iterations |
| 103 | + log : bool, optional |
| 104 | + record log if True |
| 105 | +
|
| 106 | +
|
| 107 | + Returns |
| 108 | + ------- |
| 109 | + gamma : (ns, nt) ndarray |
| 110 | + Optimal transportation matrix for the given parameters |
| 111 | + log : dict |
| 112 | + log dictionary return only if log==True in parameters |
| 113 | +
|
| 114 | +
|
| 115 | + .. _references-sparsity-constrained-ot-dual: |
| 116 | + References |
| 117 | + ---------- |
| 118 | + .. [50] Liu, T., Puigcerver, J., & Blondel, M. (2023). Sparsity-constrained optimal transport. Proceedings of the Eleventh International Conference on Learning Representations (ICLR). |
| 119 | +
|
| 120 | + See Also |
| 121 | + -------- |
| 122 | + ot.lp.emd : Unregularized OT |
| 123 | + ot.sinhorn : Entropic regularized OT |
| 124 | + ot.smooth : Entropic regularized and squared l2 regularized OT |
| 125 | + ot.optim.cg : General regularized OT |
| 126 | +
|
| 127 | + """ |
| 128 | + |
| 129 | + nx = get_backend(a, b, M) |
| 130 | + max_nz = min(max_nz, M.shape[0]) |
| 131 | + regul = SparsityConstrained(gamma=reg, max_nz=max_nz) |
| 132 | + |
| 133 | + a0, b0, M0 = a, b, M |
| 134 | + |
| 135 | + # convert to humpy |
| 136 | + a, b, M = nx.to_numpy(a, b, M) |
| 137 | + |
| 138 | + # solve dual |
| 139 | + alpha, beta, res = ot.smooth.solve_dual( |
| 140 | + a, b, M, regul, |
| 141 | + max_iter=numItermax, |
| 142 | + tol=stopThr, verbose=verbose) |
| 143 | + |
| 144 | + # reconstruct transport matrix |
| 145 | + G = nx.from_numpy(ot.smooth.get_plan_from_dual(alpha, beta, M, regul), |
| 146 | + type_as=M0) |
| 147 | + |
| 148 | + if log: |
| 149 | + log = {'alpha': nx.from_numpy(alpha, type_as=a0), |
| 150 | + 'beta': nx.from_numpy(beta, type_as=b0), 'res': res} |
| 151 | + return G, log |
| 152 | + else: |
| 153 | + return G |
| 154 | + |
| 155 | + |
| 156 | +def sparsity_constrained_ot_semi_dual( |
| 157 | + a, b, M, reg, max_nz, |
| 158 | + method="L-BFGS-B", stopThr=1e-9, |
| 159 | + numItermax=500, verbose=False, log=False): |
| 160 | + r""" |
| 161 | + Solve the regularized OT problem in the semi-dual and return the OT matrix |
| 162 | +
|
| 163 | + The function solves the sparsity-contrained OT in semi-dual formulation in |
| 164 | + :ref:`[50] <references-sparsity-constrained-ot-semi-dual>`. |
| 165 | +
|
| 166 | +
|
| 167 | + Parameters |
| 168 | + ---------- |
| 169 | + a : np.ndarray (ns,) |
| 170 | + samples weights in the source domain |
| 171 | + b : np.ndarray (nt,) or np.ndarray (nt,nbb) |
| 172 | + samples in the target domain, compute sinkhorn with multiple targets |
| 173 | + and fixed:math:`\mathbf{M}` if :math:`\mathbf{b}` is a matrix |
| 174 | + (return OT loss + dual variables in log) |
| 175 | + M : np.ndarray (ns,nt) |
| 176 | + loss matrix |
| 177 | + reg : float |
| 178 | + Regularization term >0 |
| 179 | + max_nz: int |
| 180 | + Maximum number of non-zero entries permitted in each column of the optimal transport matrix. |
| 181 | + method : str |
| 182 | + Solver to use for scipy.optimize.minimize |
| 183 | + numItermax : int, optional |
| 184 | + Max number of iterations |
| 185 | + stopThr : float, optional |
| 186 | + Stop threshold on error (>0) |
| 187 | + verbose : bool, optional |
| 188 | + Print information along iterations |
| 189 | + log : bool, optional |
| 190 | + record log if True |
| 191 | +
|
| 192 | +
|
| 193 | + Returns |
| 194 | + ------- |
| 195 | + gamma : (ns, nt) ndarray |
| 196 | + Optimal transportation matrix for the given parameters |
| 197 | + log : dict |
| 198 | + log dictionary return only if log==True in parameters |
| 199 | +
|
| 200 | +
|
| 201 | + .. _references-sparsity-constrained-ot-semi-dual: |
| 202 | + References |
| 203 | + ---------- |
| 204 | + .. [50] Liu, T., Puigcerver, J., & Blondel, M. (2023). Sparsity-constrained optimal transport. Proceedings of the Eleventh International Conference on Learning Representations (ICLR). |
| 205 | +
|
| 206 | + See Also |
| 207 | + -------- |
| 208 | + ot.lp.emd : Unregularized OT |
| 209 | + ot.sinhorn : Entropic regularized OT |
| 210 | + ot.smooth : Entropic regularized and squared l2 regularized OT |
| 211 | + ot.optim.cg : General regularized OT |
| 212 | +
|
| 213 | + """ |
| 214 | + |
| 215 | + max_nz = min(max_nz, M.shape[0]) |
| 216 | + regul = SparsityConstrained(gamma=reg, max_nz=max_nz) |
| 217 | + # solve dual |
| 218 | + alpha, res = ot.smooth.solve_semi_dual( |
| 219 | + a, b, M, regul, max_iter=numItermax, |
| 220 | + tol=stopThr, verbose=verbose) |
| 221 | + |
| 222 | + # reconstruct transport matrix |
| 223 | + G = ot.smooth.get_plan_from_semi_dual(alpha, b, M, regul) |
| 224 | + |
| 225 | + if log: |
| 226 | + log = {'alpha': alpha, 'res': res} |
| 227 | + return G, log |
| 228 | + else: |
| 229 | + return G |
0 commit comments