r""" Apply Bregman projection to a batch of affinity matrices :math:`\exp(\mathbf{K})`. The function solves the following optimization problem: .. math:: \begin{aligned} \mathbf{T} = \mathop{\arg \min}_\mathbf{T} \quad & \text{KL}(\mathbf{T} \| \exp(\mathbf{K})) \\
(
K, a=None, b=None, nx=None, max_iter=10000, tol=1e-5, grad="detach"
)
| 163 | |
| 164 | |
| 165 | def bregman_log_projection_batch( |
| 166 | K, a=None, b=None, nx=None, max_iter=10000, tol=1e-5, grad="detach" |
| 167 | ): |
| 168 | r""" |
| 169 | Apply Bregman projection to a batch of affinity matrices :math:`\exp(\mathbf{K})`. |
| 170 | |
| 171 | The function solves the following optimization problem: |
| 172 | |
| 173 | .. math:: |
| 174 | \begin{aligned} |
| 175 | \mathbf{T} = \mathop{\arg \min}_\mathbf{T} \quad & \text{KL}(\mathbf{T} \| \exp(\mathbf{K})) \\ |
| 176 | \text{s.t.} \quad & \mathbf{T} \mathbf{1} = \mathbf{a} \\ |
| 177 | & \mathbf{T}^T \mathbf{1} = \mathbf{b} \\ |
| 178 | & \mathbf{T} \geq 0 |
| 179 | \end{aligned} |
| 180 | |
| 181 | This is equivalent to: |
| 182 | |
| 183 | .. math:: |
| 184 | \begin{aligned} |
| 185 | \mathbf{T} = \mathop{\arg \max}_\mathbf{T} \quad & \langle \mathbf{T}, \mathbf{K} \rangle_F \\ |
| 186 | \text{s.t.} \quad & \mathbf{T} \mathbf{1} = \mathbf{a} \\ |
| 187 | & \mathbf{T}^T \mathbf{1} = \mathbf{b} \\ |
| 188 | & \mathbf{T} \geq 0 |
| 189 | \end{aligned} |
| 190 | |
| 191 | The optimal solution has the form :math:`\mathbf{T} = \text{diag}(\exp(\mathbf{u})) \exp(\mathbf{K}) \text{diag}(\exp(\mathbf{v}))`, |
| 192 | where the dual variables :math:`\mathbf{u}` and :math:`\mathbf{v}` are found iteratively using: |
| 193 | |
| 194 | .. math:: |
| 195 | \mathbf{u}^{(k+1)} = \log(\mathbf{a}) - \text{LSE}(\mathbf{K} + \mathbf{v}^{(k)}) |
| 196 | |
| 197 | \mathbf{v}^{(k+1)} = \log(\mathbf{b}) - \text{LSE}(\mathbf{K}^T + \mathbf{u}^{(k)}) |
| 198 | |
| 199 | where LSE denotes the log-sum-exp operation. The iterations are performed using |
| 200 | the log-sum-exp trick to avoid numerical issues. |
| 201 | |
| 202 | Parameters |
| 203 | ---------- |
| 204 | K : array-like, shape (B, n, m) |
| 205 | Affinity matrix for each problem in the batch. |
| 206 | a : array-like, shape (B, n), optional |
| 207 | Source distribution for each problem. If None, uniform distribution is used. |
| 208 | b : array-like, shape (B, m), optional |
| 209 | Target distribution for each problem. If None, uniform distribution is used. |
| 210 | nx : backend object, optional |
| 211 | Numerical backend to use for computations. If None, the default backend is used. |
| 212 | max_iter : int, optional |
| 213 | Maximum number of iterations. |
| 214 | tol : float, optional |
| 215 | Tolerance for convergence. The solver stops when the maximum change in |
| 216 | the dual variables is below this value. |
| 217 | grad : str, optional |
| 218 | Gradient computation mode: 'detach', 'autodiff', or 'last_step'. |
| 219 | |
| 220 | Returns |
| 221 | ------- |
| 222 | dict |