sum_edges(graph, feat, weight=None)¶
Sums all the values of edge field
graph, optionally multiplies the field by a scalar edge field
- graph (DGLGraph) – The graph.
- feat (str) – The feature field.
- weight (str, optional) – The weight field. If None, no weighting will be performed,
otherwise, weight each edge feature with field
feat. for summation. The weight feature associated in the
graphshould be a tensor of shape
The summed tensor.
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 edges, a zero tensor with the same shape is returned at the corresponding row.
>>> import dgl >>> import torch as th
DGLGraphobjects and initialize their edge features.
>>> g1 = dgl.DGLGraph() # Graph 1 >>> g1.add_nodes(2) >>> g1.add_edges([0, 1], [1, 0]) >>> g1.edata['h'] = th.tensor([[1.], [2.]]) >>> g1.edata['w'] = th.tensor([[3.], [6.]])
>>> g2 = dgl.DGLGraph() # Graph 2 >>> g2.add_nodes(3) >>> g2.add_edges([0, 1, 2], [1, 2, 0]) >>> g2.edata['h'] = th.tensor([[1.], [2.], [3.]])
Sum over edge attribute
hwithout weighting for each graph in a batched graph.
>>> bg = dgl.batch([g1, g2], edge_attrs='h') >>> dgl.sum_edges(bg, 'h') tensor([[3.], # 1 + 2 [6.]]) # 1 + 2 + 3
Sum edge attribute
hwith weight from edge attribute
wfor a single graph.
>>> dgl.sum_edges(g1, 'h', 'w') tensor([[15.]]) # 1 * 3 + 2 * 6