mindspore_gl.Graph

查看源文件
class mindspore_gl.Graph[源代码]

图类。

GNNCell 类的构造函数中需要被注释的类。 construct 函数中的最后一个参数将被解析成 mindspore_gl.Graph 整图类。

支持平台:

Ascend GPU

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
>>> class SrcVertex(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_vertex_attr({"h": x})
...         return [v.h for v in g.src_vertex]
>>> ret = SrcVertex()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1.0], [2.0], [1.0], [2.0], [0.0], [1.0], [2.0], [3.0], [1.0]]
adj_to_dense()[源代码]

获取图的稠密相邻矩阵。

说明

由于系统限制,目前只支持COO格式进行构建图,并生成Dense格式邻接矩阵。

返回:

Tensor,shape为 \((N, N)\)\(N\) 是图的节点数。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
...
>>> class TestAdjToDense(GNNCell):
...     def construct(self, g: Graph):
...         return g.adj_to_dense()
...
>>> ret = TestAdjToDense()(*graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 3]]
avg(neigh_feat)[源代码]

聚合邻居的节点特征,通过聚合函数“平均”来生成节点级表示。

参数:
  • neigh_feat (List[SrcVertex feature or Edge feature]) - SrcVertexEdge 表示相邻节点或边特征的属性列表,shape为 \((N, F)\)NSrcVertexEdge 的个数, FSrcVertexEdge 的特征维度。

返回:

mindspore.Tensor,shape为 \((N, F)\)N 为节点数, F 为节点的特征维度。

异常:
  • TypeError - 如果 neigh_feat 不是 EdgeSrcVertex 的列表。

样例:

>>> import math
>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
...
>>> class TestAvg(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_vertex_attr({"x": x})
...         for v in g.dst_vertex:
...             v.h = g.avg([u.x for u in v.innbs])
...         return [v.h for v in g.dst_vertex]
...
>>> ret = TestAvg()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> NAN = 1e9
>>> for row in ret:
...     if math.isnan(row[0]):
...         row[0] = NAN
>>> print(ret)
[[1.0], [1.0], [1000000000.0], [0.0], [1.5], [2.0], [1.0], [1000000000.0], [1.0]]
dot(feat_x, feat_y)[源代码]

两个节点Tensor的点乘操作。

参数:
  • feat_x (SrcVertex feature or DstVertex feature) - SrcVertexDstVertex 表示的图节点特征,shape为 \((N, F)\)N 是图上节点数量, F 是节点的特征维度。

  • feat_y (SrcVertex feature or DstVertex feature) - SrcVertexDstVertex 表示的图节点特征,shape为 \((N, F)\)N 是图上节点数量, F 是节点的特征维度。

返回:

mindspore.Tensor,shape为 \((N, 1)\)N 为节点数。

异常:
  • TypeError - 如果 feat_x 不在 ‘mul’ 操作的支持类型[Tensor、Number、List、Tuple]中。

  • TypeError - 如果 feat_y 不在 ‘mul’ 操作的支持类型[Tensor、Number、List、Tuple]中。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
...
>>> class TestDot(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_vertex_attr({"src": x, "dst": x})
...         for v in g.dst_vertex:
...             v.h = [g.dot(v.src, u.dst) for u in v.innbs]
...         return [v.h for v in g.dst_vertex]
...
>>> ret = TestDot()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[2.0], [1.0], [2.0], [2.0], [0.0], [0.0], [2.0], [0.0], [1.0], [1.0], [1.0]]
property dst_idx

一个具有shape为 \((N\_EDGES)\) 的Tensor,表示COO边矩阵的目标节点索引。

返回:

mindspore.Tensor,目标顶点列表。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
>>> class DstIdx(GNNCell):
...     def construct(self, x, g: Graph):
...         return g.dst_idx
>>> ret = DstIdx()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8]
property dst_vertex

循环遍历获取目标顶点的 innbs 入度节点。

返回:

mindspore.Tensor,目标节点列表。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
>>> class DstVertex(GNNCell):
>>>     def construct(self, x, g: Graph):
>>>         g.set_vertex_attr({"h": x})
>>>         return [v.h for v in g.dst_vertex]
>>> ret = DstVertex()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1.0], [2.0], [1.0], [2.0], [0.0], [1.0], [2.0], [3.0], [1.0]]
in_degree()[源代码]

获取图形中每个节点的入度。

返回:

Tensor,shape为 \((N, 1)\) ,表示每个节点的入度, \(N\) 是图的节点数。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
...
>>> class TestInDegree(GNNCell):
...     def construct(self, g: Graph):
...         return g.in_degree()
...
>>> ret = TestInDegree()(*graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1], [2], [0], [1], [2], [1], [1], [0], [3]]
max(neigh_feat)[源代码]

