class dgl.DGLHeteroGraph(gidx=[], ntypes=['_U'], etypes=['_V'], node_frames=None, edge_frames=None, **deprecate_kwargs)[source]

Class for storing graph structure and node/edge feature data.

There are a few ways to create a DGLGraph:

  • To create a homogeneous graph from Tensor data, use dgl.graph().

  • To create a heterogeneous graph from Tensor data, use dgl.heterograph().

  • To create a graph from other data sources, use dgl.* create ops. See Graph Create Ops.

Read the user guide chapter Chapter 1: Graph for an in-depth explanation about its usage.

Conversion to and from heterogeneous graphs

Module for converting graph from/to other object.

graph(data[, ntype, etype, num_nodes, …])

Create a graph and return.

bipartite(data[, utype, etype, vtype, …])

DEPRECATED: use dgl.heterograph instead.

hetero_from_relations(rel_graphs[, …])

DEPRECATED: use dgl.heterograph instead.

heterograph(data_dict[, num_nodes_dict, …])

Create a heterogeneous graph and return.

to_hetero(G, ntypes, etypes[, ntype_field, …])

Convert the given homogeneous graph to a heterogeneous graph.


Convert the given heterogeneous graph to a homogeneous graph.

to_networkx(g[, node_attrs, edge_attrs])

Convert a homogeneous graph to a NetworkX graph and return.

DGLHeteroGraph.adjacency_matrix([transpose, …])

Alias of adj()

DGLHeteroGraph.incidence_matrix(typestr[, …])

Return the incidence matrix representation of edges with the given edge type.

Querying metagraph structure


Return all the node type names in the graph.


Return all the edge type names in the graph.


Return all the canonical edge types in the graph.


Return the metagraph of the heterograph.


Convert an edge type to the corresponding canonical edge type in the graph.


Return the ID of the given node type.


Return the id of the given edge type.

Querying graph structure


Alias of num_nodes()


Alias of num_edges()


Return whether the graph is a multigraph with parallel edges.


DGLGraph will always be mutable.

DGLHeteroGraph.has_node(vid[, ntype])

Whether the graph has a particular node of a given type.

DGLHeteroGraph.has_nodes(vid[, ntype])

Return whether the graph contains the given nodes.

DGLHeteroGraph.has_edge_between(u, v[, etype])

Whether the graph has edges of type etype.

DGLHeteroGraph.has_edges_between(u, v[, etype])

Return whether the graph contains the given edges.

DGLHeteroGraph.predecessors(v[, etype])

Return the predecessor(s) of a particular node with the specified edge type.

DGLHeteroGraph.successors(v[, etype])

Return the successor(s) of a particular node with the specified edge type.

DGLHeteroGraph.edge_id(u, v[, force_multi, …])

Return the edge ID, or an array of edge IDs, between source node u and destination node v, with the specified edge type

DGLHeteroGraph.edge_ids(u, v[, force_multi, …])

Return the edge ID(s) given the two endpoints of the edge(s).

DGLHeteroGraph.find_edges(eid[, etype])

Return the source and destination node ID(s) given the edge ID(s).

DGLHeteroGraph.in_edges(v[, form, etype])

Return the incoming edges of the given nodes.

DGLHeteroGraph.out_edges(u[, form, etype])

Return the outgoing edges of the given nodes.

DGLHeteroGraph.all_edges([form, order, etype])

Return all edges with the specified edge type.

DGLHeteroGraph.in_degree(v[, etype])

Return the in-degree of node v with edges of type etype.

DGLHeteroGraph.in_degrees([v, etype])

Return the in-degree(s) of the given nodes.

DGLHeteroGraph.out_degree(u[, etype])

Return the out-degree of node u with edges of type etype.

DGLHeteroGraph.out_degrees([u, etype])

Return the out-degree(s) of the given nodes.

Querying and manipulating sparse format

Querying and manipulating index data type


The data type for storing the structure-related graph information such as node and edge IDs.


Cast the graph to one with idtype int64


Cast the graph to one with idtype int32

Using Node/edge features


Return a node view


Return a node data view for setting/getting node features


Return an edge view


Return an edge data view for setting/getting edge features.


Return the node feature schemes for the specified type.


Return the edge feature schemes for the specified type.


Set the initializer for node features.


Set the initializer for edge features.


Return a graph object for usage in a local function scope.


Enter a local scope context for the graph.

Computing with DGLHeteroGraph

DGLHeteroGraph.apply_nodes(func[, v, ntype, …])

Update the features of the specified nodes by the provided function.

DGLHeteroGraph.apply_edges(func[, edges, …])

Update the features of the specified edges by the provided function.

DGLHeteroGraph.group_apply_edges(group_by, func)

DEPRECATED: The API is removed in 0.5.

DGLHeteroGraph.send(edges, message_func[, etype])

Send messages along the given edges with the same edge type.

DGLHeteroGraph.recv(v, reduce_func[, …])

Receive and reduce incoming messages and update the features of node(s) \(v\).

DGLHeteroGraph.multi_recv(v, reducer_dict, …)

Receive messages from multiple edge types and perform aggregation.

DGLHeteroGraph.send_and_recv(edges, …[, …])

Send messages along the specified edges and reduce them on the destination nodes to update their features.

DGLHeteroGraph.multi_send_and_recv(…[, …])

DEPRECATED: The API is removed in v0.5.

DGLHeteroGraph.pull(v, message_func, reduce_func)

Pull messages from the specified node(s)’ predecessors along the specified edge type, aggregate them to update the node features.

DGLHeteroGraph.multi_pull(v, etype_dict, …)

DEPRECATED: The API is removed in v0.5.

DGLHeteroGraph.push(u, message_func, reduce_func)

Send message from the specified node(s) to their successors along the specified edge type and update their node features.

DGLHeteroGraph.update_all(message_func, …)

Send messages along all the edges of the specified type and update all the nodes of the corresponding destination type.

DGLHeteroGraph.multi_update_all(etype_dict, …)

Send messages along all the edges, reduce them by first type-wisely then across different types, and then update the node features of all the nodes.

DGLHeteroGraph.prop_nodes(nodes_generator, …)

Propagate messages using graph traversal by sequentially triggering pull() on nodes.

DGLHeteroGraph.prop_edges(edges_generator, …)

Propagate messages using graph traversal by sequentially triggering send_and_recv() on edges.

DGLHeteroGraph.filter_nodes(predicate[, …])

Return the IDs of the nodes with the given node type that satisfy the given predicate.

DGLHeteroGraph.filter_edges(predicate[, …])

Return the IDs of the edges with the given edge type that satisfy the given predicate.

DGLHeteroGraph.to(device, **kwargs)

Move ndata, edata and graph structure to the targeted device (cpu/gpu).