torch_geometric.data.HeteroData

class HeteroData(_mapping: Optional[Dict[str, Any]] = None, **kwargs)[source]

Bases: BaseData, FeatureStore, GraphStore

A data object describing a heterogeneous graph, holding multiple node and/or edge types in disjunct storage objects. Storage objects can hold either node-level, link-level or graph-level attributes. In general, HeteroData tries to mimic the behavior of a regular nested Python dictionary. In addition, it provides useful functionality for analyzing graph structures, and provides basic PyTorch tensor functionalities.

from torch_geometric.data import HeteroData

data = HeteroData()

# Create two node types "paper" and "author" holding a feature matrix:
data['paper'].x = torch.randn(num_papers, num_paper_features)
data['author'].x = torch.randn(num_authors, num_authors_features)

# Create an edge type "(author, writes, paper)" and building the
# graph connectivity:
data['author', 'writes', 'paper'].edge_index = ...  # [2, num_edges]

data['paper'].num_nodes
>>> 23

data['author', 'writes', 'paper'].num_edges
>>> 52

# PyTorch tensor functionality:
data = data.pin_memory()
data = data.to('cuda:0', non_blocking=True)

Note that there exists multiple ways to create a heterogeneous graph data, e.g.:

  • To initialize a node of type "paper" holding a node feature matrix x_paper named x:

    from torch_geometric.data import HeteroData
    
    data = HeteroData()
    data['paper'].x = x_paper
    
    data = HeteroData(paper={ 'x': x_paper })
    
    data = HeteroData({'paper': { 'x': x_paper }})
    
  • To initialize an edge from source node type "author" to destination node type "paper" with relation type "writes" holding a graph connectivity matrix edge_index_author_paper named edge_index:

    data = HeteroData()
    data['author', 'writes', 'paper'].edge_index = edge_index_author_paper
    
    data = HeteroData(author__writes__paper={
        'edge_index': edge_index_author_paper
    })
    
    data = HeteroData({
        ('author', 'writes', 'paper'):
        { 'edge_index': edge_index_author_paper }
    })
    
property stores: List[BaseStorage]

Returns a list of all storages of the graph.

property node_types: List[str]

Returns a list of all node types of the graph.

property node_stores: List[NodeStorage]

Returns a list of all node storages of the graph.

property edge_types: List[Tuple[str, str, str]]

Returns a list of all edge types of the graph.

property edge_stores: List[EdgeStorage]

Returns a list of all edge storages of the graph.

node_items() List[Tuple[str, NodeStorage]][source]

Returns a list of node type and node storage pairs.

edge_items() List[Tuple[Tuple[str, str, str], EdgeStorage]][source]

Returns a list of edge type and edge storage pairs.

to_dict() Dict[str, Any][source]

Returns a dictionary of stored key/value pairs.

to_namedtuple() NamedTuple[source]

Returns a NamedTuple of stored key/value pairs.

update(data: HeteroData) HeteroData[source]

Updates the data object with the elements from another data object.

__cat_dim__(key: str, value: Any, store: Optional[Union[NodeStorage, EdgeStorage]] = None, *args, **kwargs) Any[source]

Returns the dimension for which the value value of the attribute key will get concatenated when creating mini-batches using torch_geometric.loader.DataLoader.

Note

This method is for internal use only, and should only be overridden in case the mini-batch creation process is corrupted for a specific attribute.

__inc__(key: str, value: Any, store: Optional[Union[NodeStorage, EdgeStorage]] = None, *args, **kwargs) Any[source]

Returns the incremental count to cumulatively increase the value value of the attribute key when creating mini-batches using torch_geometric.loader.DataLoader.

Note

This method is for internal use only, and should only be overridden in case the mini-batch creation process is corrupted for a specific attribute.

property num_nodes: Optional[int]

Returns the number of nodes in the graph.

property num_node_features: Dict[str, int]

Returns the number of features per node type in the graph.

property num_features: Dict[str, int]

Returns the number of features per node type in the graph. Alias for num_node_features.

property num_edge_features: Dict[Tuple[str, str, str], int]

Returns the number of features per edge type in the graph.

has_isolated_nodes() bool[source]

Returns True if the graph contains isolated nodes.

is_undirected() bool[source]

Returns True if graph edges are undirected.

