torch_geometric.Index

class Index(data: Any, *args: Any, dim_size: Optional[int] = None, is_sorted: bool = False, **kwargs: Any)[source]

Bases: Tensor

一个一维的index张量,附加了额外的(元)数据。

Index 是一个 torch.Tensor,它保存了形状为 [num_indices] 的索引。

虽然 Index torch.Tensor 的一个子类,但它可以保存额外的(元)数据,

  • dim_size: 底层稀疏向量的大小,,可以通过index索引的维度的大小。默认情况下,它被推断为dim_size=index.max() + 1

  • is_sorted: 索引是否按升序排序。

此外,Index 通过 indptr 缓存数据,以便在其表示已排序的情况下快速进行 CSR 转换。 缓存是根据需求填充的(例如,当调用 Index.get_indptr() 时),或者通过 Index.fill_cache_() 明确请求时,并且在其生命周期内进行维护和调整。

This representation ensures optimal computation in GNN message passing schemes, while preserving the ease-of-use of regular COO-based workflows.

from torch_geometric import Index

index = Index([0, 1, 1, 2], dim_size=3, is_sorted=True)
>>> Index([0, 1, 1, 2], dim_size=3, is_sorted=True)
assert index.dim_size == 3
assert index.is_sorted

# Flipping order:
index.flip(0)
>>> Index([[2, 1, 1, 0], dim_size=3)
assert not index.is_sorted

# Filtering:
mask = torch.tensor([True, True, True, False])
index[:, mask]
>>> Index([[0, 1, 1], dim_size=3, is_sorted=True)
assert index.is_sorted
validate() 索引[source]

验证Index表示。

特别是,它确保了

  • 它只包含有效的索引。

  • 排序顺序已正确设置。

Return type:

索引

property dim_size: Optional[int]

底层稀疏向量的大小。

Return type:

Optional[int]

property is_sorted: bool

返回索引是否按升序排序。

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

get_dim_size() int[source]

基础稀疏向量的大小。 当未明确设置时,自动计算并缓存。

Return type:

int

dim_resize_(dim_size: Optional[int]) 索引[source]

分配或重新分配底层稀疏向量的大小。

Return type:

索引

get_indptr() Tensor[source]

如果Index已排序,则返回压缩的索引表示。

Return type:

Tensor

fill_cache_() 索引[source]

用(元)数据信息填充缓存。

Return type:

索引

as_tensor() Tensor[source]

Index表示零拷贝回torch.Tensor表示。

Return type:

Tensor