torch_geometric.sampler

BaseSampler

An abstract base class that initializes a graph sampler and provides sample_from_nodes() and sample_from_edges() routines.

NodeSamplerInput

The sampling input of sample_from_nodes().

EdgeSamplerInput

The sampling input of sample_from_edges().

SamplerOutput

The sampling output of a BaseSampler on homogeneous graphs.

HeteroSamplerOutput

The sampling output of a BaseSampler on heterogeneous graphs.

NeighborSampler

An implementation of an in-memory (heterogeneous) neighbor sampler used by NeighborLoader.

HGTSampler

An implementation of an in-memory heterogeneous layer-wise sampler user by HGTLoader.

class BaseSampler[source]

An abstract base class that initializes a graph sampler and provides sample_from_nodes() and sample_from_edges() routines.

Note

Any data stored in the sampler will be replicated across data loading workers that use the sampler since each data loading worker holds its own instance of a sampler. As such, it is recommended to limit the amount of information stored in the sampler.

sample_from_nodes(index: NodeSamplerInput, **kwargs) Union[HeteroSamplerOutput, SamplerOutput][source]

Performs sampling from the nodes specified in index, returning a sampled subgraph in the specified output format.

The index is a tuple holding the following information:

  1. The example indices of the seed nodes

  2. The node indices to start sampling from

  3. The timestamps of the given seed nodes (optional)

sample_from_edges(index: EdgeSamplerInput, **kwargs) Union[HeteroSamplerOutput, SamplerOutput][source]

Performs sampling from the edges specified in index, returning a sampled subgraph in the specified output format.

The index is a tuple holding the following information:

  1. The example indices of the seed links

  2. The source node indices to start sampling from

  3. The destination node indices to start sampling from

  4. The labels of the seed links (optional)

  5. The timestamps of the given seed nodes (optional)

property edge_permutation: Union[Tensor, None, Dict[Tuple[str, str, str], Optional[Tensor]]]

If the sampler performs any modification of edge ordering in the original graph, this function is expected to return the permutation tensor that defines the permutation from the edges in the original graph and the edges used in the sampler. If no such permutation was applied, None is returned. For heterogeneous graphs, the expected return type is a permutation tensor for each edge type.

class NodeSamplerInput(input_id: Optional[Tensor], node: Tensor, time: Optional[Tensor] = None, input_type: Optional[str] = None)[source]

The sampling input of sample_from_nodes().

Parameters
  • input_id (torch.Tensor, optional) – The indices of the data loader input of the current mini-batch.

  • node (torch.Tensor) – The indices of seed nodes to start sampling from.

  • time (torch.Tensor, optional) – The timestamp for the seed nodes. (default: None)

  • input_type (str, optional) – The input node type (in case of sampling in a heterogeneous graph). (default: None)

class EdgeSamplerInput(input_id: Optional[Tensor], row: Tensor, col: Tensor, label: Optional[Tensor] = None, time: Optional[Tensor] = None, input_type: Optional[Tuple[str, str, str]] = None)[source]

The sampling input of sample_from_edges().

Parameters
  • input_id (torch.Tensor, optional) – The indices of the data loader input of the current mini-batch.

  • row (torch.Tensor) – The source node indices of seed links to start sampling from.

  • col (torch.Tensor) – The destination node indices of seed links to start sampling from.

  • label (torch.Tensor, optional) – The label for the seed links. (default: None)

  • time (torch.Tensor, optional) – The timestamp for the seed links. (default: None)

  • input_type (Tuple[str, str, str], optional) – The input edge type (in case of sampling in a heterogeneous graph). (default: None)

class SamplerOutput(node: Tensor, row: Tensor, col: Tensor, edge: Optional[Tensor], batch: Optional[Tensor] = None, metadata: Optional[Any] = None)[source]

The sampling output of a BaseSampler on homogeneous graphs.

Parameters
  • node (torch.Tensor) – The sampled nodes in the original graph.

  • row (torch.Tensor) – The source node indices of the sampled subgraph. Indices must be re-indexed to { 0, ..., num_nodes - 1 } corresponding to the nodes in the node tensor.

  • col (torch.Tensor) – The destination node indices of the sampled subgraph. Indices must be re-indexed to { 0, ..., num_nodes - 1 } corresponding to the nodes in the node tensor.

  • edge (torch.Tensor, optional) – The sampled edges in the original graph. This tensor is used to obtain edge features from the original graph. If no edge attributes are present, it may be omitted.

  • batch (torch.Tensor, optional) – The vector to identify the seed node for each sampled node. Can be present in case of disjoint subgraph sampling per seed node. (default: None)

  • metadata – (Any, optional): Additional metadata information. (default: None)

class HeteroSamplerOutput(node: Dict[str, Tensor], row: Dict[Tuple[str, str, str], Tensor], col: Dict[Tuple[str, str, str], Tensor], edge: Optional[Dict[Tuple[str, str, str], Tensor]], batch: Optional[Dict[str, Tensor]] = None, metadata: Optional[Any] = None)[source]

The sampling output of a BaseSampler on heterogeneous graphs.

Parameters
  • node (Dict[str, torch.Tensor]) – The sampled nodes in the original graph for each node type.

  • row (Dict[Tuple[str, str, str], torch.Tensor]) – The source node indices of the sampled subgraph for each edge type. Indices must be re-indexed to { 0, ..., num_nodes - 1 } corresponding to the nodes in the node tensor of the source node type.

  • col (Dict[Tuple[str, str, str], torch.Tensor]) – The destination node indices of the sampled subgraph for each edge type. Indices must be re-indexed to { 0, ..., num_nodes - 1 } corresponding to the nodes in the node tensor of the destination node type.

  • edge (Dict[Tuple[str, str, str], torch.Tensor], optional) – The sampled edges in the original graph for each edge type. This tensor is used to obtain edge features from the original graph. If no edge attributes are present, it may be omitted.

  • batch (Dict[str, torch.Tensor], optional) – The vector to identify the seed node for each sampled node for each node type. Can be present in case of disjoint subgraph sampling per seed node. (default: None)

  • metadata – (Any, optional): Additional metadata information. (default: None)

class NeighborSampler(data: Union[Data, HeteroData, Tuple[FeatureStore, GraphStore]], num_neighbors: Union[List[int], Dict[Tuple[str, str, str], List[int]]], replace: bool = False, directed: bool = True, disjoint: bool = False, temporal_strategy: str = 'uniform', time_attr: Optional[str] = None, is_sorted: bool = False, share_memory: bool = False)[source]

An implementation of an in-memory (heterogeneous) neighbor sampler used by NeighborLoader.

class HGTSampler(data: HeteroData, num_samples: Union[List[int], Dict[str, List[int]]], is_sorted: bool = False, share_memory: bool = False)[source]

An implementation of an in-memory heterogeneous layer-wise sampler user by HGTLoader.