API Reference
Main model class
The main model class GNNModel
is there to tie together the Graph neural network backbone and a multilayer perceptron classifier model that can be configured for various tasks.
GNNModel
Bases: Module
Torch module for the full GCN model, which consists of a GCN backbone, a classifier, and a pooling layer, augmented with optional graph features network. Args: torch.nn.Module: base class
Source code in src/QuantumGrav/gnn_model.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
__init__(encoder, classifier, pooling_layer, graph_features_net=torch.nn.Identity())
Initialize the GNNModel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
encoder
|
GCNBackbone
|
GCN backbone network. |
required |
classifier
|
ClassifierBlock
|
Classifier block. |
required |
pooling_layer
|
Module
|
Pooling layer. |
required |
graph_features_net
|
Module
|
Graph features network. Defaults to torch.nn.Identity. |
Identity()
|
Source code in src/QuantumGrav/gnn_model.py
forward(x, edge_index, batch, graph_features=None, gcn_kwargs=None)
Forward run of the gnn model with optional graph features. First execute the graph-neural network backbone, then process the graph features, and finally apply the classifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input node features. |
required |
edge_index
|
Tensor
|
Graph connectivity information. |
required |
batch
|
Tensor
|
Batch vector for pooling. |
required |
graph_features
|
Tensor
|
Additional graph features. Defaults to None. |
None
|
gcn_kwargs
|
dict[Any, Any]
|
Additional arguments for the GCN. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tensor | Collection[Tensor]
|
torch.Tensor | Collection[torch.Tensor]: Class predictions. |
Source code in src/QuantumGrav/gnn_model.py
from_config(config)
classmethod
Create a GNNModel from a configuration dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict
|
Configuration dictionary containing parameters for the model. |
required |
Returns:
Name | Type | Description |
---|---|---|
GNNModel |
GNNModel
|
An instance of GNNModel. |
Source code in src/QuantumGrav/gnn_model.py
get_embeddings(x, edge_index, batch=None, gcn_kwargs=None)
Get embeddings from the GCN model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input node features. |
required |
edge_index
|
Tensor
|
Graph connectivity information. |
required |
batch
|
Tensor
|
Batch vector for pooling. |
None
|
gcn_kwargs
|
dict
|
Additional arguments for the GCN. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Embedding vector for the graph features. |
Source code in src/QuantumGrav/gnn_model.py
load(path, device=torch.device('cpu'))
classmethod
Load a model from file that has previously been save with the function 'save'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
path to load the model from. |
required |
device
|
device
|
device to put the model to. Defaults to torch.device("cpu") |
device('cpu')
|
Returns: GNNModel: model instance initialized with the sub-models loaded from file.
Source code in src/QuantumGrav/gnn_model.py
save(path)
Save the model state to file. This saves a dictionary structured like this: 'encoder': self.encoder, 'classifier': self.classifier, 'pooling_layer': self.pooling_layer, 'graph_features_net': self.graph_features_net
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
Path to save the model to |
required |
Source code in src/QuantumGrav/gnn_model.py
Multilayer Perceptron submodels
These classes provide a sequence of linear (affine) layers in various configurations that can be used to create classifiers by deriving from it.
Base class for models composed of linear layers
LinearSequential
Bases: Module
This class implements a neural network block consisting of a backbone (a sequence of linear layers with activation functions) and multiple output layers for classification tasks. It supports multi-objective classification by allowing multiple output layers, each corresponding to a different classification task, but can also be used for any other type of sequential processing that involves linear layers.
Source code in src/QuantumGrav/linear_sequential.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
__init__(input_dim, output_dims, hidden_dims=None, activation=torch.nn.ReLU, backbone_kwargs=None, output_kwargs=None, activation_kwargs=None)
Create a LinearSequential object with a backbone and multiple output layers. All layers are of type Linear
with an activation function in between (the backbone) and a set of linear output layers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim
|
int
|
input dimension of the LinearSequential object |
required |
output_dims
|
list[int]
|
list of output dimensions for each output layer, i.e., each classification task |
required |
hidden_dims
|
list[int]
|
list of hidden dimensions for the backbone |
None
|
activation
|
type[Module]
|
activation function to use. Defaults to torch.nn.ReLU. |
ReLU
|
backbone_kwargs
|
list[dict]
|
additional arguments for the backbone layers. Defaults to None. |
None
|
output_kwargs
|
list[dict]
|
additional arguments for the output layers. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If hidden_dims contains non-positive integers. |
ValueError
|
If output_dims is empty or contains non-positive integers. |
ValueError
|
If any output_dim is not a positive integer. |
Source code in src/QuantumGrav/linear_sequential.py
forward(x)
Forward pass through the LinearSequential object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
list[Tensor]
|
list[torch.Tensor]: List of output tensors from each classifier layer. |
Source code in src/QuantumGrav/linear_sequential.py
from_config(config)
classmethod
Create a LinearSequential from a configuration dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict[str, Any]
|
Configuration dictionary containing parameters for the LinearSequential. |
required |
Returns:
Name | Type | Description |
---|---|---|
LinearSequential |
LinearSequential
|
An instance of LinearSequential initialized with the provided configuration. |
Source code in src/QuantumGrav/linear_sequential.py
load(path, device=torch.device('cpu'))
classmethod
Load a LinearSequential instance from file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
path to the file to load the model from |
required |
device
|
device
|
device to put the model to. Defaults to torch.device("cpu") |
device('cpu')
|
Returns: LinearSequential: An instance of LinearSequential initialized from the loaded data.
Source code in src/QuantumGrav/linear_sequential.py
save(path)
Save the model's state to file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
path to save the model to. |
required |
Classifier model based on linear sequential base class
ClassifierBlock
Bases: LinearSequential
This class implements a neural network block consisting of a backbone (a sequence of linear layers with activation functions) and multiple output layers for classification tasks. It supports multi-objective classification by allowing multiple output layers, each corresponding to a different classification task.
Source code in src/QuantumGrav/classifier_block.py
__init__(input_dim, output_dims, hidden_dims=None, activation=torch.nn.ReLU, backbone_kwargs=None, output_kwargs=None, activation_kwargs=None)
Instantiate a ClassifierBlock.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim
|
int
|
input dimension of the ClassifierBlock |
required |
output_dims
|
list[int]
|
output dimensions for each classification task. |
required |
hidden_dims
|
list[int]
|
list of hidden dimensions for the backbone network. Defaults to None. |
None
|
activation
|
type[Module]
|
activation function to use. Defaults to torch.nn.ReLU. |
ReLU
|
backbone_kwargs
|
list[dict]
|
keyword arguments for the backbone network. Defaults to None. |
None
|
output_kwargs
|
list[dict]
|
keyword arguments for the output layers. Defaults to None. |
None
|
activation_kwargs
|
list[dict]
|
keyword arguments for the activation functions. Defaults to None. |
None
|
Source code in src/QuantumGrav/classifier_block.py
from_config(config)
classmethod
Create a ClassifierBlock from a configuration dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict
|
Configuration dictionary containing parameters for the block. |
required |
Returns:
Name | Type | Description |
---|---|---|
ClassifierBlock |
ClassifierBlock
|
An instance of ClassifierBlock. |
Source code in src/QuantumGrav/classifier_block.py
Graph Neural network submodels
The submodel classes in this section comprise the graph neural network backbone of a QuantumGrav model.
Graph features block
This is a submodel derived from linear sequential that allows us to integrate graph-level features into the model's representation.
GraphFeaturesBlock
Bases: LinearSequential
Graph Features Block for processing global graph features. Similarly to the classifier, this consists of a sequence of linear layers with activation functions.
Source code in src/QuantumGrav/graphfeatures_block.py
__init__(input_dim, output_dim, hidden_dims=None, activation=torch.nn.ReLU, layer_kwargs=None, activation_kwargs=None)
Create a GraphFeaturesBlock instance. This will create at least one hidden layer and one output layer, with the specified input and output dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim
|
int
|
input dimension of the GraphFeaturesBlock |
required |
output_dim
|
int
|
output dimension of the GraphFeaturesBlock |
required |
hidden_dims
|
list[int]
|
output dimensions of the hidden layers. Defaults to None. |
None
|
activation
|
Module
|
activation function type, e.g., torch.nn.ReLU. Defaults to torch.nn.ReLU. |
ReLU
|
layer_kwargs
|
list[dict]
|
keyword arguments for the constructors of each layer. Defaults to None. |
None
|
activation_kwargs
|
dict
|
keyword arguments for the construction of each activation function. Defaults to None. |
None
|
Source code in src/QuantumGrav/graphfeatures_block.py
forward(x)
Forward pass through the GraphFeaturesBlock. Args: x (torch.Tensor): Input tensor with shape (batch_size, input_dim). Returns: torch.Tensor: Output tensor with shape (batch_size, output_dim).
Source code in src/QuantumGrav/graphfeatures_block.py
from_config(config)
classmethod
Create a GraphFeaturesBlock from a configuration dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict
|
Configuration dictionary containing parameters for the block. |
required |
Returns:
Name | Type | Description |
---|---|---|
GraphFeaturesBlock |
GraphFeaturesBlock
|
An instance of GraphFeaturesBlock. |
Source code in src/QuantumGrav/graphfeatures_block.py
Graph model block
This submodel is the main part of the graph neural network backbone, composed of a set of GNN layers from pytorch-geometric
with dropout and BatchNorm
.
GNNBlock
Bases: Module
Graph Neural Network Block. Consists of a GNN layer, a normalizer, an activation function, and a residual connection. The gnn-layer is applied first, followed by the normalizer and activation function. The result is then projected from the input dimensions to the output dimensions using a linear layer and added to the original input (residual connection). Finally, dropout is applied for regularization.
Source code in src/QuantumGrav/gnn_block.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
__init__(in_dim, out_dim, dropout=0.3, gnn_layer_type=tgnn.conv.GCNConv, normalizer=torch.nn.Identity, activation=torch.nn.ReLU, gnn_layer_args=None, gnn_layer_kwargs=None, norm_args=None, norm_kwargs=None, activation_args=None, activation_kwargs=None, projection_args=None, projection_kwargs=None)
Create a GNNBlock instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_dim
|
int
|
The dimensions of the input features. |
required |
out_dim
|
int
|
The dimensions of the output features. |
required |
dropout
|
float
|
The dropout probability. Defaults to 0.3. |
0.3
|
gnn_layer_type
|
Module
|
The type of GNN-layer to use. Defaults to tgnn.conv.GCNConv. |
GCNConv
|
normalizer
|
Module
|
The normalizer layer to use. Defaults to torch.nn.Identity. |
Identity
|
activation
|
Module
|
The activation function to use. Defaults to torch.nn.ReLU. |
ReLU
|
gnn_layer_args
|
list[Any]
|
Additional arguments for the GNN layer. Defaults to None. |
None
|
gnn_layer_kwargs
|
dict[str, Any]
|
Additional keyword arguments for the GNN layer. Defaults to None. |
None
|
norm_args
|
list[Any]
|
Additional arguments for the normalizer layer. Defaults to None. |
None
|
norm_kwargs
|
dict[str, Any]
|
Additional keyword arguments for the normalizer layer. Defaults to None. |
None
|
activation_args
|
list[Any]
|
Additional arguments for the activation function. Defaults to None. |
None
|
activation_kwargs
|
dict[str, Any]
|
Additional keyword arguments for the activation function. Defaults to None. |
None
|
projection_args
|
list[Any]
|
Additional arguments for the projection layer. Defaults to None. |
None
|
projection_kwargs
|
dict[str, Any]
|
Additional keyword arguments for the projection layer. Defaults to None. |
None
|
Source code in src/QuantumGrav/gnn_block.py
forward(x, edge_index, **kwargs)
Forward pass for the GNNBlock. First apply the graph convolution layer, then normalize and apply the activation function. Finally, apply a residual connection and dropout. Args: x (torch.Tensor): The input node features. edge_index (torch.Tensor): The graph connectivity information. edge_weight (torch.Tensor, optional): The edge weights. Defaults to None. kwargs (dict[Any, Any], optional): Additional keyword arguments for the GNN layer. Defaults to None.
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The output node features. |
Source code in src/QuantumGrav/gnn_block.py
from_config(config)
classmethod
Create a GNNBlock from a configuration dictionary. When the config does not have 'dropout', it defaults to 0.3.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict[str, Any]
|
Configuration dictionary containing the parameters for the GNNBlock. |
required |
Returns:
Name | Type | Description |
---|---|---|
GNNBlock |
GNNBlock
|
An instance of GNNBlock initialized with the provided configuration. |
Source code in src/QuantumGrav/gnn_block.py
load(path, device=torch.device('cpu'))
classmethod
Load a mode instance from file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
Path to the file to load. |
required |
device
|
device
|
device to put the model to. Defaults to torch.device("cpu") |
device('cpu')
|
Returns: GNNBlock: A GNNBlock instance initialized from the data loaded from the file.
Source code in src/QuantumGrav/gnn_block.py
save(path)
Save the model's state to file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
path to save the model to. |
required |
Model evaluation
This module provides base classes that take the output of applying the model to a validation or training dataset, and derive useful quantities to evaluate the model quality. These do not do anything useful by default. Rather, you must derive your own class from them that implemements your desired evaluation, e.g., using an F1 score.
DefaultEarlyStopping
Early stopping based on a validation metric.
Source code in src/QuantumGrav/evaluate.py
__call__(data)
Check if early stopping criteria are met.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Iterable | DataFrame | Series
|
Iterable of validation metrics, e.g., list of scalars, list of tuples, Dataframe, numpy array... |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if training should be stopped, False otherwise. |
Source code in src/QuantumGrav/evaluate.py
__init__(patience, delta=0.0001, window=7)
Early stopping initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patience
|
int
|
Number of epochs with no improvement after which training will be stopped. |
required |
delta
|
float
|
Minimum change to consider an improvement. Defaults to 1e-4. |
0.0001
|
window
|
int
|
Size of the moving window for smoothing. Defaults to 7. |
7
|
Source code in src/QuantumGrav/evaluate.py
DefaultEvaluator
Source code in src/QuantumGrav/evaluate.py
__init__(device, criterion, apply_model=None)
Default evaluator for model evaluation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
_type_
|
The device to run the evaluation on. |
required |
criterion
|
Callable
|
The loss function to use for evaluation. |
required |
apply_model
|
Callable
|
A function to apply the model to the data. |
None
|
Source code in src/QuantumGrav/evaluate.py
evaluate(model, data_loader)
Evaluate the model on the given data loader.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
Model to evaluate. |
required |
data_loader
|
DataLoader
|
Data loader for evaluation. |
required |
Returns:
Type | Description |
---|---|
Any
|
list[Any]: A list of evaluation results. |
Source code in src/QuantumGrav/evaluate.py
report(data)
Report the evaluation results to stdout
Source code in src/QuantumGrav/evaluate.py
DefaultTester
Bases: DefaultEvaluator
Source code in src/QuantumGrav/evaluate.py
__init__(device, criterion, apply_model=None)
Default tester for model testing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
_type_
|
The device to run the testing on. |
required |
criterion
|
Callable
|
The loss function to use for testing. |
required |
apply_model
|
Callable
|
A function to apply the model to the data. |
None
|
Source code in src/QuantumGrav/evaluate.py
test(model, data_loader)
Test the model on the given data loader.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
Model to test. |
required |
data_loader
|
DataLoader
|
Data loader for testing. |
required |
Returns:
Type | Description |
---|---|
list[Any]: A list of testing results. |
Source code in src/QuantumGrav/evaluate.py
DefaultValidator
Bases: DefaultEvaluator
Source code in src/QuantumGrav/evaluate.py
validate(model, data_loader)
Validate the model on the given data loader.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
Model to validate. |
required |
data_loader
|
DataLoader
|
Data loader for validation. |
required |
Returns: list[Any]: A list of validation results.
Source code in src/QuantumGrav/evaluate.py
Datasets
The package supports three kinds of datasets with a common baseclass QGDatasetBase
. For the basics of how those work, check out the pytorch-geometric documentation of dataset
These are:
- QGDataset
: A dataset that relies on an on-disk storage of the processed data. It lazily loads csets from disk when needed.
- QGDatasetInMemory
: A dataset that holds the entire processed dataset in memory at once.
- QGDatasetOnthefly
: This dataset does not hold anything on disk or in memory, but creates the data on demand from some supplied Julia code.
Dataset base class
QGDatasetBase
Mixin class that provides common functionality for the dataset classes. Works only for file-based datasets. Provides methods for processing data.
Source code in src/QuantumGrav/dataset_base.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
processed_dir
property
Get the path to the processed directory.
Returns:
Name | Type | Description |
---|---|---|
str |
str | None
|
The path to the processed directory, or None if it doesn't exist. |
processed_file_names
property
Get a list of processed files in the processed directory.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A list of processed file paths, excluding JSON files. |
raw_file_names
property
Get the raw file paths from the input list.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A list of raw file paths. |
__init__(input, output, mode='hdf5', reader=None, float_type=torch.float32, int_type=torch.int64, validate_data=True, n_processes=1, chunksize=1000, **kwargs)
Initialize a DatasetMixin instance. This class is designed to handle the loading, processing, and writing of QuantumGrav datasets. It provides a common interface for both in-memory and on-disk datasets. It is not to be instantiated directly, but rather used as a mixin for other dataset classes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input (list[str | Path]
|
The list of input files for the dataset, or a callable that generates a set of input files. |
required | |
output
|
str | Path
|
The output directory where processed data will be stored. |
required |
mode
|
str
|
File storage mode. 'zarr' or 'hdf5' |
'hdf5'
|
reader
|
Callable[[File | Group, dtype, dtype, bool], list[Data]] | None
|
A function to load data from a file. Defaults to None. |
None
|
float_type
|
dtype
|
The data type to use for floating point values. Defaults to torch.float32. |
float32
|
int_type
|
dtype
|
The data type to use for integer values. Defaults to torch.int64. |
int64
|
validate_data
|
bool
|
Whether to validate the data after loading. Defaults to True. |
True
|
n_processes
|
int
|
The number of processes to use for parallel processing of read data. Defaults to 1. |
1
|
chunksize
|
int
|
The size of the chunks to process in parallel. Defaults to 1000. |
1000
|
Raises:
Type | Description |
---|---|
ValueError
|
If one of the input data files is not a valid HDF5 file |
ValueError
|
If the metadata retrieval function is invalid. |
FileNotFoundError
|
If an input file does not exist. |
Source code in src/QuantumGrav/dataset_base.py
process_chunk_hdf5(raw_file, start, pre_transform=None, pre_filter=None)
Process a chunk of data from the raw file. This method is intended to be used in the data loading pipeline to read a chunk of data, apply transformations, and filter the read data, and thus should not be called directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_file
|
File
|
The raw HDF5 file to read from. |
required |
start
|
int
|
The starting index of the chunk. |
required |
pre_transform
|
Callable[[Data], Data] | None
|
Transformation that adds additional features to the data. Defaults to None. |
None
|
pre_filter
|
Callable[[Data], bool] | None
|
A function that filters the data. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
list[Data]
|
list[Data]: The processed data or None if the chunk is empty. |
Source code in src/QuantumGrav/dataset_base.py
process_chunk_zarr(store, start, pre_transform=None, pre_filter=None)
Process a chunk of data from the raw file. This method is intended to be used in the data loading pipeline to read a chunk of data, apply transformations, and filter the read data, and thus should not be called directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
store
|
LocalStore
|
local zarr storage |
required |
start
|
int
|
start index |
required |
pre_transform
|
Callable[[Data], Data] | None
|
Transformation that adds additional features to the data. Defaults to None. |
None
|
pre_filter
|
Callable[[Data], bool] | None
|
A function that filters the data. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
list[Data]
|
list[Data]: The processed data or None if the chunk is empty. |
Source code in src/QuantumGrav/dataset_base.py
Dataset holding everything in memory
QGDatasetInMemory
Bases: QGDatasetBase
, InMemoryDataset
A dataset class for QuantumGrav data that can be loaded into memory.
Source code in src/QuantumGrav/dataset_inmemory.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
__init__(input, output, mode='hdf5', reader=None, float_type=torch.float32, int_type=torch.int64, validate_data=True, chunksize=1000, n_processes=1, transform=None, pre_transform=None, pre_filter=None)
Initialize a QGDatasetInMemory instance. This class is designed to handle the loading, processing, and writing of QuantumGrav datasets that can be loaded into memory completely.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input
|
list[str | Path]
|
A list of file paths (as strings or Path objects) to the input data files. |
required |
output
|
str | Path
|
A file path (as a string or Path object) to the output data file. |
required |
mode
|
str
|
File storage mode. 'zarr' or 'hdf5' |
'hdf5'
|
reader
|
Callable[[File | Group, dtype, dtype, bool], list[Data]] | None
|
A function to read the data from the input files. Defaults to None. |
None
|
float_type
|
dtype
|
Data type for float tensors. Defaults to torch.float32. |
float32
|
int_type
|
dtype
|
Data type for int tensors. Defaults to torch.int64. |
int64
|
validate_data
|
bool
|
Whether to validate the data. Defaults to True. |
True
|
chunksize
|
int
|
Size of data chunks to process at once. Defaults to 1000. |
1000
|
n_processes
|
int
|
Number of processes to use for data loading. Defaults to 1. |
1
|
transform
|
Callable[[Data], Data] | None
|
Function to transform the data each time the data is loaded. Defaults to None. |
None
|
pre_transform
|
Callable[[Data], Data] | None
|
Function to transform the read data once and store the results on the disk. Defaults to None. |
None
|
pre_filter
|
Callable[[Data], bool] | None
|
Function to pre-filter the data once and store the results on the disk. Defaults to None. |
None
|
Source code in src/QuantumGrav/dataset_inmemory.py
process()
Process the dataset from the read rawdata into its final form.
Source code in src/QuantumGrav/dataset_inmemory.py
Dataset creating csets on the fly
QGDatasetOnthefly
Bases: Dataset
A dataset that generates data on the fly using a Julia function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Dataset
|
Dataset
|
The base dataset class. |
required |
Source code in src/QuantumGrav/dataset_onthefly.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
__init__(config, jl_code_path=None, jl_constructor_name=None, jl_base_module_path=None, jl_dependencies=None, transform=None, converter=None)
Initialize the dataset. This will initialize the Julia worker and set up the dataset. The julia worker must be callable with a single argument which is the number of samples to generate. The worker will return a list of raw data dictionaries which will be transformed into PyTorch Geometric Data objects using the provided transform function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict[str, Any]
|
Configuration dictionary. |
required |
jl_code_path
|
str | Path | None
|
Path to the Julia code. Defaults to None. |
None
|
jl_constructor_name
|
str | None
|
Name of the Julia constructor. Defaults to None. |
None
|
jl_base_module_path
|
str | Path | None
|
Path to the base Julia module. Defaults to None. |
None
|
jl_dependencies
|
list[str] | None
|
List of Julia dependencies. Defaults to None. |
None
|
transform
|
Callable[[dict[Any, Any]], Data] | None
|
Function to transform raw data into PyTorch Geometric Data objects. Defaults to None. |
None
|
converter
|
Callable[[Any], Any] | None
|
Function to convert Julia objects into standard Python objects. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the transform function is not provided. |
ValueError
|
If the converter function is not provided. |
RuntimeError
|
If there is an error initializing the Julia process. |
Source code in src/QuantumGrav/dataset_onthefly.py
get(_)
Get a data point from the dataset. This relies on the Julia worker accepting a single integer argument to its call operator, which is the number of samples to generate. The index is ignored since the dataset generates data on the fly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_
|
int
|
The index of the data point to retrieve. This is ignored since the dataset generates data on the fly. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the worker process is not initialized. |
raw_data
|
If there is an error retrieving raw data from the worker. |
RuntimeError
|
If there is an error transforming the data. |
Returns:
Name | Type | Description |
---|---|---|
Data |
Data
|
The transformed data point. |
Source code in src/QuantumGrav/dataset_onthefly.py
len()
Return the length of the dataset.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of samples in the dataset. |
make_batch(size)
Create a batch of data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
The number of samples in the batch. |
required |
Returns:
Type | Description |
---|---|
list[Data]
|
list[Data]: The list of Data objects in the batch. |
Source code in src/QuantumGrav/dataset_onthefly.py
Dataset loading data from disk
QGDataset
Bases: QGDatasetBase
, Dataset
A dataset class for QuantumGrav data that is designed to handle large datasets stored on disk. This class provides methods for loading, processing, and writing data that are common to both in-memory and on-disk datasets.
Source code in src/QuantumGrav/dataset_ondisk.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
__init__(input, output, mode='hdf5', reader=None, float_type=torch.float32, int_type=torch.int64, validate_data=True, chunksize=1000, n_processes=1, transform=None, pre_transform=None, pre_filter=None)
Create a new QGDataset instance. This class is designed to handle the loading, processing, and writing of QuantumGrav datasets that are stored on disk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input
|
list[str | Path] | Callable[[Any], dict]
|
List of input hdf5 file paths. |
required |
output
|
str | Path
|
Output directory where processed data will be stored. |
required |
mode
|
str
|
File storage mode. 'zarr' or 'hdf5' |
'hdf5'
|
reader
|
Callable[[File | Group, int], list[Data]] | None
|
Function to read data from the hdf5 file. Defaults to None. |
None
|
float_type
|
dtype
|
Data type for float tensors. Defaults to torch.float32. |
float32
|
int_type
|
dtype
|
Data type for int tensors. Defaults to torch.int64. |
int64
|
validate_data
|
bool
|
Whether to validate the data. Defaults to True. |
True
|
chunksize
|
int
|
Size of data chunks to process at once. Defaults to 1000. |
1000
|
n_processes
|
int
|
Number of processes to use for data loading. Defaults to 1. |
1
|
transform
|
Callable[[Data], Data] | None
|
Function to transform the data. Defaults to None. |
None
|
pre_transform
|
Callable[[Data], Data] | None
|
Function to pre-transform the data. Defaults to None. |
None
|
pre_filter
|
Callable[[Data], bool] | None
|
Function to pre-filter the data. Defaults to None. |
None
|
Source code in src/QuantumGrav/dataset_ondisk.py
get(idx)
Get a single data sample by index.
Source code in src/QuantumGrav/dataset_ondisk.py
len()
Get the number of samples in the dataset.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of samples in the dataset. |
process()
Process the dataset from the read rawdata into its final form.
Source code in src/QuantumGrav/dataset_ondisk.py
write_data(data, idx)
Write the processed data to disk using torch.save
. This is a default implementation that can be overridden by subclasses, and is intended to be used in the data loading pipeline. Thus, is not intended to be called directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
list[Data]
|
The list of Data objects to write to disk. |
required |
idx
|
int
|
The index to use for naming the files. |
required |
Source code in src/QuantumGrav/dataset_ondisk.py
Julia-Python integration
This class provides a bridge to some user-supplied Julia code and converts its output into something Python can work with.
JuliaWorker
This class runs a given Julia callable object from a given Julia code file. It additionally imports the QuantumGrav julia module and installs given dependencies if provided. After creation, the wrapped julia callable can be called via the call method of this calls. Warning: This class requires the juliacall package to be installed in the Python environment. Warning: This class is in early development and may change in the future, be slow, or otherwise not ready for high performance production use.
Source code in src/QuantumGrav/julia_worker.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
__call__(*args, **kwargs)
Calls the wrapped Julia generator with the given arguments.
Raises:
Type | Description |
---|---|
RuntimeError
|
If the Julia module is not initialized. |
Args: args: Positional arguments to pass to the Julia generator. *kwargs: Keyword arguments to pass to the Julia generator. Returns: Any: The raw data generated by the Julia generator.
Source code in src/QuantumGrav/julia_worker.py
__init__(jl_kwargs=None, jl_code_path=None, jl_constructor_name=None, jl_base_module_path=None, jl_dependencies=None)
Initializes the JuliaWorker with the given parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jl_kwargs
|
dict[str, Any] | None
|
Keyword arguments to pass to the Julia callable object constructor. Defaults to None. |
None
|
jl_code_path
|
str | Path | None
|
Path to the Julia code file in which the callable object is defined. Defaults to None. |
None
|
jl_constructor_name
|
str | None
|
Name of the Julia constructor function. Defaults to None. |
None
|
jl_base_module_path
|
str | Path | None
|
Path to the base Julia module 'QuantumGrav.jl'. If not given, tries to load it via a default |
None
|
jl_dependencies
|
list[str] | None
|
List of Julia package dependencies. Defaults to None. Will be installed via |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the Julia function name is not provided. |
ValueError
|
If the Julia code path is not provided. |
FileNotFoundError
|
If the Julia code path does not exist. |
NotImplementedError
|
If the base module path is not provided. |
RuntimeError
|
If there is an error loading the base module. |
RuntimeError
|
If there is an error loading Julia dependencies. |
RuntimeError
|
If there is an error loading the Julia code. |
Source code in src/QuantumGrav/julia_worker.py
Model training
This consists of two classes, one which provides the basic training functionality - Trainer
, and a class derived from this, TrainerDDP
, which provides functionality for distributed data parallel training.
Trainer
This class provides wrapper functions for setting up a model and for training and evaluating it. The basic concept is that everything is defined in a yaml file and handed to this class together with evaluator classes. After construction, the train
and test
functions will take care of the training and testing of the model.
Trainer
Trainer class for training and evaluating GNN models.
Source code in src/QuantumGrav/train.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
|
__init__(config, criterion, apply_model=None, early_stopping=None, validator=None, tester=None)
Initialize the trainer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict[str, Any]
|
The configuration dictionary. |
required |
criterion
|
Callable
|
The loss function to use. |
required |
apply_model
|
Callable | None
|
A function to apply the model. Defaults to None. |
None
|
early_stopping
|
Callable[[Collection[Any]], bool] | None
|
A function for early stopping. Defaults to None. |
None
|
validator
|
DefaultValidator | None
|
A validator for model evaluation. Defaults to None. |
None
|
tester
|
DefaultTester | None
|
A tester for model evaluation. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the configuration is invalid. |
Source code in src/QuantumGrav/train.py
initialize_model()
Initialize the model for training.
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The initialized model. |
Source code in src/QuantumGrav/train.py
initialize_optimizer()
Initialize the optimizer for training.
Raises:
Type | Description |
---|---|
RuntimeError
|
If the model is not initialized. |
Returns:
Type | Description |
---|---|
Optimizer | None
|
torch.optim.Optimizer: The initialized optimizer. |
Source code in src/QuantumGrav/train.py
load_checkpoint(epoch, name_addition='')
Load model checkpoint to the device given
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epoch
|
int
|
The epoch number to load. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the model is not initialized. |
Source code in src/QuantumGrav/train.py
prepare_dataloaders(dataset, split=[0.8, 0.1, 0.1])
Prepare the data loaders for training, validation, and testing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Dataset
|
The dataset to prepare. |
required |
split
|
list[float]
|
The split ratios for training, validation, and test sets. Defaults to [0.8, 0.1, 0.1]. |
[0.8, 0.1, 0.1]
|
Returns:
Type | Description |
---|---|
Tuple[DataLoader, DataLoader, DataLoader]
|
Tuple[DataLoader, DataLoader, DataLoader]: The data loaders for training, validation, and testing. |
Source code in src/QuantumGrav/train.py
run_test(test_loader)
Run testing phase.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
test_loader
|
DataLoader
|
The data loader for the test set. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the model is not initialized. |
RuntimeError
|
If the test data is not available. |
Returns:
Type | Description |
---|---|
Collection[Any]
|
Collection[Any]: A collection of test results that can be scalars, tensors, lists, dictionaries or any other data type that the tester might return. |
Source code in src/QuantumGrav/train.py
run_training(train_loader, val_loader, trial=None)
Run the training process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_loader
|
DataLoader
|
The data loader for the training set. |
required |
val_loader
|
DataLoader
|
The data loader for the validation set. |
required |
trial
|
Trial | None
|
An Optuna trial for hyperparameter tuning. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tuple[Tensor | Collection[Any], Tensor | Collection[Any]]
|
Tuple[torch.Tensor | Collection[Any], torch.Tensor | Collection[Any]]: The training and validation results. |
Source code in src/QuantumGrav/train.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
save_checkpoint(name_addition='')
Save model checkpoint.
Raises:
Type | Description |
---|---|
ValueError
|
If the model is not initialized. |
ValueError
|
If the model configuration does not contain 'name'. |
ValueError
|
If the training configuration does not contain 'checkpoint_path'. |
Source code in src/QuantumGrav/train.py
Distributed data parallel Trainer class
This is based on this part of the pytorch documentation and is untested at the time of writing.
TrainerDDP
Bases: Trainer
Source code in src/QuantumGrav/train_ddp.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
__init__(rank, config, criterion, apply_model=None, early_stopping=None, validator=None, tester=None)
Initialize the distributed data parallel (DDP) trainer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rank
|
int
|
The rank of the current process. |
required |
config
|
dict[str, Any]
|
The configuration dictionary. |
required |
criterion
|
Callable
|
The loss function. |
required |
apply_model
|
Callable | None
|
The function to apply the model. Defaults to None. |
None
|
early_stopping
|
Callable[[list[dict[str, Any]]], bool] | None
|
The early stopping function. Defaults to None. |
None
|
validator
|
DefaultValidator | None
|
The validator for model evaluation. Defaults to None. |
None
|
tester
|
DefaultTester | None
|
The tester for model testing. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the configuration is invalid. |
Source code in src/QuantumGrav/train_ddp.py
initialize_model()
Initialize the model for training.
Returns:
Name | Type | Description |
---|---|---|
DDP |
DistributedDataParallel
|
The initialized model. |
Source code in src/QuantumGrav/train_ddp.py
prepare_dataloaders(dataset, split=[0.8, 0.1, 0.1])
Prepare the data loaders for training, validation, and testing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Dataset
|
The dataset to split. |
required |
split
|
list[float]
|
The proportions for train/val/test split. Defaults to [0.8, 0.1, 0.1]. |
[0.8, 0.1, 0.1]
|
Returns:
Type | Description |
---|---|
Tuple[DataLoader, DataLoader, DataLoader]
|
Tuple[ DataLoader, DataLoader, DataLoader, ]: The data loaders for training, validation, and testing. |
Source code in src/QuantumGrav/train_ddp.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
run_training(train_loader, val_loader, trial=None)
Run the training loop for the distributed model. This will synchronize for validation. No testing is performed in this function. The model will only be checkpointed and early stopped on the 'rank' 0 process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_loader
|
DataLoader
|
The training data loader. |
required |
val_loader
|
DataLoader
|
The validation data loader. |
required |
trial
|
Trial | None
|
An Optuna trial for hyperparameter optimization. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tuple[Tensor | Collection[Any], Tensor | Collection[Any]]
|
Tuple[torch.Tensor | Collection[Any], torch.Tensor | Collection[Any]]: The training and validation results. |
Source code in src/QuantumGrav/train_ddp.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
save_checkpoint(name_addition='')
Save model checkpoint.
Raises:
Type | Description |
---|---|
ValueError
|
If the model is not initialized. |
ValueError
|
If the model configuration does not contain 'name'. |
ValueError
|
If the training configuration does not contain 'checkpoint_path'. |
Source code in src/QuantumGrav/train_ddp.py
cleanup_ddp()
Clean up the distributed process group.
initialize_ddp(rank, worldsize, master_addr='localhost', master_port='12345', backend='nccl')
Initialize the distributed process group. This assumes one process per GPU.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rank
|
int
|
The rank of the current process. |
required |
worldsize
|
int
|
The total number of processes. |
required |
master_addr
|
str
|
The address of the master process. Defaults to "localhost". This needs to be the ip of the master node if you are running on a cluster. |
'localhost'
|
master_port
|
str
|
The port of the master process. Defaults to "12345". Choose a high port if you are running multiple jobs on the same machine to avoid conflicts. If running on a cluster, this should be the port that the master node is listening on. |
'12345'
|
backend
|
str
|
The backend to use for distributed training. Defaults to "nccl". |
'nccl'
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If the environment variables MASTER_ADDR and MASTER_PORT are already set. |
Source code in src/QuantumGrav/train_ddp.py
Utilities
General utilities that are used throughout this package.
get_registered_activation(name)
Get a registered activation layer by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the activation layer. |
required |
Returns:
Type | Description |
---|---|
type[Module] | None
|
type[torch.nn.Module] | None: The registered activation layer named |
Source code in src/QuantumGrav/utils.py
get_registered_gnn_layer(name)
Get a registered GNN layer by name. Args: name (str): The name of the GNN layer.
Returns:
Type | Description |
---|---|
type[Module] | None
|
type[torch.nn.Module] | None: The registered GNN layer named |
Source code in src/QuantumGrav/utils.py
get_registered_normalizer(name)
Get a registered normalizer layer by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the normalizer layer. |
required |
Returns:
Type | Description |
---|---|
type[Module] | None
|
type[torch.nn.Module]| None: The registered normalizer layer named |
Source code in src/QuantumGrav/utils.py
get_registered_pooling_layer(name)
Get a registered pooling layer by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the pooling layer. |
required |
Returns:
Type | Description |
---|---|
Module | None
|
torch.nn.Module | None: The registered pooling layer named |
Source code in src/QuantumGrav/utils.py
list_registered_activations()
list_registered_gnn_layers()
list_registered_normalizers()
list_registered_pooling_layers()
register_activation(activation_name, activation_layer)
Register an activation layer with the module
Parameters:
Name | Type | Description | Default |
---|---|---|---|
activation_name
|
str
|
The name of the activation layer. |
required |
activation_layer
|
type[Module]
|
The activation layer to register. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the activation layer is already registered. |
Source code in src/QuantumGrav/utils.py
register_gnn_layer(gnn_layer_name, gnn_layer)
Register a GNN layer with the module
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gnn_layer_name
|
str
|
The name of the GNN layer. |
required |
gnn_layer
|
type[Module]
|
The GNN layer to register. |
required |
Source code in src/QuantumGrav/utils.py
register_normalizer(normalizer_name, normalizer_layer)
Register a normalizer layer with the module
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normalizer_name
|
str
|
The name of the normalizer. |
required |
normalizer_layer
|
type[Module]
|
The normalizer layer to register. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the normalizer layer is already registered. |
Source code in src/QuantumGrav/utils.py
register_pooling_layer(pooling_layer_name, pooling_layer)
Register a pooling layer with the module
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pooling_layer_name
|
str
|
The name of the pooling layer. |
required |
pooling_layer
|
Module
|
The pooling layer to register. |
required |