validate(raise_on_error: bool = True) bool[source]

Validates the correctness of the data.

metadata() Tuple[List[str], List[Tuple[str, str, str]]][source]

Returns the heterogeneous meta-data, i.e. its node and edge types.

data = HeteroData()
data['paper'].x = ...
data['author'].x = ...
data['author', 'writes', 'paper'].edge_index = ...

print(data.metadata())
>>> (['paper', 'author'], [('author', 'writes', 'paper')])
collect(key: str) Dict[Union[str, Tuple[str, str, str]], Any][source]

Collects the attribute key from all node and edge types.

data = HeteroData()
data['paper'].x = ...
data['author'].x = ...

print(data.collect('x'))
>>> { 'paper': ..., 'author': ...}

Note

This is equivalent to writing data.x_dict.

get_node_store(key: str) NodeStorage[source]

Gets the NodeStorage object of a particular node type key. If the storage is not present yet, will create a new torch_geometric.data.storage.NodeStorage object for the given node type.

data = HeteroData()
node_storage = data.get_node_store('paper')
get_edge_store(src: str, rel: str, dst: str) EdgeStorage[source]

Gets the EdgeStorage object of a particular edge type given by the tuple (src, rel, dst). If the storage is not present yet, will create a new torch_geometric.data.storage.EdgeStorage object for the given edge type.

data = HeteroData()
edge_storage = data.get_edge_store('author', 'writes', 'paper')
rename(name: str, new_name: str) HeteroData[source]

Renames the node type name to new_name in-place.

subgraph(subset_dict: Dict[str, Tensor]) HeteroData[source]

Returns the induced subgraph containing the node types and corresponding nodes in subset_dict.

data = HeteroData()
data['paper'].x = ...
data['author'].x = ...
data['conference'].x = ...
data['paper', 'cites', 'paper'].edge_index = ...
data['author', 'paper'].edge_index = ...
data['paper', 'conference'].edge_index = ...
print(data)
>>> HeteroData(
    paper={ x=[10, 16] },
    author={ x=[5, 32] },
    conference={ x=[5, 8] },
    (paper, cites, paper)={ edge_index=[2, 50] },
    (author, to, paper)={ edge_index=[2, 30] },
    (paper, to, conference)={ edge_index=[2, 25] }
)

subset_dict = {
    'paper': torch.tensor([3, 4, 5, 6]),
    'author': torch.tensor([0, 2]),
}

print(data.subgraph(subset_dict))
>>> HeteroData(
    paper={ x=[4, 16] },
    author={ x=[2, 32] },
    (paper, cites, paper)={ edge_index=[2, 24] },
    (author, to, paper)={ edge_index=[2, 5] }
)
Parameters

subset_dict (Dict[str, LongTensor or BoolTensor]) – A dictonary holding the nodes to keep for each node type.

edge_subgraph(subset_dict: Dict[Tuple[str, str, str], Tensor]) HeteroData[source]

Returns the induced subgraph given by the edge indices in subset_dict for certain edge types. Will currently preserve all the nodes in the graph, even if they are isolated after subgraph computation.

Parameters

subset_dict (Dict[Tuple[str, str, str], LongTensor or BoolTensor]) – A dictonary holding the edges to keep for each edge type.

node_type_subgraph(node_types: List[str]) HeteroData[source]

Returns the subgraph induced by the given node_types, i.e. the returned HeteroData object only contains the node types which are included in node_types, and only contains the edge types where both end points are included in node_types.

edge_type_subgraph(edge_types: List[Tuple[str, str, str]]) HeteroData[source]

Returns the subgraph induced by the given edge_types, i.e. the returned HeteroData object only contains the edge types which are included in edge_types, and only contains the node types of the end points which are included in node_types.

to_homogeneous(node_attrs: Optional[List[str]] = None, edge_attrs: Optional[List[str]] = None, add_node_type: bool = True, add_edge_type: bool = True, dummy_values: bool = True) Data[source]

Converts a HeteroData object to a homogeneous Data object. By default, all features with same feature dimensionality across different types will be merged into a single representation, unless otherwise specified via the node_attrs and edge_attrs arguments. Furthermore, attributes named node_type and edge_type will be added to the returned Data object, denoting node-level and edge-level vectors holding the node and edge type as integers, respectively.

