Learner¶
Learner, TbpttLearner, and Recorder — pure-PyTorch training loop.
Recorder ¶
Learner ¶
Learner(model: Module, dls: DataLoaders, loss_func: Callable, metrics: list[Callable] | None = None, lr: float = 0.003, opt_func: type = torch.optim.Adam, transforms: list | None = None, augmentations: list | None = None, aux_losses: list | None = None, n_skip: int = 0, grad_clip: float | None = None, plot_fn: Callable | None = None, device: device | None = None)
Pure-PyTorch training loop for time-series models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
the model to train |
required |
dls
|
DataLoaders
|
train/valid/test DataLoaders |
required |
loss_func
|
Callable
|
primary loss function |
required |
metrics
|
list[Callable] | None
|
list of metric functions |
None
|
lr
|
float
|
default learning rate |
0.003
|
opt_func
|
type
|
optimizer class |
Adam
|
transforms
|
list | None
|
list of |
None
|
augmentations
|
list | None
|
list of |
None
|
aux_losses
|
list | None
|
list of |
None
|
n_skip
|
int
|
number of initial time steps to skip in loss computation |
0
|
grad_clip
|
float | None
|
maximum gradient norm (None disables clipping) |
None
|
plot_fn
|
Callable | None
|
plotting function for show_batch/show_results |
None
|
device
|
device | None
|
target device (auto-detected if None) |
None
|
Source code in tsfast/training/learner.py
add_aux_loss ¶
add_transform ¶
add_augmentation ¶
no_bar ¶
Suppress tqdm progress bars (useful for Ray Tune).
training_step ¶
training_step(batch: tuple[Tensor, Tensor], optimizer, state=None, n_skip: int | None = None) -> tuple[float | None, object]
Single training step: apply transforms/augmentations, forward, loss, backward, step.
Source code in tsfast/training/learner.py
validate ¶
Run validation and compute loss + metrics on concatenated predictions.
Returns:
| Type | Description |
|---|---|
tuple[float, dict[str, float]]
|
(val_loss, {metric_name: value}) |
Source code in tsfast/training/learner.py
fit ¶
Train for n_epoch epochs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_epoch
|
int
|
number of epochs |
required |
lr
|
float | None
|
learning rate (uses self.lr if None) |
None
|
make_scheduler
|
Callable | None
|
factory |
None
|
Source code in tsfast/training/learner.py
fit_flat_cos ¶
Convenience: flat LR then cosine decay.
Source code in tsfast/training/learner.py
get_preds ¶
Batch-concatenated predictions and targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds_idx
|
int
|
DataLoader index (0=train, 1=valid) |
1
|
dl
|
explicit DataLoader (overrides ds_idx) |
None
|
|
with_inputs
|
bool
|
if True, also return concatenated inputs |
False
|
Source code in tsfast/training/learner.py
get_worst ¶
Inputs, targets, and predictions for the samples with highest loss.
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor, Tensor]
|
(inputs, targets, predictions) sliced to the |
Source code in tsfast/training/learner.py
show_batch ¶
Plot a batch of input/target pairs.
Source code in tsfast/training/learner.py
show_results ¶
Plot predictions vs targets.
Source code in tsfast/training/learner.py
show_worst ¶
Plot samples with highest per-sample loss.
Source code in tsfast/training/learner.py
TbpttLearner ¶
Bases: Learner
Learner with truncated backpropagation through time (TBPTT).
Full sequences are loaded from the DataLoader, then split into
sub-sequences of sub_seq_len. Hidden state is carried across
sub-sequences within a batch but reset between batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sub_seq_len
|
int
|
length of each sub-sequence chunk |
required |
Source code in tsfast/training/learner.py
CudaGraphTbpttLearner ¶
Bases: TbpttLearner
TbpttLearner accelerated with CUDA Graphs.
Captures the forward + backward pass for a single TBPTT chunk into a CUDA graph and replays it, eliminating per-kernel CPU launch overhead. The optimizer step runs outside the graph so that LR schedulers work normally.
When n_skip > 0, a second graph is captured for the first chunk (which
has different loss tensor shapes due to skip-slicing).
Constraints
- Requires CUDA device
win_sz % sub_seq_len == 0(all chunks must have equal shape)- Model must use
return_state=True - Loss function must have static tensor shapes (use
"nanmean"reduction, notignore_nan)