ToSimple

class dgl.transforms.ToSimple(return_counts='count', aggregator='arbitrary')[source]

Bases: BaseTransform

Convert a graph to a simple graph without parallel edges and return a new graph.

Parameters:
  • return_counts (str, optional) – The edge feature name to hold the edge count in the original graph.

  • aggregator (str, optional) –

    The way to coalesce features of duplicate edges.

    • 'arbitrary': select arbitrarily from one of the duplicate edges

    • 'sum': take the sum over the duplicate edges

    • 'mean': take the mean over the duplicate edges

Example

The following example uses PyTorch backend.

>>> import dgl
>>> import torch
>>> from dgl import ToSimple

Case1: Convert a homogeneous graph to a simple graph

>>> transform = ToSimple()
>>> g = dgl.graph(([0, 1, 1], [1, 2, 2]))
>>> g.edata['w'] = torch.tensor([[0.1], [0.2], [0.3]])
>>> sg = transform(g)
>>> print(sg.edges())
(tensor([0, 1]), tensor([1, 2]))
>>> print(sg.edata['count'])
tensor([1, 2])
>>> print(sg.edata['w'])
tensor([[0.1000], [0.2000]])

Case2: Convert a heterogeneous graph to a simple graph

>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): ([0, 1, 1], [1, 2, 2]),
...     ('user', 'plays', 'game'): ([0, 1, 0], [1, 1, 1])
... })
>>> sg = transform(g)
>>> print(sg.edges(etype='follows'))
(tensor([0, 1]), tensor([1, 2]))
>>> print(sg.edges(etype='plays'))
(tensor([0, 1]), tensor([1, 1]))