Parameters
  • node_attrs (List[str], optional) – The node features to combine across all node types. These node features need to be of the same feature dimensionality. If set to None, will automatically determine which node features to combine. (default: None)

  • edge_attrs (List[str], optional) – The edge features to combine across all edge types. These edge features need to be of the same feature dimensionality. If set to None, will automatically determine which edge features to combine. (default: None)

  • add_node_type (bool, optional) – If set to False, will not add the node-level vector node_type to the returned Data object. (default: True)

  • add_edge_type (bool, optional) – If set to False, will not add the edge-level vector edge_type to the returned Data object. (default: True)

  • dummy_values (bool, optional) – If set to True, will fill attributes of remaining types with dummy values. Dummy values are NaN for floating point attributes, and -1 for integers. (default: True)

get_all_tensor_attrs() List[TensorAttr][source]

Obtains all tensor attributes stored in this FeatureStore.

get_all_edge_attrs() List[EdgeAttr][source]

Obtains all edge attributes stored in the GraphStore.

apply(func: Callable, *args: List[str])

Applies the function func, either to all attributes or only the ones given in *args.

apply_(func: Callable, *args: List[str])

Applies the in-place function func, either to all attributes or only the ones given in *args.

clone(*args: List[str])

Performs cloning of tensors, either for all attributes or only the ones given in *args.

coalesce()

Sorts and removes duplicated entries from edge indices edge_index.

contiguous(*args: List[str])

Ensures a contiguous memory layout, either for all attributes or only the ones given in *args.

coo(edge_types: Optional[List[Any]] = None, store: bool = False) Tuple[Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Optional[Tensor]]]

Obtains the edge indices in the GraphStore in COO format.

Parameters
  • edge_types (List[Any], optional) – The edge types of edge indices to obtain. If set to None, will return the edge indices of all existing edge types. (default: None)

  • store (bool, optional) – Whether to store converted edge indices in the GraphStore. (default: False)

cpu(*args: List[str])

Copies attributes to CPU memory, either for all attributes or only the ones given in *args.

csc(edge_types: Optional[List[Any]] = None, store: bool = False) Tuple[Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Optional[Tensor]]]

Obtains the edge indices in the GraphStore in CSC format.

Parameters
  • edge_types (List[Any], optional) – The edge types of edge indices to obtain. If set to None, will return the edge indices of all existing edge types. (default: None)

  • store (bool, optional) – Whether to store converted edge indices in the GraphStore. (default: False)

csr(edge_types: Optional[List[Any]] = None, store: bool = False) Tuple[Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Optional[Tensor]]]

Obtains the edge indices in the GraphStore in CSR format.

Parameters
  • edge_types (List[Any], optional) – The edge types of edge indices to obtain. If set to None, will return the edge indices of all existing edge types. (default: None)

  • store (bool, optional) – Whether to store converted edge indices in the GraphStore. (default: False)

cuda(device: Optional[Union[int, str]] = None, *args: List[str], non_blocking: bool = False)

Copies attributes to CUDA memory, either for all attributes or only the ones given in *args.

detach(*args: List[str])

Detaches attributes from the computation graph by creating a new tensor, either for all attributes or only the ones given in *args.

detach_(*args: List[str])

Detaches attributes from the computation graph, either for all attributes or only the ones given in *args.

edge_attrs() List[str]

Returns all edge-level tensor attribute names.

generate_ids()

Generates and sets n_id and e_id attributes to assign each node and edge to a continuously ascending and unique ID.

get_edge_index(*args, **kwargs) Tuple[Tensor, Tensor]

Synchronously obtains an edge_index tuple from the GraphStore.

Parameters

**kwargs (EdgeAttr) – Any relevant edge attributes that correspond to the edge_index tuple. See the EdgeAttr documentation for required and optional attributes.

Raises

KeyError – If the edge_index corresponding to the input EdgeAttr was not found.

get_tensor(*args, **kwargs) Union[Tensor, ndarray]

Synchronously obtains a tensor from the FeatureStore.

Parameters

**kwargs (TensorAttr) – Any relevant tensor attributes that correspond to the feature tensor. See the TensorAttr documentation for required and optional attributes.

Raises
get_tensor_size(*args, **kwargs) Optional[Tuple[int, ...]]

