dgl.DGLGraph.local_scope

DGLGraph.local_scope()[source]

Enter a local scope context for the graph.

By entering a local scope, any out-place mutation to the feature data will not reflect to the original graph, thus making it easier to use in a function scope (e.g. forward computation of a model).

If set, the local scope will use same initializers for node features and edge features.

Notes

Inplace operations do reflect to the original graph. This function also has little overhead when the number of feature tensors in this graph is small.

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Create a function for computation on graphs.

>>> def foo(g):
...     with g.local_scope():
...         g.edata['h'] = torch.ones((g.num_edges(), 3))
...         g.edata['h2'] = torch.ones((g.num_edges(), 3))
...         return g.edata['h']

local_scope avoids changing the graph features when exiting the function.

>>> g = dgl.graph((torch.tensor([0, 1, 1]), torch.tensor([0, 0, 2])))
>>> g.edata['h'] = torch.zeros((g.num_edges(), 3))
>>> newh = foo(g)
>>> print(g.edata['h'])  # still get tensor of all zeros
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
>>> 'h2' in g.edata      # new feature set in the function scope is not found
False

In-place operations will still reflect to the original graph.

>>> def foo(g):
...     with g.local_scope():
...         # in-place operation
...         g.edata['h'] += 1
...         return g.edata['h']
>>> g = dgl.graph((torch.tensor([0, 1, 1]), torch.tensor([0, 0, 2])))
>>> g.edata['h'] = torch.zeros((g.num_edges(), 1))
>>> newh = foo(g)
>>> print(g.edata['h'])  # the result changes
tensor([[1.],
        [1.],
        [1.]])

See also

local_var