# mindspore_gl.parser¶

Public API for graph operators.

class mindspore_gl.parser.Graph[source]

Graph class.

This is the class which should be annotated in construct function for GNNCell class.

adj_to_dense()[source]

Get the dense adjacent matrix of the graph.

Note

You must set vertex attr first due to the current limits of our system.

Returns

Tensor, a tensor with shape $$(N, N)$$, $$N$$ is the number of nodes of the graph.

Supported Platforms:

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)
...
...     def construct(self, g: Graph):
...
>>> 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 $$(N, F)$$, $$N$$ is the number of SrcVertex or Edge, $$F$$ is the feature dimension of the SrcVertex or Edge attribute.

Returns

Tensor, a tensor with shape $$(N, F)$$, $$N$$ is the number of nodes of the graph, $$F$$ is the feature dimension of the node.

Raises

TypeError – If neigh_feat is not a list of Edge or SrcVertex.

Supported Platforms:

GPU

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 $$(N, F)$$, $$N$$ is the number of nodes of the graph, $$F$$ 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 $$(N, F)$$, $$N$$ is the number of nodes of the graph, $$F$$ is the feature dimension of the node.

Returns

Tensor, a tensor with shape $$(N, 1)$$, $$N$$ is the number of nodes of the graph.

Raises
• TypeError – If feat_x is not in the ‘mul’ operation support types [Tensor,Number,List,Tuple].

• TypeError – If feat_y is not in the ‘mul’ operation support types [Tensor,Number,List,Tuple].

Supported Platforms:

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 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 $$(N\_EDGES)$$, represents the destination node index of COO edge matrix.

property dst_vertex

Return a list of destination vertex that only supports iterate its innbs.

Examples

>>> for v in g.dst_vertex:
...     pass

in_degree()[source]

Get the in degree of each node in a graph.

Returns

Tensor, a tensor with shape $$(N, 1)$$, represent the in degree of each node, $$N$$ is the number of nodes of the graph.

Supported Platforms:

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)
...
>>> 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 $$(N, F)$$, $$N$$ is the number of SrcVertex or Edge, $$F$$ is the feature dimension of the SrcVertex or Edge attribute.

Returns

Tensor, a tensor with shape $$(N, F)$$, $$N$$ is the number of nodes of the graph, $$F$$ is the feature dimension of the node.

Raises

TypeError – If neigh_feat is not a list of Edge or SrcVertex.

Supported Platforms:

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 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 $$(N, F)$$, $$N$$ is the number of SrcVertex or Edge, $$F$$ is the feature dimension of the SrcVertex or Edge attribute.

Returns

Tensor, a tensor with shape $$(N, F)$$, $$N$$ is the number of nodes of the graph, $$F$$ is the feature dimension of the node.

Raises

TypeError – If neigh_feat is not a list of Edge or SrcVertex.

Supported Platforms:

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 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.

property n_nodes

An integer, represent the nodes count of the graph.

out_degree()[source]

Get the out degree of each node in a graph.

Returns

Tensor, a tensor with shape $$(N, 1)$$, represent the out degree of each node, $$N$$ is the number of nodes of the graph.

Supported Platforms:

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)
...
>>> 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 $$(N\_NODES, F)$$, $$F$$ is the dimension of the node feature.

Raises

TypeError – If feat_dict is not a Dict.

Supported Platforms:

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 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 $$(N\_EDGES, F)$$, $$F$$ is the dimension of the edge feature. Recommend the shape of value is $$(N\_EDGES, 1)$$ when the feature dimension is 1.

Raises

TypeError – If feat_dict is not a Dict.

Supported Platforms:

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)
>>> 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.

Raises

TypeError – If feat_dict is not a Dict.

Supported Platforms:

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)
>>> 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 $$(N\_NODES, F)$$, $$F$$ is the dimension of the node feature.

Raises

TypeError – If feat_dict is not a Dict.

Supported Platforms:

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 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 $$(N\_NODES, F)$$, $$F$$ is the dimension of the node feature.

Raises

TypeError – If feat_dict is not a Dict.

Supported Platforms:

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 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 $$(N\_EDGES)$$, represents the source node index of COO edge matrix.

