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