dgl.DGLGraph.incidence_matrix
- DGLGraph.incidence_matrix(typestr, ctx=device(type='cpu'), etype=None)
返回具有给定边类型的边的关联矩阵表示。
关联矩阵是一个n乘m的稀疏矩阵,其中n是节点的数量,m是边的数量。每个非零值表示边是否与节点相关联。
There are three types of incidence matrices \(I\):
in
:\(I[v, e] = 1\) 如果 \(e\) 是 \(v\) 的入边 (或 \(v\) 是 \(e\) 的目标节点);
\(I[v, e] = 0\) otherwise.
out
:\(I[v, e] = 1\) 如果 \(e\) 是 \(v\) 的出边 (或 \(v\) 是 \(e\) 的源节点);
\(I[v, e] = 0\) otherwise.
both
(仅当源节点和目标节点类型相同时):\(I[v, e] = 1\) 如果 \(e\) 是 \(v\) 的入边;
\(I[v, e] = -1\) 如果 \(e\) 是 \(v\) 的出边;
\(I[v, e] = 0\) 否则(包括自环)。
- Parameters:
typestr (str) – 可以是
in
,out
或both
ctx (context, optional) – 返回的关联矩阵的上下文。(默认:cpu)
etype (str or (str, str, str), optional) –
The type names of the edges. The allowed type name formats are:
(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.
Can be omitted if the graph has only one type of edges.
- Returns:
关联矩阵。
- Return type:
框架稀疏张量
示例
以下示例使用PyTorch后端。
>>> import dgl
>>> g = dgl.graph(([0, 1], [0, 2])) >>> g.inc('in') tensor(indices=tensor([[0, 2], [0, 1]]), values=tensor([1., 1.]), size=(3, 2), nnz=2, layout=torch.sparse_coo) >>> g.inc('out') tensor(indices=tensor([[0, 1], [0, 1]]), values=tensor([1., 1.]), size=(3, 2), nnz=2, layout=torch.sparse_coo) >>> g.inc('both') tensor(indices=tensor([[1, 2], [1, 1]]), values=tensor([-1., 1.]), size=(3, 2), nnz=2, layout=torch.sparse_coo)