property src_vertex

Return a list of vertex that only supports iterate with its outnbs

Examples

>>> for u in g.src_vertex:
...     pass

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 $$(N, F)$$, $$N$$ is the number of SrcVertex or Edge, $$F$$ is the feature dimension of the SrcVertex or Edge attribute.

Returns

Tensor, a tensor with shape $$(N, F)$$, $$N$$ is the number of nodes of the graph, $$F$$ is the feature dimension of the node.

Raises

TypeError – If neigh_feat is not a list of Edge or SrcVertex.

Supported Platforms:

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 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]]

class mindspore_gl.parser.BatchedGraph[source]

Batched Graph class.

This is the class which should be annotated in construct function for GNNCell class.

avg_edges(edge_feat)[source]

Aggregating edge features and generates a graph-level representation by aggregation type ‘avg’.

The edge_feat should have shape $$(N\_EDGES, F)$$. Avg_edges operation will aggregate the edge_feat according to edge_subgraph_idx. The output tensor will have a shape $$(N\_GRAPHS, F)$$. $$F$$ is the dimension of the edge feature.

Parameters

edge_feat (Tensor) – a tensor represents the edge feature, with shape $$(N\_EDGES, F)$$. $$F$$ is the dimension of the edge feature.

Returns

Tensor, a tensor with shape $$(N\_GRAPHS, F)$$, $$F$$ is the dimension of the edge feature.

Raises

