/- Copyright (c) 2026 TorchLean Released under MIT license as described in the file LICENSE. Authors: TorchLean Team -/ module public import TorchLean.NN.Sequential.Training public import TorchLean.NN.Optimizer.Constructor public import TorchLean.NN.Builder public import TorchLean.NN.Training.Reporting public import TorchLean.Tensor.Operations /-! # Training with explicit precision Fit an affine model to one sample using the loss `(prediction - target)^2 / 2`. The derivative with respect to the prediction is its residual. A typed VJP converts that residual into parameter gradients, and `nn.sgdStep` applies the SGD update in the same scalar. The model has no buffer-update hooks, which `nn.sgdStep` would reject. State, data, gradients, and learning rate use the selected scalar throughout. Call `run` for a binary128 example, or `fit` with another supported `Context`. The example runs as CPU software arithmetic, with no CUDA path and no checkpoint. -/ @[expose] public section namespace TorchLean.Examples.Quickstart.TypedTraining open FloatLib.Floats /-- A scalar affine model with caller-supplied typed weight and bias. -/ def model : nn.Sequential [1] [1] := nn.build 0 (nn.linear 1 1) /-- Reuse one typed graph for several SGD updates of half squared error on one sample. -/ def fit {α : Type} [Storage α] [Context α] (initial : TensorPack α (nn.Sequential.stateShapes model)) (input target : Tensor α [1]) (lr : α) (steps : Nat) : IO (TensorPack α (nn.Sequential.stateShapes model)) := do let graph ← nn.Sequential.lowerToTypedGraph model (α := α) (mode := nn.Mode.train) let mut state := initial for _ in [:steps] do let prediction := nn.TypedGraphModel.forward graph state input let residual := Tensor.sub prediction target let (gradient, _) := nn.TypedGraphModel.vjp graph state input residual state ← IO.ofExcept (nn.sgdStep model lr state gradient) return state /-- Train from a coefficient that cannot be represented in binary64, then print exact rationals. -/ def run : IO Unit := do let initial : TensorPack Binary128 (nn.Sequential.stateShapes model) := .fill (Rat.cast (1 + 1 / (2 ^ 100 : Nat) : Rat)) let input : Tensor Binary128 [1] := Tensor.full [1] 2 let trained ← fit initial input (Tensor.zeros [1]) (Rat.cast (1 / 8 : Rat)) 2 let graph ← nn.Sequential.lowerToTypedGraph model (α := Binary128) let before := nn.TypedGraphModel.forward graph initial input let after := nn.TypedGraphModel.forward graph trained input IO.println s!"initial prediction: {ExecFloat.Binary.toRat? before[0]}" IO.println s!"trained prediction: {ExecFloat.Binary.toRat? after[0]}" end TorchLean.Examples.Quickstart.TypedTraining