Runtime Supervised Training #
Supervised tasks, runners, steppers, optimizer configs, trainer aliases, and the low-level session exports that back executable examples.
Built-in loss choices for SeqTask.
- mse (reduction : Loss.Reduction := Runtime.Autograd.TorchLean.Loss.Reduction.mean) : SeqLoss
- crossEntropyOneHot (reduction : Loss.Reduction := Runtime.Autograd.TorchLean.Loss.Reduction.mean) : SeqLoss
Instances For
A supervised task is just a model plus a choice of loss.
Instances For
Build a ScalarModuleDef for a task, choosing an explicit model mode (train/eval).
This is the underlying "instantiate me as a runnable module" step for training.
Instances For
Default module definition for a task (training mode).
Instances For
Constructor: regression task (MSE loss).
Instances For
Constructor: one-hot classification task (cross-entropy loss).
Instances For
Parameter shapes for a task (delegates to Seq.paramShapes).
Instances For
Optimizer hyperparameter configuration for the supervised training helpers.
This configuration covers the optimizer choices exposed by the public training helpers. It mirrors
a few common PyTorch optimizers by name/defaults, but it does not try to cover the full option surface of
torch.optim.*.
- sgd
(lr : Float)
(momentum : Float := 0.0)
: OptimizerConfig
SGD optimizer config.
PyTorch analogy:
torch.optim.SGD(..., lr=..., momentum=...)whenmomentum > 0, and plain SGD whenmomentum = 0. - adam
(lr : Float)
(beta1 : Float := 0.9)
(beta2 : Float := 0.999)
(epsilon : Float := 1e-8)
: OptimizerConfig
Adam optimizer config.
- adamw
(lr : Float)
(weightDecay : Float := 1e-2)
(beta1 : Float := 0.9)
(beta2 : Float := 0.999)
(epsilon : Float := 1e-8)
: OptimizerConfig
AdamW optimizer config (decoupled weight decay).
Instances For
Instances For
Step-based training configuration for trainSamples / trainDataset.
Fields:
steps: number of parameter updates,batchSize: number of samples consumed by one public step for in-memory datasets,optimizer: optimizer hyperparameters,scheduler: optional learning-rate schedule (applied per step),logEvery: progress printing frequency (0disables logging).
- steps : ℕ
Number of training steps.
- batchSize : ℕ
Number of samples consumed by one public training step.
The in-memory supervised loop applies one optimizer update per sample in the group, while reporting/logging at the outer step cadence. Loader-backed training uses the loader batches directly.
- optimizer : OptimizerConfig
Optimizer configuration.
- scheduler : Option Schedulers.Config
Scheduler configuration.
- logEvery : ℕ
Log once every this many steps.
Instances For
Instances For
Epoch-based training configuration for trainLoader (data-loader training).
Fields:
epochs: number of epochs (each epoch iterates once over the loader),optimizer: optimizer hyperparameters,scheduler: optional learning-rate schedule (applied per step/epoch depending on helper),logEvery: progress printing frequency (0disables logging).
- epochs : ℕ
Number of epochs to train for.
- optimizer : OptimizerConfig
Optimizer configuration.
- scheduler : Option Schedulers.Config
Scheduler configuration.
- logEvery : ℕ
Log once every this many steps.
Instances For
Instances For
Extract the base learning rate encoded in an optimizer configuration.
Instances For
Resolve the learning rate to use at a given training step.
If a scheduler is present, it takes precedence over the optimizer's baked-in base learning rate.
Otherwise this simply returns optimizerLR cfg.
Instances For
Map a state update over every optimizer-state entry in a shape-indexed parameter list.
Instances For
Set the learning rate field of every Adam optimizer state entry to lr.
Instances For
Set the learning rate field of every momentum-SGD optimizer state entry to lr.
Instances For
Set the learning rate field of every AdamW optimizer state entry to lr.
Instances For
A fully instantiated supervised task runner.
This bundles:
- the imperative
ScalarModule(parameters/buffers stored in refs), - compiled predictors and loss functions for both
.trainand.evalmodes (so switching mode is low-overhead), - and the current mode stored in an
IO.Ref.
The mode influences both operator behavior (e.g. dropout/batchnorm) and whether buffers are updated during training.
- module : Runtime.Autograd.TorchLean.ScalarModule α (paramShapes task) [σ, τ]
Instantiated scalar module storing parameters/buffers in mutable refs.
- predictorTrain : CompiledOut α (paramShapes task ++ [σ]) τ
Compiled forward predictor specialized to training-mode behavior.
- predictorEval : CompiledOut α (paramShapes task ++ [σ]) τ
Compiled forward predictor specialized to eval-mode behavior.
- lossTrain : CompiledScalar α (paramShapes task ++ [σ, τ])
Compiled loss function for training-mode behavior.
- lossEval : CompiledScalar α (paramShapes task ++ [σ, τ])
Compiled loss function for eval-mode behavior.
Mutable mode flag (
.train/.eval) used by stateful layers (e.g. dropout/batchnorm).
Instances For
Instantiate a Runner by explicitly providing a Float → α cast and backend.
Use this when you want to run the same task over different numeric backends (e.g. Float vs
IEEE32Exec) or when you want custom literal injection.
Instances For
Instantiate a Runner by explicitly providing a Float → α cast and a backend selector.
Instantiate a module with explicit runtime options.
Instances For
Instantiate a Runner using the standard runtime literal injection API.Runtime.ofFloat.
This is the common entrypoint for executable examples.
Instances For
Instantiate a Runner using the standard runtime literal injection API.Runtime.ofFloat and a
backend selector.
Instantiate a module after parsing runtime options.
Instances For
Run a TorchLean task with CLI-style dtype/backend selection, then call k with a fully constructed
runner.
This is used by lake exe entrypoints: run takes care of parsing dtype flags and instantiating
the underlying module/compiled programs.
Instances For
Read the current parameter list from a runner.
Instances For
Read the runner's current mode (.train or .eval).
Instances For
Set the runner mode (.train or .eval).
Instances For
Convenience: setMode runner .train.
Instances For
Convenience: setMode runner .eval.
Instances For
Predicate: are we in training mode?
Instances For
Pick the predictor compiled for the runner's current mode (.train or .eval).
Instances For
Pick the loss program compiled for the runner's current mode (.train or .eval).
Instances For
Refresh mode-dependent runner buffers using one supervised sample.
This mutates the module parameters only in .train mode, mirroring PyTorch-style buffer updates
for layers such as normalization. In .eval mode it is a no-op.
Instances For
Run one forward/backward pass on a single supervised sample and return gradients for all parameters.
This is the TorchLean analogue of the loss.backward() payload in PyTorch, except TorchLean returns
the gradients explicitly instead of storing them in .grad fields.
Instances For
Predict on one input tensor using the runner's active mode (.train or .eval).
Instances For
Predict on a list of inputs by repeatedly calling predict.
Instances For
For classification heads: run predict, then take argmax over the logits (if defined).
Instances For
Compute (correct, total) for a one-hot classification dataset.
Instances For
Instances For
Mean scalar loss over a list of supervised samples (uses the runner's active mode).
Instances For
Mean scalar loss over a dataset (materialized via dataset.toList).
Instances For
Scalar loss for one sample through the instantiated runtime module.
Instances For
Treat 0 as the conservative single-sample step size.
Instances For
Train on a small in-memory list of supervised samples for a fixed number of steps.
This is the simplest training-loop helper: it is intended for examples and small synthetic datasets.
For loader-based training, see trainLoader.
Instances For
Train over a dataset by materializing it as a list.
Instances For
Train over a DataLoader for cfg.epochs epochs, returning the final report and updated loader.
This corresponds to the common PyTorch pattern:
for epoch in ...: for batch in loader: step(batch).
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Stateful training loop object: a Runner plus an optimizer state and a step counter.
This is the TorchLean analogue of holding a PyTorch optimizer object plus the model, ready to
step() on batches.
- runner : Runner α task
Underlying task runner (module + compiled predictors/losses).
- stepSample : TensorPack α [σ, τ] → IO α
Run a single optimization step on one supervised sample, returning the loss value.
Run an epoch over an explicit list of samples, returning the per-step loss values.
Read the total number of
stepSamplecalls performed so far.
Instances For
Construct a Stepper for a runner, optimizer config, and optional scheduler.
This is the recommended way to build custom training loops without reimplementing the optimizer
logic: call stepper, then choose stepSample for single batches or epochSamples for explicit
sample lists.
Instances For
Run one optimization step on a single supervised sample.
Instances For
Run one epoch over a list of supervised samples, returning the per-step losses.
Instances For
NN.API.TorchLean.Trainer is the stable public namespace for supervised training machinery.
Usage note:
If you are writing ordinary model code, prefer the NN umbrella:
import NN; open TorchLean; Trainer.new .... This namespace is the lower runtime
layer underneath that facade, and it exposes lower-level controls for custom training loops.
The intended workflow is:
- pick a
Task(regression / classification), - call
instantiateto get aRunner(parameters + buffers + backend state), - call
trainSamples/trainDataset/trainLoader, or build aStepperfor custom loops.
This API is backend-agnostic: the same code can run in .eager mode or via a compiled backend,
depending on the backend argument passed to instantiate.
A supervised task is just a model plus a choice of loss.
Instances For
A fully instantiated supervised task runner.
This bundles:
- the imperative
ScalarModule(parameters/buffers stored in refs), - compiled predictors and loss functions for both
.trainand.evalmodes (so switching mode is low-overhead), - and the current mode stored in an
IO.Ref.
The mode influences both operator behavior (e.g. dropout/batchnorm) and whether buffers are updated during training.
Instances For
Optimizer hyperparameter configuration for the supervised training helpers.
This configuration covers the optimizer choices exposed by the public training helpers. It mirrors
a few common PyTorch optimizers by name/defaults, but it does not try to cover the full option surface of
torch.optim.*.
Instances For
Step-based training configuration for trainSamples / trainDataset.
Fields:
steps: number of parameter updates,batchSize: number of samples consumed by one public step for in-memory datasets,optimizer: optimizer hyperparameters,scheduler: optional learning-rate schedule (applied per step),logEvery: progress printing frequency (0disables logging).
Instances For
Epoch-based training configuration for trainLoader (data-loader training).
Fields:
epochs: number of epochs (each epoch iterates once over the loader),optimizer: optimizer hyperparameters,scheduler: optional learning-rate schedule (applied per step/epoch depending on helper),logEvery: progress printing frequency (0disables logging).
Instances For
Stateful training loop object: a Runner plus an optimizer state and a step counter.
This is the TorchLean analogue of holding a PyTorch optimizer object plus the model, ready to
step() on batches.
Instances For
Lower-runtime regression task with mean-squared error loss by default.
Instances For
Lower-runtime classifier task with cross-entropy loss by default.
Instances For
SGD optimizer config.
PyTorch analogue: torch.optim.SGD
(https://pytorch.org/docs/stable/generated/torch.optim.SGD.html).
Instances For
Momentum SGD optimizer config. Pass lr and optionally override momentum.
Instances For
Adam optimizer config. Pass lr; override beta1, beta2, or epsilon when the run needs it.
PyTorch analogue: torch.optim.Adam
(https://pytorch.org/docs/stable/generated/torch.optim.Adam.html).
Instances For
AdamW optimizer config. Pass lr; override weight decay or moment parameters when the run needs it.
PyTorch analogue: torch.optim.AdamW
(https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html).
Instances For
Optimizer algorithm accepted by simple CLI commands that expose a --optim flag.
- sgd : OptimizerKind
Stochastic gradient descent.
- adam : OptimizerKind
Adam with TorchLean's usual public defaults.
- adamw : OptimizerKind
AdamW with decoupled weight decay.
Instances For
Instances For
Parse an optimizer name accepted by a command-line --optim flag.
Instances For
Human-readable optimizer name used in logs.
Instances For
Build a public optimizer config for this optimizer kind and learning rate.
Instances For
Fixed-step training config over an in-memory sample list or dataset.
Instances For
Epoch-based training config over a data loader.
Instances For
Attach a scheduler to a step-based training config.
Instances For
Attach a scheduler to an epoch-based loader training config.
Instances For
Step-based constant learning-rate schedule.
Instances For
Step-based step-decay schedule.
Instances For
Step-based exponential-decay schedule.
Instances For
Epoch-based constant learning-rate schedule.
Instances For
Epoch-based step-decay schedule.
Instances For
Epoch-based exponential-decay schedule.
Instances For
Instantiate a runner under explicit Torch options (backend, useGpu, fastKernels, ...).
This is the recommended entrypoint when you want CUDA eager execution from the training helpers
without dropping down to TorchLean.Module directly.
Instances For
Instantiate a runner (parameters + buffers + backend state) for the given task.
This allocates and initializes model parameters (via Seq.initParams) and sets up the chosen
execution backend (.eager vs .compiled).
Instances For
CLI-oriented runner entry point.
This parses dtype/backend flags (via NN.API.DType / Module.ExecConfig) and then calls the
continuation k under the selected scalar backend.
Instances For
Get the current model parameters from a runner.
Instances For
Read the current mode (train vs eval).
Instances For
Set the mode (train vs eval).
Instances For
Put the runner in training mode so layers such as dropout/batchnorm use training behavior.
Instances For
Put the runner in evaluation mode so stateful layers use inference behavior.
Instances For
Check whether the runner is in training mode.
Instances For
Run forward+backward on one supervised sample and return gradients for all parameters.
Instances For
Predict on a single input tensor.
This runs the forward pass under the runner's current mode.
Instances For
Predict on a list of inputs (runs the forward pass repeatedly).
Instances For
Predict the argmax class for a classification task, if argmax is well-defined for α.
It runs predict and then applies Metrics.argmax? to the output vector.
Instances For
Count correct predictions in a one-hot labeled sample list (returns (correct, total)).
Instances For
Mean loss over an explicit list of samples.
Instances For
Mean loss over an entire Dataset.
Instances For
Train on an explicit list of samples for a fixed number of steps.
Returns a small report with mean loss before/after.
Instances For
Train on a Dataset for a fixed number of steps.
Instances For
Train using a DataLoader for a fixed number of epochs.
Instances For
Construct a stateful stepper for custom loops.
This is useful if you want to control:
- evaluation cadence,
- logging,
- validation, early stopping, etc.
The returned Stepper still uses TorchLean's optimizer/scheduler implementations.
Instances For
Run a single training step on one sample using a Stepper.
Instances For
Run an epoch over a list of samples using a Stepper (returns the per-step losses).
Instances For
Convenience: instantiate + train on a list of samples.
Returns both the Runner (so you can keep using the trained parameters) and the train report.
Instances For
Convenience: instantiate a task and train on a Dataset.
Returns both the Runner and the train report.
Instances For
Convenience: instantiate a task and train using a DataLoader.
Returns the Runner, the train report, and the updated loader state (shuffled epoch cursor).
Instances For
Execution config parsed from CLI flags (dtype/backend/fast-kernels).
Instances For
Parse and strip execution flags, returning (config, remainingArgs).
Instances For
Log the chosen execution config to stdout for reproducible runs.
Instances For
Re-export of the low-level imperative scalar trainer interface.
This exposes forwardT/backwardT/stepT from Runtime.Autograd.TorchLean.ScalarTrainer.
Use the higher-level TorchLean.Trainer facade unless a file needs these lower-level training hooks.
Imperative session API: a tape-backed interface that can run in eager or compiled mode.
This is approximately analogous to using PyTorch "eager tensors", except TorchLean makes the tape/session
explicit. The Session surface is useful for:
- interactive experiments in
IO, - debugging (inspect intermediate values),
- building higher-level runners.