mooon

Functional

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 from the "Graph Contrastive Learning with Augmentations" paper (NeurIPS'20)

drop_path

DropPath: a structured form of drop_edge from the "MaskGAE: Masked Graph Modeling Meets Graph Autoencoders" paper (arXiv'22)

add_random_walk_edge

Adds edges and corresponding edge weights based on random walks.

drop_edge(edge_index: Tensor, edge_weight: Optional[Tensor] = None, p: float = 0.5, training: bool = True) Tuple[Tensor, Optional[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 (torch.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, Optional[Tensor]]

Raises:

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

Example

from mooon import drop_edge
edge_index = torch.tensor([[1, 2], [3,4]])
drop_edge(edge_index, p=0.5)

See also

mooon.DropEdge

drop_node(edge_index: Tensor, edge_weight: Optional[Tensor] = None, p: float = 0.5, training: bool = True, num_nodes: Optional[int] = None) Tuple[Tensor, Optional[Tensor]][source]

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

Parameters:
  • edge_index (torch.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, Optional[Tensor]]

Raises:

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

Example

from mooon import drop_node
edge_index = torch.tensor([[1, 2], [3,4]])
drop_node(edge_index, p=0.5)

See also

mooon.DropNode

drop_path(edge_index: Tensor, edge_weight: Optional[Tensor] = None, p: float = 0.5, walks_per_node: int = 1, walk_length: int = 3, num_nodes: Optional[int] = None, start: Union[str, Tensor] = 'node', is_sorted: bool = False, training: bool = True) Tuple[Tensor, Optional[Tensor]][source]

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

Parameters:
  • edge_index (torch.Tensor) – the input edge index

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

  • p (float, optional) – the percentage of nodes in the graph that chosen as root nodes to perform random walks. By default, p=0.5.

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

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

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

  • start (Union[str, Tensor], optional) – the type of starting node chosen from “node”, “edge”, or custom nodes, by default ‘node’

  • is_sorted (bool, optional) – whether the input edge_index is sorted

  • 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, Optional[Tensor]]

Raises:

Example

from mooon import drop_path
edge_index = torch.tensor([[1, 2], [3,4]])
drop_path(edge_index, p=0.5)

# specify root nodes
drop_path(edge_index, start=torch.tensor([1,2]))

See also

mooon.DropPath

add_random_walk_edge(edge_index: Tensor, edge_weight: Optional[Tensor] = None, start: Optional[Tensor] = None, walks_per_node: int = 1, walk_length: int = 3, skip_first: bool = True, num_nodes: Optional[int] = None, is_sorted: bool = False, training: bool = True) Tuple[Tensor, Optional[Tensor]][source]

Adds edges and corresponding edge weights based on random walks.

Parameters:
  • edge_index (torch.Tensor) – the input edge index

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

  • start (Tensor, optional) – the starting node to perform random walks, if None, use all nodes in the graph as root nodes, by default None

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

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

  • skip_first (bool, optional) – whether to skip the first-hop node when adding edges between root nodes and nodes sampled from random walks, by default False

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

  • is_sorted (bool, optional) – whether the input edge_index is sorted

  • 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, Optional[Tensor]]

Raises:

ImportError – if torch_cluster is not installed.

Example

from mooon import add_random_walk_edge
edge_index = torch.tensor([[1, 2], [3,4]])
add_random_walk_edge(edge_index)

# specify root nodes
add_random_walk_edge(edge_index, start=torch.tensor([1,2]))

See also

mooon.AddRandomWalkEdge

Layers

DropEdge

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

DropNode

DropNode: Sampling node using a uniform distribution from the "Graph Contrastive Learning with Augmentations" paper (NeurIPS'20)

DropPath

DropPath: a structured form of mooon.drop_edge from the "MaskGAE: Masked Graph Modeling Meets Graph Autoencoders" paper (arXiv'22)

AddRandomWalkEdge

Adds edges and corresponding edge weights based on random walks.

class DropEdge(p: float = 0.5)[source]

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

Parameters:

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

Returns:

the output edge index and edge weight

Return type:

Tuple[Tensor, Optional[Tensor]]

Raises:

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

Example

from mooon import DropEdge
edge_index = torch.tensor([[1, 2], [3,4]])
DropEdge(p=0.5)(edge_index)

See also

mooon.drop_edge

forward(edge_index: Tensor, edge_weight: Optional[Tensor] = None) Tuple[Tensor, Optional[Tensor]][source]
training: bool
class DropNode(p: float = 0.5)[source]

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

Parameters:

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

Returns:

the output edge index and edge weight

Return type:

Tuple[Tensor, Optional[Tensor]]

Example

from mooon import DropNode
edge_index = torch.tensor([[1, 2], [3,4]])
DropNode(p=0.5)(edge_index)

See also

mooon.drop_node

forward(edge_index: Tensor, edge_weight: Optional[Tensor] = None) Tuple[Tensor, Optional[Tensor]][source]
training: bool
class DropPath(p: float = 0.5, walks_per_node: int = 1, walk_length: int = 3, num_nodes: Optional[int] = None, start: Union[str, Tensor] = 'node', is_sorted: bool = False)[source]

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

Parameters:
  • p (float, optional) – the percentage of nodes in the graph that chosen as root nodes to perform random walks. By default, p=0.5.

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

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

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

  • start (Union[str, Tensor], optional) – the type of starting node chosen from “node”, “edge”, or custom nodes, by default ‘node’

  • is_sorted (bool, optional) – whether the input edge_index is sorted

Returns:

the output edge index and edge weight

Return type:

Tuple[Tensor, Optional[Tensor]]

Raises:

Example

from mooon import DropPath
edge_index = torch.tensor([[1, 2], [3,4]])
DropPath(p=0.5)(edge_index)

# specify root nodes
DropPath(start=torch.tensor([1,2]))(edge_index)

See also

mooon.drop_path

forward(edge_index: Tensor, edge_weight: Optional[Tensor] = None) Tuple[Tensor, Optional[Tensor]][source]
training: bool
class AddRandomWalkEdge(start: Optional[Tensor] = None, walks_per_node: int = 1, walk_length: int = 3, skip_first: bool = True, num_nodes: Optional[int] = None, is_sorted: bool = False)[source]

Adds edges and corresponding edge weights based on random walks.

Parameters:
  • start (Tensor, optional) – the starting node to perform random walks, if None, use all nodes in the graph as root nodes, by default None

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

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

  • skip_first (bool, optional) – whether to skip the first-hop node when adding edges between root nodes and nodes sampled from random walks, by default False

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

  • is_sorted (bool, optional) – whether the input edge_index is sorted

Returns:

the output edge index and edge weight

Return type:

Tuple[Tensor, Optional[Tensor]]

Raises:

ImportError – if torch_cluster is not installed.

Example

from mooon import AddRandomWalkEdge
edge_index = torch.tensor([[1, 2], [3,4]])
AddRandomWalkEdge()(edge_index)

# specify root nodes
AddRandomWalkEdge(start=torch.tensor([1,2]))(edge_index)

See also

mooon.add_random_walk_edge

forward(edge_index: Tensor, edge_weight: Optional[Tensor] = None) Tuple[Tensor, Optional[Tensor]][source]
training: bool

Models

FLAG

The Free Large-scale Adversarial Augmentation (FLAG) from the "Robust Optimization as Data Augmentation for Large-scale Graphs" paper

class FLAG(criterion: Callable, steps: int = 3, step_size: float = 0.001)[source]

The Free Large-scale Adversarial Augmentation (FLAG) from the “Robust Optimization as Data Augmentation for Large-scale Graphs” paper

Parameters:
  • criterion (Callable) – The loss function to be used for training the model.

  • steps (int, optional) – The number of steps to be taken for adversarial trainin, by default 3

  • step_size (float, optional) – The size of the perturbation to be added to the input at each step, by default 1e-3

Example

import torch
from mooon import FLAG
data = ... # PyG-like data
model = ... # GNN model
optimizer = torch.optim.Adam()
criterion = torch.nn.CrossEntropycriterion()
flag = FLAG(criterion)

def forward(perturb):
    out = model(data.x + perturb, data.edge_index, data.edge_attr)
    return out[data.train_mask]

def train():
    model.train()
    optimizer.zero_grad()
    loss = flag(forward, data.x, data.y[data.train_mask])
    loss.backward()
    optimizer.step()
    return float(loss)

train()

Reference:

https://github.com/devnkong/FLAG

forward(forward: Callable, x: Tensor, y: Tensor) Tensor[source]

Performs forward pass and adversarial training with FLAG algorithm.

Parameters:
  • forward (Callable) – The self-defined forward function of the model, which accepts obj:perturb as input.

  • x (Tensor) – The input node features.

  • y (Tensor) – The target node labels.

Returns:

The loss after adversarial training.

Return type:

Tensor

training: bool