Trainer
Trainer
This is the API for Trainer
class.
Trainer(model, optimizer, config, loss_fn='mse', train_dataloader=None, val_dataloader=None, test_dataloader=None, optimize_step=optimize_step, max_batches=None)
Bases: BaseTrainer
Trainer class to manage and execute training, validation, and testing loops for a model (eg.
QNN).
This class handles the overall training process, including: - Managing epochs and steps - Handling data loading and batching - Computing and updating gradients - Logging and monitoring training metrics
ATTRIBUTE | DESCRIPTION |
---|---|
current_epoch |
The current epoch number.
TYPE:
|
global_step |
The global step across all epochs.
TYPE:
|
Inherited Attributes
use_grad (bool): Indicates if gradients are used for optimization. Default is True.
model (nn.Module): The neural network model. optimizer (optim.Optimizer | NGOptimizer | None): The optimizer for training. config (TrainConfig): The configuration settings for training. train_dataloader (DataLoader | DictDataLoader | None): DataLoader for training data. val_dataloader (DataLoader | DictDataLoader | None): DataLoader for validation data. test_dataloader (DataLoader | DictDataLoader | None): DataLoader for testing data.
optimize_step (Callable): Function for performing an optimization step. loss_fn (Callable): loss function to use.
num_training_batches (int): Number of training batches. num_validation_batches (int): Number of validation batches. num_test_batches (int): Number of test batches.
state (str): Current state in the training process
Default training routine
for epoch in max_iter + 1:
# Training
for batch in train_batches:
train model
# Validation
if val_every % epoch == 0:
for batch in val_batches:
train model
Notes
- In case of InfiniteTensorDataset, number of batches = 1.
- In case of TensorDataset, number of batches are default.
- Training is run for max_iter + 1 epochs. Epoch 0 logs untrained model.
- Please look at the CallbackManager initialize_callbacks method to review the default logging behavior.
Examples: IMP: This uses qadence models (QNN), and should be used carefully.
import torch
from torch.optim import SGD
from perceptrain import (
feature_map,
hamiltonian_factory,
hea,
QNN,
QuantumCircuit,
TrainConfig,
Z,
)
from perceptrain.trainer import Trainer
from perceptrain.optimize_step import optimize_step
from perceptrain import TrainConfig
from perceptrain.data import to_dataloader
# Initialize the model
n_qubits = 2
fm = feature_map(n_qubits)
ansatz = hea(n_qubits=n_qubits, depth=2)
observable = hamiltonian_factory(n_qubits, detuning=Z)
circuit = QuantumCircuit(n_qubits, fm, ansatz)
model = QNN(circuit, observable, backend="pyqtorch", diff_mode="ad")
# Set up the optimizer
optimizer = SGD(model.parameters(), lr=0.001)
# Use TrainConfig for configuring the training process
config = TrainConfig(
max_iter=100,
print_every=10,
write_every=10,
checkpoint_every=10,
val_every=10
)
# Create the Trainer instance with TrainConfig
trainer = Trainer(
model=model,
optimizer=optimizer,
config=config,
loss_fn="mse",
optimize_step=optimize_step
)
batch_size = 25
x = torch.linspace(0, 1, 32).reshape(-1, 1)
y = torch.sin(x)
train_loader = to_dataloader(x, y, batch_size=batch_size, infinite=True)
val_loader = to_dataloader(x, y, batch_size=batch_size, infinite=False)
# Train the model
model, optimizer = trainer.fit(train_loader, val_loader)
This also supports both gradient based and gradient free optimization. The default support is for gradient based optimization.
Notes:
- set_use_grad() (class level):This method is used to set the global
use_grad
flag, controlling whether the trainer uses gradient-based optimization. - Context Managers (instance level):
enable_grad_opt()
anddisable_grad_opt()
are context managers that temporarily switch the optimization mode for specific code blocks. This is useful when you want to mix gradient-based and gradient-free optimization in the same training process.
Examples
Gradient based optimization example Usage:
from torch import optim
optimizer = optim.SGD(model.parameters(), lr=0.01)
Trainer.set_use_grad(True)
trainer = Trainer(
model=model,
optimizer=optimizer,
config=config,
loss_fn="mse"
)
trainer.fit(train_loader, val_loader)
trainer = Trainer(
model=model,
config=config,
loss_fn="mse"
)
with trainer.enable_grad_opt(optimizer):
trainer.fit(train_loader, val_loader)
Gradient free optimization example Usage:
import nevergrad as ng
from perceptrain.parameters import num_parameters
ng_optimizer = ng.optimizers.NGOpt(
budget=config.max_iter, parametrization= num_parameters(model)
)
Trainer.set_use_grad(False)
trainer = Trainer(
model=model,
optimizer=ng_optimizer,
config=config,
loss_fn="mse"
)
trainer.fit(train_loader, val_loader)
import nevergrad as ng
from perceptrain.parameters import num_parameters
ng_optimizer = ng.optimizers.NGOpt(
budget=config.max_iter, parametrization= num_parameters(model)
)
trainer = Trainer(
model=model,
config=config,
loss_fn="mse"
)
with trainer.disable_grad_opt(ng_optimizer):
trainer.fit(train_loader, val_loader)
Initializes the Trainer class.
PARAMETER | DESCRIPTION |
---|---|
model
|
The PyTorch model to train.
TYPE:
|
optimizer
|
The optimizer for training.
TYPE:
|
config
|
Training configuration object.
TYPE:
|
loss_fn
|
Loss function used for training. If not specified, default mse loss will be used.
TYPE:
|
train_dataloader
|
DataLoader for training data.
TYPE:
|
val_dataloader
|
DataLoader for validation data.
TYPE:
|
test_dataloader
|
DataLoader for test data.
TYPE:
|
optimize_step
|
Function to execute an optimization step.
TYPE:
|
max_batches
|
Maximum number of batches to process per epoch. This is only valid in case of finite TensorDataset dataloaders. if max_batches is not None, the maximum number of batches used will be min(max_batches, len(dataloader.dataset)) In case of InfiniteTensorDataset only 1 batch per epoch is used.
TYPE:
|
Source code in perceptrain/trainer.py
build_optimize_result(result)
Builds and stores the optimization result by calculating the average loss and metrics.
Result (or loss_metrics) can have multiple formats:
- None
Indicates no loss or metrics data is provided.
- tuple[torch.Tensor, dict[str, Any]]
A single tuple containing the loss tensor
and metrics dictionary - at the end of batch.
- list[tuple[torch.Tensor, dict[str, Any]]]
A list of tuples for
multiple batches.
- list[list[tuple[torch.Tensor, dict[str, Any]]]]
A list of lists of tuples,
where each inner list represents metrics across multiple batches within an epoch.
PARAMETER | DESCRIPTION |
---|---|
result
|
(None | tuple[torch.Tensor, dict[Any, Any]] | list[tuple[torch.Tensor, dict[Any, Any]]] | list[list[tuple[torch.Tensor, dict[Any, Any]]]]) The loss and metrics data, which can have multiple formats
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return anything. It sets
TYPE:
|
None
|
the computed average loss and metrics. |
Source code in perceptrain/trainer.py
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
|
fit(train_dataloader=None, val_dataloader=None)
Fits the model using the specified training configuration.
The dataloaders can be provided to train on new datasets, or the default dataloaders provided in the trainer will be used.
PARAMETER | DESCRIPTION |
---|---|
train_dataloader
|
DataLoader for training data.
TYPE:
|
val_dataloader
|
DataLoader for validation data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Module, Optimizer]
|
tuple[nn.Module, optim.Optimizer]: The trained model and optimizer. |
Source code in perceptrain/trainer.py
get_ic_grad_bounds(eta, epsilons, variation_multiple=20, dataloader=None)
Calculate the bounds on the gradient norm of the loss using Information Content.
PARAMETER | DESCRIPTION |
---|---|
eta
|
The sensitivity IC.
TYPE:
|
epsilons
|
The epsilons to use for thresholds to for discretization of the finite derivatives.
TYPE:
|
variation_multiple
|
The number of sets of variational parameters to generate per each variational parameter. The number of variational parameters required for the statisctiacal analysis scales linearly with the amount of them present in the model. This is that linear factor.
TYPE:
|
dataloader
|
The dataloader for training data. A new dataloader can be provided, or the dataloader provided in the trinaer will be used. In case no dataloaders are provided at either places, it assumes that the model does not require any input data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[float, float, float]
|
tuple[float, float, float]: The max IC lower bound, max IC upper bound, and sensitivity IC upper bound. |
Examples:
import torch
from torch.optim.adam import Adam
from perceptrain.constructors import ObservableConfig
from perceptrain.config import AnsatzConfig, FeatureMapConfig, TrainConfig
from perceptrain.data import to_dataloader
from perceptrain import QNN
from perceptrain.optimize_step import optimize_step
from perceptrain.trainer import Trainer
from perceptrain.operations.primitive import Z
fm_config = FeatureMapConfig(num_features=1)
ansatz_config = AnsatzConfig(depth=4)
obs_config = ObservableConfig(detuning=Z)
qnn = QNN.from_configs(
register=4,
obs_config=obs_config,
fm_config=fm_config,
ansatz_config=ansatz_config,
)
optimizer = Adam(qnn.parameters(), lr=0.001)
batch_size = 25
x = torch.linspace(0, 1, 32).reshape(-1, 1)
y = torch.sin(x)
train_loader = to_dataloader(x, y, batch_size=batch_size, infinite=True)
train_config = TrainConfig(max_iter=100)
trainer = Trainer(
model=qnn,
optimizer=optimizer,
config=train_config,
loss_fn="mse",
train_dataloader=train_loader,
optimize_step=optimize_step,
)
# Perform exploratory landscape analysis with Information Content
ic_sensitivity_threshold = 1e-4
epsilons = torch.logspace(-2, 2, 10)
max_ic_lower_bound, max_ic_upper_bound, sensitivity_ic_upper_bound = (
trainer.get_ic_grad_bounds(
eta=ic_sensitivity_threshold,
epsilons=epsilons,
)
)
# Resume training as usual...
trainer.fit(train_loader)
Source code in perceptrain/trainer.py
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 |
|
run_test_batch(batch)
Runs a single test batch.
PARAMETER | DESCRIPTION |
---|---|
batch
|
Batch of data from the DataLoader.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Tensor, dict[str, Any]]
|
tuple[torch.Tensor, dict[str, Any]]: Loss and metrics for the batch. |
Source code in perceptrain/trainer.py
run_train_batch(batch)
Runs a single training batch, performing optimization.
We use the step function to optimize the model based on use_grad. use_grad = True entails gradient based optimization, for which we use optimize_step function. use_grad = False entails gradient free optimization, for which we use update_ng_parameters function.
PARAMETER | DESCRIPTION |
---|---|
batch
|
Batch of data from the DataLoader.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Tensor, dict[str, Any]]
|
tuple[torch.Tensor, dict[str, Any]]: Loss and metrics for the batch. tuple of (loss, metrics) |
Source code in perceptrain/trainer.py
run_training(dataloader)
Runs the training for a single epoch, iterating over multiple batches.
PARAMETER | DESCRIPTION |
---|---|
dataloader
|
DataLoader for training data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[tuple[Tensor, dict[str, Any]]]
|
list[tuple[torch.Tensor, dict[str, Any]]]: Loss and metrics for each batch. list -> tuples Training Batches -> (loss, metrics) |
Source code in perceptrain/trainer.py
run_val_batch(batch)
Runs a single validation batch.
PARAMETER | DESCRIPTION |
---|---|
batch
|
Batch of data from the DataLoader.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Tensor, dict[str, Any]]
|
tuple[torch.Tensor, dict[str, Any]]: Loss and metrics for the batch. |
Source code in perceptrain/trainer.py
run_validation(dataloader)
Runs the validation loop for a single epoch, iterating over multiple batches.
PARAMETER | DESCRIPTION |
---|---|
dataloader
|
DataLoader for validation data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[tuple[Tensor, dict[str, Any]]]
|
list[tuple[torch.Tensor, dict[str, Any]]]: Loss and metrics for each batch. list -> tuples Validation Batches -> (loss, metrics) |
Source code in perceptrain/trainer.py
stop_training()
Helper function to indicate if the training should be stopped.
We all_reduce the indicator across all processes to ensure all processes are stopped.
Notes
self._stop_training indicator indicates if the training should be stopped. 0 is continue. 1 is stop.
Source code in perceptrain/trainer.py
test(test_dataloader=None)
Runs the testing loop if a test DataLoader is provided.
if the test_dataloader is not provided, default test_dataloader defined in the Trainer class is used.
PARAMETER | DESCRIPTION |
---|---|
test_dataloader
|
DataLoader for test data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[tuple[Tensor, dict[str, Any]]]
|
list[tuple[torch.Tensor, dict[str, Any]]]: Loss and metrics for each batch. list -> tuples Test Batches -> (loss, metrics) |
Source code in perceptrain/trainer.py
BaseTrainer(model, optimizer, config, loss_fn='mse', optimize_step=optimize_step, train_dataloader=None, val_dataloader=None, test_dataloader=None, max_batches=None)
Base class for training machine learning models using a given optimizer.
The base class implements contextmanager for gradient based/free optimization, properties, property setters, input validations, callback decorator generator, and empty hooks for different training steps.
This class provides
- Context managers for enabling/disabling gradient-based optimization
- Properties for managing models, optimizers, and dataloaders
- Input validations and a callback decorator generator
- Config and callback managers using the provided
TrainConfig
ATTRIBUTE | DESCRIPTION |
---|---|
use_grad |
Indicates if gradients are used for optimization. Default is True.
TYPE:
|
model |
The neural network model.
TYPE:
|
optimizer |
The optimizer for training.
TYPE:
|
config |
The configuration settings for training.
TYPE:
|
train_dataloader |
DataLoader for training data.
TYPE:
|
val_dataloader |
DataLoader for validation data.
TYPE:
|
test_dataloader |
DataLoader for testing data.
TYPE:
|
optimize_step |
Function for performing an optimization step.
TYPE:
|
loss_fn |
loss function to use. Default loss function used is 'mse'
TYPE:
|
num_training_batches |
Number of training batches. In case of InfiniteTensorDataset only 1 batch per epoch is used.
TYPE:
|
num_validation_batches |
Number of validation batches. In case of InfiniteTensorDataset only 1 batch per epoch is used.
TYPE:
|
num_test_batches |
Number of test batches. In case of InfiniteTensorDataset only 1 batch per epoch is used.
TYPE:
|
state |
Current state in the training process
TYPE:
|
Initializes the BaseTrainer.
PARAMETER | DESCRIPTION |
---|---|
model
|
The model to train.
TYPE:
|
optimizer
|
The optimizer for training.
TYPE:
|
config
|
The TrainConfig settings for training.
TYPE:
|
loss_fn
|
The loss function to use. str input to be specified to use a default loss function. currently supported loss functions: 'mse', 'cross_entropy'. If not specified, default mse loss will be used.
TYPE:
|
train_dataloader
|
DataLoader for training data. If the model does not need data to evaluate loss, no dataset should be provided.
TYPE:
|
val_dataloader
|
DataLoader for validation data.
TYPE:
|
test_dataloader
|
DataLoader for testing data.
TYPE:
|
max_batches
|
Maximum number of batches to process per epoch. This is only valid in case of finite TensorDataset dataloaders. if max_batches is not None, the maximum number of batches used will be min(max_batches, len(dataloader.dataset)) In case of InfiniteTensorDataset only 1 batch per epoch is used.
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
config
property
writable
Returns the training configuration.
RETURNS | DESCRIPTION |
---|---|
TrainConfig
|
The configuration object.
TYPE:
|
model
property
writable
Returns the model if set, otherwise raises an error.
RETURNS | DESCRIPTION |
---|---|
Module
|
nn.Module: The model. |
optimizer
property
writable
Returns the optimizer if set, otherwise raises an error.
RETURNS | DESCRIPTION |
---|---|
Optimizer | Optimizer | None
|
optim.Optimizer | NGOptimizer | None: The optimizer. |
test_dataloader
property
writable
Returns the test DataLoader, validating its type.
RETURNS | DESCRIPTION |
---|---|
DataLoader
|
The DataLoader for testing data.
TYPE:
|
train_dataloader
property
writable
Returns the training DataLoader, validating its type.
RETURNS | DESCRIPTION |
---|---|
DataLoader
|
The DataLoader for training data.
TYPE:
|
use_grad
property
writable
Returns the optimization framework for the trainer.
use_grad = True : Gradient based optimization use_grad = False : Gradient free optimization
RETURNS | DESCRIPTION |
---|---|
bool
|
Bool value for using gradient.
TYPE:
|
val_dataloader
property
writable
Returns the validation DataLoader, validating its type.
RETURNS | DESCRIPTION |
---|---|
DataLoader
|
The DataLoader for validation data.
TYPE:
|
callback(phase)
staticmethod
Decorator for executing callbacks before and after a phase.
Phase are different hooks during the training. list of valid phases is defined in Callbacks. We also update the current state of the training process in the callback decorator.
PARAMETER | DESCRIPTION |
---|---|
phase
|
The phase for which the callback is executed (e.g., "train", "train_epoch", "train_batch").
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Callable
|
The decorated function.
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
disable_grad_opt(optimizer=None)
Context manager to temporarily disable gradient-based optimization.
PARAMETER | DESCRIPTION |
---|---|
optimizer
|
The Nevergrad optimizer to use. If no optimizer is provided, default optimizer for trainer object will be used.
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
enable_grad_opt(optimizer=None)
Context manager to temporarily enable gradient-based optimization.
PARAMETER | DESCRIPTION |
---|---|
optimizer
|
The PyTorch optimizer to use. If no optimizer is provided, default optimizer for trainer object will be used.
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_test_batch_end(test_batch_loss_metrics)
Called at the end of each testing batch.
PARAMETER | DESCRIPTION |
---|---|
test_batch_loss_metrics
|
Metrics for the testing batch loss. tuple of (loss, metrics)
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_test_batch_start(batch)
Called at the start of each testing batch.
PARAMETER | DESCRIPTION |
---|---|
batch
|
A batch of data from the DataLoader. Typically a tuple containing input tensors and corresponding target tensors.
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_train_batch_end(train_batch_loss_metrics)
Called at the end of each training batch.
PARAMETER | DESCRIPTION |
---|---|
train_batch_loss_metrics
|
Metrics for the training batch loss. tuple of (loss, metrics)
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_train_batch_start(batch)
Called at the start of each training batch.
PARAMETER | DESCRIPTION |
---|---|
batch
|
A batch of data from the DataLoader. Typically a tuple containing input tensors and corresponding target tensors.
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_train_end(train_losses, val_losses=None)
Called at the end of training.
PARAMETER | DESCRIPTION |
---|---|
train_losses
|
Metrics for the training losses. list -> list -> tuples Epochs -> Training Batches -> (loss, metrics)
TYPE:
|
val_losses
|
Metrics for the validation losses. list -> list -> tuples Epochs -> Validation Batches -> (loss, metrics)
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_train_epoch_end(train_epoch_loss_metrics)
Called at the end of each training epoch.
PARAMETER | DESCRIPTION |
---|---|
train_epoch_loss_metrics
|
Metrics for the training epoch losses. list -> tuples Training Batches -> (loss, metrics)
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_train_epoch_start()
on_train_start()
on_val_batch_end(val_batch_loss_metrics)
Called at the end of each validation batch.
PARAMETER | DESCRIPTION |
---|---|
val_batch_loss_metrics
|
Metrics for the validation batch loss. tuple of (loss, metrics)
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_val_batch_start(batch)
Called at the start of each validation batch.
PARAMETER | DESCRIPTION |
---|---|
batch
|
A batch of data from the DataLoader. Typically a tuple containing input tensors and corresponding target tensors.
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_val_epoch_end(val_epoch_loss_metrics)
Called at the end of each validation epoch.
PARAMETER | DESCRIPTION |
---|---|
val_epoch_loss_metrics
|
Metrics for the validation epoch loss. list -> tuples Validation Batches -> (loss, metrics)
TYPE:
|
Source code in perceptrain/train_utils/base_trainer.py
on_val_epoch_start()
set_use_grad(value)
classmethod
Sets the global use_grad flag.
PARAMETER | DESCRIPTION |
---|---|
value
|
Whether to use gradient-based optimization.
TYPE:
|