Obtains the size of a tensor given its TensorAttr, or None if the tensor does not exist.

has_self_loops() bool

Returns True if the graph contains self-loops.

is_coalesced() bool

Returns True if edge indices edge_index are sorted and do not contain duplicate entries.

property is_cuda: bool

Returns True if any torch.Tensor attribute is stored on the GPU, False otherwise.

is_directed() bool

Returns True if graph edges are directed.

property keys: List[str]

Returns a list of all graph attribute names.

multi_get_tensor(attrs: List[TensorAttr]) List[Union[Tensor, ndarray]]

Synchronously obtains a list of tensors from the FeatureStore for each tensor associated with the attributes in attrs.

Note

The default implementation simply iterates over all calls to get_tensor(). Implementor classes that can provide additional, more performant functionality are recommended to to override this method.

Parameters

attrs (List[TensorAttr]) – A list of input TensorAttr objects that identify the tensors to obtain.

Raises
node_attrs() List[str]

Returns all node-level tensor attribute names.

property num_edges: int

Returns the number of edges in the graph. For undirected graphs, this will return the number of bi-directional edges, which is double the amount of unique edges.

pin_memory(*args: List[str])

Copies attributes to pinned memory, either for all attributes or only the ones given in *args.

put_edge_index(edge_index: Tuple[Tensor, Tensor], *args, **kwargs) bool

Synchronously adds an edge_index tuple to the GraphStore. Returns whether insertion was successful.

Parameters
  • tensor (Tuple[torch.Tensor, torch.Tensor]) – The edge_index tuple in a format specified in EdgeAttr.

  • **kwargs (EdgeAttr) – Any relevant edge attributes that correspond to the edge_index tuple. See the EdgeAttr documentation for required and optional attributes.

put_tensor(tensor: Union[Tensor, ndarray], *args, **kwargs) bool

Synchronously adds a tensor to the FeatureStore. Returns whether insertion was successful.

Parameters
  • tensor (torch.Tensor or np.ndarray) – The feature tensor to be added.

  • **kwargs (TensorAttr) – Any relevant tensor attributes that correspond to the feature tensor. See the TensorAttr documentation for required and optional attributes.

Raises

ValueError – If the input TensorAttr is not fully specified.

record_stream(stream: Stream, *args: List[str])

Ensures that the tensor memory is not reused for another tensor until all current work queued on stream has been completed, either for all attributes or only the ones given in *args.

remove_edge_index(*args, **kwargs) bool

Synchronously deletes an edge_index tuple from the GraphStore. Returns whether deletion was successful.

Parameters

**kwargs (EdgeAttr) – Any relevant edge attributes that correspond to the edge_index tuple. See the EdgeAttr documentation for required and optional attributes.

remove_tensor(*args, **kwargs) bool

Removes a tensor from the FeatureStore. Returns whether deletion was successful.

Parameters

**kwargs (TensorAttr) – Any relevant tensor attributes that correspond to the feature tensor. See the TensorAttr documentation for required and optional attributes.

Raises

ValueError – If the input TensorAttr is not fully specified.

requires_grad_(*args: List[str], requires_grad: bool = True)

Tracks gradient computation, either for all attributes or only the ones given in *args.

share_memory_(*args: List[str])

Moves attributes to shared memory, either for all attributes or only the ones given in *args.

size(dim: Optional[int] = None) Optional[Union[Tuple[Optional[int], Optional[int]], int]]

Returns the size of the adjacency matrix induced by the graph.

to(device: Union[int, str], *args: List[str], non_blocking: bool = False)

Performs tensor device conversion, either for all attributes or only the ones given in *args.

update_tensor(tensor: Union[Tensor, ndarray], *args, **kwargs) bool

Updates a tensor in the FeatureStore with a new value. Returns whether the update was succesful.

Note

Implementor classes can choose to define more efficient update methods; the default performs a removal and insertion.

Parameters
  • tensor (torch.Tensor or np.ndarray) – The feature tensor to be updated.

  • **kwargs (TensorAttr) – Any relevant tensor attributes that correspond to the feature tensor. See the TensorAttr documentation for required and optional attributes.

view(*args, **kwargs) AttrView

Returns a view of the FeatureStore given a not yet fully-specified TensorAttr.