← AI Terminology
Cost Function
A cost function (also called a loss function or objective function) is a mathematical function that quantifies how wrong a model's predictions are — the single number that training seeks to minimise.
The choice of cost function defines what "better" means for the model.
The choice of cost function defines what "better" means for the model.
Why It Matters in AI
The cost function is the specification of the task: change it and you train a completely different model. MSE produces regression models that penalise large errors quadratically; cross-entropy trains probabilistic classifiers; RLHF reward signals are cost functions that encode human preferences. A poorly chosen cost function leads to reward hacking — the model optimises the metric rather than the actual goal.
Key Points
| Aspect | Description |
|---|---|
| Custom | RLHF reward models, perceptual loss (image generation), DICE loss (segmentation) |
| Generation | Negative log-likelihood of next token — the universal LLM pre-training objective |
| Regression | MSE (mean squared error), MAE (mean absolute error), Huber loss (robust to outliers) |
| Contrastive | InfoNCE, triplet loss — used in embedding and contrastive learning |
| Classification | Cross-entropy (standard), focal loss (class imbalance), hinge loss (SVM-style) |
| Regularisation | Cost function often includes regularisation term: total_loss = task_loss + λ × penalty |
Simple Analogy
If a GPS navigates by minimising "time to destination," it will choose fast roads even if you want scenic ones. If you change the cost function to "scenic value per minute," you get a different route. The cost function is your compass — it determines what the optimiser is actually working toward.
Common Usage Examples
nn.CrossEntropyLoss()— standard classification loss in PyTorch (softmax + NLL combined)nn.MSELoss()— regression;nn.L1Loss()— more robust to outlierstorch.nn.functional.binary_cross_entropy_with_logits— binary classification- Focal loss:
FL = -(1 − p_t)^γ × log(p_t)— downweights easy examples, focuses on hard ones - Custom RLHF reward:
reward = quality_score − kl_penalty_from_base_model
Summary
In short: The cost function is the single number training minimises — choose it wisely, because the model will optimise exactly what you specify, not what you intended.