dgl.broadcast_nodes

dgl.broadcast_nodes(graph, feat_data)[source]

Broadcast feat_data to all nodes in graph, and return a tensor of node features.

Parameters:
  • graph (DGLGraph or BatcheDGLGraph) – The graph.
  • feat_data (tensor) – The feature to broadcast. Tensor shape is \((*)\) for single graph, and \((B, *)\) for batched graph.
Returns:

The node features tensor with shape \((N, *)\).

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)
>>> g2 = dgl.DGLGraph()                           # Graph 2
>>> g2.add_nodes(3)
>>> bg = dgl.batch([g1, g2])
>>> feat = th.rand(2, 5)
>>> feat
tensor([[0.4325, 0.7710, 0.5541, 0.0544, 0.9368],
        [0.2721, 0.4629, 0.7269, 0.0724, 0.1014]])

Broadcast feature to all nodes in the batched graph, feat[i] is broadcast to nodes in the i-th example in the batch.

>>> dgl.broadcast_nodes(bg, feat)
tensor([[0.4325, 0.7710, 0.5541, 0.0544, 0.9368],
        [0.4325, 0.7710, 0.5541, 0.0544, 0.9368],
        [0.2721, 0.4629, 0.7269, 0.0724, 0.1014],
        [0.2721, 0.4629, 0.7269, 0.0724, 0.1014],
        [0.2721, 0.4629, 0.7269, 0.0724, 0.1014]])

Broadcast feature to all nodes in the batched graph.

>>> dgl.broadcast_nodes(g1, feat[0])
tensor([[0.4325, 0.7710, 0.5541, 0.0544, 0.9368],
        [0.4325, 0.7710, 0.5541, 0.0544, 0.9368]])

Notes

If graph is a BatchedDGLGraph object, feat[i] is broadcast to the nodes in i-th example in the batch.