pygmtools.utils.compute_affinity_score
- pygmtools.utils.compute_affinity_score(X, K, backend=None)[source]
Compute the affinity score of graph matching. It is the objective score of the corresponding Quadratic Assignment Problem.
\[\texttt{vec}(\mathbf{X})^\top \mathbf{K} \texttt{vec}(\mathbf{X})\]here \(\texttt{vec}\) means column-wise vectorization.
- Parameters
X – \((b\times n_1 \times n_2)\) the permutation matrix that represents the matching result
K – \((b\times n_1n_2 \times n_1n_2)\) the affinity matrix
backend – (default:
pygmtools.BACKEND
variable) the backend for computation.
- Returns
\((b)\) the objective score
Note
This function also supports non-batched input if the batch dimension of
X, K
is ignored.Pytorch Example
>>> import pygmtools as pygm >>> import torch >>> pygm.BACKEND = 'pytorch' # Generate a graph matching problem >>> X_gt = torch.zeros(4, 4) >>> X_gt[torch.arange(0, 4, dtype=torch.int64), torch.randperm(4)] =1 >>> A1 = torch.rand(4, 4) >>> A2 = torch.mm(torch.mm(X_gt.transpose(0,1), A1), X_gt) >>> conn1, edge1 = pygm.utils.dense_to_sparse(A1) >>> conn2, edge2 = pygm.utils.dense_to_sparse(A2) >>> import functools >>> gaussian_aff = functools.partial(pygm.utils.gaussian_aff_fn, sigma=1.) >>> K = pygm.utils.build_aff_mat(None, edge1, conn1, None, edge2, conn2, None, None, None, None, edge_aff_fn=gaussian_aff) # Compute the objective score of ground truth matching >>> pygm.utils.compute_affinity_score(X_gt, K) tensor(16.)