dgl.DGLHeteroGraph.set_e_initializer

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

Set the initializer for edge features.

When only part of the edges have a feature (e.g. new edges are added, features are set for a subset of edges), the initializer initializes features for the rest edges.

Parameters
  • initializer (callable) –

    A function of signature func(shape, dtype, ctx, id_range) -> Tensor. The tensor will be the initialized features. The arguments are:

    • shape: The shape of the tensor to return, which is a tuple of int. The first dimension is the number of edges for feature initialization.

    • dtype: The data type of the tensor to return, which is a framework-specific data type object.

    • ctx: The device of the tensor to return, which is a framework-specific device object.

    • id_range: The start and end ID of the edges for feature initialization, which is a slice.

  • field (str, optional) – The name of the feature that the initializer applies. If not given, the initializer applies to all features.

  • etype (str or (str, str, str), optional) –

    The type names of the edges. The allowed type name formats are:

    • (str, str, str) for source node type, edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Notes

Without setting an edge feature initializer, zero tensors are generated for edges without a feature.

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Define a function for initializer.

>>> def init_feats(shape, dtype, device, id_range):
...     return torch.ones(shape, dtype=dtype, device=device)

An example for a homogeneous graph.

>>> g = dgl.graph((torch.tensor([0]), torch.tensor([1])))
>>> g.edata['h1'] = torch.zeros(1, 2)
>>> g.edata['h2'] = torch.ones(1, 1)
>>> # Apply the initializer to feature 'h2' only.
>>> g.set_e_initializer(init_feats, field='h2')
>>> g.add_edges(torch.tensor([1]), torch.tensor([1]))
>>> print(g.edata['h1'])
tensor([[0., 0.],
        [0., 0.]])
>>> print(g.edata['h2'])
tensor([[1.], [1.]])

An example for a heterogeneous graph of multiple edge types.

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): (torch.tensor([0, 1]),
...                                 torch.tensor([0, 0])),
...     ('developer', 'develops', 'game'): (torch.tensor([0, 1]),
...                                         torch.tensor([0, 1]))
...     })
>>> g.edges['plays'].data['h'] = torch.zeros(2, 2)
>>> g.edges['develops'].data['w'] = torch.ones(2, 2)
>>> g.set_e_initializer(init_feats, etype='plays')
>>> # Initializer not set for 'develops', use zero tensors by default
>>> g.add_edges(torch.tensor([1]), torch.tensor([1]), etype='develops')
>>> g.edges['develops'].data['w']
tensor([[1., 1.],
        [1., 1.],
        [0., 0.]])
>>> # Initializer set for 'plays'
>>> g.add_edges(torch.tensor([1]), torch.tensor([1]), etype='plays')
>>> g.edges['plays'].data['h']
tensor([[0., 0.],
        [0., 0.],
        [1., 1.]])