dgl.DGLGraph.dstdata

property DGLGraph.dstdata

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

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

If g is a graph of multiple destination node types, g.dstdata[feat] returns a dict[str, Tensor] mapping destination 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 destination node type(s) by setting g.dstdata[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 destination node type.

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

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

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

See also

nodes, ndata, dstnodes