dgl.DGLGraph.adj_tensors

DGLGraph.adj_tensors(fmt, etype=None)[source]

返回给定边类型的边的邻接矩阵,作为稀疏矩阵表示的张量。 默认情况下,返回的邻接矩阵的一行表示边的源,列表示目标。 :param fmt: 可以是 coo, csrcsc。 :type fmt: str :param etype: 边的类型名称。允许的类型名称格式为:

  • (str, str, str) for source node type, edge type and destination node type.

  • or one str edge type name if the name can uniquely identify a triplet format in the graph.

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

Returns:

如果 fmtcoo,则返回一对源节点和目标节点 ID 张量。 如果 fmtcsrcsc,则返回邻接矩阵的 CSR 或 CSC 表示形式,作为三个张量的三元组 (indptr, indices, edge_ids)。即 edge_ids 可能是一个空张量,元素为 0,在这种情况下,边 ID 是从 0 开始的连续整数。

Return type:

tuple[Tensor]

示例

>>> g = dgl.graph(([0, 1, 2], [1, 2, 3]))
>>> g.adj_tensors('coo')
(tensor([0, 1, 2]), tensor([1, 2, 3]))
>>> g.adj_tensors('csr')
(tensor([0, 1, 2, 3, 3]), tensor([1, 2, 3]), tensor([0, 1, 2]))