Plain training and evaluation loops for variable-length sequence models.
Ported op-for-op from the train/evaluate_many helpers of the schu25a compressor-map study (src/rnn/train.py), so that runs seeded with spotoptim.utils.seed.seed_everything reproduce the original results bit-identically on the same device. The models are called as model(x, lengths) on padded batches (e.g. spotoptim.nn.many_to_many_rnn.ManyToManyRNN with batches collated by spotoptim.data.manydataset.PadSequenceManyToMany); losses and metrics are computed on the squeezed padded tensors, exactly as in the original. The original’s unused seed parameters were dropped.
Requires the torch optional extra (pip install 'spotoptim[torch]'), which includes torchmetrics.
Evaluate a sequence model with per-batch MAPE and RMSE.
Metrics are the torchmetrics MeanAbsolutePercentageError and MeanSquaredError(squared=False) between model(x, lengths).squeeze() and the squeezed target batch, as in the schu25a original. Inputs, predictions, and targets are additionally returned as nested Python lists for plotting.
Parameters
Name
Type
Description
Default
model
nn.Module
Model called as model(x, lengths).
required
val_loader
DataLoader
Loader yielding (x, lengths, y) batches. Batches are converted to NumPy, so device should be “cpu” unless metrics_only=True.
One optimizer step per batch; the loss is computed between model(x, lengths).squeeze() and the (padded) target batch. The recorded training loss is the batch-mean loss per epoch.
Parameters
Name
Type
Description
Default
model
nn.Module
Model called as model(x, lengths).
required
train_loader
DataLoader
Loader yielding (x, lengths, y) batches, e.g. collated by PadSequenceManyToMany.