TorchLean API

NN.Runtime.Autograd.Model.Loss

Loss #

TorchLean loss helpers in the style of torch.nn.functional.

These helpers keep training loops close to the familiar torch.nn.functional style:

loss = Loss.mse yhat y
loss = Loss.mse yhat y (reduction := .sum)

They are execution-mode generic: eager tape and typed SSA/DAG both work.

PyTorch references #

Reduction mode for losses that start as elementwise tensors.

PyTorch analogy: reduction="mean" or reduction="sum".

Instances For
    @[instance_reducible]

    Reduce an elementwise loss tensor to a scalar according to reduction.

    This is the common final step for losses like MSE and cross-entropy.

    Instances For

      Mean squared error (MSE) loss between predictions and targets.

      This is backend-generic and supports both mean and sum reduction.

      Instances For

        Weighted mean-squared error.

        This returns sum (weights * (prediction - target)^2) without implicit normalization. Weights whose sum is one therefore define a weighted mean, and zero weights exclude coordinates without requiring a separate masking operation.

        Instances For
          def TorchLean.Loss.nllOneHot {α : Type} [Storage α] [Context α] {m : TypeType} [Monad m] [Runtime.Autograd.Torch.Ops m α] {s : Shape} (axis : ) [Shape.AxisInBounds axis s] (logProbs targetOneHot : Runtime.Autograd.Model.RefTy m α s) (reduction : Reduction := Reduction.mean) :

          Negative log-likelihood (one-hot targets), assuming inputs are log-probabilities.

          Instances For

            Cross-entropy (one-hot targets), computed as -sum(y * log(softmax(logits))).

            Note: this is one-hot only. For integer class labels, use crossEntropy, whose Fin classes target tensor records the class bound in its element type.

            Instances For
              def TorchLean.Loss.nllUnreduced {α : Type} [Storage α] [Context α] {m : TypeType} [Monad m] [Runtime.Autograd.Torch.Ops m α] {leading trailing : Shape} {classes : } (classAxis : ) (_hClassAxis : classAxis = leading.rank) (logProbs : Runtime.Autograd.Model.RefTy m α (leading.concat (trailing.prependDim classes))) (target : Runtime.Autograd.Torch.DataRef (Fin classes) (leading.concat trailing)) :
              m (Runtime.Autograd.Model.RefTy m α (leading.concat trailing))

              Unreduced indexed negative log-likelihood along an arbitrary class axis.

              The shape contract is:

              • logProbs has shape prefix ++ [classes] ++ suffix;
              • classAxis = rank prefix identifies the classes dimension;
              • target has shape prefix ++ suffix and element type Fin classes;
              • the result has the same shape as target, with the class axis removed.

              Each output coordinate is -logProbs[..., target[...], ...]. In particular, suffix coordinates remain aligned with their corresponding labels. Invalid class labels are unrepresentable.

              Instances For
                def TorchLean.Loss.nll {α : Type} [Storage α] [Context α] {m : TypeType} [Monad m] [Runtime.Autograd.Torch.Ops m α] {leading trailing : Shape} {classes : } (classAxis : ) (hClassAxis : classAxis = leading.rank) (logProbs : Runtime.Autograd.Model.RefTy m α (leading.concat (trailing.prependDim classes))) (target : Runtime.Autograd.Torch.DataRef (Fin classes) (leading.concat trailing)) (reduction : Reduction := Reduction.mean) :

                Indexed negative log-likelihood along an arbitrary class axis.

                The logits, labels, and class-axis contract are those of nllUnreduced. The requested reduction is applied over every coordinate of the class-erased output shape.

                Instances For
                  def TorchLean.Loss.nllWeighted {α : Type} [Storage α] [Context α] {m : TypeType} [Monad m] [Runtime.Autograd.Torch.Ops m α] {leading trailing : Shape} {classes : } (classAxis : ) (hClassAxis : classAxis = leading.rank) (logProbs : Runtime.Autograd.Model.RefTy m α (leading.concat (trailing.prependDim classes))) (target : Runtime.Autograd.Torch.DataRef (Fin classes) (leading.concat trailing)) (weights : Runtime.Autograd.Model.RefTy m α (leading.concat trailing)) :

                  Weighted indexed negative log-likelihood along an arbitrary class axis.

                  The logits, labels, weights, and output coordinates obey the nllUnreduced shape contract. This returns sum (weights * losses) without implicit normalization; normalized weights therefore give a weighted mean, while zero weights mask coordinates without a division-by-zero convention.

                  Instances For
                    def TorchLean.Loss.crossEntropy {α : Type} [Storage α] [Context α] {m : TypeType} [Monad m] [Runtime.Autograd.Torch.Ops m α] {leading trailing : Shape} {classes : } (classAxis : ) (hClassAxis : classAxis = leading.rank) (logits : Runtime.Autograd.Model.RefTy m α (leading.concat (trailing.prependDim classes))) (target : Runtime.Autograd.Torch.DataRef (Fin classes) (leading.concat trailing)) (reduction : Reduction := Reduction.mean) :

                    Indexed cross-entropy along an arbitrary class axis.

                    This applies log-softmax along classAxis and then uses nll. Shapes follow the prefix ++ [classes] ++ suffix contract documented on nllUnreduced.

                    Instances For
                      def TorchLean.Loss.crossEntropyWeighted {α : Type} [Storage α] [Context α] {m : TypeType} [Monad m] [Runtime.Autograd.Torch.Ops m α] {leading trailing : Shape} {classes : } (classAxis : ) (hClassAxis : classAxis = leading.rank) (logits : Runtime.Autograd.Model.RefTy m α (leading.concat (trailing.prependDim classes))) (target : Runtime.Autograd.Torch.DataRef (Fin classes) (leading.concat trailing)) (weights : Runtime.Autograd.Model.RefTy m α (leading.concat trailing)) :

                      Weighted indexed cross-entropy along an arbitrary class axis.

                      This applies log-softmax along classAxis, multiplies each class-erased loss by the corresponding weight, and sums without implicit normalization. Shapes follow the contract on nllUnreduced.

                      Instances For

                        Binary cross-entropy with logits (elementwise), using the stable identity: BCEWithLogits(x,y) = y * softplus(-x) + (1-y) * softplus(x).

                        Targets are expected in [0,1] (typically 0/1), same shape as logits.

                        Instances For