dgl.DGLGraph.adj

DGLGraph.adj(etype=None, eweight_name=None)[source]

获取图的邻接矩阵。

参数:
  • etype (str(str, str, str), 可选) –

    边的类型名称。允许的类型名称格式有

    • (str, str, str) 表示源节点类型、边类型和

    目标节点类型。* 或一个 str 边类型名称,如果该名称可以唯一标识图中的一个

    三元组格式。

    如果图只有一种边类型,则可以省略。

  • eweight_name (str, 可选) – 用作非零值的边特征名称。如果未给出,非零值全部为 1。

返回值:

邻接矩阵。

返回类型:

SparseMatrix

示例

以下示例使用 PyTorch 后端。

>>> import dgl
>>> import torch
>>> g = dgl.graph(([0, 1, 2], [1, 2, 3]))
>>> g.adj()
SparseMatrix(indices=tensor([[0, 1, 2],
                             [1, 2, 3]]),
             values=tensor([1., 1., 1.]),
             shape=(4, 4), nnz=3)
>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): ([0, 1], [0, 1]),
...     ('developer', 'develops', 'game'): ([0, 1], [0, 2])
... })
>>> g.adj(etype='develops')
SparseMatrix(indices=tensor([[0, 1],
                             [0, 2]]),
             values=tensor([1., 1.]),
             shape=(2, 3), nnz=2)
>>> g.edata['h'] = {('user', 'follows', 'user'): torch.tensor([3, 2])}
>>> g.adj(etype='follows', eweight_name='h')
SparseMatrix(indices=tensor([[0, 1],
                             [0, 1]]),
             values=tensor([3, 2]),
             shape=(2, 2), nnz=2)