dgl.from_scipyο
- dgl.from_scipy(sp_mat, eweight_name=None, idtype=None, device=None)[source]ο
Create a graph from a SciPy sparse matrix and return.
- Parameters:
sp_mat (scipy.sparse.spmatrix) β The graph adjacency matrix. Each nonzero entry
sp_mat[i, j]
represents an edge from nodei
toj
. The matrix must have square shape(N, N)
, whereN
is the number of nodes in the graph.eweight_name (str, optional) β The edata name for storing the nonzero values of
sp_mat
. If given, DGL will store the nonzero values ofsp_mat
inedata[eweight_name]
of the returned graph.idtype (int32 or int64, optional) β The data type for storing the structure-related graph information such as node and edge IDs. It should be a framework-specific data type object (e.g.,
torch.int32
). By default, DGL uses int64.device (device context, optional) β The device of the resulting graph. It should be a framework-specific device object (e.g.,
torch.device
). By default, DGL stores the graph on CPU.
- Returns:
The created graph.
- Return type:
Notes
The function supports all kinds of SciPy sparse matrix classes (e.g.,
scipy.sparse.csr.csr_matrix
). It converts the input matrix to the COOrdinate format usingscipy.sparse.spmatrix.tocoo()
before creates aDGLGraph
. Creating from ascipy.sparse.coo.coo_matrix
is hence the most efficient way.DGL internally maintains multiple copies of the graph structure in different sparse formats and chooses the most efficient one depending on the computation invoked. If memory usage becomes an issue in the case of large graphs, use
dgl.DGLGraph.formats()
to restrict the allowed formats.
Examples
The following example uses PyTorch backend.
>>> import dgl >>> import numpy as np >>> import torch >>> from scipy.sparse import coo_matrix
Create a small three-edge graph.
>>> # Source nodes for edges (2, 1), (3, 2), (4, 3) >>> src_ids = np.array([2, 3, 4]) >>> # Destination nodes for edges (2, 1), (3, 2), (4, 3) >>> dst_ids = np.array([1, 2, 3]) >>> # Weight for edges (2, 1), (3, 2), (4, 3) >>> eweight = np.array([0.2, 0.3, 0.5]) >>> sp_mat = coo_matrix((eweight, (src_ids, dst_ids)), shape=(5, 5)) >>> g = dgl.from_scipy(sp_mat)
Retrieve the edge weights.
>>> g = dgl.from_scipy(sp_mat, eweight_name='w') >>> g.edata['w'] tensor([0.2000, 0.3000, 0.5000], dtype=torch.float64)
Create a graph on the first GPU with data type int32.
>>> g = dgl.from_scipy(sp_mat, idtype=torch.int32, device='cuda:0')
See also