采样子图
- class dgl.graphbolt.SampledSubgraph[source]
Bases:
object
一个用于采样子图的抽象类。在异构图的情况下,每个字段应为Dict类型。否则,对于同构图,每个字段应与其相应的值类型对应。
- exclude_edges(edges: Dict[str, Tensor] | Tensor, assume_num_node_within_int32: bool = True)[source]
从采样的子图中排除边。
此函数可用于采样子图,无论它们是否具有压缩的行/列节点。如果原始子图具有压缩的行或列节点,则返回的子图中的相应行或列节点也将被压缩。
- Parameters:
self (SampledSubgraph) – 采样子图。
edges (Union[torch.Tensor, Dict[str, torch.Tensor]]) – 要排除的边。如果采样的子图是同质的,那么 edges 应该是一个 N*2 的张量,表示要排除的边。如果采样的子图是异质的,那么 edges 应该是一个边类型和对应要排除的边的字典。
assume_num_node_within_int32 (bool) – 如果为True,则假定提供的edges中的节点ID值在int32范围内,这可以显著提高计算速度。默认值:True
- Returns:
继承自 SampledSubgraph 的类的一个实例。
- Return type:
示例
>>> import dgl.graphbolt as gb >>> import torch >>> sampled_csc = {"A:relation:B": gb.CSCFormatBase( ... indptr=torch.tensor([0, 1, 2, 3]), ... indices=torch.tensor([0, 1, 2]))} >>> original_column_node_ids = {"B": torch.tensor([10, 11, 12])} >>> original_row_node_ids = {"A": torch.tensor([13, 14, 15])} >>> original_edge_ids = {"A:relation:B": torch.tensor([19, 20, 21])} >>> subgraph = gb.SampledSubgraphImpl( ... sampled_csc=sampled_csc, ... original_column_node_ids=original_column_node_ids, ... original_row_node_ids=original_row_node_ids, ... original_edge_ids=original_edge_ids ... ) >>> edges_to_exclude = {"A:relation:B": torch.tensor([[14, 11], [15, 12]])} >>> result = subgraph.exclude_edges(edges_to_exclude) >>> print(result.sampled_csc) {'A:relation:B': CSCFormatBase(indptr=tensor([0, 1, 1, 1]), indices=tensor([0]), )} >>> print(result.original_column_node_ids) {'B': tensor([10, 11, 12])} >>> print(result.original_row_node_ids) {'A': tensor([13, 14, 15])} >>> print(result.original_edge_ids) {'A:relation:B': tensor([19])}
- property original_column_node_ids: Tensor | Dict[str, Tensor]
返回原始图中对应的反向列节点ID。 原始图中列的反向节点ID。图结构可以被视为一个协调的行和列对,这是列的映射ID。
如果 original_column_node_ids 是一个张量:它表示原始节点ID。
如果 original_column_node_ids 是一个字典:键应该是节点类型,值应该是对应的原始异构节点ID。
如果存在,意味着列ID被压缩,并且sampled_csc列ID与这些压缩的列ID匹配。
- property original_edge_ids: Tensor | Dict[str, Tensor]
返回原始图中对应的反向边ID。 原始图中的反向边ID。这在需要边特征时非常有用。
如果 original_edge_ids 是一个张量:它表示原始的边ID。
如果 original_edge_ids 是一个字典:键应该是边的类型,值应该是相应的原始异构边ID。
- property original_row_node_ids: Tensor | Dict[str, Tensor]
返回原始图中对应的反向行节点ID。 原始图中行的反向节点ID。图结构可以视为协调的行和列对,这是行的映射ID。
如果 original_row_node_ids 是一个张量:它表示原始节点ID。
如果original_row_node_ids是一个字典:键应该是节点类型,值应该是相应的原始异构节点ID。
如果存在,这意味着行ID被压缩,并且sampled_csc行ID与这些压缩的行ID匹配。
- property sampled_csc: CSCFormatBase | Dict[str, CSCFormatBase]
- Returns the node pairs representing edges in csc format.
如果 sampled_csc 是 CSCFormatBase:它应该是 csc 格式。indptr 存储数据数组中每列开始的索引。indices 存储非零元素的行索引。
如果 sampled_csc 是一个字典:键应该是边类型,值应该是对应的节点对。内部的 id 是异构 id。
示例
同构图。
>>> import dgl.graphbolt as gb >>> import torch >>> sampled_csc = gb.CSCFormatBase( ... indptr=torch.tensor([0, 1, 2, 3]), ... indices=torch.tensor([0, 1, 2])) >>> print(sampled_csc) CSCFormatBase(indptr=tensor([0, 1, 2, 3]), indices=tensor([0, 1, 2]), )
异构图。
>>> sampled_csc = {"A:relation:B": gb.CSCFormatBase( ... indptr=torch.tensor([0, 1, 2, 3]), ... indices=torch.tensor([0, 1, 2]))} >>> print(sampled_csc) {'A:relation:B': CSCFormatBase(indptr=tensor([0, 1, 2, 3]), indices=tensor([0, 1, 2]), )}