r""" Compute the Gromov-Wasserstein cost tensor for a batch of problems. The Gromov-Wasserstein distance can be expressed as: .. math:: \text{GW}(\mathbf{T}, \mathbf{C}_1, \mathbf{C}_2) = \sum_{ijkl} T_{ik} T_{jl} \ell(C_{1,ij}, C_{2,kl}) = \langle \mathcal{L} \times \mathbf{T}
(
a, b, C1, C2, symmetric=True, nx=None, loss="sqeuclidean", logits=None
)
| 15 | |
| 16 | |
| 17 | def tensor_batch( |
| 18 | a, b, C1, C2, symmetric=True, nx=None, loss="sqeuclidean", logits=None |
| 19 | ): |
| 20 | r""" |
| 21 | Compute the Gromov-Wasserstein cost tensor for a batch of problems. |
| 22 | |
| 23 | The Gromov-Wasserstein distance can be expressed as: |
| 24 | |
| 25 | .. math:: |
| 26 | \text{GW}(\mathbf{T}, \mathbf{C}_1, \mathbf{C}_2) = \sum_{ijkl} T_{ik} T_{jl} \ell(C_{1,ij}, C_{2,kl}) = \langle \mathcal{L} \times \mathbf{T}, \mathbf{T} \rangle |
| 27 | |
| 28 | where :math:`\mathcal{L}` is a 4D tensor with elements :math:`\mathcal{L}[i,j,k,l] = \ell(C_{1,ij}, C_{2,kl})`. |
| 29 | |
| 30 | For loss functions of the form :math:`\ell(a,b) = f_1(a) + f_2(b) - \langle h_1(a), h_2(b) \rangle`, |
| 31 | the tensor product :math:`\mathcal{L} \times \mathbf{T}` can be computed efficiently without explicitly computing :math:`\mathcal{L}` [12]. |
| 32 | |
| 33 | This function precomputes all matrices that implicitly define `\mathcal{L}` for various loss functions. |
| 34 | |
| 35 | Parameters |
| 36 | ---------- |
| 37 | a : array-like, shape (B, n) |
| 38 | Source distributions for each problem in the batch. |
| 39 | b : array-like, shape (B, m) |
| 40 | Target distributions for each problem in the batch. |
| 41 | C1 : array-like, shape (B, n, n) or (B, n, n, d) |
| 42 | Source cost matrices for each problem. Can be a 3D array for scalar costs or a 4D array for vector-valued costs (edge features). |
| 43 | C2 : array-like, shape (B, m, m) or (B, n, n, d) |
| 44 | Target cost matrices for each problem. Can be a 3D array for scalar costs or a 4D array for vector-valued costs (edge features). |
| 45 | symmetric : bool, optional |
| 46 | Whether the cost matrices are symmetric. Default is True. |
| 47 | nx : backend object, optional |
| 48 | Numerical backend to use for computations. If None, the default backend is used. |
| 49 | loss : str, optional |
| 50 | Loss function to use. Supported values: 'sqeuclidean', 'kl'. |
| 51 | Default is 'sqeuclidean'. |
| 52 | logits : bool, optional |
| 53 | For KL divergence, whether inputs are logits (unnormalized log probabilities). |
| 54 | If True, inputs are treated as logits. Default is None. |
| 55 | |
| 56 | Returns |
| 57 | ------- |
| 58 | dict |
| 59 | Dictionary containing: |
| 60 | - constC : array-like, shape (B, n, m) |
| 61 | Constant term in the tensor product. |
| 62 | - hC1 : array-like, shape (B, n, n, d) or (B, n, n) |
| 63 | - hC2 : array-like, shape (B, m, m, d) or (B, m, m) |
| 64 | - fC1 : array-like, shape (B, n, n) |
| 65 | - fC2 : array-like, shape (B, m, m) |
| 66 | |
| 67 | |
| 68 | Supported loss functions: |
| 69 | ------------------------------ |
| 70 | |
| 71 | **Squared Euclidean loss**: |
| 72 | |
| 73 | .. math:: |
| 74 | \ell(a, b) = \|a - b\|_2^2 = \sum_i (a_i - b_i)^2 |
no test coverage detected