TorchLean API

NN.API.Public.Facade.Trainer.Core

TorchLean Public Trainer Facade #

Public training starts from one handle:

let trainer := Trainer.new model
  { task := .regression
    optimizer := optim.adam { lr := 0.03 } }
let y0 ← trainer.eval x
let trained ← trainer.train data { steps := 200, batchSize := 16, logEvery := 25 }
trained.printSummary

Runtime modules, tensor packs, and callback runners remain available under Trainer.Advanced. Application code should usually construct one Trainer value, call trainer.eval for initial inference, and call trainer.train to get a trained handle.

Advanced training hooks.

Runtime trainer hooks for code that needs direct control.

Use this namespace for manual callbacks, explicit runtime mode changes, custom streams, or verification bridges. Model examples should stay on Trainer.new and trainer.train.

Runtime settings carried by a public trainer.

Instances For
    inductive TorchLean.Trainer.Task (σ τ : Shape) :

    Task carried by the unified public trainer.

    The shape parameters belong to the model. The task only decides how (prediction, target) becomes a scalar objective.

    Instances For

      Construction options for the unified public trainer.

      Instances For
        structure TorchLean.Trainer.Handle (σ τ : Shape) :

        One public trainer handle.

        The main public trainer handle: a checked model, one task description, runtime choices, and a seed for model builders.

        • model : nn.Sequential σ τ

          Checked TorchLean model.

        • task : Task σ τ

          Supervised objective used by train.

        • runtime : RuntimeSettings

          Runtime/backend/optimizer choices carried by this trainer.

        • seed :

          Seed used to build this trainer when the input was an nn.M model builder.

        Instances For
          def TorchLean.Trainer.Handle.info {σ τ : Shape} (trainer : Handle σ τ) :

          Checked model summary for this unified trainer.

          Instances For
            def TorchLean.Trainer.Handle.printInfo {σ τ : Shape} (trainer : Handle σ τ) :

            Print the checked model summary with the standard public-example heading.

            Instances For
              def TorchLean.Trainer.Handle.printInfoAs {σ τ : Shape} (trainer : Handle σ τ) (label : String) :

              Print the checked model summary with a caller-chosen heading.

              Instances For

                Typed dispatch record for supervised regression.

                The exported API still has one trainer handle. This record exists so the runtime implementation can carry task-specific equalities without splitting the public surface into separate trainer classes.

                • model : nn.Sequential σ τ

                  The checked TorchLean model used by this trainer.

                • reduction : Loss.Reduction

                  Mean vs sum loss reduction for the built regression task.

                • runtime : RuntimeSettings

                  Runtime/backend/optimizer choices carried by this trainer.

                Instances For

                  Typed dispatch record for general one-hot cross-entropy training.

                  Classification and sequence models use the same checked one-hot cross-entropy runtime path. Image examples usually batch their dataset first with Data.batchDataset; text examples often train on a whole matrix of one-hot next-token rows:

                  let trainer := Trainer.new model { task := .crossEntropy, optimizer := optim.adam { lr := 1e-3 } }
                  let trained ← trainer.train tokenWindows { steps := 200 }
                  

                  The output handle exposes prediction tensors, not class labels, because token decoding is model-specific and belongs in the text example.

                  • model : nn.Sequential σ τ

                    The checked TorchLean model used by this trainer.

                  • reduction : Loss.Reduction

                    Mean vs sum loss reduction for the one-hot cross-entropy task.

                  • runtime : RuntimeSettings

                    Runtime/backend/optimizer choices carried by this trainer.

                  Instances For

                    Typed dispatch record for a checked custom scalar loss.

                    Custom losses cover masked language-model objectives, physics residuals, and algorithmic tasks where the model is still an ordinary nn.Sequential, but the loss has task logic that does not fit a canned reduction. The boundary stays precise: the loss is a TorchLean program over (prediction, target), so module construction and optimizer wiring remain inside the trainer facade.

                    Instances For

                      Checked TorchLean task induced by this regression dispatch record.

                      Instances For

                        Checked model summary for this trainer.

                        Instances For

                          Print the checked model summary with the standard public-example heading.

                          Examples use this instead of open-coding IO.println "model:"; IO.println trainer.info, so the first thing users see is consistent across regression, classifier, sequence, and custom trainers.

                          Instances For

                            Print this trainer's checked model summary with a caller-chosen heading.

                            Most examples should use trainer.printInfo; paired-model examples such as GANs use this labeled variant so both summaries still go through the same formatting path.

                            Instances For

                              The runtime task induced by this handle has exactly the model's parameter shapes.

                              Checked TorchLean task induced by this cross-entropy dispatch record.

                              Instances For

                                Checked model summary for this trainer.

                                Instances For

                                  Print the checked model summary with the standard public-example heading.

                                  Instances For

                                    The runtime task induced by this handle has exactly the model's parameter shapes.

                                    Checked model summary for this custom-loss trainer.

                                    Instances For

                                      Print the checked model summary with the standard public-example heading.

                                      Instances For