function.torch_objective.TorchObjective

function.torch_objective.TorchObjective(experiment, seed=None, use_scaler=False)

A callable objective function for SpotOptim that trains and evaluates a PyTorch model.

Attributes

Name Description
bounds Returns the bounds of the hyperparameters.
objective_names Returns the names of the objectives.
var_name Returns the names of the hyperparameters.
var_trans Returns the transformations of the hyperparameters.
var_type Returns the types of the hyperparameters.

Methods

Name Description
train_model Trains the model and returns a dictionary of metrics.

train_model

function.torch_objective.TorchObjective.train_model(
    model,
    train_loader,
    val_loader,
    params,
)

Trains the model and returns a dictionary of metrics.

Executes the training loop for the specified number of epochs. Handles optimizer creation, loss calculation, backward pass, and validation evaluation. Schedule-free optimizers (those exposing train()/eval(), e.g. AdamWScheduleFree) are switched to training mode before each epoch and to evaluation mode before validation, so validation metrics are computed on the averaged parameters; the optimizer is left in evaluation mode on return.

Parameters

Name Type Description Default
model nn.Module The PyTorch model to train. required
train_loader DataLoader DataLoader for training data. required
val_loader Optional[DataLoader] DataLoader for validation data (can be None). required
params Dict[str, Any] Hyperparameters dictionary containing ‘epochs’, ‘lr’, ‘optimizer’ name, etc. required

Returns

Name Type Description
Dict[str, float] Dict[str, float]: Dictionary containing computed metrics, e.g., {‘val_loss’: …, ‘train_loss’: …, ‘mse’: …, ‘epochs’: …}.

Examples

>>> import torch
>>> import torch.nn as nn
>>> from torch.utils.data import DataLoader, TensorDataset
>>> from spotoptim.function.torch_objective import TorchObjective
>>> from unittest.mock import MagicMock
>>>
>>> # 1. Create dataset and loader
>>> X = torch.randn(10, 2)
>>> y = torch.randn(10, 1)
>>> loader = DataLoader(TensorDataset(X, y), batch_size=2)
>>>
>>> # 2. Create model
>>> model = nn.Linear(2, 1)
>>>
>>> # 3. Mock Objective context
>>> exp = MagicMock()
>>> exp.loss_function = nn.MSELoss()
>>> exp.epochs = 1
>>> exp.torch_device = "cpu"
>>> objective = TorchObjective(exp)
>>>
>>> # 4. Train
>>> params = {'lr': 1e-2, 'optimizer': 'Adam'}
>>> metrics = objective.train_model(model, loader, None, params)
>>> print(f"Train Loss: {metrics['train_loss']:.4f}")
>>> print(f"Epochs: {metrics['epochs']}")