dgl.DGLGraph.edata

property DGLGraph.edata

Return an edge data view for setting/getting edge features.

Let g be a DGLGraph. If g is a graph of a single edge type, g.edata[feat] returns the edge feature associated with the name feat. One can also set an edge feature associated with the name feat by setting g.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 name feat for the corresponding type. One can also set an edge feature associated with the name feat for some edge type(s) by setting g.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

edges