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
| Class | Constructor Parameters | Forward Signature | Description |
|---|---|---|---|
MSELoss | reduction="mean" ("mean" or "sum") | forward(pred: Tensor, target: Tensor) -> Tensor | Mean (or sum) squared error between pred and target. |
CrossEntropyLoss | label_smoothing=0.0, ignore_index=None | forward(logits: Tensor, targets) -> Tensor | Fused 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())