TorchLean API

NN.API.Trainer.Handle

Trainer Handle #

Create a trainer from a checked model, choose its loss and optimizer, then train or predict:

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

Trainer.Manual provides direct access to runtime modules, tensor packs, and callbacks for custom loops.

Runtime settings stored with a trainer.

  • optimizer : optim.Optimizer

    Optimizer used unless a training call supplies another run configuration.

  • Scalar dtype used for the run.

  • backend : Runtime.Backend

    Eager or compiled execution backend.

  • executionProfile : NN.Backend.BackendProfile

    Contract profile controlling device, providers, assurance, and VJP ownership.

  • showBackend : Bool

    Print each accepted backend capsule when it is first used.

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

    Loss used to train a model.

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

    Instances For

      Model-independent options accepted by Trainer.new.

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

        A checked model together with its loss, runtime settings, and initialization seed.

        • 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 TorchLean.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 under the heading model.

            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.

                This record carries the equalities needed by the regression implementation. Users construct a Trainer.Handle rather than this task-specific record.

                • 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 TorchLean.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 API.

                    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