pygmtools.utils.build_aff_mat_from_pyg
- pygmtools.utils.build_aff_mat_from_pyg(G1, G2, node_aff_fn=None, edge_aff_fn=None, backend=None)[source]
Convert torch_geometric.data.Data object to affinity matrix
- Parameters
G1 – Graph object, whose type must be torch_geometric.data.Data
G2 – Graph object, whose type must be torch_geometric.data.Data
node_aff_fn – (default: inner_prod_aff_fn) the node affinity function with the characteristic
node_aff_fn(2D Tensor, 2D Tensor) -> 2D Tensor
, which accepts two node feature tensors and outputs the node-wise affinity tensor. Seeinner_prod_aff_fn()
as an example.edge_aff_fn – (default: inner_prod_aff_fn) the edge affinity function with the characteristic
edge_aff_fn(2D Tensor, 2D Tensor) -> 2D Tensor
, which accepts two edge feature tensors and outputs the edge-wise affinity tensor. Seeinner_prod_aff_fn()
as an example.backend – (default:
pygmtools.BACKEND
variable) the backend for computation.
- Returns
the affinity matrix corresponding to the torch_geometric.data.Data object G1 and G2
Example
>>> import torch >>> from torch_geometric.data import Data >>> import pygmtools as pygm >>> pygm.set_backend('pytorch') # Generate Graph object >>> x1 = torch.rand((4, 2), dtype=torch.float) >>> e1 = torch.tensor([[0, 0, 1, 1, 2, 2, 3], [1, 2, 0, 2, 0, 3, 1]], dtype=torch.long) >>> G1 = Data(x=x1, edge_index=e1) >>> x2 = torch.rand((5, 2), dtype=torch.float) >>> e2 = torch.tensor([[0, 0, 1, 1, 2, 2, 3, 4, 4], [1, 3, 2, 3, 1, 3, 4, 2, 3]], dtype=torch.long) >>> G2 = Data(x=x2, edge_index=e2) # Obtain Affinity Matrix >>> K = pygm.utils.build_aff_mat_from_pyg(G1, G2) >>> K.shape (20, 20) # The affinity matrices K can be further processed by GM solvers