TypeError – If edge_feat is not a Tensor which is the type of operation ‘shape’.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> edge_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [9, 7, 5, 8],
...     [8, 7, 6, 5],
...     [8, 6, 4, 6],
...     [1, 2, 1, 1],
...     [3, 2, 3, 3],
... ], 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestAvgEdges(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.avg_edges(x)
...
>>> ret = TestAvgEdges()(edge_feat, *batched_graph_field.get_batched_graph()).asnumpy().tolist()
>>> print(ret)
[[1.3333333730697632, 3.0, 2.0, 3.6666667461395264],
[5.800000190734863, 4.800000190734863, 3.799999952316284, 4.599999904632568]]

avg_nodes(node_feat)[source]

Aggregating node features and generates a graph-level representation by aggregation type ‘avg’.

The node_feat should have shape $$(N\_NODES, F)$$. Avg_nodes operation will aggregate the node_feat according to ver_subgraph_idx. The output tensor will have a shape $$(N\_GRAPHS, F)$$. $$F$$ is the dimension of the node feature.

Parameters

node_feat (Tensor) – a tensor represents the node feature, with shape $$(N\_NODES, F)$$. $$F$$ is the dimension of the node feature.

Returns

Tensor, a tensor with shape $$(N\_GRAPHS, F)$$. $$F$$ is the dimension of the node feature.

Raises

TypeError – If node_feat is not a Tensor which is the type of operation.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> node_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestAvgNodes(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.avg_nodes(x)
...
>>> ret = TestAvgNodes()(node_feat, *batched_graph_field.get_batched_graph()).asnumpy().tolist()
>>> print(ret)
[[1.3333333730697632, 3.0, 2.0, 3.6666667461395264], [6.5, 5.5, 4.0, 5.0]]

broadcast_edges(graph_feat)[source]

Broadcast graph-level features to edge-level representation.

Parameters

graph_feat (Tensor) – a tensor represent the graph feature, with shape $$(N\_GRAPHS, F)$$, $$F$$ is the feature size.

Returns

Tensor, a tensor with shape $$(N\_EDGES, F)$$, $$F$$ is the feature size.

Raises

TypeError – If graph_feat is not a Tensor.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> edge_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [9, 7, 5, 8],
...     [8, 7, 6, 5],
...     [8, 6, 4, 6],
...     [1, 2, 1, 1],
...     [3, 2, 3, 3],
... ], 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
...     def construct(self, x, bg: BatchedGraph):
...         ret = bg.max_edges(x)
...
>>> print(ret)
[[2.0, 4.0, 3.0, 4.0], [2.0, 4.0, 3.0, 4.0], [2.0, 4.0, 3.0, 4.0],
[9.0, 7.0, 6.0, 8.0], [9.0, 7.0, 6.0, 8.0], [9.0, 7.0, 6.0, 8.0],
[9.0, 7.0, 6.0, 8.0], [9.0, 7.0, 6.0, 8.0]]

broadcast_nodes(graph_feat)[source]

Broadcast graph-level features to node-level representation.

Parameters

graph_feat (Tensor) – a tensor represent the graph feature, with shape $$(N\_GRAPHS, F)$$, $$F$$ is the feature size.

Returns

Tensor, a tensor with shape $$(N\_NODES, F)$$, $$F$$ is the feature size.

Raises

TypeError – If graph_feat is not a Tensor.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> node_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
...     def construct(self, x, bg: BatchedGraph):
...         ret = bg.max_nodes(x)
...
>>> print(ret)
[[2.0, 4.0, 3.0, 4.0], [2.0, 4.0, 3.0, 4.0], [2.0, 4.0, 3.0, 4.0],
[9.0, 7.0, 6.0, 8.0], [9.0, 7.0, 6.0, 8.0], [9.0, 7.0, 6.0, 8.0], [9.0, 7.0, 6.0, 8.0]]

edge_mask()[source]

Returns

Tensor, a tensor with shape $$(N\_EDGES,)$$. Inside tensor, 1 represent the edge exists and 0 represent the edge is generated by padding.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 2, 2], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2], ms.int32)
>>> graph_mask = ms.Tensor([1, 1, 0], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
...     def construct(self, bg: BatchedGraph):
...
>>> print(ret)
[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]

property edge_subgraph_idx

A tensor with shape $$(N\_EDGES,)$$, indicates each edge belonging to which subgraph.

property graph_mask

A tensor with shape $$(N\_GRAPHS,)$$, indicates whether the subgraph is exist.

max_edges(edge_feat)[source]

Aggregating edge features and generates a graph-level representation by aggregation type ‘max’.

The edge_feat should have shape $$(N\_EDGES, F)$$. Max_edges operation will aggregate the edge_feat according to edge_subgraph_idx. The output tensor will have a shape $$(N\_GRAPHS, F)$$. $$F$$ is the dimension of the edge feature.

Parameters

edge_feat (Tensor) – a tensor represents the edge feature, with shape $$(N\_EDGES, F)$$. $$F$$ is the dimension of the edge feature.

Returns

Tensor, a tensor with shape $$(N\_GRAPHS, F)$$. $$F$$ is the dimension of the edge feature.

Raises

TypeError – If edge_feat is not a Tensor which is the type of operation ‘shape’.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> edge_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [9, 7, 5, 8],
...     [8, 7, 6, 5],
...     [8, 6, 4, 6],
...     [1, 2, 1, 1],
...     [3, 2, 3, 3],
... ], 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestMaxEdges(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.max_edges(x)
...
>>> ret = TestMaxEdges()(edge_feat, *batched_graph_field.get_batched_graph()).asnumpy().tolist()
>>> print(ret)
[[2.0, 4.0, 3.0, 4.0], [9.0, 7.0, 6.0, 8.0]]

max_nodes(node_feat)[source]

Aggregating node features and generates a graph-level representation by aggregation type ‘max’.

The node_feat should have shape $$(N\_NODES, F)$$. Max_nodes operation will aggregate the node_feat according to ver_subgraph_idx. The output tensor will have a shape $$(N\_GRAPHS, F)$$. $$F$$ is the dimension of the node feature.

Parameters

node_feat (Tensor) – a tensor represents the node feature, with shape $$(N\_NODES, F)$$. $$F$$ is the dimension of the node feature.

Returns

Tensor, a tensor with shape $$(N\_GRAPHS, F)$$, $$F$$ is the dimension of the node feature.

Raises

TypeError – If node_feat is not a Tensor which is the type of operation ‘shape’.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> node_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestMaxNodes(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.max_nodes(x)
...
>>> ret = TestMaxNodes()(node_feat, *batched_graph_field.get_batched_graph()).asnumpy().tolist()
>>> print(ret)
[[2.0, 4.0, 3.0, 4.0], [9.0, 7.0, 6.0, 8.0]]

property n_graphs

An integer, represent the graphs count of the batched graph.

node_mask()[source]

Returns

Tensor, a tensor with shape $$(N\_NODES, )$$. Inside tensor, 1 represent the node exists and 0 represent the node is generated by padding.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 2, 2], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2], ms.int32)
>>> graph_mask = ms.Tensor([1, 1, 0], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
...     def construct(self, bg: BatchedGraph):
...
>>> print(ret)
[1, 1, 1, 1, 1, 1, 1, 0, 0]

num_of_edges()[source]

Get the number of edges of each subgraph in a batched graph.

Returns

Tensor, a tensor with shape $$(N\_GRAPHS, 1)$$, represent each subgraph contains how many edges.

Note

After padding operation, a not existing subgraph is created and all not existing edges created belong to this subgraph. If you want to clear it, you need to multiply it with a graph mask manually.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 2, 2], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2], ms.int32)
>>> graph_mask = ms.Tensor([1, 1, 0], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestNumOfEdges(GNNCell):
...     def construct(self, bg: BatchedGraph):
...         return bg.num_of_edges()
...
>>> ret = TestNumOfEdges()(*batched_graph_field.get_batched_graph()).asnumpy().tolist()
>>> print(ret)
[[3], [5], [3]]

num_of_nodes()[source]

Get the number of nodes of each subgraph in a batched graph.

Returns

Tensor, a tensor with shape $$(N\_GRAPHS, 1)$$ represent each subgraph contains how many nodes.

Note

After padding operation, a not existing subgraph is created and all not existing nodes created belong to this subgraph. If you want to clear it, you need to multiply it with a graph mask manually.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 2, 2], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2], ms.int32)
>>> graph_mask = ms.Tensor([1, 1, 0], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestNumOfNodes(GNNCell):
...     def construct(self, bg: BatchedGraph):
...         return bg.num_of_nodes()
...
>>> ret = TestNumOfNodes()(*batched_graph_field.get_batched_graph()).asnumpy().tolist()
>>> print(ret)
[[3], [4], [2]]

softmax_edges(edge_feat)[source]

Perform graph-wise softmax on the edge features.

For each edge $$v\in\mathcal{V}$$ and its feature $$x_v$$, calculate its normalized feature as follows:

$z_v = \frac{\exp(x_v)}{\sum_{u\in\mathcal{V}}\exp(x_u)}$

Each subgraph computes softmax independently. The result tensor has the same shape as the original edge feature.

Parameters

edge_feat (Tensor) – a tensor represent the edge feature, with shape $$(N\_EDGES, F)$$, $$F$$ is the feature size.

Returns

Tensor, a tensor with shape $$(N\_EDGES, F)$$, $$F$$ is the feature size.

Raises

TypeError – If edge_feat is not a Tensor.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> edge_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [9, 7, 5, 8],
...     [8, 7, 6, 5],
...     [8, 6, 4, 6],
...     [1, 2, 1, 1],
...     [3, 2, 3, 3],
... ], 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestSoftmaxEdges(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.softmax_edges(x)
...
>>> ret = TestSoftmaxEdges()(edge_feat, *batched_graph_field.get_batched_graph()).asnumpy()
>>> print(np.array2string(ret, formatter={'float_kind':'{0:.5f}'.format}))
[[0.21194, 0.09003, 0.66524, 0.42232],
[0.57612, 0.66524, 0.09003, 0.15536],
[0.21194, 0.24473, 0.24473, 0.42232],
[0.57518, 0.41993, 0.23586, 0.83838],
[0.21160, 0.41993, 0.64113, 0.04174],
[0.21160, 0.15448, 0.08677, 0.11346],
[0.00019, 0.00283, 0.00432, 0.00076],
[0.00143, 0.00283, 0.03192, 0.00565]]

softmax_nodes(node_feat)[source]

Perform graph-wise softmax on the node features.

For each node $$v\in\mathcal{V}$$ and its feature $$x_v$$, calculate its normalized feature as follows:

$z_v = \frac{\exp(x_v)}{\sum_{u\in\mathcal{V}}\exp(x_u)}$

Each subgraph computes softmax independently. The result tensor has the same shape as the original node feature.

Parameters

node_feat (Tensor) – a tensor represent the node feature, with shape $$(N\_NODES, F)$$, $$F$$ is the feature size.

Returns

Tensor, a tensor with shape $$(N\_NODES, F)$$, $$F$$ is the feature size.

Raises

TypeError – If node_feat is not a Tensor.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> node_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestSoftmaxNodes(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.softmax_nodes(x)
...
>>> ret = TestSoftmaxNodes()(node_feat, *batched_graph_field.get_batched_graph()).asnumpy()
>>> print(np.array2string(ret, formatter={'float_kind':'{0:.5f}'.format}))
[[0.21194, 0.09003, 0.66524, 0.42232],
[0.57612, 0.66524, 0.09003, 0.15536],
[0.21194, 0.24473, 0.24473, 0.42232],
[0.57601, 0.42112, 0.24364, 0.84315],
[0.21190, 0.42112, 0.66227, 0.04198],
[0.21190, 0.15492, 0.08963, 0.11411],
[0.00019, 0.00284, 0.00446, 0.00077]]

sum_edges(edge_feat)[source]

Aggregating edge features and generates a graph-level representation by aggregation type ‘sum’.

The edge_feat should have shape $$(N\_EDGES, F)$$. Sum_edges operation will aggregate the edge_feat. according to edge_subgraph_idx. The output tensor will have a shape $$(N\_GRAPHS, F)$$. $$F$$ is the dimension of the edge feature.

Parameters

edge_feat (Tensor) – a tensor represents the edge feature, with shape $$(N\_EDGES, F)$$. $$F$$ is the dimension of the edge attribute.

Returns

Tensor, a tensor with shape $$(N\_GRAPHS, F)$$. $$F$$ is the dimension of the edge attribute.

Raises

TypeError – If edge_feat is not a Tensor which is the type of operation ‘shape’.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> edge_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [9, 7, 5, 8],
...     [8, 7, 6, 5],
...     [8, 6, 4, 6],
...     [1, 2, 1, 1],
...     [3, 2, 3, 3],
... ], 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestSumEdges(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.sum_edges(x)
...
>>> ret = TestSumEdges()(edge_feat, *batched_graph_field.get_batched_graph()).asnumpy().tolist()
>>> print(ret)
[[4.0, 9.0, 6.0, 11.0], [29.0, 24.0, 19.0, 23.0]]

sum_nodes(node_feat)[source]

Aggregating node features and generates a graph-level representation by aggregation type ‘sum’.

The node_feat should have shape $$(N\_NODES, F)$$, Sum_nodes operation will aggregate the nodes feat according to ver_subgraph_idx. The output tensor will have a shape $$(N\_GRAPHS, F)$$. $$F$$ is the dimension of the node feature.

Parameters

node_feat (Tensor) – a tensor represents the node feature, with shape $$(N\_NODES, F)$$, $$F$$ is the dimension of the node node feature.

Returns

Tensor, a tensor with shape $$(N\_GRAPHS, F)$$, $$F$$ is the dimension of the node feature.

Raises

TypeError – If node_feat is not a Tensor which is the type of operation ‘shape’.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> node_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestSumNodes(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.sum_nodes(x)
...
>>> ret = TestSumNodes()(node_feat, *batched_graph_field.get_batched_graph()).asnumpy().tolist()
>>> print(ret)
[[4.0, 9.0, 6.0, 11.0], [26.0, 22.0, 16.0, 20.0]]

topk_edges(edge_feat, k, sortby=None)[source]

Return a graph-level representation by a graph-wise top-k on edge features.

If sortby is set to None, the function would perform top-k on all dimensions independently.

Parameters
• edge_feat (Tensor) – A tensor represent the edge feature, with shape $$(N\_EDGES, F)$$, $$F$$ is the feature size.

• k (int) – Represent how many edges for top-k.

• sortby (int) – Sort according to which feature. If is None, all features are sorted independently. Default is None.

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 $$(B, K, F)$$, where $$B$$ is the batch size of the input graph, $$K$$ is input ‘k’, $$F$$ is the feature size.

• topk_indices (Tensor), - a tensor with shape $$(B, K)$$ ($$(B, K, F)$$ if sortby is set to None) where $$B$$ is the batch size of the input graph, $$K$$ is input ‘k’, $$F$$ is the feature size.

Raises
Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> edge_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [9, 7, 5, 8],
...     [8, 7, 6, 5],
...     [8, 6, 4, 6],
...     [1, 2, 1, 1],
...     [3, 2, 3, 3],
... ], 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestTopkEdges(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.topk_edges(x, 2, 1)
...
>>> output, indices = TestTopkEdges()(edge_feat, *batched_graph_field.get_batched_graph())
>>> output = output.asnumpy().tolist()
>>> indices = indices.asnumpy().tolist()
>>> print(output)
[[[2.0, 4.0, 1.0, 3.0], [1.0, 3.0, 2.0, 4.0]], [[9.0, 7.0, 5.0, 8.0], [8.0, 7.0, 6.0, 5.0]]]
>>> print(indices)
[[1, 2], [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.

Parameters
• node_feat (Tensor) – A tensor represent the node feature, with shape $$(N\_NODES, F)$$. $$F$$ is the dimension of the node feature.

• k (int) – Represent how many nodes for top-k.

• sortby (int) – Sort according to which feature. If is None, all features are sorted independently. Default is None.

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 $$(B, K, F)$$, where $$B$$ is the batch size of the input graph. $$K$$ is the input ‘k’, $$F$$ is the feature size.

• topk_indices (Tensor), - a tensor with shape $$(B, K)$$ if sortby is set to None), where $$B$$ is the batch size of the input graph, $$F$$ is the feature size.

Raises
Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraph, BatchedGraphField
>>> from mindspore_gl.nn import GNNCell
>>> node_feat = ms.Tensor([
...     # graph 1:
...     [1, 2, 3, 4],
...     [2, 4, 1, 3],
...     [1, 3, 2, 4],
...     # graph 2:
...     [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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1], ms.int32)
>>> graph_mask = ms.Tensor([1, 1], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,
...
>>> class TestTopkNodes(GNNCell):
...     def construct(self, x, bg: BatchedGraph):
...         return bg.topk_nodes(x, 2, 1)
...
>>> output, indices = TestTopkNodes()(node_feat, *batched_graph_field.get_batched_graph())
>>> output = output.asnumpy().tolist()
>>> indices = indices.asnumpy().tolist()
>>> print(output)
[[2.0, 4.0, 1.0, 3.0], [1.0, 3.0, 2.0, 4.0]], [[9.0, 7.0, 5.0, 8.0], [8.0, 7.0, 6.0, 5.0]]
>>> print(indices)
[[1, 2], [3, 4]]

property ver_subgraph_idx

A tensor with shape $$(N)$$, indicates each node belonging to which subgraph, $$N$$ is the number of the nodes of the graph.

class mindspore_gl.parser.HeterGraph[source]

The heterogeneous Graph.

This is the class which should be annotated in construct function for GNNCell class.

property dst_idx

A list of tensor with shape $$(N\_EDGES)$$, represents the destination node index of COO edge matrix.

get_homo_graph(etype)[source]

Get the specific nodes, edges for etype.

Parameters

etype (int) – The edge type.

Returns

List[Tensor], a homo graph.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import Graph, HeterGraph, HeterGraphField
>>> from mindspore_gl.nn import GNNCell
>>> n_nodes = [9, 2]
>>> n_edges = [11, 1]
>>> src_idx = [ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32), ms.Tensor([0], ms.int32)]
>>> dst_idx = [ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32), ms.Tensor([1], ms.int32)]
>>> heter_graph_field = HeterGraphField(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]
...
>>> class TestHeterGraph(GNNCell):
...     def __init__(self):
...         super().__init__()
...         self.sum = TestSum()
...
...     def construct(self, x, hg: HeterGraph):
...         return self.sum(x, *hg.get_homo_graph(0))
...
>>> ret = TestHeterGraph()(node_feat, *heter_graph_field.get_heter_graph()).asnumpy().tolist()
>>> print(ret)
[[1.0], [2.0], [0.0], [0.0], [3.0], [2.0], [1.0], [0.0], [3.0]]

property n_edges

A list of integer, represent the edges count of the graph.

property n_nodes

A list of integer, represent the nodes count of the graph.

property src_idx

A list of tensor with shape $$(N\_EDGES)$$, represents the source node index of COO edge matrix.

class mindspore_gl.parser.GraphField(src_idx, dst_idx, n_nodes, n_edges)[source]

The data container for a graph.

The edge information are stored in COO format.

Parameters
• src_idx (Tensor) – A tensor with shape $$(N\_EDGES)$$, with int dtype, represents the source node index of COO edge matrix.

• dst_idx (Tensor) – A tensor with shape $$(N\_EDGES)$$, with int dtype, represents the destination node index of COO edge matrix.

• n_nodes (int) – An integer, represent the nodes count of the graph.

• n_edges (int) – An integer, represent the edges count of the graph.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import GraphField
>>> 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)

get_graph()[source]

Get the Graph.

Returns

List, A list of tensor, which should be used for construct function.

class mindspore_gl.parser.BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges, ver_subgraph_idx, edge_subgraph_idx, graph_mask)[source]

The data container for a batched graph.

The edge information are stored in COO format.

Parameters
• src_idx (Tensor) – A tensor with shape $$(N\_EDGES)$$, with int dtype, represents the source node index of COO edge matrix.

• dst_idx (Tensor) – A tensor with shape $$(N\_EDGES)$$, with int dtype, represents the destination node index of COO edge matrix.

• n_nodes (int) – An integer, represent the nodes count of the graph.

• n_edges (int) – An integer, represent the edges count of the graph.

• ver_subgraph_idx (Tensor) – A tensor with shape $$(N\_NODES)$$, with int dtype, indicates each node belonging to which subgraph.

• edge_subgraph_idx (Tensor) – A tensor with shape $$(N\_EDGES,)$$, with int dtype, indicates each edge belonging to which subgraph.

• graph_mask (Tensor) – A tensor with shape $$(N\_GRAPHS,)$$, with int dtype, indicates whether the subgraph is exist.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import BatchedGraphField
>>> 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)
>>> ver_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 2, 2], ms.int32)
>>> edge_subgraph_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2], ms.int32)
>>> graph_mask = ms.Tensor([1, 1, 0], ms.int32)
>>> batched_graph_field = BatchedGraphField(src_idx, dst_idx, n_nodes, n_edges,

get_batched_graph()[source]

Get the batched Graph.

Returns

List, A list of tensor, which should be used for construct function.

class mindspore_gl.parser.HeterGraphField(src_idx, dst_idx, n_nodes, n_edges)[source]

The data container for a heterogeneous graph. The edge information are stored in COO format.

Parameters
• src_idx (List[Tensor]) – A list of tensor with shape $$(N\_EDGES)$$, with int dtype, represents the source node index of COO edge matrix.

• dst_idx (List[Tensor]) – A list of tensor with shape $$(N\_EDGES)$$, with int dtype, represents the destination node index of COO edge matrix.

• n_nodes (List[int]) – A list of integer, represent the nodes count of the graph.

• n_edges (List[int]) – A list of integer, represent the edges count of the graph.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore_gl import HeterGraphField
>>> n_nodes = [9, 2]
>>> n_edges = [11, 1]
>>> src_idx = [ms.Tensor([0, 2, 2, 3, 4, 5, 5, 6, 8, 8, 8], ms.int32), ms.Tensor([0], ms.int32)]
>>> dst_idx = [ms.Tensor([1, 0, 1, 5, 3, 4, 6, 4, 8, 8, 8], ms.int32), ms.Tensor([1], ms.int32)]
>>> heter_graph_field = HeterGraphField(src_idx, dst_idx, n_nodes, n_edges)

get_heter_graph()[source]

Get the hetergenous Graph.

Returns

List, A list of tensor list, which should be used for construct function.

mindspore_gl.parser.translate(obj, method_name: str)[source]

Translate the vertex central code into MindSpore understandable code.

After translation, a new function will generate in /.mindspore_gl. The origin method will be replaced with this function.

Parameters
• obj – (Object): The object.

• method_name (str) – The name of the method to be translated.