TorchLean API

NN.Runtime.Autograd.Train.Trainer

Trainer API with metrics and logging #

This module defines a small, higher-level training API on top of a step function. It stays lightweight (no global state), while making it easy to:

Metrics and step reports #

A named scalar metric for logging/monitoring.

Examples: "acc", "top5", "grad_norm", "lr".

  • name : String

    Metric name (used as the key in logs).

  • value : a

    Metric value (typically the same scalar type as the loss).

Instances For

    Render a metric as name=value.

    Instances For

      Per-step training report.

      This is a lightweight record: a single scalar loss (to drive optimization) plus optional metrics for logging/monitoring.

      • loss : a

        Scalar objective value for this step or evaluation pass.

      • metrics : List (Metric a)

        Additional named scalar metrics for logging/monitoring.

      Instances For

        Render a list of metrics as a comma-separated string.

        Instances For

          Render a single step report (loss + metrics).

          Instances For

            Render reports for a full run, with step numbers starting at 0.

            Instances For

              Generic training loop #

              This is a light wrapper around a "step" function that returns a new state and an output. It is still useful for very small tests that do not need full metrics.

              def Runtime.Autograd.Train.runStepsM {m : TypeType} [Monad m] {state out : Type} (steps : ) (init : state) (step : statem (state × out)) :
              m (state × List out)

              Run a monadic step function for a fixed number of steps, collecting the per-step outputs.

              This is a generic utility (not Torch-specific): it threads a state value and accumulates an out value per step.

              Instances For
                def Runtime.Autograd.Train.runStepsM.go {m : TypeType} [Monad m] {state out : Type} (step : statem (state × out)) :
                stateList outm (state × List out)
                Instances For

                  Trainer structure #

                  Trainer bundles the initial state, step function, and optional logger. The logger runs after each step and can observe the updated state and report.

                  structure Runtime.Autograd.Train.Trainer (m : TypeType) (state a : Type) :

                  A small "trainer bundle": initial state, step function, and a per-step logger.

                  The logger runs after each step and can observe both the updated state and the report, which matches how training scripts typically print "after-update" metrics.

                  • init : state

                    Initial training state.

                  • step : statem (state × StepReport a)

                    A single training step: update state and produce a report.

                  • logger : stateStepReport am Unit

                    Optional logger hook called after each step.

                  Instances For
                    def Runtime.Autograd.Train.Trainer.noLog {m : TypeType} [Monad m] {state a : Type} (init : state) (step : statem (state × StepReport a)) :
                    Trainer m state a

                    Construct a Trainer with a no-op logger.

                    This is a convenient default for experiments that just want to collect reports.

                    Instances For
                      def Runtime.Autograd.Train.Trainer.run {m : TypeType} [Monad m] {state a : Type} (steps : ) (t : Trainer m state a) :
                      m (state × List (StepReport a))

                      Run a trainer for steps steps, returning the final state and the collected reports.

                      Instances For
                        def Runtime.Autograd.Train.Trainer.run.go {m : TypeType} [Monad m] {state a : Type} (t : Trainer m state a) :
                        stateList (StepReport a)m (state × List (StepReport a))
                        Instances For
                          def Runtime.Autograd.Train.Trainer.runLosses {m : TypeType} [Monad m] {state a : Type} (steps : ) (t : Trainer m state a) :
                          m (state × List a)

                          Run a trainer and project out just the per-step losses.

                          Instances For