Manual Training #
Dependent runners, callback composition, custom batch streams, and reporting helpers for workflows
that need more control than the ordinary Trainer.new interface.
Manual Training #
Direct control over runners, callbacks, data streams, and optimization steps. The ordinary
Trainer interface is built from these definitions; custom training code can use them without
crossing into a second API namespace.
Count correct predictions in a one-hot labeled batched dataset.
Minibatch analogue of accuracyOneHot. The task already has a leading batch axis, so the
implementation scores each row independently and accumulates totals.
Returns (correct, total) where total = batch * numBatches.
Instances For
Callback event fired at the end of an epoch (how many steps ran).
Instances For
Hooks for instrumenting callback-based training loops.
Callbacks are ordinary IO hooks. They can print progress, update an in-memory curve, sample CUDA
allocator state, or forward events to a project-specific metrics backend.
Called once before training starts.
Called after each training step.
- onEpochEnd : EpochEvent → IO Unit
Called after each epoch.
- onTrainEnd : TrainReport α → IO Unit
Called once after training finishes.
Instances For
No-op callbacks.
Instances For
Combine two callback collections by running them in sequence.
Instances For
∅ for callbacks: a no-op callback collection.
Build a training callback that samples the CUDA allocator at a fixed step cadence.
The callback owns a small IO.Ref for the previous sample, so examples can compose it with ordinary
loss-logging callbacks without threading allocator state through their training loops.
Instances For
Build callbacks that run at the end of each epoch.
Instances For
Build callbacks that run once at the end of training, with the final report.
Instances For
Step-indexed source of already-collated module inputs.
TorchLean.Data.batchLoader is the right interface when the data is a finite supervised dataset. Other
training jobs draw batches from a rule or an external source: replay buffers, collocation samplers,
synthetic scale inputs, or file-backed sequence windows. StepBatchStream is the direct stream
interface for those cases.
The stream is still fully typed: each produced sample is a _root_.Runtime.Autograd.Torch.TList matching the module's
inputShapes. The training loop below is model-agnostic and only assumes that the module can run
forward and stepWith on those samples.
- sample : ℕ → IO (Runtime.Autograd.Torch.TList α inputShapes)
Produce the input sample used at logical optimizer step
step.
Instances For
Constant stream for fixed-batch overfit runs and fixed-sample training jobs.
Instances For
Build a stream from a pure step-indexed sample function.
Instances For
Cycle through a nonempty list of samples.
This adapter lets list-backed datasets use the step-stream trainer. The explicit nonempty proof keeps empty datasets from turning into silent modulo-by-zero behavior.
Instances For
Run an action with the runner temporarily switched to value mode.
Use this for callback-based validation passes during training.
Instances For
Mean loss for an already-instantiated scalar module over a typed minibatch loader.
General streaming evaluation path used by the runtime examples. It is not CIFAR-specific: any
supervised task whose loss module consumes
[dim n σ, dim n τ] can use the same loader. The loader stores ordinary per-example samples
(x : σ, y : τ); this definition asks TorchLean.Data.epoch for raw minibatches and calls
TorchLean.Data.collateSupervised to build one shape-typed batch at a time.
Two details matter for larger examples:
- We force
shuffle := falsefor evaluation so before/after metrics are deterministic. - We do not call
TorchLean.Data.BatchLoader.batchDataset, because that would materialize every collated minibatch at once. Streaming keeps the same API usable for image, sequence, and scientific ML examples where the batch tensors are much larger than small tabular datasets.
Instances For
Mean loss over a typed minibatch loader through a Trainer.Manual.Runner.
Runner-facing form of meanLossModuleLoader. Use it when the example is built around
Trainer.Manual.run, task modes, and the proof layer trainer abstraction. Use
meanLossModuleLoader directly when the example has already instantiated a runtime
TorchLean.Module.ScalarModule, which is the common fast path for CUDA examples.
Instances For
One-hot accuracy over a typed minibatch loader without materializing all collated batches.
Instances For
Train a runtime scalar module from a typed minibatch loader.
Shared real epoch loop for model examples that already have a runtime module, including CUDA runs. It mirrors the PyTorch structure:
- create an optimizer state for the module parameters;
- for each epoch, ask the general
TorchLean.Data.batchLoaderfor shuffled raw batches; - collate each raw batch into a shape-typed
(xBatch, yBatch)sample; - report the scalar loss through callbacks;
- run
forward/backward/optimizer.stepthroughTorchLean.Module.stepWith.
The function is polymorphic in the input shape σ, target shape τ, batch size n, scalar type
α, parameter shapes, and optimizer. It is not image-specific. CNN, ResNet, ViT, MLP,
sequence, operator-learning, and future model examples should all be able to use this path whenever
their supervised loss module has input shapes [dim n σ, dim n τ].
Instances For
Train a runtime scalar module for exactly steps optimizer updates.
trainModuleLoaderWith above is epoch-based: each unit means one full pass over the loader. This
variant is update-based, which is the convention used by runnable examples that expose a --steps
flag.
The loop still draws shuffled minibatches from TorchLean.Data.batchLoader epoch by epoch, but it stops as
soon as the requested number of optimizer updates has run. The returned loader is the next loader
state, so callers can continue training from the next shuffled epoch if they want to.
Instances For
Train a scalar module from a step-indexed batch stream.
Shared loop for workloads whose batches are produced step by step rather than by one finite
TorchLean.Data.batchLoader epoch:
- RL algorithms can sample replay or rollout batches,
- PDE examples can resample collocation points,
- generated workloads can stream synthetic inputs without storing a dataset.
The function is generic in inputShapes. It does not know whether the sample is
[x, y], [state, action, target], or []; it only asks the stream for the next typed input list
and then runs the same forward/backward/optimizer.step machinery as the loader-based trainer.
Instances For
Report-oriented stream-training entrypoint.
Callers pass the module, optimizer, runtime options, step count, and stream, and get standard before/after reporting plus CUDA memory watching.
Instances For
Float stream trainer that records a per-step loss curve.
Generated and file-backed batches do not always have one finite loader to summarize. This entrypoint keeps their training curves in the same JSON format as the supervised examples.
Instances For
Train from a runner-backed loader with explicit callbacks instead of inline printing in example code.
Runner-facing public path for PyTorch-style custom loops:
- keep the optimizer/scheduler logic in the library,
- inject logging, evaluation, and prediction reporting through callbacks.
This path keeps the Runner abstraction, including task modes and scheduler support. For
CUDA-heavy entrypoints that already have a TorchLean.Module.ScalarModule, prefer
trainModuleLoaderWith; both paths consume the same general TorchLean.Data.batchLoader.
Instances For
Small Reporting Helpers (IO) #
These definitions factor out common "print a loss/accuracy table" patterns for runnable model
commands.
They do not affect semantics: they only call the underlying runner functions and print
human-facing summaries. Public examples should reach them through Trainer.Manual only when the
ordinary Trainer.new / trainer.train API is too small for the example.
Convenience: mean loss on a dataset, printed with a label.
Instances For
Convenience: mean loss on a typed minibatch loader, streamed batch by batch.
Instances For
Convenience: mean loss on a typed minibatch loader for an already-instantiated runtime module.
Use this in direct CUDA/runtime examples to avoid building a Runner only for logging. The data
path is still the same public loader path: TorchLean.Data.batchLoader plus TorchLean.Data.collateSupervised.
Instances For
Report predicted classes on a list of named inputs.
Each entry is (name, x, expectedClass).
If includeLogits := true, also prints the raw model outputs.
Instances For
Report predicted classes on a list of named inputs, for a batched model.
This expects inputs of the unbatched input shape σ and replicates each one across the batch
axis, then reports the prediction for row 0.
Instances For
Convenience: mean loss + one-hot accuracy on a dataset, printed with a label.
Instances For
Batched variant of reportLossAccuracyOneHot.
Instances For
Loader variant of reportLossAccuracyOneHotBatched, streaming through minibatches.
Instances For
Train a runtime module for a fixed number of optimizer updates with the standard runtime reports.
Common path for direct-module training, not example-only code. It composes the generic step loop with before/after mean-loss reporting and CUDA allocator telemetry, while still accepting extra callbacks for projects that want their own metrics, validation, or tracing.
Instances For
Float-specialized module training that also records a scalar loss curve.
The training loop itself is the same as trainModuleLoaderStepsReport; this entrypoint adds the
standard Curve callback used by JSON logs and website widgets.
Instances For
Train a Float runtime module, write a standard scalar-curve log, and return the train report.
High-level path used by runnable training commands. The caller provides the model, optimizer, loader, runtime options, and metadata notes; the library owns the callback composition, CUDA telemetry, before/after reports, and JSON curve emission.