dgl.DGLGraph.srcdata

property DGLGraph.srcdata

Return a node data view for setting/getting source node features.

Let g be a DGLGraph. If g is a graph of a single source node type, g.srcdata[feat] returns the source node feature associated with the name feat. One can also set a source node feature associated with the name feat by setting g.srcdata[feat] to a tensor.

If g is a graph of multiple source node types, g.srcdata[feat] returns a dict[str, Tensor] mapping source node types to the node features associated with the name feat for the corresponding type. One can also set a node feature associated with the name feat for some source node type(s) by setting g.srcdata[feat] to a dictionary as described.

Notes

For setting features, the device of the features must be the same as the device of the graph.

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Set and get feature β€˜h’ for a graph of a single source node type.

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): (torch.tensor([0, 1]), torch.tensor([1, 2]))})
>>> g.srcdata['h'] = torch.ones(2, 1)
>>> g.srcdata['h']
tensor([[1.],
        [1.]])

Set and get feature β€˜h’ for a graph of multiple source node types.

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): (torch.tensor([1, 2]), torch.tensor([3, 4])),
...     ('player', 'plays', 'game'): (torch.tensor([2, 2]), torch.tensor([1, 1]))
... })
>>> g.srcdata['h'] = {'user': torch.zeros(3, 1), 'player': torch.ones(3, 1)}
>>> g.srcdata['h']
{'player': tensor([[1.], [1.], [1.]]),
 'user': tensor([[0.], [0.], [0.]])}
>>> g.srcdata['h'] = {'user': torch.ones(3, 1)}
>>> g.srcdata['h']
{'player': tensor([[1.], [1.], [1.]]),
 'user': tensor([[1.], [1.], [1.]])}

See also

nodes, ndata, srcnodes