dgl.max_nodes

dgl.max_nodes(graph, feat)[source]

Take elementwise maximum over all the values of node field feat in graph

Parameters:
  • graph (DGLGraph) – The graph.
  • feat (str) – The feature field.
Returns:

The tensor obtained.

Return type:

tensor

Examples

>>> import dgl
>>> import torch as th

Create two DGLGraph objects and initialize their node features.

>>> g1 = dgl.DGLGraph()                           # Graph 1
>>> g1.add_nodes(2)
>>> g1.ndata['h'] = th.tensor([[1.], [2.]])
>>> g2 = dgl.DGLGraph()                           # Graph 2
>>> g2.add_nodes(3)
>>> g2.ndata['h'] = th.tensor([[1.], [2.], [3.]])

Max over node attribute h in a batched graph.

>>> bg = dgl.batch([g1, g2], node_attrs='h')
>>> dgl.max_nodes(bg, 'h')
tensor([[2.],    # max(1, 2)
        [3.]])   # max(1, 2, 3)

Max over node attribute h in a single graph.

>>> dgl.max_nodes(g1, 'h')
tensor([[2.]])

Notes

Return a stacked tensor with an extra first dimension whose size equals batch size of the input graph. The i-th row of the stacked tensor contains the readout result of the i-th graph in the batched graph. If a graph has no nodes, a tensor filed with -inf of the same shape is returned at the corresponding row.