dgl.DGLGraph.adj

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

获取图的邻接矩阵。

Parameters:
  • etype (str(str, str, str), 可选) –

    边的类型名称。允许的类型名称格式为:

    • (str, str, str) 用于源节点类型、边类型和

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

    三元组格式。

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

  • eweight_name (str, optional) – 用作非零值的边特征的名称。如果未给出,则所有非零值均为1。

Returns:

邻接矩阵。

Return type:

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)