dgl.DGLGraph.set_n_initializer

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

Set the initializer for empty node features.

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

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

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

Examples

Note

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)

Set initializer for all node features

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

Set feature for partial nodes

>>> G.nodes[[0, 2]].data['x'] = th.ones((2, 5))
>>> G.ndata
{'x' : tensor([[1., 1., 1., 1., 1.],
               [0., 0., 0., 0., 0.],
               [1., 1., 1., 1., 1.]])}

Note

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