mindspore_gl.Graph
- class mindspore_gl.Graph[source]
Graph class.
This is the class which should be annotated in the construct function for GNNCell class. The last argument in the ‘construct’ function will be resolved into the ‘mindspore_gl.Graph’ whole graph class.
- Supported Platforms:
Ascend
GPU
Examples
>>> 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()[source]
Get the dense adjacent matrix of the graph.
Note
Due to system limitations, only COO format are supported for build graph and dense format adjacency matrix can be generated.
- Returns
Tensor, a tensor with shape
, is the number of nodes of the graph.
Examples
>>> 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)[source]
Aggregating node features from their neighbour and generates a node-level representation by aggregate function ‘avg’.
- Parameters
neigh_feat (List[SrcVertex feature or Edge feature]) – a list of SrcVertex or Edge attributes represents the neighbour nodes or edges feature, with shape
, is the number of SrcVertex or Edge, is the feature dimension of the SrcVertex or Edge attribute.- Returns
mindspore.Tensor, a tensor with shape
, is the number of nodes of the graph, is the feature dimension of the node.
- Raises
TypeError – If neigh_feat is not a list of Edge or SrcVertex.
Examples
>>> 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)[source]
Dot mul operation for two node Tensors.
- Parameters
feat_x (SrcVertex feature or DstVertex feature) – the arttribute of SrcVertex or DstVertex represent feature tensor of graph nodes with shape
, is the number of nodes of the graph, is the feature dimension of the node.feat_y (SrcVertex feature or DstVertex feature) – the arttribute of SrcVertex or DstVertex represent feature tensor of graph nodes with shape
, is the number of nodes of the graph, is the feature dimension of the node.
- Returns
mindspore.Tensor, a tensor with shape
, N is the number of nodes of the graph.
- Raises
Examples
>>> 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
A tensor with shape
, represents the destination node index of COO edge matrix.- Returns
mindspore.Tensor, a list of destination vertex.
Examples
>>> 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
Return a list of destination vertex that only supports iterate its innbs .
- Returns
mindspore.Tensor, a list of destination vertex.
Examples
>>> 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()[source]
Get the in degree of each node in a graph.
- Returns
Tensor, a tensor with shape
, represent the in degree of each node, is the number of nodes of the graph.
Examples
>>> 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)[source]
Aggregating node features from their neighbour and generates a node-level representation by aggregate function ‘max’.
- Parameters
neigh_feat (List[SrcVertex feature or Edge feature]) – a list of SrcVertex or Edge attributes represents the neighbour nodes or edges feature, with shape
, is the number of SrcVertex or Edge, is the feature dimension of the SrcVertex or Edge attribute.- Returns
mindspore.Tensor, a tensor with shape
, is the number of nodes of the graph, is the feature dimension of the node.
- Raises
TypeError – If neigh_feat is not a list of Edge or SrcVertex.
Examples
>>> 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)[source]
Aggregating node features from their neighbour and generates a node-level representation by aggregate function ‘min’.
- Parameters
neigh_feat (List[SrcVertex feature or Edge feature]) – a list of SrcVertex or Edge attributes represents the neighbour nodes or edges feature, with shape
, is the number of SrcVertex or Edge, is the feature dimension of the SrcVertex or Edge attribute.- Returns
mindspore.Tensor, a tensor with shape
, is the number of nodes of the graph, is the feature dimension of the node.
- Raises
TypeError – If neigh_feat is not a list of Edge or SrcVertex.
Examples
>>> 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
An integer, represent the edges count of the graph.
- Returns
int, edges numbers of the graph.
Examples
>>> 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
An integer, represent the nodes count of the graph.
- Returns
int, nodes numbers of the graph.
Examples
>>> 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()[source]
Get the out degree of each node in a graph.
- Returns
Tensor, a tensor with shape
, represent the out degree of each node, is the number of nodes of the graph.
Examples
>>> 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)[source]
Set attributes for destination vetices in vertex-centric environment Keys will be attribute’s name, values will be attributes’ data.
- Parameters
feat_dict (Dict) – key type: str, value type: recommend tensor of shape
, is the dimension of the node feature.- Returns
mindspore.Tensor, the feature of destination vertex.
- Raises
TypeError – If feat_dict is not a Dict.
Examples
>>> 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)[source]
Set attributes for edges in vertex-centric environment. Keys will be attribute’s name, values will be attributes’ data.
- Parameters
feat_dict (Dict) – key type: str, value type: recommend feature tensor of shape
, is the shape of the feature per edge. Recommend the shape of value is when the feature dimension is 1.- Returns
mindspore.Tensor, the feature of edges.
- Raises
TypeError – If feat_dict is not a Dict.
Examples
>>> 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)[source]
Set attributes for the whole graph in vertex-centric environment. Keys will be attribute’s name, values will be attributes’ data.
- Parameters
feat_dict (Dict) – key type: str, value type: recommend feature tensor for the whole graph.
- Returns
mindspore.Tensor, the feature of graph.
- Raises
TypeError – If feat_dict is not a Dict.
Examples
>>> 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)[source]
Set attributes for source vertices in vertex-centric environment. Keys will be attribute’s name, values will be attributes’ data.
- Parameters
feat_dict (Dict) – key type: str, value type: recommend tensor of shape
, is the dimension of the node feature.- Returns
mindspore.Tensor, the feature of source vertex.
- Raises
TypeError – If feat_dict is not a Dict.
Examples
>>> 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)[source]
Set attributes for vertices in vertex-centric environment. Keys will be attribute’s name, values will be attributes’ data.
Note
set_vertex_attr is equals to set_src_attr + set_dst_attr.
- Parameters
feat_dict (Dict) – key type: str, value type: recommend tensor of shape
, is the dimension of the node feature.- Returns
mindspore.Tensor, the feature of vertex.
- Raises
TypeError – If feat_dict is not a Dict.
Examples
>>> 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
A tensor with shape
, represents the source node index of COO edge matrix.- Returns
mindspore.Tensor, a list of source vertex.
Examples
>>> 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
Return a list of source vertex that only supports iterate with its outnbs .
- Returns
mindspore.Tensor, a list of source vertex.
Examples
>>> 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)[source]
Aggregating node features from their neighbour and generates a node-level representation by aggregate function ‘sum’.
- Parameters
neigh_feat (List[SrcVertex feature or Edge feature]) – a list of SrcVertex or Edge attribute represents the neighbour nodes or edges feature, with shape
, is the number of SrcVertex or Edge, is the feature dimension of the SrcVertex or Edge attribute.- Returns
mindspore.Tensor, a tensor with shape
, is the number of nodes of the graph, is the feature dimension of the node.
- Raises
TypeError – If neigh_feat is not a list of Edge or SrcVertex.
Examples
>>> 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)[source]
Return a graph-level representation by a graph-wise top-k on node features.
If sortby is set to
None
, the function would perform top-k on all dimensions independently.- Parameters
Note
The value participated in the sort by axis (all value if sortby is
None
) should be greater than zero. Due to the reason that we create zero value for padding and they may cover the features.- Returns
topk_output (Tensor) - a tensor with shape
, where is the batch size of the input graph. is the input ‘k’, is the feature size.topk_indices (Tensor), - a tensor with shape
( if sortby is set to None), where is the batch size of the input graph, is the feature size.
- Raises
TypeError – If node_feat is not a Tensor.
TypeError – If k is not an int.
ValueError – If sortby is not an int.
Examples
>>> 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)[source]
Return a graph-level representation by a graph-wise top-k on node features.
If sortby is set to
None
, the function would perform top-k on all dimensions independently.Note
The value participated in the sort by axis (all value if sortby is
None
) should be greater than zero. Due to the reason that we create zero value for padding and they may cover the features.- Parameters
- Returns
topk_output (Tensor) - a tensor with shape
, where is the batch size of the input graph. is the input ‘k’, is the feature size.topk_indices (Tensor), - a tensor with shape
( if sortby is set toNone
), where is the batch size of the input graph, is the feature size.
- Raises
TypeError – If node_feat is not a Tensor.
TypeError – If k is not an int.
ValueError – If sortby is not an int.
Examples
>>> 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]