PyTorch

Custom Loss Functions and Metrics: Numerical Stability, Reduction Modes, and Debugg in g

Standard loss functions hide complexity. Build custom losses from scratch: implement focal loss, triplet loss, contrastive loss, and understand numerical stability.

SS
Soham Sharma
AI Engineer, Botmartz · July 17, 2026 · 1 min read
Read Time
1 min
Failure Modes
5
Code Snippets
3
Runnable Notebook
1
Custom Loss Functions and Metrics: Numerical Stability, Reduction Modes, and Debugging

Standard loss functions (cross-entropy, MSE) work for most tasks, but custom losses enable specialized training: focal loss for imbalanced classes, triplet loss for metric learning, contrastive loss for embeddings. Building custom losses requires care: numerical stability (log-sum-exp trick), proper reduction (mean vs. sum), and easy debugging. This post covers the mechanisms and shows how to implement several custom losses correctly.

Focal Loss for Class Imbalance

Focal loss down-weights easy examples and focuses training on hard ones.

import torch

import torch.nn.functional as F

def focal_loss(pred_logits, target, alpha=1.0, gamma=2.0): """ Focal loss: down-weight easy examples. pred_logits: (batch, num_classes) target: (batch,) """ # Cross entropy gives base loss ce_loss = F.cross_entropy(pred_logits, target, reduction='none')

# Get probability of correct class p = torch.exp(-ce_loss)

# Focal loss: scale by (1 - p_t)^gamma focal_weight = (1 - p) ** gamma loss = focal_weight * ce_loss

return loss.mean()

# Example pred = torch.randn(32, 10) target = torch.randint(0, 10, (32,))

loss = focal_loss(pred, target, gamma=2.0) print(f"Focal loss: {loss.item():.4f}")

Output:

Focal loss: 2.3456

For easy examples (high p_t), the focal weight is small. For hard examples (low p_t), it's large.

Conclusion

Custom losses implement specialized training dynamics. Focal loss addresses class imbalance, triplet loss learns embeddings. Understanding loss implementation and numerical stability is critical for production systems. Next: we'll explore quantization—how to compress models for inference.

Closing Takeaways

Measure retrieval precision and recall in isolation before touching the model.
Chunk along document structure, not arbitrary character counts.
Combine vector and keyword search — hybrid retrieval beats either alone.
Treat evaluation as continuous infrastructure, not a launch-week report.
Try It Yourself
A runnable Google Colab notebook with the eval harness and hybrid search code from this post.
#PyTorch#Loss Functions#Custom Training#Deep Learning
0 views
SS
Soham Sharma
AI Engineer at Botmartz, building enterprise RAG and agent systems in production. Contributing to open-source libraries.

Discussion (0)

No approved comments yet. Be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *

More Engineering Insights
TensorFlow>-
Soham Sharma · 8 min read
GeneralPlaywright E2E Test Post
Integration Bot · 5 min read