聚合邻居的节点特征,通过聚合函数“最大值”来生成节点级表示。

参数:
  • neigh_feat (List[SrcVertex feature or Edge feature]) - SrcVertexEdge 表示相邻节点或边特征的属性列表,shape为 \((N, F)\)NSrcVertexEdge 的个数, FSrcVertexEdge 的特征维度。

返回:

mindspore.Tensor,shape为 \((N, F)\)N 为节点数, F 为节点的特征维度。

异常:
  • TypeError - 如果 neigh_feat 不是 EdgeSrcVertex 的列表。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
...
>>> class TestMax(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_vertex_attr({"x": x})
...         for v in g.dst_vertex:
...             v.h = g.max([u.x for u in v.innbs])
...         return [v.h for v in g.dst_vertex]
...
>>> ret = TestMax()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1.0], [1.0], [0.0], [0.0], [2.0], [2.0], [1.0], [0.0], [1.0]]
min(neigh_feat)[源代码]

聚合邻居的节点特征,通过聚合函数“最小值”来生成节点级表示。

参数:
  • neigh_feat (List[SrcVertex feature or Edge feature]) - SrcVertexEdge 表示相邻节点或边特征的属性列表,shape为 \((N, F)\)NSrcVertexEdge 的个数, FSrcVertexEdge 的特征维度。

返回:

mindspore.Tensor,shape为 \((N, F)\)N 为节点数, F 为节点的特征维度。

异常:
  • TypeError - 如果 neigh_feat 不是 EdgeSrcVertex 的列表。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
...
>>> class TestMin(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_vertex_attr({"x": x})
...         for v in g.dst_vertex:
...             v.h = g.min([u.x for u in v.innbs])
...         return [v.h for v in g.dst_vertex]
...
>>> ret = TestMin()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]]
property n_edges

图的边数。

返回:

int,图的边数。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
>>> class NEdge(GNNCell):
...     def construct(self, x, g: Graph):
...         return g.n_edges
>>> ret = NEdge()(node_feat, *graph_field.get_graph())
>>> print(ret)
11
property n_nodes

图的节点数。

返回:

int,图的节点数。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
>>> class NNode(GNNCell):
...     def construct(self, x, g: Graph):
...         return g.n_nodes
>>> ret = NNode()(node_feat, *graph_field.get_graph())
>>> print(ret)
9
out_degree()[源代码]

获取图形中每个节点的出度。

返回:

Tensor,shape为 \((N, 1)\) ,表示每个节点的出度, \(N\) 是图的节点数。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
...
>>> class TestOutDegree(GNNCell):
...     def construct(self, g: Graph):
...         return g.out_degree()
...
>>> ret = TestOutDegree()(*graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1], [0], [2], [1], [1], [2], [1], [0], [3]]
set_dst_attr(feat_dict)[源代码]

在以顶点为中心的环境中设置目标顶点的属性。 参数 feat_dict 的key是属性的名称,value是属性的数据。

参数:
  • feat_dict (Dict) - key的类型为str,value的类型为Tensor,shape为 \((N\_NODES, F)\) ,其中 \(F\) 是特征维度。

异常:
  • TypeError - 如果 feat_dict 不是dict。

返回:

mindspore.Tensor,目标顶点的特征。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
...
>>> class TestSetDstAttr(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_dst_attr({"h": x})
...         return [v.h for v in g.dst_vertex]
...
>>> ret = TestSetDstAttr()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1.0], [2.0], [1.0], [2.0], [0.0], [1.0], [2.0], [3.0], [1.0]]
set_edge_attr(feat_dict)[源代码]

在以顶点为中心的环境中设置边的属性。 参数 feat_dict 的key是属性的名称,value是属性的数据。

参数:
  • feat_dict (Dict) - key的类型为str,value的类型为Tensor,shape为 \((N\_NODES, F)\) ,其中 \(F\) 是特征维度。 当特征维度为1时,推荐的边特征shape为 \((N\_EDGES, 1)\)

异常:
  • TypeError - 如果 feat_dict 不是dict。

返回:

mindspore.Tensor,边的特征。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
>>> edge_feat = ms.Tensor([[1], [2], [1], [3], [1], [4], [1], [5], [1], [1], [1]], ms.float32)
...
>>> class TestSetEdgeAttr(GNNCell):
...     def construct(self, nh, eh, g: Graph):
...         g.set_vertex_attr({"nh": nh})
...         g.set_edge_attr({"eh": eh})
...         for v in g.dst_vertex:
...             v.h = g.sum([u.nh * e.eh for u, e in v.inedges])
...         return [v.h for v in g.dst_vertex]
...
>>> ret = TestSetEdgeAttr()(node_feat, edge_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[2.0], [2.0], [0.0], [0.0], [14.0], [6.0], [1.0], [0.0], [3.0]]
set_graph_attr(feat_dict)[源代码]

