dgl.DGLGraph.dstdataο
- property DGLGraph.dstdataο
Return a node data view for setting/getting destination node features.
Let
g
be a DGLGraph. Ifg
is a graph of a single destination node type,g.dstdata[feat]
returns the destination node feature associated with the namefeat
. One can also set a destination node feature associated with the namefeat
by settingg.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 namefeat
for the corresponding type. One can also set a node feature associated with the namefeat
for some destination node type(s) by settingg.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.]])}