dgl.heterograph๏ƒ

dgl.heterograph(data_dict, num_nodes_dict=None, idtype=None, device=None)[source]๏ƒ

Create a heterogeneous graph and return.

Parameters:
  • data_dict (graph data) โ€“

    The dictionary data for constructing a heterogeneous graph. The keys are in the form of string triplets (src_type, edge_type, dst_type), specifying the source node, edge, and destination node types. The values are graph data in the form of \((U, V)\), where \((U[i], V[i])\) forms the edge with ID \(i\). The allowed graph data formats are:

    • (Tensor, Tensor): Each tensor must be a 1D tensor containing node IDs. DGL calls this format โ€œtuple of node-tensorsโ€. The tensors should have the same data type, which must be either int32 or int64. They should also have the same device context (see below the descriptions of idtype and device).

    • ('coo', (Tensor, Tensor)): Same as (Tensor, Tensor).

    • ('csr', (Tensor, Tensor, Tensor)): The three tensors form the CSR representation of the graphโ€™s adjacency matrix. The first one is the row index pointer. The second one is the column indices. The third one is the edge IDs, which can be empty (i.e. with 0 elements) to represent consecutive integer IDs starting from 0.

    • ('csc', (Tensor, Tensor, Tensor)): The three tensors form the CSC representation of the graphโ€™s adjacency matrix. The first one is the column index pointer. The second one is the row indices. The third one is the edge IDs, which can be empty to represent consecutive integer IDs starting from 0.

    The tensors can be replaced with any iterable of integers (e.g. list, tuple, numpy.ndarray).

  • num_nodes_dict (dict[str, int], optional) โ€“ The number of nodes for some node types, which is a dictionary mapping a node type \(T\) to the number of \(T\)-typed nodes. If not given for a node type \(T\), DGL finds the largest ID appearing in every graph data whose source or destination node type is \(T\), and sets the number of nodes to be that ID plus one. If given and the value is no greater than the largest ID for some node type, DGL will raise an error. By default, DGL infers the number of nodes for all node types.

  • idtype (int32 or int64, optional) โ€“ The data type for storing the structure-related graph information such as node and edge IDs. It should be a framework-specific data type object (e.g., torch.int32). If None (default), DGL infers the ID type from the data_dict argument.

  • device (device context, optional) โ€“ The device of the returned graph, which should be a framework-specific device object (e.g., torch.device). If None (default), DGL uses the device of the tensors of the data argument. If data is not a tuple of node-tensors, the returned graph is on CPU. If the specified device differs from that of the provided tensors, it casts the given tensors to the specified device first.

Returns:

The created graph.

Return type:

DGLGraph

Notes

  1. If the idtype argument is not given then:

    • in the case of the tuple of node-tensor format, DGL uses the data type of the given ID tensors.

    • in the case of the tuple of sequence format, DGL uses int64.

    Once the graph has been created, you can change the data type by using dgl.DGLGraph.long() or dgl.DGLGraph.int().

    If the specified idtype argument differs from the data type of the provided tensors, it casts the given tensors to the specified data type first.

  2. The most efficient construction approach is to provide a tuple of node tensors without specifying idtype and device. This is because the returned graph shares the storage with the input node-tensors in this case.

  3. DGL internally maintains multiple copies of the graph structure in different sparse formats and chooses the most efficient one depending on the computation invoked. If memory usage becomes an issue in the case of large graphs, use dgl.DGLGraph.formats() to restrict the allowed formats.

  4. DGL internally decides a deterministic order for the same set of node types and canonical edge types, which does not necessarily follow the order in data_dict.

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Create a heterograph with three canonical edge types.

>>> data_dict = {
...     ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])),
...     ('user', 'follows', 'topic'): (torch.tensor([1, 1]), torch.tensor([1, 2])),
...     ('user', 'plays', 'game'): (torch.tensor([0, 3]), torch.tensor([3, 4]))
... }
>>> g = dgl.heterograph(data_dict)
>>> g
Graph(num_nodes={'game': 5, 'topic': 3, 'user': 4},
      num_edges={('user', 'follows', 'topic'): 2, ('user', 'follows', 'user'): 2,
                 ('user', 'plays', 'game'): 2},
      metagraph=[('user', 'topic', 'follows'), ('user', 'user', 'follows'),
                 ('user', 'game', 'plays')])

Explicitly specify the number of nodes for each node type in the graph.

>>> num_nodes_dict = {'user': 4, 'topic': 4, 'game': 6}
>>> g = dgl.heterograph(data_dict, num_nodes_dict=num_nodes_dict)

Create a graph on the first GPU with data type int32.

>>> g = dgl.heterograph(data_dict, idtype=torch.int32, device='cuda:0')