在以顶点为中心的环境中设置整图的属性。 参数 feat_dict 的key是属性的名称,value是属性的数据。

参数:
  • feat_dict (Dict) - key的类型为str,value的为整图的特征。

异常:
  • TypeError - 如果 feat_dict 不是dict。

返回:

mindspore.Tensor,图的特征。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> g_attr = ms.Tensor([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]], ms.float32)
>>> v_attr = ms.Tensor([1.0, 1.0], ms.float32)
...
>>> class TestSetGraphAttr(GNNCell):
...     def construct(self, vh, gh, g: Graph):
...         g.set_graph_attr({"x": gh})
...         g.set_vertex_attr({"h": vh})
...         for v in g.dst_vertex:
...             v.h = g.sum([u.h * g.x for u in v.innbs])
...         return [v.h for v in g.dst_vertex]
...
>>> ret = TestSetGraphAttr()(v_attr, g_attr, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[0.0, 1.0], [0.0, 2.0], [0.0, 0.0], [0.0, 0.0],
[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]
set_src_attr(feat_dict)[源代码]

在以顶点为中心的环境中设置源顶点的属性。 参数 feat_dict 的key是属性的名称,value是属性的数据。

参数:
  • feat_dict (Dict) - key的类型为str,value的类型为Tensor,shape为 \((N\_NODES, F)\) ,其中 \(F\) 是特征维度。

异常:
  • TypeError - 如果 feat_dict 不是dict。

返回:

mindspore.Tensor,源顶点的特征。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
...
>>> class TestSetSrcAttr(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_src_attr({"h": x})
...         return [u.h for u in g.src_vertex]
...
>>> ret = TestSetSrcAttr()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1.0], [2.0], [1.0], [2.0], [0.0], [1.0], [2.0], [3.0], [1.0]]
set_vertex_attr(feat_dict)[源代码]

在以顶点为中心的环境中为顶点设置属性。 参数 feat_dict 的key是属性的名称,value是属性的数据。

说明

set_vertex_attr 的功能等价于 set_src_attr + set_dst_attr

参数:
  • feat_dict (Dict) - key的类型为str,value的类型为Tensor,shape为 \((N\_NODES, F)\) ,其中 \(F\) 是特征维度。

异常:
  • TypeError - 如果 feat_dict 不是dict。

返回:

mindspore.Tensor,顶点的特征。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
...
>>> class TestSetVertexAttr(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_vertex_attr({"h": x})
...         return [v.h for v in g.dst_vertex] * [u.h for u in g.src_vertex]
...
>>> ret = TestSetVertexAttr()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1.0], [4.0], [1.0], [4.0], [0.0], [1.0], [4.0], [9.0], [1.0]]
property src_idx

一个具有shape为 \((N\_EDGES)\) 的Tensor,表示COO边矩阵的源节点索引。

返回:

mindspore.Tensor,源顶点列表。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
>>> class SrcIdx(GNNCell):
...     def construct(self, x, g: Graph):
...         return g.src_idx
>>> ret = SrcIdx()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8]
property src_vertex

循环遍历获取目标顶点的 outnbs 出度节点。

返回:

mindspore.Tensor,源节点列表。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
>>> class SrcVertex(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_vertex_attr({"h": x})
...         return [v.h for v in g.src_vertex]
>>> ret = SrcVertex()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1.0], [2.0], [1.0], [2.0], [0.0], [1.0], [2.0], [3.0], [1.0]]
sum(neigh_feat)[源代码]

聚合邻居的节点特征,通过聚合函数“求和”来生成节点级表示。

参数:
  • neigh_feat (List[SrcVertex feature or Edge feature]) - SrcVertexEdge 表示相邻节点或边特征的属性列表,shape为 \((N, F)\)NSrcVertexEdge 的个数, FSrcVertexEdge 的特征维度。

返回:

mindspore.Tensor,shape为 \((N, F)\)N 为节点数, F 为节点的特征维度。

异常:
  • TypeError - 如果 neigh_feat 不是 EdgeSrcVertex 的列表。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import Graph, GraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = 9
>>> n_edges = 11
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> node_feat = ms.Tensor([[1], [2], [1], [2], [0], [1], [2], [3], [1]], ms.float32)
...
>>> class TestSum(GNNCell):
...     def construct(self, x, g: Graph):
...         g.set_vertex_attr({"x": x})
...         for v in g.dst_vertex:
...             v.h = g.sum([u.x for u in v.innbs])
...         return [v.h for v in g.dst_vertex]
...
>>> ret = TestSum()(node_feat, *graph_field.get_graph()).asnumpy().tolist()
>>> print(ret)
[[1.0], [2.0], [0.0], [0.0], [3.0], [2.0], [1.0], [0.0], [3.0]]
topk_edges(node_feat, k, sortby=None)[源代码]

