DGLGraph.set_e_initializer(initializer, field=None)[source]

Set the initializer for empty edge features.

Initializer is a callable that returns a tensor given the shape, data type and device context.

When a subset of the edges are assigned a new feature, initializer is used to create feature for rest of the edges.

  • initializer (callable) – The initializer.
  • field (str, optional) – The feature field name. Default is set an initializer for all the feature fields.



Here we use pytorch syntax for demo. The general idea applies to other frameworks with minor syntax change (e.g. replace torch.tensor with mxnet.ndarray).

>>> import torch as th
>>> G = dgl.DGLGraph()
>>> G.add_nodes(3)
>>> G.add_edges([0, 1], 2)  # 0->2, 1->2

Set initializer for edge features

>>> G.set_e_initializer(dgl.init.zero_initializer)

Set feature for partial edges

>>> G.edges[1, 2].data['y'] = th.ones((1, 4))
>>> G.edata
{'y' : tensor([[0., 0., 0., 0.],
               [1., 1., 1., 1.]])}


User defined initializer must follow the signature of dgl.init.base_initializer()