torch_geometric.graphgym
Workflow and Register Modules
Loads the model checkpoint at a given epoch. |
|
Saves the model checkpoint at a given epoch. |
|
Removes the model checkpoint at a given epoch. |
|
Removes all but the last model checkpoint. |
|
Parses the command line arguments. |
|
|
|
This function sets the default config value. |
|
Load configurations from file system and command line. |
|
Dumps the config to the output directory specified in |
|
Create the directory for each random seed experiment run. |
|
Create the directory for full experiment run. |
|
Extract filename from file name path. |
|
Performs weight initialization. |
|
Create data loader object. |
|
Set up printing options. |
|
Create logger for the experiment. |
|
Compute loss and prediction score. |
|
Create model for graph machine learning. |
|
Creates a config-driven optimizer. |
|
Creates a config-driven learning rate scheduler. |
|
Trains a GraphGym model using PyTorch Lightning. |
|
Base function for registering a module in GraphGym. |
|
Registers an activation function in GraphGym. |
|
Registers a node feature encoder in GraphGym. |
|
Registers an edge feature encoder in GraphGym. |
|
Registers a customized GNN stage in GraphGym. |
|
Registers a GNN prediction head in GraphGym. |
|
Registers a GNN layer in GraphGym. |
|
Registers a GNN global pooling/readout layer in GraphGym. |
|
Registers a GNN model in GraphGym. |
|
Registers a configuration group in GraphGym. |
|
Registers a dataset in GraphGym. |
|
Registers a data loader in GraphGym. |
|
Registers an optimizer in GraphGym. |
|
Registers a learning rate scheduler in GraphGym. |
|
Registers a loss function in GraphGym. |
|
Registers a training function in GraphGym. |
|
Register a metric function in GraphGym. |
- load_ckpt(model: Module, optimizer: Optional[Optimizer] = None, scheduler: Optional[Any] = None, epoch: int = -1) int [source]
Loads the model checkpoint at a given epoch.
- Return type:
- save_ckpt(model: Module, optimizer: Optional[Optimizer] = None, scheduler: Optional[Any] = None, epoch: int = 0)[source]
Saves the model checkpoint at a given epoch.
- set_cfg(cfg)[source]
This function sets the default config value.
Note that for an experiment, only part of the arguments will be used The remaining unused arguments won’t affect anything. So feel free to register any argument in graphgym.contrib.config
We support at most two levels of configs, e.g.,
cfg.dataset.name
.
- Returns:
Configuration use by the experiment.
- load_cfg(cfg, args)[source]
Load configurations from file system and command line.
- Parameters:
cfg (CfgNode) – Configuration node
args (ArgumentParser) – Command argument parser
- dump_cfg(cfg)[source]
Dumps the config to the output directory specified in
cfg.out_dir
.- Parameters:
cfg (CfgNode) – Configuration node
- set_run_dir(out_dir)[source]
Create the directory for each random seed experiment run.
- Parameters:
out_dir (str) – Directory for output, specified in
cfg.out_dir
- get_fname(fname)[source]
Extract filename from file name path.
- Parameters:
fname (str) – Filename for the yaml format configuration file
- compute_loss(pred, true)[source]
Compute loss and prediction score.
- Parameters:
pred (torch.tensor) – Unnormalized prediction
true (torch.tensor) – Ground truth labels
Returns: Loss, normalized prediction score
- create_model(to_device=True, dim_in=None, dim_out=None) GraphGymModule [source]
Create model for graph machine learning.
- create_optimizer(params: Iterator[Parameter], cfg: Any) Any [source]
Creates a config-driven optimizer.
- Return type:
- create_scheduler(optimizer: Optimizer, cfg: Any) Any [source]
Creates a config-driven learning rate scheduler.
- Return type:
- train(model: GraphGymModule, datamodule: GraphGymDataModule, logger: bool = True, trainer_config: Optional[Dict[str, Any]] = None)[source]
Trains a GraphGym model using PyTorch Lightning.
- register_base(mapping: Dict[str, Any], key: str, module: Optional[Any] = None) Union[None, Callable] [source]
Base function for registering a module in GraphGym.
- register_act(key: str, module: Optional[Any] = None)[source]
Registers an activation function in GraphGym.
- register_node_encoder(key: str, module: Optional[Any] = None)[source]
Registers a node feature encoder in GraphGym.
- register_edge_encoder(key: str, module: Optional[Any] = None)[source]
Registers an edge feature encoder in GraphGym.
- register_stage(key: str, module: Optional[Any] = None)[source]
Registers a customized GNN stage in GraphGym.
- register_head(key: str, module: Optional[Any] = None)[source]
Registers a GNN prediction head in GraphGym.
- register_pooling(key: str, module: Optional[Any] = None)[source]
Registers a GNN global pooling/readout layer in GraphGym.
- register_network(key: str, module: Optional[Any] = None)[source]
Registers a GNN model in GraphGym.
- register_config(key: str, module: Optional[Any] = None)[source]
Registers a configuration group in GraphGym.
- register_loader(key: str, module: Optional[Any] = None)[source]
Registers a data loader in GraphGym.
- register_optimizer(key: str, module: Optional[Any] = None)[source]
Registers an optimizer in GraphGym.
- register_scheduler(key: str, module: Optional[Any] = None)[source]
Registers a learning rate scheduler in GraphGym.
- register_loss(key: str, module: Optional[Any] = None)[source]
Registers a loss function in GraphGym.
Model Modules
Provides an encoder for integer node features. |
|
The atom encoder used in OGB molecule dataset. |
|
The bond encoder used in OGB molecule dataset. |
|
Creates a GNN layer, given the specified input and output dimensions and the underlying configuration in |
|
Creates a NN layer used before message passing, given the specified input and output dimensions and the underlying configuration in |
|
Stacks a number of GNN layers. |
|
Encodes node and edge features, given the specified input dimension and the underlying configuration in |
|
A general Graph Neural Network (GNN) model. |
|
A GNN prediction head for node-level prediction tasks. |
|
A GNN prediction head for edge-level/link-level prediction tasks. |
|
A GNN prediction head for graph-level prediction tasks. |
|
A general wrapper for layers. |
|
A general wrapper class for a stacking multiple NN layers. |
|
A basic Linear layer. |
|
A batch normalization layer for node-level features. |
|
A batch normalization layer for edge-level features. |
|
A basic MLP model. |
|
A Graph Convolutional Network (GCN) layer. |
|
A GraphSAGE layer. |
|
A Graph Attention Network (GAT) layer. |
|
A Graph Isomorphism Network (GIN) layer. |
|
A SplineCNN layer. |
|
A general GNN layer. |
|
A general GNN layer with edge feature support. |
|
A general GNN layer that supports edge features and edge sampling. |
|
Returns batch-wise graph-level-outputs by adding node features across the node dimension. |
|
Returns batch-wise graph-level-outputs by averaging node features across the node dimension. |
|
Returns batch-wise graph-level-outputs by taking the channel-wise maximum across the node dimension. |
- class IntegerFeatureEncoder(emb_dim: int, num_classes: int)[source]
Provides an encoder for integer node features.
- Parameters:
Example
>>> encoder = IntegerFeatureEncoder(emb_dim=16, num_classes=10) >>> batch = torch.randint(0, 10, (10, 2)) >>> encoder(batch).size() torch.Size([10, 16])
- class AtomEncoder(emb_dim, *args, **kwargs)[source]
The atom encoder used in OGB molecule dataset.
- Parameters:
emb_dim (int) – The output embedding dimension.
Example
>>> encoder = AtomEncoder(emb_dim=16) >>> batch = torch.randint(0, 10, (10, 3)) >>> encoder(batch).size() torch.Size([10, 16])
- class BondEncoder(emb_dim: int)[source]
The bond encoder used in OGB molecule dataset.
- Parameters:
emb_dim (int) – The output embedding dimension.
Example
>>> encoder = BondEncoder(emb_dim=16) >>> batch = torch.randint(0, 10, (10, 3)) >>> encoder(batch).size() torch.Size([10, 16])
- GNNLayer(dim_in: int, dim_out: int, has_act: bool = True) GeneralLayer [source]
Creates a GNN layer, given the specified input and output dimensions and the underlying configuration in
cfg
.- Parameters:
- Return type:
- GNNPreMP(dim_in: int, dim_out: int, num_layers: int) GeneralMultiLayer [source]
Creates a NN layer used before message passing, given the specified input and output dimensions and the underlying configuration in
cfg
.- Parameters:
- Return type:
- class FeatureEncoder(dim_in: int)[source]
Encodes node and edge features, given the specified input dimension and the underlying configuration in
cfg
.- Parameters:
dim_in (int) – The input feature dimension.
- class GNN(dim_in: int, dim_out: int, **kwargs)[source]
A general Graph Neural Network (GNN) model.
The GNN model consists of three main components:
An encoder to transform input features into a fixed-size embedding space.
A processing or message passing stage for information exchange between nodes.
A head to produce the final output features/predictions.
The configuration of each component is determined by the underlying configuration in
cfg
.
- class GNNNodeHead(dim_in: int, dim_out: int)[source]
A GNN prediction head for node-level prediction tasks.
- class GNNEdgeHead(dim_in: int, dim_out: int)[source]
A GNN prediction head for edge-level/link-level prediction tasks.
- class GNNGraphHead(dim_in: int, dim_out: int)[source]
A GNN prediction head for graph-level prediction tasks. A post message passing layer (as specified by
cfg.gnn.post_mp
) is used to transform the pooled graph-level embeddings using an MLP.
- class GeneralLayer(name, layer_config: LayerConfig, **kwargs)[source]
A general wrapper for layers.
- Parameters:
name (str) – The registered name of the layer.
layer_config (LayerConfig) – The configuration of the layer.
**kwargs (optional) – Additional keyword arguments.
- class GeneralMultiLayer(name, layer_config: LayerConfig, **kwargs)[source]
A general wrapper class for a stacking multiple NN layers.
- Parameters:
name (str) – The registered name of the layer.
layer_config (LayerConfig) – The configuration of the layer.
**kwargs (optional) – Additional keyword arguments.
- class Linear(layer_config: LayerConfig, **kwargs)[source]
A basic Linear layer.
- Parameters:
layer_config (LayerConfig) – The configuration of the layer.
**kwargs (optional) – Additional keyword arguments.
- class BatchNorm1dNode(layer_config: LayerConfig)[source]
A batch normalization layer for node-level features.
- Parameters:
layer_config (LayerConfig) – The configuration of the layer.
- class BatchNorm1dEdge(layer_config: LayerConfig)[source]
A batch normalization layer for edge-level features.
- Parameters:
layer_config (LayerConfig) – The configuration of the layer.
- class MLP(layer_config: LayerConfig, **kwargs)[source]
A basic MLP model.
- Parameters:
layer_config (LayerConfig) – The configuration of the layer.
**kwargs (optional) – Additional keyword arguments.
- class GCNConv(layer_config: LayerConfig, **kwargs)[source]
A Graph Convolutional Network (GCN) layer.
- class GINConv(layer_config: LayerConfig, **kwargs)[source]
A Graph Isomorphism Network (GIN) layer.
- class GeneralEdgeConv(layer_config: LayerConfig, **kwargs)[source]
A general GNN layer with edge feature support.
- class GeneralSampleEdgeConv(layer_config: LayerConfig, **kwargs)[source]
A general GNN layer that supports edge features and edge sampling.
- global_add_pool(x: Tensor, batch: Optional[Tensor], size: Optional[int] = None) Tensor [source]
Returns batch-wise graph-level-outputs by adding node features across the node dimension.
For a single graph \(\mathcal{G}_i\), its output is computed by
\[\mathbf{r}_i = \sum_{n=1}^{N_i} \mathbf{x}_n.\]Functional method of the
SumAggregation
module.- Parameters:
x (torch.Tensor) – Node feature matrix \(\mathbf{X} \in \mathbb{R}^{(N_1 + \ldots + N_B) \times F}\).
batch (torch.Tensor, optional) – The batch vector \(\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N\), which assigns each node to a specific example.
size (int, optional) – The number of examples \(B\). Automatically calculated if not given. (default:
None
)
- Return type:
- global_mean_pool(x: Tensor, batch: Optional[Tensor], size: Optional[int] = None) Tensor [source]
Returns batch-wise graph-level-outputs by averaging node features across the node dimension.
For a single graph \(\mathcal{G}_i\), its output is computed by
\[\mathbf{r}_i = \frac{1}{N_i} \sum_{n=1}^{N_i} \mathbf{x}_n.\]Functional method of the
MeanAggregation
module.- Parameters:
x (torch.Tensor) – Node feature matrix \(\mathbf{X} \in \mathbb{R}^{(N_1 + \ldots + N_B) \times F}\).
batch (torch.Tensor, optional) – The batch vector \(\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N\), which assigns each node to a specific example.
size (int, optional) – The number of examples \(B\). Automatically calculated if not given. (default:
None
)
- Return type:
- global_max_pool(x: Tensor, batch: Optional[Tensor], size: Optional[int] = None) Tensor [source]
Returns batch-wise graph-level-outputs by taking the channel-wise maximum across the node dimension.
For a single graph \(\mathcal{G}_i\), its output is computed by
\[\mathbf{r}_i = \mathrm{max}_{n=1}^{N_i} \, \mathbf{x}_n.\]Functional method of the
MaxAggregation
module.- Parameters:
x (torch.Tensor) – Node feature matrix \(\mathbf{X} \in \mathbb{R}^{(N_1 + \ldots + N_B) \times F}\).
batch (torch.Tensor, optional) – The batch vector \(\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N\), which assigns each element to a specific example.
size (int, optional) – The number of examples \(B\). Automatically calculated if not given. (default:
None
)
- Return type:
Utility Modules
Aggregate over different random seeds of a single experiment. |
|
Aggregate across results from multiple experiments via grid search. |
|
Computes the number of parameters. |
|
Match the computational budget of a given baseline model. |
|
Get the current GPU memory usage. |
|
Auto select device for the current experiment. |
|
Determines if the model should be evaluated at the current epoch. |
|
Determines if the model should be evaluated at the current epoch. |
|
Dump a Python dictionary to a JSON file. |
|
Dump a list of Python dictionaries to a JSON file. |
|
Add a dictionary of statistics to a Tensorboard writer. |
|
Make a directory, remove any existing data. |
|
Default context manager that does nothing. |
- agg_runs(dir, metric_best='auto')[source]
Aggregate over different random seeds of a single experiment.
- agg_batch(dir, metric_best='auto')[source]
Aggregate across results from multiple experiments via grid search.
- params_count(model)[source]
Computes the number of parameters.
- Parameters:
model (nn.Module) – PyTorch model
- match_baseline_cfg(cfg_dict, cfg_dict_baseline, verbose=True)[source]
Match the computational budget of a given baseline model. The current configuration dictionary will be modifed and returned.
- dict_list_to_json(dict_list, fname)[source]
Dump a list of Python dictionaries to a JSON file.
- Parameters:
dict_list (list of dict) – List of Python dictionaries.
fname (str) – the output file name.