loss-functions
loss-functions
Comprehensive Notes
Introduction to Loss Functions
A loss function, also known as a cost function or objective function, measures the difference between predicted values
and actual values in a machine learning model. It quantifies how well a model performs and provides the basis for model
optimization.
Properties:
Range: [0, ∞)
Perfect prediction: 0
Heavily penalizes confident wrong predictions
Requires predicted probabilities
Implementation:
Properties:
Implementation:
L(y, ŷ) = max(0, 1 - y * ŷ)
where:
- y: true label (-1 or 1)
- ŷ: predicted score
Properties:
Properties:
Implementation:
Properties:
Implementation:
3. Huber Loss
Combines MSE and MAE
Formula:
L(y, ŷ) = {
0.5(y - ŷ)² if |y - ŷ| ≤ δ
δ|y - ŷ| - 0.5δ² otherwise
}
Properties:
Robust to outliers
Differentiable everywhere
Adjustable sensitivity (δ)
Range: [0, ∞)
Implementation:
Properties:
3. Triplet Loss
Used in metric learning
Formula:
class CustomLoss:
def __init__(self, weights):
self.weights = weights
2. Regression Tasks
General purpose: MSE
Outlier robust: MAE or Huber
Custom requirements: Combined loss
3. Special Cases
Metric learning: Contrastive/Triplet Loss
Generative models: Custom losses
Multi-task learning: Weighted combinations
Practical Considerations
1. Numerical Stability
Add small epsilon to logs
Clip prediction ranges
Use stable implementations
Monitor for NaN values
3. Gradient Properties
Check gradients magnitude
Monitor gradient flow
Implement gradient clipping
Use appropriate optimizers
Best Practices
1. Loss Function Selection
2. Implementation
3. Debugging
2. Exploding Gradients
Clip gradient norms
Scale loss appropriately
Use stable implementations
Monitor loss values
3. Class Imbalance
Use weighted losses
Implement focal loss
Balance dataset
Adjust class weights
Conclusion
Choosing and implementing appropriate loss functions is crucial for:
1. Model performance
2. Training stability
3. Convergence speed
4. Robustness to outliers
5. Handling specific problem requirements