🏋️ ODETrainer API¶
The ODETrainer class acts as the high-level orchestrator for the training pipeline. It wraps PyTorch Lightning to provide a standardized interface for training Neural ODEs on flight data.
It functions as the central bridge in the pipeline: it validates the configuration, retrieves the specific model architecture from the registry, initializes the training environment, and manages the lifecycle of model checkpoints and metadata artifacts.
📘 Class Reference¶
ode_trainer
¶
Training utilities for neural ODE-based flight dynamics models.
ODETrainer
¶
Handle data preparation, training loops, and checkpointing for ODE models.
Source code in src/node_fdm/ode_trainer.py
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 471 472 473 474 475 476 477 478 479 480 481 | |
__init__(data_df, model_config, model_dir, num_workers=4, load_parallel=True, train_val_num=(5000, 500))
¶
Initialize trainer with data, model configuration, and I/O paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_df
|
DataFrame
|
DataFrame containing file paths and split labels. |
required |
model_config
|
Dict[str, Any]
|
Dictionary describing architecture, hyperparameters, and loader settings. |
required |
model_dir
|
Any
|
Base directory to store checkpoints and metadata. |
required |
num_workers
|
int
|
Number of workers for DataLoaders. |
4
|
load_parallel
|
bool
|
Whether to load flights in parallel. |
True
|
train_val_num
|
Tuple[int, int]
|
Tuple specifying how many train/val files to load. |
(5000, 500)
|
Source code in src/node_fdm/ode_trainer.py
cat_to_dict_vects(vect_list, col_list, alpha_dict, normalize=True)
¶
Concatenate vectors and build a dict keyed by column definitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vect_list
|
Sequence[Tensor]
|
Sequence of tensors to concatenate along the feature axis. |
required |
col_list
|
Sequence[Any]
|
Column identifiers matching the concatenated tensors. |
required |
alpha_dict
|
Dict[Any, float]
|
Optional scaling factors applied per column. |
required |
normalize
|
bool
|
Whether to normalize columns that request it. |
True
|
Returns:
| Type | Description |
|---|---|
Dict[Any, Tensor]
|
Dictionary mapping columns to (optionally) scaled and normalized tensors. |
Source code in src/node_fdm/ode_trainer.py
compute_loss_ode_step(batch, alpha_dict, method='rk4')
¶
Compute loss for a single ODE rollout batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Sequence[Tensor]
|
Tuple of tensors |
required |
alpha_dict
|
Dict[Any, float]
|
Scaling factors per monitored column. |
required |
method
|
str
|
ODE solver method. |
'rk4'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss tensor for the batch. |
Source code in src/node_fdm/ode_trainer.py
get_or_create_model(load=False, load_loss=False)
¶
Instantiate a new model or load existing checkpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load
|
bool
|
Whether to attempt loading existing checkpoints. |
False
|
load_loss
|
bool
|
Whether to restore tracked best validation loss when loading. |
False
|
Returns:
| Type | Description |
|---|---|
FlightDynamicsModel
|
Initialized or restored |
Source code in src/node_fdm/ode_trainer.py
load_best_checkpoint(load_loss=False)
¶
Create and populate a model from saved checkpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_loss
|
bool
|
Whether to restore tracked best validation loss. |
False
|
Returns:
| Type | Description |
|---|---|
FlightDynamicsModel
|
Model with layer weights loaded when available. |
Source code in src/node_fdm/ode_trainer.py
load_layer_checkpoint(layer_name)
¶
Load checkpoint dictionary for a specific layer if available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_name
|
str
|
Name of the layer to load. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Checkpoint dictionary if found, otherwise None. |
Source code in src/node_fdm/ode_trainer.py
norm_vect(vect, col)
¶
Normalize tensor using stored statistics for a column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vect
|
Tensor
|
Tensor to normalize. |
required |
col
|
Any
|
Column identifier used to fetch statistics. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Normalized tensor. |
Source code in src/node_fdm/ode_trainer.py
ode_step(x_seq, u_seq, e_seq, method, alpha_dict)
¶
Integrate one ODE step and return true/predicted trajectories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_seq
|
Tensor
|
State sequences for the batch. |
required |
u_seq
|
Tensor
|
Control sequences for the batch. |
required |
e_seq
|
Tensor
|
Environment sequences for the batch. |
required |
method
|
str
|
ODE solver method passed to |
required |
alpha_dict
|
Dict[Any, float]
|
Scaling factors per monitored column. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor, Sequence[Any]]
|
Tuple of (true trajectories, predicted trajectories, monitored columns). |
Source code in src/node_fdm/ode_trainer.py
save_layer_checkpoint(layer_name, epoch)
¶
Save checkpoint for an individual layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_name
|
str
|
Name of the layer to checkpoint. |
required |
epoch
|
int
|
Current epoch offset for tracking. |
required |
Source code in src/node_fdm/ode_trainer.py
save_meta()
¶
Persist training metadata and statistics to disk.
Creates or updates meta.json within the model directory.
Source code in src/node_fdm/ode_trainer.py
save_model(epoch)
¶
Save checkpoints for all layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
int
|
Epoch index used when saving checkpoints. |
required |
train(epochs=800, batch_size=512, val_batch_size=10000, scheduler=None, method='rk4', alpha_dict=None)
¶
Train the ODE model and persist checkpoints/metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epochs
|
int
|
Number of training epochs. |
800
|
batch_size
|
int
|
Training batch size. |
512
|
val_batch_size
|
int
|
Validation batch size. |
10000
|
scheduler
|
Optional[Any]
|
Optional learning-rate scheduler. |
None
|
method
|
str
|
ODE solver method. |
'rk4'
|
alpha_dict
|
Optional[Dict[Any, float]]
|
Optional scaling factors per monitored column. |
None
|
Source code in src/node_fdm/ode_trainer.py
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 471 472 473 474 475 476 477 478 479 480 481 | |