mindspore_gl.Graph
- class mindspore_gl.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) ... >>> 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 \((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
- 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, *)\), \(*\) is the shape of the feature per edge. 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]]
- 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 \((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
TypeError – If node_feat is not a Tensor.
TypeError – If k is not an int.
ValueError – If sortby is not an int.
- Supported Platforms:
GPU
Examples
>>> import mindspore as ms >>> from mindspore_gl import BatchedGraph, BatchedGraphField >>> 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.
- 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 \((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
TypeError – If node_feat is not a Tensor.
TypeError – If k is not an int.
ValueError – If sortby is not an int.
- Supported Platforms:
GPU
Examples
>>> import mindspore as ms >>> from mindspore_gl import BatchedGraph, BatchedGraphField >>> 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]