GitHub
deepbox/nn

Loss Functions

Differentiable loss functions for training neural networks. All support 'mean', 'sum', and 'none' reduction modes.
mseLoss
mseLoss(predictions: Tensor, targets: Tensor, reduction?: 'mean' | 'sum' | 'none'): Tensor

Mean Squared Error: (1/n) Σ(ŷ − y)². Standard loss for regression. Penalizes large errors quadratically.

maeLoss
maeLoss(predictions: Tensor, targets: Tensor, reduction?: 'mean' | 'sum' | 'none'): Tensor

Mean Absolute Error (L1 loss): (1/n) Σ|ŷ − y|. Robust to outliers. Linear penalty.

crossEntropyLoss
crossEntropyLoss(logits: Tensor, targets: Tensor): number | crossEntropyLoss(logits: GradTensor, targets: AnyTensor): GradTensor

Cross-entropy loss for multi-class classification. Expects raw logits (not softmax). Combines log-softmax and NLL loss for numerical stability. Returns a number when called with plain Tensors, or a GradTensor when called with GradTensors (for backpropagation).

binaryCrossEntropyLoss
binaryCrossEntropyLoss(predictions: Tensor, targets: Tensor, reduction?: 'mean' | 'sum' | 'none'): Tensor

Binary cross-entropy: −[y·log(ŷ) + (1−y)·log(1−ŷ)]. For binary classification. Expects probabilities in (0, 1).

binaryCrossEntropyWithLogitsLoss
binaryCrossEntropyWithLogitsLoss(logits: Tensor, targets: Tensor): number | binaryCrossEntropyWithLogitsLoss(logits: GradTensor, targets: AnyTensor): GradTensor

Numerically stable BCE that accepts raw logits (before sigmoid). Combines sigmoid and BCE in one step. Returns a number when called with plain Tensors, or a GradTensor when called with GradTensors (for backpropagation).

huberLoss
huberLoss(predictions: Tensor, targets: Tensor, delta?: number, reduction?: 'mean' | 'sum' | 'none'): Tensor

Huber loss: quadratic for small errors, linear for large errors. Controlled by delta parameter. Combines benefits of MSE and MAE.

rmseLoss
rmseLoss(predictions: Tensor, targets: Tensor): Tensor

Root Mean Squared Error: √MSE. Same units as the target. More interpretable than MSE.

MSE

L = (1/n) Σᵢ (ŷᵢ − yᵢ)²

Where:

  • ŷ = Prediction

Cross-Entropy

L = −Σᵢ yᵢ · log(softmax(ẑᵢ))

Where:

  • = Raw logits

BCE

L = −[y·log(ŷ) + (1−y)·log(1−ŷ)]

Where:

  • ŷ = Predicted probability

Huber

L = 0.5a² if |a| ≤ δ, else δ(|a| − 0.5δ)

Where:

  • a = ŷ − y (error)
  • δ = Transition threshold
losses.ts
import { mseLoss, crossEntropyLoss, binaryCrossEntropyLoss, huberLoss } from "deepbox/nn";import { tensor } from "deepbox/ndarray";// Regression lossconst pred = tensor([2.5, 0.0, 2.1]);const target = tensor([3.0, -0.5, 2.0]);mseLoss(pred, target);  // Scalar tensorhuberLoss(pred, target, 1.0); // Robust to outliers// Multi-class classification (expects raw logits)const logits = tensor([[2.0, 1.0, 0.1]]);const labels = tensor([0], { dtype: "int32" }); // Class 0crossEntropyLoss(logits, labels);// Binary classification (expects probabilities)const probs = tensor([0.9, 0.1, 0.8]);const binLabels = tensor([1, 0, 1]);binaryCrossEntropyLoss(probs, binLabels);

Choosing a Loss

  • Regression → mseLoss (default), maeLoss (robust), huberLoss (balanced)
  • Binary classification → binaryCrossEntropyLoss (with sigmoid output) or binaryCrossEntropyWithLogitsLoss (with raw logits)
  • Multi-class classification → crossEntropyLoss (with raw logits, no softmax needed)