Skip to Content
OchreML
Training (Losses, Optimizers & Schedulers)

Losses

MSELoss and fused N9 CrossEntropyLoss with label smoothing and ignore_index support.

ochreml.nn provides two differentiable loss modules backed by Rust autograd: MSELoss for regression and fused N9 CrossEntropyLoss for classification and language modeling.

Import & Signatures

import ochreml.nn as nn

mse = nn.MSELoss(reduction="mean")
ce = nn.CrossEntropyLoss(label_smoothing=0.0, ignore_index=None)

Parameters & Forward

ClassConstructor ParametersForward SignatureDescription
MSELossreduction="mean" ("mean" or "sum")forward(pred: Tensor, target: Tensor) -> TensorMean (or sum) squared error between pred and target.
CrossEntropyLosslabel_smoothing=0.0, ignore_index=Noneforward(logits: Tensor, targets) -> TensorFused N9 log-softmax + NLL loss + dLogits backward pass. Accepts raw unnormalized logits and integer class indices targets.

Usage Example

import numpy as np
import ochreml.nn as nn

# Classification with label smoothing and padding token ignored
criterion = nn.CrossEntropyLoss(label_smoothing=0.05, ignore_index=-100)
logits = nn.Tensor.randn([4, 10], requires_grad=True)
targets = np.array([2, 7, -100, 0], dtype=np.int64)

loss = criterion(logits, targets)
loss.backward()
print("Cross-entropy loss:", loss.item())

On this page