dgl.DGLGraph.edata¶
-
property
DGLGraph.
edata
¶ Return an edge data view for setting/getting edge features.
Let
g
be a DGLGraph. Ifg
is a graph of a single edge type,g.edata[feat]
returns the edge feature associated with the namefeat
. One can also set an edge feature associated with the namefeat
by settingg.edata[feat]
to a tensor.If
g
is a graph of multiple edge types,g.edata[feat]
returns a dict[str, Tensor] mapping canonical edge types to the edge features associated with the namefeat
for the corresponding type. One can also set an edge feature associated with the namefeat
for some edge type(s) by settingg.edata[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 edge type.
>>> g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2]))) >>> g.edata['h'] = torch.ones(2, 1) >>> g.edata['h'] tensor([[1.], [1.]])
Set and get feature ‘h’ for a graph of multiple edge types.
>>> g = dgl.heterograph({ ... ('user', 'follows', 'user'): (torch.tensor([1, 2]), torch.tensor([3, 4])), ... ('user', 'plays', 'user'): (torch.tensor([2, 2]), torch.tensor([1, 1])), ... ('player', 'plays', 'game'): (torch.tensor([2, 2]), torch.tensor([1, 1])) ... }) >>> g.edata['h'] = {('user', 'follows', 'user'): torch.zeros(2, 1), ... ('user', 'plays', 'user'): torch.ones(2, 1)} >>> g.edata['h'] {('user', 'follows', 'user'): tensor([[0.], [0.]]), ('user', 'plays', 'user'): tensor([[1.], [1.]])} >>> g.edata['h'] = {('user', 'follows', 'user'): torch.ones(2, 1)} >>> g.edata['h'] {('user', 'follows', 'user'): tensor([[1.], [1.]]), ('user', 'plays', 'user'): tensor([[1.], [1.]])}
See also