pygmtools.utils.to_pyg

pygmtools.utils.to_pyg(adj_matrix, edge_attr=None, backend=None)[source]

Convert adjacency matrix to torch_geometric.data.Data object

Parameters
  • adj_matrix – the adjacency matrix to convert, whose type must be torch.Tensor, it can be 2D matrix (num_nodes, num_nodes) or 3D matrix (num_nodes, num_nodes, num_edge_features)

  • backend – (default: pygmtools.BACKEND variable) the backend for computation.

Returns

the torch_geometric.data.Data object corresponding to the adjacency matrix

Example
>>> import torch
>>> import pygmtools as pygm
>>> pygm.set_backend('pytorch')

# Generate 2D adjacency matrix (num_nodes, num_nodes)
>>> adj_matrix = torch.rand((4, 4))

# Obtain torch_geometric.data.Data object
>>> pygm.utils.to_pyg(adj_matrix)
Data(edge_index=[2, 16], edge_attr=[16])

# Generate 3D adjacency matrix (num_nodes, num_nodes, num_edge_features)
>>> adj_matrix = torch.rand((4, 4, 3))

# Obtain torch_geometric.data.Data object
>>> pygm.utils.to_pyg(adj_matrix)
Data(edge_index=[2, 16], edge_attr=[16, 3])