torch_geometric.EdgeIndex

class EdgeIndex(data: Any, *args: Any, sparse_size: Optional[Tuple[Optional[int], Optional[int]]] = None, sort_order: Optional[Union[str, SortOrder]] = None, is_undirected: bool = False, **kwargs: Any)[source]

基础类:Tensor

一个带有附加(元)数据的COO edge_index 张量。

EdgeIndex 是一个 torch.Tensor,它保存了形状为 [2, num_edges]edge_index 表示。边以稀疏 COO 格式的成对源节点和目标节点索引给出。

虽然 EdgeIndex torch.Tensor 的一个子类,但它可以包含额外的(元)数据,

此外,EdgeIndex 缓存数据以便快速进行CSR或CSC转换,前提是其表示已排序,例如其 rowptrcolptr,或者用于从CSR到CSC或反之的置换向量。 缓存是根据需求填充的(例如,当调用 EdgeIndex.sort_by() 时),或者通过 EdgeIndex.fill_cache_() 明确请求时,并且在其生命周期内维护和调整(例如,当调用 EdgeIndex.flip() 时)。

这种表示确保了在GNN消息传递方案中的最佳计算,同时保留了基于常规COO的工作流程的易用性。

from torch_geometric import EdgeIndex

edge_index = EdgeIndex(
    [[0, 1, 1, 2],
     [1, 0, 2, 1]]
    sparse_size=(3, 3),
    sort_order='row',
    is_undirected=True,
    device='cpu',
)
>>> EdgeIndex([[0, 1, 1, 2],
...            [1, 0, 2, 1]])
assert edge_index.is_sorted_by_row
assert edge_index.is_undirected

# Flipping order:
edge_index = edge_index.flip(0)
>>> EdgeIndex([[1, 0, 2, 1],
...            [0, 1, 1, 2]])
assert edge_index.is_sorted_by_col
assert edge_index.is_undirected

# Filtering:
mask = torch.tensor([True, True, True, False])
edge_index = edge_index[:, mask]
>>> EdgeIndex([[1, 0, 2],
...            [0, 1, 1]])
assert edge_index.is_sorted_by_col
assert not edge_index.is_undirected

# Sparse-Dense Matrix Multiplication:
out = edge_index.flip(0) @ torch.randn(3, 16)
assert out.size() == (3, 16)
validate() EdgeIndex[source]

验证 EdgeIndex 表示。

特别是,它确保了

  • 它只包含有效的索引。

  • 排序顺序已正确设置。

  • 如果指定为无向,则索引是双向的。

Return type:

EdgeIndex

sparse_size() Tuple[Optional[int], Optional[int]][source]
sparse_size(dim: int) Optional[int]

底层稀疏矩阵的大小。 如果指定了dim,则返回一个整数,表示该稀疏维度的大小。

Parameters:

dim (int, optional) – 要检索大小的维度。 (default: None)

Return type:

Union[Tuple[Optional[int], Optional[int]], int, None]

property num_rows: Optional[int]

底层稀疏矩阵的行数。

Return type:

Optional[int]

property num_cols: Optional[int]

底层稀疏矩阵的列数。

Return type:

Optional[int]

property sort_order: Optional[str]

索引的排序顺序,可以是"row""col"None

Return type:

Optional[str]

property is_sorted: bool

返回索引是否按行或列排序。

Return type:

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

property is_sorted_by_row: bool

返回索引是否按行排序。

Return type:

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

property is_sorted_by_col: bool

返回索引是否按列排序。

Return type:

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

property is_undirected: bool

返回索引是否是双向的。

Return type:

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

get_sparse_size() Size[source]
get_sparse_size(dim: int) int

底层稀疏矩阵的大小。 当未明确设置时,自动计算并缓存。 如果指定了dim,则返回一个整数,表示该稀疏维度的大小。

Parameters:

dim (int, optional) – The dimension for which to retrieve the size. (default: None)

Return type:

Union[Size, int]

sparse_resize_(num_rows: Optional[int], num_cols: Optional[int]) EdgeIndex[source]

分配或重新分配底层稀疏矩阵的大小。

Parameters:
  • num_rows (int, optional) – 行数。

  • num_cols (int, optional) – 列的数量。

Return type:

EdgeIndex

get_num_rows() int[source]

底层稀疏矩阵的行数。 当未明确设置时,自动计算并缓存。

Return type:

int

get_num_cols() int[source]

底层稀疏矩阵的列数。 当未明确设置时,自动计算并缓存。

Return type:

int

get_indptr() Tensor[source]

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

Return type:

Tensor

get_csr() Tuple[Tuple[Tensor, Tensor], Optional[Tensor]][source]

返回压缩的CSR表示 (rowptr, col), perm 如果 EdgeIndex 已排序。

Return type:

Tuple[Tuple[Tensor, Tensor], Optional[Tensor]]

get_csc() Tuple[Tuple[Tensor, Tensor], Optional[Tensor]][source]

返回压缩的CSC表示 (colptr, row), perm 如果 EdgeIndex 已排序。

Return type:

Tuple[Tuple[Tensor, Tensor], Optional[Tensor]]

fill_cache_(no_transpose: bool = False) EdgeIndex[source]

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

Parameters:

