pygmtools.utils.build_aff_mat_from_networkx
- pygmtools.utils.build_aff_mat_from_networkx(G1: Graph, G2: Graph, node_aff_fn=None, edge_aff_fn=None, backend=None)[source]
Convert networkx object to affinity matrix
- Parameters
G1 – networkx object, whose type must be networkx.Graph
G2 – networkx object, whose type must be networkx.Graph
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 networkx object G1 and G2
Example
>>> import networkx as nx >>> import pygmtools as pygm >>> pygm.set_backend('numpy') # Generate networkx images >>> G1 = nx.DiGraph() >>> G1.add_weighted_edges_from([(1, 2, 0.5), (2, 3, 0.8), (3, 4, 0.7)]) >>> G2 = nx.DiGraph() >>> G2.add_weighted_edges_from([(1, 2, 0.3), (2, 3, 0.6), (3, 4, 0.9), (4, 5, 0.4)]) # Obtain Affinity Matrix >>> K = pygm.utils.build_aff_mat_from_networkx(G1, G2) >>> K.shape (20, 20) # The affinity matrices K can be further processed by GM solvers