graphwar.functional

to_sparse_tensor

Convert edge index to a torch_sparse sparse tensor

to_dense_adj

Convert edge index to dense adjacency matrix

spmm

Sparse matrix multiplication using torch_scatter.

drop_edge

DropEdge: Sampling edge using a uniform distribution from the "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification" paper (ICLR'20)

drop_node

DropNode: Sampling node using a uniform distribution.

drop_path

DropPath: a structured form of graphwar.functional.drop_edge.

to_sparse_tensor(edge_index: Tensor, edge_weight: Optional[Tensor] = None, num_nodes: Optional[int] = None, is_sorted: bool = False) SparseTensor[source]

Convert edge index to a torch_sparse sparse tensor

Parameters
  • edge_index (Tensor) – edge index with shape [2, M]

  • edge_weight (Optional[Tensor], optional) – edge weight with shape [M], by default None

  • num_nodes (Optional[int], optional) – the number of nodes in the graph, by default None

  • is_sorted (bool, optional) – whether the edge_index is sorted, by default False

Returns

the output sparse adjacency matrix denoted as torch_sparse.SparseTensor, with shape [num_nodes, num_nodes]

Return type

SparseTensor

to_dense_adj(edge_index: Tensor, edge_weight: Optional[Tensor] = None, num_nodes: Optional[int] = None, fill_value: float = 1.0) Tensor[source]

Convert edge index to dense adjacency matrix

Parameters
  • edge_index (Tensor) – edge index with shape [2, M]

  • edge_weight (Optional[Tensor], optional) – edge weight with shape [M], by default None

  • num_nodes (Optional[int], optional) – the number of nodes in the graph, by default None

  • fill_value (float) – filling value for elements in the adjacency matrix where edges existed, by default 1.0

Returns

output dense adjacency matrix with shape [num_nodes, num_nodes]

Return type

Tensor

spmm(x: Tensor, edge_index: Tensor, edge_weight: Optional[Tensor] = None, reduce: str = 'sum') Tensor[source]

Sparse matrix multiplication using torch_scatter.

Parameters
  • x (Tensor) – the input dense 2D-matrix

  • edge_index (Tensor) – the location of the non-zeros elements in the sparse matrix, denoted as edge_index with shape [2, M]

  • edge_weight (Optional[Tensor], optional) – the edge weight of the sparse matrix, by default None

  • reduce (str, optional) – reduction of the sparse matrix multiplication, by default ‘sum’

Returns

the output result of the multiplication.

Return type

Tensor

Example

>>> import torch
>>> from graphwar.functional import spmm
>>> x = torch.randn(5,2)
>>> edge_index = torch.LongTensor([[1,2], [3,4]])
>>> spmm(x, edge_index)
>>> # which is equivalent to:
>>> A = torch.zeros(5,5)
>>> A[edge_index[0], edge_index[1]] = 1.0
>>> torch.mm(A,x)
drop_edge(edge_index: Tensor, edge_weight: Optional[Tensor] = None, p: float = 0.5, training: bool = True) Tuple[Tensor, Tensor][source]

DropEdge: Sampling edge using a uniform distribution from the “DropEdge: Towards Deep Graph Convolutional Networks on Node Classification” paper (ICLR’20)

Parameters
  • edge_index (Tensor) – the input edge index

  • edge_weight (Optional[Tensor], optional) – the input edge weight, by default None

  • p (float, optional) – the probability of dropping out on each edge, by default 0.5

  • training (bool, optional) – whether the model is during training, do nothing if training=True,, by default True

Returns

the output edge index and edge weight

Return type

Tuple[Tensor, Tensor]

Raises

ValueError – p is out of range [0,1]

Example

>>> from graphwar.functional import drop_edge
>>> edge_index = torch.LongTensor([[1, 2], [3,4]])
>>> drop_edge(edge_index, p=0.5)
drop_node(edge_index: Tensor, edge_weight: Optional[Tensor] = None, p: float = 0.5, training: bool = True, num_nodes: Optional[int] = None) Tuple[Tensor, Tensor][source]

DropNode: Sampling node using a uniform distribution. from the “Graph Contrastive Learning with Augmentations” paper (NeurIPS’20)

Parameters
  • edge_index (Tensor) – the input edge index

  • edge_weight (Optional[Tensor], optional) – the input edge weight, by default None

  • p (float, optional) – the probability of dropping out on each node, by default 0.5

  • training (bool, optional) – whether the model is during training, do nothing if training=True,, by default True

Returns

the output edge index and edge weight

Return type

Tuple[Tensor, Tensor]

Raises

ValueError – p is out of range [0,1]

Example

>>> from graphwar.functional import drop_node
>>> edge_index = torch.LongTensor([[1, 2], [3,4]])
>>> drop_node(edge_index, p=0.5)
drop_path(edge_index: Tensor, edge_weight: Optional[Tensor] = None, r: Optional[Union[float, Tensor]] = 0.5, walks_per_node: int = 2, walk_length: int = 4, p: float = 1, q: float = 1, training: bool = True, num_nodes: Optional[int] = None, by: str = 'degree') Tuple[Tensor, Tensor][source]

DropPath: a structured form of graphwar.functional.drop_edge. From the “MaskGAE: Masked Graph Modeling Meets Graph Autoencoders” paper (arXiv’22)

Parameters
  • edge_index (Tensor) – the input edge index

  • edge_weight (Optional[Tensor], optional) – the input edge weight, by default None

  • r (Optional[Union[float, Tensor]], optional) – if r is integer value: the percentage of nodes in the graph that chosen as root nodes to perform random walks, by default 0.5 if r is torch.Tensor: a set of custom root nodes

  • walks_per_node (int, optional) – number of walks per node, by default 2

  • walk_length (int, optional) – number of walk length per node, by default 4

  • p (float, optional) – p in random walks, by default 1

  • q (float, optional) – q in random walks, by default 1

  • training (bool, optional) – whether the model is during training, do nothing if training=True, by default True

  • num_nodes (int, optional) – number of total nodes in the graph, by default None

  • by (str, optional) – sampling root nodes uniformly uniform or by degree distribution degree, by default ‘degree’

Returns

the output edge index and edge weight

Return type

Tuple[Tensor, Tensor]

Raises

Example

>>> from graphwar.functional import drop_path
>>> edge_index = torch.LongTensor([[1, 2], [3,4]])
>>> drop_path(edge_index, r=0.5)
>>> drop_path(edge_index, r=torch.tensor([1,2])) # specify root nodes