no_transpose (bool, 可选) – 如果设置为 True,将不会填充 缓存中关于转置的 EdgeIndex 的信息。(默认值:False

Return type:

EdgeIndex

as_tensor() Tensor[source]

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

Return type:

Tensor

sort_by(sort_order: Union[str, SortOrder], stable: bool = False) SortReturnType[source]

按行或列索引对元素进行排序。

Parameters:
  • sort_order (str) – 排序顺序,可以是 "row""col"

  • stable (bool, optional) – 使排序例程稳定,保证等价元素的顺序保持不变。 (默认: False)

Return type:

SortReturnType

to_dense(value: Optional[Tensor] = None, fill_value: float = 0.0, dtype: Optional[dtype] = None) Tensor[source]

EdgeIndex转换为密集的torch.Tensor

警告

在存在重复边的情况下,行为是不确定的(将任意选择value中的一个值)。为了确保确定性行为,可以考虑事先调用coalesce()

Parameters:
  • value (torch.Tensor, optional) – 非零元素的值。 如果未指定,非零元素将被赋值为 1.0。(默认值:None

  • fill_value (float, optional) – 密集矩阵中剩余元素的填充值。(默认值:0.0

  • dtype (torch.dtype, optional) – 返回张量的数据类型。(默认值:None

Return type:

Tensor

to_sparse_coo(value: Optional[Tensor] = None) Tensor[source]

EdgeIndex转换为 torch.sparse_coo_tensor

Parameters:

value (torch.Tensor, optional) – 非零元素的值。 如果未指定,非零元素将被赋值为 1.0。(默认值:None

Return type:

Tensor

to_sparse_csr(value: Optional[Tensor] = None) Tensor[source]

EdgeIndex转换为 torch.sparse_csr_tensor

Parameters:

value (torch.Tensor, optional) – 非零元素的值。 如果未指定,非零元素将被赋值为 1.0。(默认值:None

Return type:

Tensor

to_sparse_csc(value: Optional[Tensor] = None) Tensor[source]

EdgeIndex转换为 torch.sparse_csc_tensor

Parameters:

value (torch.Tensor, optional) – 非零元素的值。 如果未指定,非零元素将被赋值为 1.0。(默认值:None

Return type:

Tensor

to_sparse(*, layout: layout = torch.sparse_coo, value: Optional[Tensor] = None) Tensor[source]

EdgeIndex转换为 torch.sparse 张量。

Parameters:
  • 布局 (torch.layout, 可选) – 所需的稀疏布局。可以是 torch.sparse_coo, torch.sparse_csr, 或 torch.sparse_csc。 (默认: torch.sparse_coo)

  • value (torch.Tensor, optional) – 非零元素的值。 如果未指定,非零元素将被赋值为 1.0。(默认值:None

Return type:

Tensor

to_sparse_tensor(value: Optional[Tensor] = None) SparseTensor[source]

EdgeIndex转换为 torch_sparse.SparseTensor。 需要安装torch-sparse

Parameters:

value (torch.Tensor, optional) – 非零元素的值。 (default: None)

Return type:

SparseTensor

matmul(other: EdgeIndex, input_value: Optional[Tensor] = None, other_value: Optional[Tensor] = None, reduce: Literal['sum', 'mean', 'amin', 'amax', 'add', 'min', 'max'] = 'sum', transpose: bool = False) Tuple[EdgeIndex, Tensor][source]
matmul(other: Tensor, input_value: Optional[Tensor] = None, other_value: None = None, reduce: Literal['sum', 'mean', 'amin', 'amax', 'add', 'min', 'max'] = 'sum', transpose: bool = False) Tensor

执行矩阵 inputother 的矩阵乘法。 如果 input 是一个 \((n \times m)\) 矩阵,而 other 是一个 \((m \times p)\) 张量,那么输出将是一个 \((n \times p)\) 张量。 有关更多信息,请参见 torch.matmul()

input 是一个稀疏矩阵,由 EdgeIndex 中的索引表示,而 input_value 对应于 input 中非零元素的值。 如果未指定,非零元素将被赋值为 1.0

other 可以是一个密集的 torch.Tensor 或者一个稀疏的 EdgeIndex。 如果 other 是一个稀疏的 EdgeIndex,那么 other_value 对应于其非零元素的值。

此函数还接受一个可选的 reduce 参数, 该参数允许指定一个可选的归约操作。 有关更多信息,请参见 torch.sparse.mm()

最后,transpose 选项允许执行矩阵乘法,其中 input 将首先被转置,

\[\textrm{input}^{\top} \cdot \textrm{other}\]
Parameters:
  • 其他 (torch.TensorEdgeIndex) – 要相乘的第二个矩阵,可以是稀疏的或密集的。

  • input_value (torch.Tensor, optional) – input 中非零元素的值。 如果未指定,非零元素将被赋值为 1.0。(默认值:None

  • other_value (torch.Tensor, optional) – 如果other是稀疏的,则为非零元素的值。 如果未指定,非零元素将被赋值为1.0。(默认值:None

  • reduce (str, optional) – 归约操作,可以是 "sum"/"add", "mean", "min"/amin"max"/amax. (默认: "sum")

  • transpose (bool, 可选) – 如果设置为 True,将基于转置的 input 执行矩阵乘法。 (默认: False)

Return type:

Union[Tensor, Tuple[EdgeIndex, Tensor]]

sparse_narrow(dim: int, start: Union[int, Tensor], length: int) EdgeIndex[source]

返回一个新的EdgeIndex,它是自身的缩小版本。缩小是通过将EdgeIndex解释为形状为(num_rows, num_cols)的稀疏矩阵来执行的。

torch.narrow()相比,返回的张量不再共享相同的底层存储。

Parameters:
  • dim (int) – 沿着哪个维度进行缩小。

  • 开始 (inttorch.Tensor) – 从该元素的索引开始缩小维度。

  • length (int) – 缩小维度的长度。

Return type:

EdgeIndex

to_vector() Tensor[source]

EdgeIndex转换为一维索引向量表示。

Return type:

Tensor