dgl.DGLHeteroGraph.adjacency_matrix¶
-
DGLHeteroGraph.
adjacency_matrix
(transpose=None, ctx=device(type='cpu'), scipy_fmt=None, etype=None)[source]¶ Return the adjacency matrix of edges of the given edge type.
By default, a row of returned adjacency matrix represents the destination of an edge and the column represents the source.
When transpose is True, a row represents the source and a column represents a destination.
Parameters: - transpose (bool, optional) – A flag to transpose the returned adjacency matrix. (Default: False)
- ctx (context, optional) – The context of returned adjacency matrix. (Default: cpu)
- scipy_fmt (str, optional) – If specified, return a scipy sparse matrix in the given format. Otherwise, return a backend dependent sparse tensor. (Default: None)
- etype (str, optional) – The edge type. Can be omitted if there is only one edge type in the graph. (Default: None)
Returns: Adjacency matrix.
Return type: SparseTensor or scipy.sparse.spmatrix
Examples
Instantiate a heterogeneous graph.
>>> follows_g = dgl.graph([(0, 0), (1, 1)], 'user', 'follows') >>> devs_g = dgl.bipartite([(0, 0), (1, 2)], 'developer', 'develops', 'game') >>> g = dgl.hetero_from_relations([follows_g, devs_g])
Get a backend dependent sparse tensor. Here we use PyTorch for example.
>>> g.adjacency_matrix(etype='develops') tensor(indices=tensor([[0, 2], [0, 1]]), values=tensor([1., 1.]), size=(3, 2), nnz=2, layout=torch.sparse_coo)
Get a scipy coo sparse matrix.
>>> g.adjacency_matrix(scipy_fmt='coo', etype='develops') <3x2 sparse matrix of type '<class 'numpy.int64'>' with 2 stored elements in COOrdinate format>