Utilities
Utility functions.
- prepare_num_nodes(edge_index, num_nodes=None)[source]
Prepare the number of nodes.
If an explicit number is given, this number will be used. Otherwise, infers the number of nodes as the maximum id in the edge index.
- edge_index_to_sparse_matrix(edge_index, num_nodes=None)[source]
Convert an edge index to a sparse matrix.
Uses the edge index for non-zero entries, and fills in
1as entries.- Parameters
- Return type
- Returns
shape:
(n, n)the adjacency matrix as a sparse tensor, cf.torch.sparse_coo_tensor().
- prepare_page_rank_adjacency(adj=None, edge_index=None, num_nodes=None, add_identity=False)[source]
Prepare the page-rank adjacency matrix.
If no explicit adjacency is given, the methods first creates an adjacency matrix from the edge index, cf.
edge_index_to_sparse_matrix(). Next, the matrix is symmetrized as\[A := A + A^T\]Finally, the matrix is normalized such that the columns sum to one.
- Parameters
edge_index (
Optional[LongTensor]) – shape:(2, m)the edge indexnum_nodes (
Optional[int]) – the number of nodes used to determine the shape of the adjacency matrix. IfNone, andadjis not already provided, it is inferred fromedge_index.add_identity (
bool) – whether to add an identity matrix toAto ensure that each node has a degree of at least one.
- Raises
ValueError – if neither is provided, or the adjacency matrix is invalid
- Return type
- Returns
shape:
(n, n)the symmetric, normalized, and sparse adjacency matrix
- validate_x(x, n=None)[source]
Validate a (batched) page-rank vector.
In particular, the method checks that
the tensor dimension is
(n,)or(n, batch_size)all entries are between
0and1the entries sum to
1(along the first dimension)
- Parameters
- Raises
ValueError – if the input is invalid.
- Return type
- prepare_x0(x0=None, indices=None, n=None)[source]
Prepare a start value.
The following precedence order is used:
an explicit start value, via
x0. If present, this tensor is passed through without further modification.a one-hot matrix created via
indices. The matrix is of shape(n, len(indices))and has a single 1 per column at the given indices.a uniform
1/nvector of shape(n,)
- Parameters
- Raises
ValueError – if neither
x0nornare provided- Return type
- Returns
shape:
(n,)or(n, batch_size)the initial valuex
- power_iteration(adj, x0, alpha=0.05, max_iter=1000, use_tqdm=False, epsilon=0.0001, device=None)[source]
Perform the power iteration.
\[\mathbf{x}^{(i+1)} = (1 - \alpha) \cdot \mathbf{A} \mathbf{x}^{(i)} + \alpha \mathbf{x}^{(0)}\]- Parameters
adj (
Tensor) – shape:(n, n)the (sparse) adjacency matrixx0 (
Tensor) – shape:(n,), or(n, batch_size)the initial value forx.alpha (
float) –0 < alpha < 1the smoothing value / teleport probabilitymax_iter (
int) –0 < max_iterthe maximum number of iterationsepsilon (
float) –epsilon > 0a (small) constant to check for convergenceuse_tqdm (
bool) – whether to use a tqdm progress bardevice (
Union[None,str,device]) – the device to use, or a hint thereof, cf.resolve_device()
- Return type
- Returns
shape:
(n,)or(n, batch_size)thexvalue after convergence (or maximum number of iterations).
- batched_personalized_page_rank(adj, indices, batch_size, **kwargs)[source]
Batch-wise PPR computation with automatic memory optimization.
- Parameters
adj (
Tensor) – shape:(n, n)the adjacency matrix.indices (
Tensor) – shape:kthe indices for which to compute PPRbatch_size (
int) –batch_size > 0the batch size. Will be reduced if necessarykwargs – additional keyword-based parameters passed to
power_iteration()
- Return type
- Returns
shape:
(n, k)the PPR vectors for each node index