Cost Functions: The Navigation System of AI
Cost functions (also known as Objective Functions) are the mathematical metrics that evaluate how "wrong" a neural network's predictions are. They act as the compass for the optimization algorithm, defining the "surface" that the network must navigate to find the optimal weights.
Loss vs. Cost: A Nuanced Difference
While often used interchangeably, there is a technical distinction:
- Loss Function (): Refers to the error of a single training example.
- Cost Function (): Refers to the average loss over the entire training set (or a mini-batch).
1. Regression Losses
Mean Squared Error (MSE / L2 Loss)
The standard for regression. It penalizes large errors more heavily due to the squaring term.
- Best for: Data where outliers are rare or errors should be penalized exponentially.
Mean Absolute Error (MAE / L1 Loss)
Calculates the average of absolute differences. It is more robust to outliers than MSE.
Huber Loss
The best of both worlds. It behaves like MSE when the error is small and like MAE when the error is large (defined by a threshold ).
2. Classification Losses
Binary Cross-Entropy (Log Loss)
Used for binary classification. It is mathematically derived from Maximum Likelihood Estimation.
- Pro Tip: We use the logarithm because it makes the cost function convex when combined with Sigmoid, ensuring that Gradient Descent can find the global minimum.
Categorical Cross-Entropy
The multi-class generalization of Log Loss, typically used with a Softmax output layer.
3. Advanced SOTA Losses
Focal Loss
Introduced in the RetinaNet paper, Focal Loss is designed to handle extreme class imbalance (e.g., detecting a small object in a large image). It down-weights "easy" examples and focuses training on "hard" negatives.
- Why it's Pro: It allows models to learn from rare classes without being overwhelmed by the background/majority class.
Summary Table
| Loss Function | Problem Type | Robust to Outliers? | Key Property |
|---|---|---|---|
| MSE | Regression | No | Smooth gradient, penalizes big errors |
| MAE | Regression | Yes | Linear penalty, stable for outliers |
| BCE | Binary Class | N/A | Standard for probabilities |
| CCE | Multi-class | N/A | Works with Softmax distributions |
| Focal Loss | Imbalanced Class | N/A | Focuses on "hard" examples |
Python Implementation
import numpy as np
class CostFunctions:
"""
A robust collection of loss and cost functions for neural network training.
"""
@staticmethod
def mse(y_true: np.ndarray, y_pred: np.ndarray) -> float:
return np.mean(np.square(y_true - y_pred))
@staticmethod
def mae(y_true: np.ndarray, y_pred: np.ndarray) -> float:
return np.mean(np.abs(y_true - y_pred))
@staticmethod
def binary_cross_entropy(y_true: np.ndarray, y_pred: np.ndarray) -> float:
# Numerical stability: clip predictions
y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
return -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))
@staticmethod
def categorical_cross_entropy(y_true: np.ndarray, y_pred: np.ndarray) -> float:
y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
return -np.mean(np.sum(y_true * np.log(y_pred), axis=-1))
@staticmethod
def focal_loss(y_true: np.ndarray, y_pred: np.ndarray, gamma: float = 2.0, alpha: float = 0.25) -> float:
"""
Implementation of Focal Loss for addressing class imbalance.
"""
y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
bce = -(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))
# Calculate the 'modulating factor'
p_t = (y_true * y_pred) + ((1 - y_true) * (1 - y_pred))
modulating_factor = (1.0 - p_t) ** gamma
return np.mean(alpha * modulating_factor * bce)
@staticmethod
def mse_derivative(y_true: np.ndarray, y_pred: np.ndarray) -> np.ndarray:
return 2 * (y_pred - y_true) / y_true.size
If your loss is not decreasing, check if your loss function matches your output activation. MSE pairs with Linear, BCE with Sigmoid, and CCE with Softmax. Mixing them can lead to non-convex surfaces that are nearly impossible to optimize.