通过图上top-k个节点特征来表征图的特征。

如果排序方式设置为无,则函数将独立对所有维度执行top-k。

说明

将按选定维度排序的值(如果 sortbyNone,则为所有维度)大于零。

由于通过零值来对特征进行填充,其余特征可能会被零覆盖。

参数:
  • node_feat (Tensor) - 节点特征,shape为 \((N\_NODES, F)\)F 是特征维度。

  • k (int) - top-k的节点个数。

  • sortby (int) - 根据哪个特征维度排序。如果为 None,则所有特征都独立排序。默认值:None

返回:
  • topk_output (Tensor) - 特征Tensor的shape为 \((B, K, F)\) ,其中 \(B\) 为输入图的批次大小, \(K\) 为输入的 ‘k’, \(F\) 为特征维度。

  • topk_indices (Tensor) - top-k的输出索引,shape为 \((B, K)\) (当 sortbyNone 时, \((B, K, F)\)), 其中 \(B\) 为输入图的批次大小, \(F\) 为特征维度。

异常:
  • TypeError - 如果 node_feat 不是Tensor。

  • TypeError - 如果 k 不是int。

  • ValueError - 如果 sortby 不是int。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import GraphField, Graph
>>> from mindspore_gl.nn import GNNCell
>>> node_feat = ms.Tensor([
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     [9, 7, 5, 8],
...     [8, 7, 6, 5],
...     [8, 6, 4, 6],
...     [1, 2, 1, 1],
... ], ms.float32)
...
>>> n_nodes = 7
>>> n_edges = 8
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
...
>>> class TestTopkEdges(GNNCell):
...     def construct(self, x, g: Graph):
...         return g.topk_edges(x, 2, 1)
...
>>> output, indices = TestTopkEdges()(node_feat, *graph_field.get_graph())
>>> output = output.asnumpy().tolist()
>>> indices = indices.asnumpy().tolist()
>>> print(output)
[[9.0, 7.0, 5.0, 8.0], [8.0, 7.0, 6.0, 5.0]]
>>> print(indices)
[3, 4]
topk_nodes(node_feat, k, sortby=None)[源代码]

通过图上top-k个节点特征来表征图的特征。

如果排序方式设置为无,则函数将独立对所有维度执行top-k。

说明

将按选定维度排序的值(如果 sortbyNone ,则为所有维度)大于零。

由于通过零值来对特征进行填充,其余特征可能会被零覆盖。

参数:
  • node_feat (Tensor) - 节点特征,shape为 \((N\_NODES, F)\)F 是特征维度。

  • k (int) - top-k的节点个数。

  • sortby (int) - 根据哪个特征维度排序。如果为 None,则所有特征都独立排序。默认值:None

返回:
  • topk_output (Tensor) - 特征Tensor的shape为 \((B, K, F)\) ,其中 \(B\) 为输入图的批次大小,

    \(K\) 为输入的 ‘k’, \(F\) 为特征维度。

  • topk_indices (Tensor) - top-k的输出索引,shape为 \((B, K)\) (当 sortbyNone 时, \((B, K, F)\)),

    其中 \(B\) 为输入图的批次大小, \(F\) 为特征维度。

异常:
  • TypeError - 如果 node_feat 不是Tensor。

  • TypeError - 如果 k 不是int。

  • ValueError - 如果 sortby 不是int。

样例:

>>> import mindspore as ms
>>> from mindspore_gl import GraphField, Graph
>>> from mindspore_gl.nn import GNNCell
>>> node_feat = ms.Tensor([
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     [9, 7, 5, 8],
...     [8, 7, 6, 5],
...     [8, 6, 4, 6],
...     [1, 2, 1, 1],
... ], ms.float32)
...
>>> n_nodes = 7
>>> n_edges = 8
>>> src_idx = ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6], ms.int32)
>>> dst_idx = ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4], ms.int32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
...
>>> class TestTopkNodes(GNNCell):
...     def construct(self, x, g: Graph):
...         return g.topk_nodes(x, 2, 1)
...
>>> output, indices = TestTopkNodes()(node_feat, *graph_field.get_graph())
>>> output = output.asnumpy().tolist()
>>> indices = indices.asnumpy().tolist()
>>> print(output)
[[9.0, 7.0, 5.0, 8.0], [8.0, 7.0, 6.0, 5.0]]
>>> print(indices)
[3, 4]