graphnetz.training¶
training
¶
Lightweight training loops shared by the example notebooks.
Each function returns a plain dict of per-epoch metrics, ready to feed into
plot_history.
All trainers accept device='auto' (the default), which dispatches to
CUDA when available, then Apple-silicon MPS, then CPU. Pass an explicit
torch.device or string to pin placement.
Functions:
| Name | Description |
|---|---|
train_node_classification |
Train a node classifier with Planetoid-style train/val/test masks. |
train_graph_classification |
Train a graph-level classifier. |
train_graph_regression |
Train a graph-level regressor (MSE loss, MAE on val). |
train_node_degree_regression |
Self-supervised node-level regression: predict log node degree. |
train_dgi |
Train a Deep Graph Infomax model (unsupervised). |
train_link_prediction |
Train a link predictor with binary cross-entropy on RandomLinkSplit. |
train_relational_link_prediction |
Train a relational link predictor (DistMult) on knowledge graph triples. |
train_node_classification
¶
train_node_classification(
model: Module,
data: Data,
epochs: int = 100,
lr: float = 0.01,
weight_decay: float = 0.0005,
verbose: bool = False,
device: device | str | None = "auto",
) -> dict[str, list[float]]
Train a node classifier with Planetoid-style train/val/test masks.
Source code in src/graphnetz/training.py
train_graph_classification
¶
train_graph_classification(
model: Module,
train_loader: DataLoader,
val_loader: DataLoader,
epochs: int = 30,
lr: float = 0.001,
verbose: bool = False,
device: device | str | None = "auto",
test_loader: DataLoader | None = None,
) -> dict[str, list[float]]
Train a graph-level classifier.
Handles single-label and multi-label datasets transparently:
when batch.y is shaped [B, C] with float dtype (e.g. LRGB Peptides-func, OGB molhiv variants), the loss switches to
binary cross-entropy with logits and the reported metric is the
average correctly-classified label fraction.
With a test_loader the history also carries test_acc, a held-out
series distinct from the val_acc one, which is what makes
epoch_selection="best_val" and hyperparameter search legitimate.
Source code in src/graphnetz/training.py
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 | |
train_graph_regression
¶
train_graph_regression(
model: Module,
train_loader: DataLoader,
val_loader: DataLoader,
epochs: int = 30,
lr: float = 0.001,
verbose: bool = False,
device: device | str | None = "auto",
test_loader: DataLoader | None = None,
) -> dict[str, list[float]]
Train a graph-level regressor (MSE loss, MAE on val).
With a test_loader the history also carries test_mae on a
held-out split distinct from the validation one.
Source code in src/graphnetz/training.py
train_node_degree_regression
¶
train_node_degree_regression(
model: Module,
data: Data,
epochs: int = 100,
lr: float = 0.01,
verbose: bool = False,
device: device | str | None = "auto",
) -> dict[str, list[float]]
Self-supervised node-level regression: predict log node degree.
Source code in src/graphnetz/training.py
train_dgi
¶
train_dgi(
model: _DGILike,
data: Data,
epochs: int = 100,
lr: float = 0.001,
verbose: bool = False,
device: device | str | None = "auto",
) -> dict[str, list[float]]
Train a Deep Graph Infomax model (unsupervised).
Source code in src/graphnetz/training.py
train_link_prediction
¶
train_link_prediction(
model: _LinkPredLike,
train_data: Data,
val_data: Data,
test_data: Data,
epochs: int = 100,
lr: float = 0.01,
verbose: bool = False,
device: device | str | None = "auto",
) -> dict[str, list[float]]
Train a link predictor with binary cross-entropy on RandomLinkSplit.
The model is expected to expose encode(data) returning per-node
embeddings and decode(z, edge_label_index) returning per-edge scores
(see LinkPredWrapper).
Source code in src/graphnetz/training.py
train_relational_link_prediction
¶
train_relational_link_prediction(
model: _RelationalLinkPredLike,
train_data: Data,
val_data: Data,
test_data: Data,
epochs: int = 100,
lr: float = 0.01,
verbose: bool = False,
device: device | str | None = "auto",
) -> dict[str, list[float]]
Train a relational link predictor (DistMult) on knowledge graph triples.
The model is expected to expose encode(data) returning per-node
embeddings and decode(z, edge_index, edge_type) returning per-edge
scores (see graphnetz.models._adapters.RelationalLinkPredWrapper).