Prediction and Training #
Prediction, dataset training, checkpoint restoration, and stream training on TorchLean.Trainer.
Every method here opens a Session, drives it, and finishes it. Programs that need a different
loop use trainer.open directly.
Take the next cyclic group from a finite stream, evaluating only the requested samples.
Instances For
Restore the optional checkpoint named by the training options.
Instances For
Write the optional checkpoint named by the training options.
Instances For
Reject training options that cannot describe a run.
Instances For
Run the requested number of optimizer updates over a finite sample stream.
Batches cycle through the stream. Steps whose loss is logged read it back; every other step updates without a host readback. CUDA allocator state is sampled at the configured cadence.
Instances For
Predict one input using the trainer's current model and runtime settings.
Inference before any training call. After training, use the returned trained result's
trained.predict / trained.predictMany methods to predict with the trained parameters.
Example:
-- This is inference with the freshly initialized parameters, which is what makes it a useful
-- baseline to print before training starts.
def baseline (trainer : TorchLean.Trainer [2] [1]) : IO (Tensor Float [1]) :=
trainer.predict [0.25, -0.75]
Instances For
Predict a tensor batch using the trainer's current model and runtime settings.
Instances For
Train the model with the loss and runtime settings stored in trainer.
The signature uses Tensor Float, but the run itself executes in binary32: .native arithmetic
instantiates the model over Float32 and .ieee over ExecFloat.Binary 8 23. Dataset samples are
converted
into that scalar as they are used, and results are read back to Float.
The result stores the trained parameters together with prediction, reporting, state access, and
verification methods. trained.verify center (radius := r) (norm := .inf) checks the model that
was actually trained. Add (property := .topLabel label) when the verification goal is a class
label. trained.save path writes the parameters; trainer.load path data restores them.
A positive step count requires a nonempty materialized dataset.
This is trainer.open, a loop of step, and finish; the reported losses are the evaluation-mode
mean losses over data before and after the loop.
Example:
-- `trained` owns the parameters the run produced; `trainer` still describes the untrained model.
def run (trainer : TorchLean.Trainer [2] [1])
(data : Trainer.Dataset [2] [1]) : IO Unit := do
let trained ← trainer.train data { steps := 200, logEvery := 25 }
trained.printSummary
let prediction ← trained.predict [0.25, -0.75]
IO.println s!"trained(heldout) = {reprStr prediction}"
Instances For
Restore a checkpoint written by Result.save or Checkpoint.State.save and return it as a trained
result for this trainer's model, objective, and runtime settings.
The stored Float state is cast into the run's binary32 scalar, exactly when it came from
Result.save. data is evaluated once so that the result carries an honest report: steps is
0 and both losses equal the mean loss of the restored parameters on data. To continue training
instead, pass loadCheckpoint? := some path in TrainOptions; the optimizer and schedule start
fresh at step zero.
Example:
-- Reads back what `trained.save` wrote and re-measures the report on `data`, so both reported
-- losses are the restored model's loss and the step count is `0`.
def restore (trainer : TorchLean.Trainer [2] [1])
(data : Trainer.Dataset [2] [1]) : IO (Trainer.Result [2] [1]) :=
trainer.load "checkpoints/mlp.state" data
Instances For
Train a supervised model from a Float sample stream.
Generated-data examples use this when there is no fixed Dataset to hand to trainer.train. The
evaluation curve is measured on evalSample; onEval receives the completed step count, a label,
and a prediction function at every curve point.
Example:
-- No fixed dataset here: a sample is generated per step, and the loss curve is measured on one
-- held-out sample every `curveEvery` updates.
def sampleAt (step : Nat) : Sample.Supervised Float [1] [1] :=
let x := step.toFloat * 0.01
{ input := [x], target := [2.0 * x] }
def run (trainer : TorchLean.Trainer [1] [1]) (runtime : Runtime.Config) :
IO (Trainer.StreamResult [1] [1]) :=
trainer.trainStream runtime sampleAt
{ input := [0.5], target := [1.0] }
{ steps := 500, logEvery := 100 }
(curveEvery := 50)
Instances For
Train two supervised models from coupled Float streams.
The first model receives one supervised sample per step, while the second may receive several.
evalTotal predictFirst predictSecond computes the scalar curve to record; it sees only prediction
functions. Both trained results report that paired value as their before/after losses.
This alternates independent supervised updates. It does not differentiate one model's loss through the other model, so it cannot express an adversarial generator loss.