This module provides standard loss functions used in neural network training. All loss functions are numerically stable and differentiable through Rune's autodiff system.
Loss functions measure the difference between predictions and targets, returning a scalar loss value that can be minimized during training. Most functions return the mean loss across all examples in the batch.
Classification Losses
For multi-class and binary classification tasks. These assume specific input formats (logits vs probabilities) and target representations (one-hot vs indices).
Regression Losses
For continuous value prediction tasks. These measure distance between predicted and target values using different metrics.
if any index is outside the valid range [0, num_classes).
Example
Classification where first example belongs to class 0, second to class 2:
let logits = Rune.create device Rune.float32 [|2; 3|] [|2.0; 1.0; 0.1; 0.5; 1.5; 2.1|] in
let indices = Rune.create device Rune.int32 [|2|] [|0; 2|] in
let loss = Loss.softmax_cross_entropy_with_indices logits indices
sigmoid_binary_cross_entropy logits labels computes binary cross-entropy loss from raw logits.
Applies sigmoid normalization to logits internally. More numerically stable than manually applying sigmoid then binary_cross_entropy, as it uses log_sigmoid internally.
Unlike binary_cross_entropy, this returns loss per example without taking the mean, allowing for sample-weighted training.
parameterlogits
Raw model outputs of shape batch_size; .... Can be any real values.
parameterlabels
Binary ground truth labels of shape batch_size; .... Values should be 0.0 or 1.0.
returns
Tensor of shape batch_size; ... containing loss per example.
Example
Binary classification with raw logits:
let logits = Rune.create device Rune.float32 [|4; 1|] [|1.5; -0.8; 0.9; -2.1|] in
let labels = Rune.create device Rune.float32 [|4; 1|] [|1.0; 0.0; 1.0; 0.0|] in
let loss_per_example = Loss.sigmoid_binary_cross_entropy logits labels in
let mean_loss = Rune.mean loss_per_example
mse predictions targets computes mean squared error between predictions and targets.
Suitable for regression tasks. Penalizes large errors more heavily than small errors due to squaring.
parameterpredictions
Model predictions of any shape.
parametertargets
Ground truth targets of the same shape as predictions.
returns
Scalar tensor containing the mean squared error: mean((predictions - targets)^2).
Example
Regression with continuous targets:
let predictions = Rune.create device Rune.float32 [|3|] [|2.1; 0.8; 1.5|] in
let targets = Rune.create device Rune.float32 [|3|] [|2.0; 1.0; 1.2|] in
let loss = Loss.mse predictions targets
Mathematical formula: L = 1/N * sum_i (pred_i - target_i)^2 where N is the total number of elements.
mae predictions targets computes mean absolute error between predictions and targets.
Suitable for regression tasks where you want equal penalty for all errors regardless of magnitude. Less sensitive to outliers than MSE.
parameterpredictions
Model predictions of any shape.
parametertargets
Ground truth targets of the same shape as predictions.
returns
Scalar tensor containing the mean absolute error: mean(|predictions - targets|).
Example
Regression with robust loss function:
let predictions = Rune.create device Rune.float32 [|3|] [|2.1; 0.8; 1.5|] in
let targets = Rune.create device Rune.float32 [|3|] [|2.0; 1.0; 1.2|] in
let loss = Loss.mae predictions targets
Mathematical formula: L = 1/N * sum_i |pred_i - target_i| where N is the total number of elements.