TechByteByByte

Loss Functions

Understand what a loss function is, why models need one, MSE, MAE, and cross-entropy, and how a model actually knows its prediction was wrong — connecting directly to neural network and LLM training.

#Machine Learning#AI#Loss Functions#Cross-Entropy#Neural Networks#LLM Training

Begin with the central question

How does a model receive one number that tells it how bad its current predictions are?

This question explains why Loss Functions deserves its own lesson. Each definition, calculation, and code example below will answer a specific part of it.

predictions + targets → per-example errors → loss value

Before you continue: three tools for this module

  • Loss: the numerical training objective to minimize.
  • Target: the correct answer for an example.
  • Outlier: an unusually extreme example that may dominate some losses.

You do not need to memorize these yet. Return to this small map whenever a term reappears.


What You Will Understand

  • Quantifying Loss: Master how loss functions serve as the model’s feedback loops, turning prediction errors into a single, optimizable numeric value.
  • MSE vs. MAE: Compare Mean Squared Error and Mean Absolute Error for regression, understanding how outliers dictate which metric is appropriate.
  • Cross-Entropy Loss: Dive into Binary and Multiclass Cross-Entropy, learning why they are the standard for classification, token prediction, and LLM fine-tuning.

A loss function turns model error into a training signal:

input → model prediction ─┐
                          ├→ loss number → gradient → parameter update
label / target ───────────┘

Training minimizes the chosen loss, so the loss must reward behaviour aligned with the task. Loss is optimized on batches during training; an evaluation metric is selected to communicate real-world quality.


Why Training Needs a Numerical Definition of Error

Every training process in this course so far has referenced “adjusting parameters to reduce error” — but how does a model quantify “error” in the first place? A loss function exists to answer exactly this: it’s a mathematical formula that takes a model’s prediction and the true label, and outputs a single number representing how wrong that prediction was.

Without a loss function, there’s no well-defined signal for training to actually optimize against.


A Coach Who Measures Each Attempt

A loss function is like a strict, consistent judge scoring an archer’s shots. Each individual shot (prediction) gets scored based on how far it landed from the bullseye (the true label) — and the archer (the model) adjusts their technique specifically to minimize that score across many shots. Without a scoring system, the archer would have no concrete signal for what “getting better” even means.


4. Core Concept

TermDefinition
Loss functionA formula measuring how wrong a single prediction is, compared to the true label
MSE (Mean Squared Error)Average squared difference between predictions and true labels — used for regression
MAE (Mean Absolute Error)Average absolute (unsquared) difference between predictions and true labels — used for regression
Log loss / Binary cross-entropyLoss for binary classification, penalizing confident wrong predictions heavily
Categorical cross-entropyLoss for multiclass classification, generalizing binary cross-entropy to many classes

MSE vs. MAE, concretely

MSE = average of (prediction - true_label)²
MAE = average of |prediction - true_label|

🧠 Key difference: MSE squares the error, so large errors are penalized disproportionately more than small ones — one prediction that’s off by 10 contributes 100 to MSE’s sum, while ten predictions each off by 1 only contribute 10 total. MAE treats every unit of error equally, regardless of magnitude — more robust to a few large outlier errors, since it doesn’t amplify them the way squaring does.

# Build a small, inspectable example of Loss Functions.
# Follow the data, learned values, predictions, and evaluation in order.
predictions = [10, 20, 100]   # one prediction (100) is way off
true_labels = [12, 22, 30]

mse_errors = [(p - t) ** 2 for p, t in zip(predictions, true_labels)]
mae_errors = [abs(p - t) for p, t in zip(predictions, true_labels)]

print("MSE:", sum(mse_errors) / len(mse_errors))   # heavily dominated by the big miss
print("MAE:", sum(mae_errors) / len(mae_errors))   # more balanced across all errors

Cross-entropy, concretely

Binary cross-entropy for ONE example:
loss = -(true_label × log(predicted_probability) +
         (1 - true_label) × log(1 - predicted_probability))
  • If true_label = 1 and the model predicted probability = 0.9 (confident and correct): loss is small.
  • If true_label = 1 and the model predicted probability = 0.1 (confident and WRONG): loss is large — the log() term grows sharply as probability approaches 0 for the true class.
# Build a small, inspectable example of Loss Functions.
# Follow the data, learned values, predictions, and evaluation in order.
import numpy as np

def binary_cross_entropy(true_label, predicted_prob):
    return -(true_label * np.log(predicted_prob) + (1 - true_label) * np.log(1 - predicted_prob))

print(binary_cross_entropy(1, 0.9))    # confident + correct -> low loss
print(binary_cross_entropy(1, 0.1))    # confident + WRONG -> high loss
print(binary_cross_entropy(1, 0.5))    # uncertain -> moderate loss

Expected Output:

0.105
2.303
0.693

5. How It Works — Step by Step

1. Model makes a prediction for a training example
2. Compare the prediction to the TRUE label using the chosen loss function
3. Compute the loss VALUE for this example (or averaged across a batch)
4. Use this loss to compute GRADIENTS — how much and in which
   direction each parameter should change to REDUCE the loss
   (this is gradient descent, covered fully in Module 14)
5. Adjust parameters slightly in that direction
6. Repeat across all training examples, many times over
7. Training is considered complete when loss stops meaningfully
   decreasing (or a stopping criterion, like Module 16's early
   stopping, is reached)

🧠 The absolutely central question this answers: “How does a model know that its prediction was wrong?” — it doesn’t “know” in any human sense; the loss function is the entire mechanism. A lower loss value literally is the model’s operational definition of “better,” and training is nothing more than a search process for parameters that minimize this one number.


6. Mathematical Intuition

Read the mathematics as a story

predictions + targets → per-example errors → loss value

First identify the input, the operation, and the output. Then read the symbols as a shorter way to describe that same journey; do not begin by memorizing the formula.

Why does cross-entropy use log() specifically?

log(1.0)  = 0       →  perfectly confident + correct = zero loss
log(0.5)  ≈ -0.69    →  uncertain = moderate loss
log(0.01) ≈ -4.6     →  confident + wrong = very high loss (steep penalty)

The log() function’s shape is exactly what creates the “confident wrong answers are punished much more severely” behavior described in Section 4 — as predicted probability for the true class approaches 0, log() approaches negative infinity, creating an increasingly steep penalty.

This isn’t an arbitrary design choice — it directly encodes the idea that a model should be appropriately humble (less confident) when it’s less certain, rather than confidently wrong.


7. Small Worked Example

Walk through the example

  1. Identify what each input number represents.
  2. Follow one operation at a time and keep the units or class meanings attached.
  3. Translate the result back into an ordinary sentence about the original problem.

The goal is not merely to obtain the answer; it is to expose the model’s decision process.

A spam classifier makes three predictions:

True label (1=spam)Predicted probabilityCross-entropy loss (approx.)
10.95Low (0.05) — confident and correct
10.55Moderate (0.60) — barely correct, low confidence
10.05Very high (3.00) — confident and WRONG

Notice the type of error matters, not just whether the final 0.5-threshold decision was right or wrong — a barely-correct prediction (0.55) is penalized much less than a confidently wrong one (0.05), even though a simple “right/wrong” accuracy check would only see one as an outright mistake.


8. Python Example

What the code will demonstrate

The following Loss Functions code turns the worked example into an experiment you can repeat. First predict the result; then prepare the small dataset, apply the technique, inspect the important intermediate values, and compare the actual output with your prediction.

Python and library symbols used below

  • NumPy (np) stores and calculates with numeric arrays.
  • pandas (pd) represents table-shaped data when it is used.
  • scikit-learn provides tested implementations with a consistent .fit(...) and .predict(...) workflow.
# Build a small, inspectable example of Loss Functions.
# Follow the data, learned values, predictions, and evaluation in order.
import numpy as np
from sklearn.metrics import mean_squared_error, mean_absolute_error, log_loss

# --- Regression: comparing MSE and MAE with an outlier present ---
true_values = np.array([100, 105, 110, 300])       # one genuine outlier (300)
predictions_a = np.array([98, 106, 112, 200])        # model A: mostly close, big miss on outlier
predictions_b = np.array([90, 95, 100, 290])         # model B: consistently off by ~10

print("Model A - MSE:", mean_squared_error(true_values, predictions_a))
print("Model A - MAE:", mean_absolute_error(true_values, predictions_a))
print("Model B - MSE:", mean_squared_error(true_values, predictions_b))
print("Model B - MAE:", mean_absolute_error(true_values, predictions_b))

# --- Classification: cross-entropy / log loss ---
true_labels = [1, 1, 0, 0]
confident_correct = [0.95, 0.90, 0.05, 0.10]     # mostly right, and confident
confident_wrong    = [0.10, 0.15, 0.90, 0.85]     # mostly WRONG, and confident

print("\nConfident + correct - log loss:", log_loss(true_labels, confident_correct))
print("Confident + WRONG - log loss:", log_loss(true_labels, confident_wrong))

Expected Output (approximate):

Model A - MSE: 2512.0
Model A - MAE: 26.0
Model B - MSE: 200.0
Model B - MAE: 10.0

Confident + correct - log loss: 0.108
Confident + WRONG - log loss: 2.211

How It Works

  • Model A has a much lower MAE than Model B’s error pattern might suggest at first glance, but its MSE is far higher than Model B’s — because Model A’s one large outlier miss (100 units off) gets squared, dominating the MSE total, while Model B’s consistent small errors don’t get amplified the same way. This concretely demonstrates Section 4’s “MSE penalizes large errors disproportionately” behavior.
  • The log loss comparison shows exactly how sharply cross-entropy punishes confidently wrong predictions compared to confidently correct ones — over 20x higher loss for the “confident and wrong” scenario.

9. Real-World Example

A company building a house price prediction model must choose between MSE and MAE as its training objective. If a few genuinely unusual, ultra-expensive mansion sales are present in the data, MSE will cause the model to focus disproportionately on getting those extreme cases closer (since their large errors are heavily penalized), potentially at the cost of accuracy on typical houses.

MAE would instead treat every house’s error equally, likely producing a model that’s more consistently accurate on the typical case, at the cost of being further off on the rare extreme cases. This is a genuine, practical business decision — which kind of error matters more? — not just a technical detail.


10. How This Is Used in AI

From mechanism to product

LLMs commonly train with token-level cross-entropy, while production usefulness requires additional evaluations. A lower training loss does not guarantee factual, safe, or helpful responses.

How this connects to LLMs

request → data or context preparation → model computation → evaluated output

An LLM may use this idea during training, or an AI application may use a separate ML component around the LLM. Those are different locations in the system, and the explanation below identifies which one applies.

🤖 How Is This Used in AI?

Direct relevance to Agentic AI: High. This is one of the most directly connected ML concepts to how modern AI models are actually trained.

Loss functionWhere it’s used in AI
Cross-entropyThe core loss function used to train LLMs — at every training step, the model predicts a probability distribution over its entire vocabulary for “what’s the next token?”, and cross-entropy measures how far that distribution is from the actual next token in the training text
MSEUsed in training reward models (regression-style scoring) in RLHF, and various regression-style scoring tasks embedded in AI pipelines
Contrastive loss (a related, more advanced concept)Used to train embedding models specifically — pulling similar items’ embeddings closer together and pushing dissimilar items’ embeddings apart (Module 18 references this)

🧠 Token prediction, explicitly connected to cross-entropy: when an LLM is being pretrained (Module 2’s self-supervised learning), at each position in a training sentence, the model outputs a probability distribution across its entire vocabulary (often 50,000+ possible next tokens).

Cross-entropy loss compares this predicted distribution against the actual next token (which gets a “true probability” of 1, and every other token in the vocabulary gets 0) — exactly the same mathematical mechanism from Section 4, just applied across tens of thousands of possible classes instead of two.

LLM at one position in training text

Outputs probability distribution over ~50,000 vocabulary tokens

Cross-entropy loss compares this distribution to the ACTUAL next token

Gradient descent (Module 14) adjusts billions of parameters
to make the correct token more probable next time

Repeated across every token, in every sentence,
across the entire training corpus

This is genuinely the entire training mechanism — nothing more conceptually exotic than the binary cross-entropy example in Section 4, scaled up enormously in vocabulary size, data volume, and parameter count.


11. How This Is Used in Agentic AI

Trace one agent step

goal + state → model proposes → runtime validates → tool or response → evaluation

The model produces a prediction or proposal. The agent runtime is ordinary software that manages tools, permissions, state, retries, and execution; it may use this ML concept directly, indirectly through an LLM, or not at all.

🤖 Any reward model used in RLHF-style agent training (scoring how good a given agent action or response is) is fundamentally trained using a loss function — typically a regression-style loss (like MSE) if predicting a continuous quality score, or cross-entropy if trained on pairwise preference comparisons (“response A is better than response B”).

When evaluating or fine-tuning agent behavior using any learned scoring component, that component’s training — and therefore its reliability and failure modes — is governed by exactly the loss function principles covered in this module.


12. Common Beginner Mistakes

⚠️ Mistake

Incorrect idea: Using MSE for classification, or cross-entropy for regression

Why it is incorrect: These loss functions are built for fundamentally different kinds of outputs (continuous numbers vs. probability distributions over categories) — using the wrong one produces a poorly-behaved, mismatched training signal.

⚠️ Mistake

Incorrect idea: Assuming a lower loss value always means a “better” model in every practical sense

Why it is incorrect: Loss is a proxy for what you actually care about (accuracy, business value, user satisfaction) — Module 17 covers cases where a model can have excellent loss/accuracy numbers while still being practically useless for the real task.

⚠️ Mistake

Incorrect idea: Not considering which loss function’s error-weighting behavior actually matches the business problem

Why it is incorrect: As Section 9 shows, MSE vs. MAE isn’t just a technical detail — it encodes a real judgment call about how much large errors should matter relative to small ones, which should be a deliberate decision, not a default.


13. Important Distinctions

MSEMAE
Squares errors — penalizes large errors much more heavilyTreats all error magnitudes proportionally, more robust to outliers
Smoother gradients, often easier to optimizeCan have less smooth gradients near zero error
Sensitive to outliersLess sensitive to outliers
Loss FunctionModel Performance Metric (Module 17)
Used DURING training, to guide parameter updatesUsed AFTER training, to evaluate/report final performance
Often mathematically “smooth” for optimization purposes (e.g., cross-entropy)Often more directly interpretable for humans (e.g., accuracy, precision)
Example: cross-entropyExample: accuracy, F1 score

14. When Should You Use This?

  • MSE: the default choice for regression when large errors should be penalized more heavily, and you don’t have significant outlier concerns (or you specifically want the model to prioritize reducing large errors).
  • MAE: when your regression data has significant outliers you don’t want to disproportionately influence training, or when all error magnitudes should genuinely be treated as equally important.
  • Cross-entropy (binary or categorical): the standard, near-universal choice for classification tasks, including the token-prediction objective underlying LLM training.

15. When Should You NOT Use This?

  • Don’t use a plain regression loss (MSE/MAE) for a task that’s actually classification in disguise (e.g., predicting a 1-5 star rating might seem like regression, but is sometimes better modeled as ordinal classification, depending on the specific problem).
  • Don’t assume the “standard” loss function is automatically right for every business context — as shown in Section 9, the choice between MSE and MAE has real, practical business implications that deserve explicit consideration, not blind default usage.

16. Production Considerations

  • Loss during training vs. metrics for reporting — teams should track both: loss to monitor and debug the training process itself, and separate, more human-interpretable metrics (Module 17) to actually evaluate and report on real-world model quality.
  • Loss curves as a diagnostic tool — plotting training loss and validation loss over time is one of the most practical tools for spotting overfitting in real time (Module 6): if training loss keeps dropping while validation loss starts rising, that’s the overfitting signature, visible directly in the loss curves during training.
  • Numerical stability — cross-entropy’s log() term can become numerically unstable near probability 0 or 1; production ML frameworks handle this internally, but it’s worth being aware this is a real engineering concern, not just a theoretical one.

17. AI Engineer Takeaway

🎯 AI Engineer Takeaway: A loss function is the single number a model is mathematically optimizing to reduce — it’s the entire, literal definition of “wrong” that training operates against, and every choice of loss function encodes a real judgment about which kinds of errors matter more.

This isn’t an abstract detail: cross-entropy, specifically, is the exact loss function powering LLM pretraining at the token level — every single token an LLM was ever trained to predict was scored using precisely this mechanism, just at a scale of billions of tokens and parameters rather than the small examples in this module.


18. Interview Questions

Basic Questions

Q: What is a loss function, and why does a model need one?

A: A loss function is a formula that measures how wrong a model’s prediction is compared to the true label, producing a single number. It’s essential because training is fundamentally a search for parameter values that minimize this number — without a loss function, there’s no concrete, mathematical definition of “better” for the training process to optimize toward.

Q: What’s the difference between MSE and MAE?

A: MSE (Mean Squared Error) squares each prediction’s error before averaging, which penalizes large errors disproportionately more than small ones and makes it more sensitive to outliers. MAE (Mean Absolute Error) averages the absolute value of each error, treating all error magnitudes proportionally and making it more robust to a few large outlier errors.

Intermediate Questions

Q: Why does cross-entropy loss penalize confident wrong predictions much more heavily than uncertain wrong predictions?

A: Because of the mathematical shape of the logarithm function used in its formula: as a model’s predicted probability for the true class approaches 0, the loss grows sharply toward infinity, while a prediction closer to uncertain (like 0.5) produces a much more moderate loss even if it’s also technically “wrong” by the final decision threshold. This behavior encourages models to express appropriate uncertainty rather than being confidently incorrect.

Q: How is cross-entropy loss used in training an LLM, at a mechanical level?

A: At each position in a training text sequence, the LLM outputs a probability distribution across its entire vocabulary, representing its prediction for the next token. Cross-entropy loss compares this predicted distribution to the actual next token that appeared in the real training text (which is treated as the “correct” class), producing a loss value that gradient descent then uses to adjust the model’s parameters, making correct predictions more likely in the future. This process repeats across every token position, in every training example, across the entire training corpus.

Scenario-Based Questions

Q: A team is training a model to predict delivery time in minutes. They notice that switching from MSE to MAE as the loss function significantly changed the model’s typical predictions — MAE-trained predictions are systematically a bit further from the true value on “normal” deliveries, but much closer on a small number of unusually long deliveries. What’s happening, and which loss function should they choose?

A: Thought process: This is a direct, practical illustration of the outlier-sensitivity difference between MSE and MAE described throughout this module, now showing up as a real trade-off in a real model’s behavior.

Investigation: The MSE-trained model was likely disproportionately influenced by the unusually long deliveries (outliers) during training, because their large errors were squared and heavily weighted in the loss — pulling the model’s predictions closer to those extreme cases, at the cost of slightly worse accuracy on the far more common “normal” deliveries. The MAE-trained model treated every delivery’s error equally, resulting in more consistent accuracy on typical cases, at the cost of being further off on the rare long-delivery outliers.

Correct answer: The right choice depends entirely on the actual business priority: if getting typical, everyday deliveries as accurate as possible matters most (likely the majority of customer experience), MAE is the better choice. If accurately predicting the rare-but-important long deliveries matters disproportionately (e.g., because those are the cases that generate customer complaints or need special handling), MSE’s extra sensitivity to large errors might actually be desirable, not a flaw.

Production consideration: This is a good example of a modeling decision that shouldn’t be made purely on a technical/mathematical basis — it needs input from the actual business stakeholders about which type of error genuinely costs more in practice, since the “objectively correct” choice of loss function is inseparable from what the business actually cares about.


Next: Module 14 — Optimization and Gradient Descent — how a model actually learns, step by step, connecting directly to neural network and LLM training.

Author
TechByteByByte Editorial Team
Reviewed by
TechByteByByte Admin
Published
Last reviewed