TorchLean API

NN.API.Runtime.Training

Runtime Supervised Training #

Supervised tasks, runners, steppers, optimizer configs, trainer aliases, and the low-level session exports that back executable examples.

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
            @[reducible, inline]

            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=...) when momentum > 0, and plain SGD when momentum = 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

                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 (0 disables 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 configuration.

                • logEvery :

                  Log once every this many steps.

                Instances For

                  Small summary returned by lower training helpers.

                  By default, before and after are mean loss values, but the type is polymorphic so callers can report other scalars in the same shape.

                  • before : α

                    Metrics before training.

                  • after : α

                    Metrics after training.

                  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 (0 disables logging).
                    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
                          def NN.API.TorchLean.Supervised.mapStateList {State : TypeSpec.ShapeType} {α : Type} {ss : List Spec.Shape} :
                          ({s : Spec.Shape} → State α sState α s)Runtime.Autograd.TorchLean.StateList State α ssRuntime.Autograd.TorchLean.StateList State α ss

                          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 .train and .eval modes (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

                                    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
                                            def NN.API.TorchLean.Supervised.run {σ τ : Spec.Shape} (task : SeqTask σ τ) (args : List String) (k : {α : Type} → [inst : Semantics.Scalar α] → [inst_1 : DecidableEq Spec.Shape] → [ToString α] → [Runtime.Scalar α] → Runner α taskList StringIO Unit) :

                                            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
                                              def NN.API.TorchLean.Supervised.params {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                              Read the current parameter list from a runner.

                                              Instances For
                                                def NN.API.TorchLean.Supervised.mode {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                Read the runner's current mode (.train or .eval).

                                                Instances For
                                                  def NN.API.TorchLean.Supervised.setMode {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (value : NN.Mode) :

                                                  Set the runner mode (.train or .eval).

                                                  Instances For
                                                    def NN.API.TorchLean.Supervised.trainMode {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                    Convenience: setMode runner .train.

                                                    Instances For
                                                      def NN.API.TorchLean.Supervised.evalMode {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                      Convenience: setMode runner .eval.

                                                      Instances For
                                                        def NN.API.TorchLean.Supervised.isTraining {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                        Predicate: are we in training mode?

                                                        Instances For
                                                          def NN.API.TorchLean.Supervised.activePredictor {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :
                                                          IO (CompiledOut α (paramShapes task ++ [σ]) τ)

                                                          Pick the predictor compiled for the runner's current mode (.train or .eval).

                                                          Instances For
                                                            def NN.API.TorchLean.Supervised.activeLoss {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                            Pick the loss program compiled for the runner's current mode (.train or .eval).

                                                            Instances For
                                                              def NN.API.TorchLean.Supervised.updateRunnerBuffers {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (sample : TensorPack α [σ, τ]) :

                                                              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
                                                                def NN.API.TorchLean.Supervised.backward {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (sample : TensorPack α [σ, τ]) :

                                                                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
                                                                  def NN.API.TorchLean.Supervised.predict {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (x : Spec.Tensor α σ) :
                                                                  IO (Spec.Tensor α τ)

                                                                  Predict on one input tensor using the runner's active mode (.train or .eval).

                                                                  Instances For
                                                                    def NN.API.TorchLean.Supervised.predictBatch {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (xs : List (Spec.Tensor α σ)) :
                                                                    IO (List (Spec.Tensor α τ))

                                                                    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
                                                                          def NN.API.TorchLean.Supervised.meanLoss {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (samples : List (TensorPack α [σ, τ])) :
                                                                          IO α

                                                                          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
                                                                              def NN.API.TorchLean.Supervised.moduleLoss {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (sample : TensorPack α [σ, τ]) :
                                                                              IO α

                                                                              Scalar loss for one sample through the instantiated runtime module.

                                                                              Instances For

                                                                                Treat 0 as the conservative single-sample step size.

                                                                                Instances For
                                                                                  def NN.API.TorchLean.Supervised.nextCyclicBatch {α : Type} (context : String) (samples : List α) (restRef : IO.Ref (List α)) (batchSize : ) :
                                                                                  IO (List α)

                                                                                  Take the next cyclic group of samples from an in-memory training set.

                                                                                  Instances For
                                                                                    def NN.API.TorchLean.Supervised.trainSamples {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (cfg : TrainConfig) (samples : List (TensorPack α [σ, τ])) :

                                                                                    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
                                                                                          def NN.API.TorchLean.Supervised.trainLoader.trainSgdBatches {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [Runtime.Scalar α] (runner : Runner α task) (cfg : LoaderTrainConfig) (epoch : ) (batches : List (List (TensorPack α [σ, τ]))) :
                                                                                          Instances For
                                                                                            def NN.API.TorchLean.Supervised.trainLoader.trainMomSamples {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (cfg : LoaderTrainConfig) (lr momentum : Float) (epoch stepIdx : ) (state : (Runtime.Autograd.TorchLean.momentumSGD (Runtime.ofFloat lr) (Runtime.ofFloat momentum)).State) (samples : List (TensorPack α [σ, τ])) :
                                                                                            Instances For
                                                                                              def NN.API.TorchLean.Supervised.trainLoader.trainMomBatches {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (cfg : LoaderTrainConfig) (lr momentum : Float) (epoch stepIdx : ) (state : (Runtime.Autograd.TorchLean.momentumSGD (Runtime.ofFloat lr) (Runtime.ofFloat momentum)).State) (batches : List (List (TensorPack α [σ, τ]))) :
                                                                                              Instances For
                                                                                                def NN.API.TorchLean.Supervised.trainLoader.trainAdamSamples {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (cfg : LoaderTrainConfig) (lr beta1 beta2 epsilon : Float) (epoch stepIdx : ) (state : (Runtime.Autograd.TorchLean.adam (Runtime.ofFloat lr) (Runtime.ofFloat beta1) (Runtime.ofFloat beta2) (Runtime.ofFloat epsilon)).State) (samples : List (TensorPack α [σ, τ])) :
                                                                                                Instances For
                                                                                                  def NN.API.TorchLean.Supervised.trainLoader.trainAdamBatches {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (cfg : LoaderTrainConfig) (lr beta1 beta2 epsilon : Float) (epoch stepIdx : ) (state : (Runtime.Autograd.TorchLean.adam (Runtime.ofFloat lr) (Runtime.ofFloat beta1) (Runtime.ofFloat beta2) (Runtime.ofFloat epsilon)).State) (batches : List (List (TensorPack α [σ, τ]))) :
                                                                                                  Instances For
                                                                                                    def NN.API.TorchLean.Supervised.trainLoader.trainAdamWSamples {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (cfg : LoaderTrainConfig) (lr weightDecay beta1 beta2 epsilon : Float) (epoch stepIdx : ) (state : (Runtime.Autograd.TorchLean.adamw (Runtime.ofFloat lr) (Runtime.ofFloat weightDecay) (Runtime.ofFloat beta1) (Runtime.ofFloat beta2) (Runtime.ofFloat epsilon)).State) (samples : List (TensorPack α [σ, τ])) :
                                                                                                    Instances For
                                                                                                      def NN.API.TorchLean.Supervised.trainLoader.trainAdamWBatches {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (cfg : LoaderTrainConfig) (lr weightDecay beta1 beta2 epsilon : Float) (epoch stepIdx : ) (state : (Runtime.Autograd.TorchLean.adamw (Runtime.ofFloat lr) (Runtime.ofFloat weightDecay) (Runtime.ofFloat beta1) (Runtime.ofFloat beta2) (Runtime.ofFloat epsilon)).State) (batches : List (List (TensorPack α [σ, τ]))) :
                                                                                                      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.

                                                                                                        • epochSamples : List (TensorPack α [σ, τ])IO (List α)

                                                                                                          Run an epoch over an explicit list of samples, returning the per-step loss values.

                                                                                                        • stepCount : IO

                                                                                                          Read the total number of stepSample calls performed so far.

                                                                                                        Instances For
                                                                                                          def NN.API.TorchLean.Supervised.stepper {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (optimizer : OptimizerConfig) (scheduler : Option Schedulers.Config := none) :
                                                                                                          IO (Stepper α task)

                                                                                                          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
                                                                                                            def NN.API.TorchLean.Supervised.step {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (loop : Stepper α task) (sample : TensorPack α [σ, τ]) :
                                                                                                            IO α

                                                                                                            Run one optimization step on a single supervised sample.

                                                                                                            Instances For
                                                                                                              def NN.API.TorchLean.Supervised.epoch {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (loop : Stepper α task) (samples : List (TensorPack α [σ, τ])) :
                                                                                                              IO (List α)

                                                                                                              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:

                                                                                                                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.

                                                                                                                @[reducible, inline]

                                                                                                                A supervised task is just a model plus a choice of loss.

                                                                                                                Instances For
                                                                                                                  @[reducible, inline]

                                                                                                                  A fully instantiated supervised task runner.

                                                                                                                  This bundles:

                                                                                                                  • the imperative ScalarModule (parameters/buffers stored in refs),
                                                                                                                  • compiled predictors and loss functions for both .train and .eval modes (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
                                                                                                                    @[reducible, inline]

                                                                                                                    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
                                                                                                                      @[reducible, inline]

                                                                                                                      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 (0 disables logging).
                                                                                                                      Instances For
                                                                                                                        @[reducible, inline]

                                                                                                                        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 (0 disables logging).
                                                                                                                        Instances For
                                                                                                                          @[reducible, inline]

                                                                                                                          Small summary returned by lower training helpers.

                                                                                                                          By default, before and after are mean loss values, but the type is polymorphic so callers can report other scalars in the same shape.

                                                                                                                          Instances For
                                                                                                                            @[reducible, inline]

                                                                                                                            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
                                                                                                                                  def NN.API.TorchLean.Trainer.sgd (lr : Float) (momentum : Float := 0.0) :

                                                                                                                                  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
                                                                                                                                      def NN.API.TorchLean.Trainer.adam (lr : Float) (beta1 : Float := 0.9) (beta2 : Float := 0.999) (epsilon : Float := 1e-8) :

                                                                                                                                      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
                                                                                                                                        def NN.API.TorchLean.Trainer.adamw (lr : Float) (weightDecay : Float := 1e-2) (beta1 : Float := 0.9) (beta2 : Float := 0.999) (epsilon : Float := 1e-8) :

                                                                                                                                        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.

                                                                                                                                          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
                                                                                                                                                  def NN.API.TorchLean.Trainer.steps (count : ) (optimizer : Optimizer := sgd 1e-2) (logEvery : := 1) :

                                                                                                                                                  Fixed-step training config over an in-memory sample list or dataset.

                                                                                                                                                  Instances For
                                                                                                                                                    def NN.API.TorchLean.Trainer.epochs (count : ) (optimizer : Optimizer := sgd 1e-2) (logEvery : := 1) :

                                                                                                                                                    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
                                                                                                                                                            def NN.API.TorchLean.Trainer.stepLR (cfg : TrainConfig) (base : Float) (stepSize : ) (gamma : Float := 0.1) :

                                                                                                                                                            Step-based step-decay schedule.

                                                                                                                                                            Instances For

                                                                                                                                                              Step-based exponential-decay schedule.

                                                                                                                                                              Instances For

                                                                                                                                                                Epoch-based constant learning-rate schedule.

                                                                                                                                                                Instances For
                                                                                                                                                                  def NN.API.TorchLean.Trainer.stepEpochLR (cfg : LoaderTrainConfig) (base : Float) (stepSize : ) (gamma : Float := 0.1) :

                                                                                                                                                                  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
                                                                                                                                                                          def NN.API.TorchLean.Trainer.run {σ τ : Spec.Shape} (task : Task σ τ) (args : List String) (k : {α : Type} → [inst : Semantics.Scalar α] → [inst_1 : DecidableEq Spec.Shape] → [ToString α] → [Runtime.Scalar α] → Runner α taskList StringIO Unit) :

                                                                                                                                                                          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
                                                                                                                                                                            def NN.API.TorchLean.Trainer.params {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                                                                                                                                            Get the current model parameters from a runner.

                                                                                                                                                                            Instances For
                                                                                                                                                                              def NN.API.TorchLean.Trainer.mode {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                                                                                                                                              Read the current mode (train vs eval).

                                                                                                                                                                              Instances For
                                                                                                                                                                                def NN.API.TorchLean.Trainer.setMode {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (value : NN.Mode) :

                                                                                                                                                                                Set the mode (train vs eval).

                                                                                                                                                                                Instances For
                                                                                                                                                                                  def NN.API.TorchLean.Trainer.trainMode {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                                                                                                                                                  Put the runner in training mode so layers such as dropout/batchnorm use training behavior.

                                                                                                                                                                                  Instances For
                                                                                                                                                                                    def NN.API.TorchLean.Trainer.evalMode {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                                                                                                                                                    Put the runner in evaluation mode so stateful layers use inference behavior.

                                                                                                                                                                                    Instances For
                                                                                                                                                                                      def NN.API.TorchLean.Trainer.isTraining {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) :

                                                                                                                                                                                      Check whether the runner is in training mode.

                                                                                                                                                                                      Instances For
                                                                                                                                                                                        def NN.API.TorchLean.Trainer.backward {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (sample : TensorPack α [σ, τ]) :

                                                                                                                                                                                        Run forward+backward on one supervised sample and return gradients for all parameters.

                                                                                                                                                                                        Instances For
                                                                                                                                                                                          def NN.API.TorchLean.Trainer.predict {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (x : Spec.Tensor α σ) :
                                                                                                                                                                                          IO (Spec.Tensor α τ)

                                                                                                                                                                                          Predict on a single input tensor.

                                                                                                                                                                                          This runs the forward pass under the runner's current mode.

                                                                                                                                                                                          Instances For
                                                                                                                                                                                            def NN.API.TorchLean.Trainer.predictBatch {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (runner : Runner α task) (xs : List (Spec.Tensor α σ)) :
                                                                                                                                                                                            IO (List (Spec.Tensor α τ))

                                                                                                                                                                                            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
                                                                                                                                                                                                  def NN.API.TorchLean.Trainer.meanLoss {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (samples : List (TensorPack α [σ, τ])) :
                                                                                                                                                                                                  IO α

                                                                                                                                                                                                  Mean loss over an explicit list of samples.

                                                                                                                                                                                                  Instances For
                                                                                                                                                                                                    def NN.API.TorchLean.Trainer.meanLossDataset {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (dataset : Runtime.Autograd.Train.Dataset (TensorPack α [σ, τ])) :
                                                                                                                                                                                                    IO α

                                                                                                                                                                                                    Mean loss over an entire Dataset.

                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                      def NN.API.TorchLean.Trainer.trainSamples {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (cfg : TrainConfig) (samples : List (TensorPack α [σ, τ])) :

                                                                                                                                                                                                      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
                                                                                                                                                                                                            def NN.API.TorchLean.Trainer.stepper {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (runner : Runner α task) (optimizer : Optimizer) (scheduler : Option Schedulers.Config := none) :
                                                                                                                                                                                                            IO (Stepper α task)

                                                                                                                                                                                                            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
                                                                                                                                                                                                              def NN.API.TorchLean.Trainer.step {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (loop : Stepper α task) (sample : TensorPack α [σ, τ]) :
                                                                                                                                                                                                              IO α

                                                                                                                                                                                                              Run a single training step on one sample using a Stepper.

                                                                                                                                                                                                              Instances For
                                                                                                                                                                                                                def NN.API.TorchLean.Trainer.epoch {σ τ : Spec.Shape} {task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] (loop : Stepper α task) (samples : List (TensorPack α [σ, τ])) :
                                                                                                                                                                                                                IO (List α)

                                                                                                                                                                                                                Run an epoch over a list of samples using a Stepper (returns the per-step losses).

                                                                                                                                                                                                                Instances For
                                                                                                                                                                                                                  def NN.API.TorchLean.Trainer.train {σ τ : Spec.Shape} {_task : Task σ τ} {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [ToString α] [Runtime.Scalar α] (task : Task σ τ) (cfg : TrainConfig) (samples : List (TensorPack α [σ, τ])) (backend : Backend := Runtime.Autograd.Torch.Backend.eager) :
                                                                                                                                                                                                                  IO (Runner α task × TrainReport α)

                                                                                                                                                                                                                  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
                                                                                                                                                                                                                        @[reducible, inline]

                                                                                                                                                                                                                        Execution config parsed from CLI flags (dtype/backend/fast-kernels).

                                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                                          @[reducible, inline]

                                                                                                                                                                                                                          Parse and strip execution flags, returning (config, remainingArgs).

                                                                                                                                                                                                                          Instances For
                                                                                                                                                                                                                            @[reducible, inline]

                                                                                                                                                                                                                            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: