Module 4 — Prediction: learning patterns from the past
Lesson 8 of 16
Loss functions
A machine-learning model makes predictions.
But to learn, it needs something more.
It needs a way of answering:
How wrong was that prediction?
That is the role of a loss function.
A loss function converts the difference between:
what the model predicted
and:
what actually happened
into a numerical value.
Conceptually:
PREDICTION
ACTUAL OUTCOME
↓
LOSS FUNCTION
↓
NUMBER REPRESENTING HOW WRONG THE MODEL WAS
The learning algorithm then tries to reduce that number.
This makes the loss function one of the most important design choices in machine learning.
Prediction alone does not tell us how to learn
Suppose we are predicting electricity demand.
Actual demand:
42 GW
Model prediction:
38 GW
We know the model is wrong by:
4 GW.
But what should the learning algorithm do with that information?
Should an error of 4 GW be:
- twice as bad as an error of 2 GW?
- four times as bad?
- much worse if demand was underestimated?
- relatively unimportant during normal conditions?
- extremely important during a grid emergency?
There is no universal answer.
We need to define one.
Loss turns error into an objective
Suppose the actual value is:
y
and the model predicts:
ŷ.
A loss function calculates something like:
L(y, ŷ).
The notation is less important than the idea:
Take the prediction and the actual outcome and calculate how undesirable the difference is.
Training then tries to find parameters that make this loss smaller.
The basic learning loop
We can now expand our learning process:
INPUT
↓
MODEL
↓
PREDICTION
↓
LOSS FUNCTION
↓
LOSS
↓
UPDATE PARAMETERS
↓
NEW MODEL
The loss provides the feedback signal that guides learning.
A simple loss: absolute error
Suppose:
Actual = 42
Prediction = 38
The absolute error is:
|42 - 38| = 4.
If the prediction were:
40
the loss would be:
|42 - 40| = 2.
Smaller error means smaller loss.
The learning algorithm therefore prefers the second prediction.
Mean absolute error
Across many examples, we can calculate the average absolute error.
Suppose the prediction errors are:
2, -3, 1, 4.
Take their absolute values:
2, 3, 1, 4.
Then average them:
(2 + 3 + 1 + 4) / 4 = 2.5.
This gives the Mean Absolute Error, or MAE.
Conceptually:
MAE = average magnitude of prediction error.
Why use absolute values?
Suppose the errors are:
+10
and:
-10.
If we simply average them:
(+10 - 10) / 2 = 0.
That would suggest perfect predictions.
Clearly they were not perfect.
Absolute error prevents positive and negative mistakes from cancelling each other out.
Another loss: squared error
Instead of taking the absolute error, we can square it.
Suppose:
Actual = 42
Prediction = 38.
Error:
4.
Squared error:
4² = 16.
If the error were:
2,
squared error would be:
2² = 4.
If the error were:
10,
squared error would be:
10² = 100.
Large errors become disproportionately expensive.
Mean squared error
Across many observations, we can average these squared errors.
This gives:
Mean Squared Error, or MSE.
Conceptually:
MSE = average squared prediction error.
MSE is extremely common in regression.
MAE and MSE care about errors differently
Consider two errors:
2
and:
10.
Using absolute error:
2 → loss 2
10 → loss 10.
The larger error is:
5 times larger.
Using squared error:
2 → loss 4
10 → loss 100.
The larger error now creates:
25 times the loss.
So MSE tells the learner:
Large mistakes are especially bad.
The choice changes what the model learns
Suppose most predictions are close to correct, but one observation has an enormous error.
With MAE, that observation matters proportionally to its error.
With MSE, it can dominate the total loss.
The same:
- data,
- features,
- model architecture
can therefore produce different learned parameters depending on the loss function.
This is fundamental:
The loss function shapes the model that emerges from training.
Outliers illustrate the difference
Suppose actual values are:
10, 11, 12, 100.
The final observation is unusual.
A model trained using squared error may shift substantially to reduce the large error associated with:
100.
A model trained using absolute error may be less strongly influenced by it.
Neither choice is automatically correct.
The appropriate choice depends on what the unusual observation represents.
An outlier may be noise
Perhaps:
100
was caused by:
- sensor failure,
- data-entry error,
- corrupted measurement.
In that case, allowing it to dominate training may be undesirable.
Or the outlier may be the most important observation
Perhaps:
100
represents a genuine extreme event:
- electricity demand during extreme weather,
- catastrophic equipment stress,
- severe flooding,
- financial crisis.
Then dismissing it as an inconvenient outlier could be disastrous.
Rare does not mean irrelevant.
Loss functions encode what matters
This is the deeper lesson.
Suppose two models make these errors:
Model A
Usually accurate, but occasionally makes enormous mistakes.
Model B
Makes small errors more frequently, but rarely makes enormous mistakes.
Which is better?
There is no answer until we decide:
What kinds of errors matter to us?
The loss function provides a mathematical answer to that question.
Loss is not the same as reality
A model does not directly understand:
- inconvenience,
- money,
- injury,
- fairness,
- reliability,
- social harm.
It sees the numerical objective we give it.
We therefore translate something we care about in the real world into a mathematical loss.
Conceptually:
REAL-WORLD OBJECTIVE
↓
MATHEMATICAL LOSS FUNCTION
↓
LEARNING
This translation is a design decision.
Classification needs loss functions too
Suppose we classify emails as:
SPAM
or:
NOT SPAM.
The model might output:
Probability of spam = 0.9.
If the email really is spam, that is a good prediction.
If it is not spam, the model was confidently wrong.
A classification loss should capture that distinction.
Accuracy alone is difficult to learn from
Suppose a model predicts:
49% probability of spam.
We classify anything above:
50%
as spam.
The result is:
NOT SPAM.
Now the model changes slightly:
51% probability of spam.
The classification suddenly changes to:
SPAM.
But the underlying model prediction changed by only two percentage points.
A simple correct/incorrect score throws away information about how confident the prediction was.
Cross-entropy loss
A common classification loss is cross-entropy.
We do not need the full mathematics yet.
The important idea is:
Cross-entropy rewards assigning high probability to the correct outcome and strongly penalises confident predictions that are wrong.
Suppose the actual outcome is:
SPAM.
Compare:
Model A: 90% spam
Model B: 55% spam
Both might classify the email correctly.
But Model A assigned much greater probability to the observed outcome.
Cross-entropy distinguishes between them.
Confidently wrong predictions are expensive
Suppose the email is actually:
NOT SPAM.
Compare:
Model A: 51% spam
and:
Model B: 99.9% spam.
Both make the wrong classification.
But Model B was extraordinarily confident.
Cross-entropy penalises this much more heavily.
This encourages probabilistic models not merely to:
choose the right class
but to:
assign sensible probabilities.
Loss can encourage calibration
Recall our earlier discussion of calibration.
If a model repeatedly says:
80% probability,
we would like the predicted event to occur roughly:
80% of the time
under comparable conditions.
Appropriate probabilistic loss functions can encourage models to produce meaningful probabilities rather than simply confident classifications.
Not all classification mistakes are equally serious
Suppose we are detecting cancer.
There are two important errors:
False positive
The model predicts cancer when the patient does not have cancer.
False negative
The model predicts no cancer when the patient does have cancer.
These errors have different consequences.
Accuracy treats them symmetrically
A simple accuracy metric might count:
one false positive = one mistake
and:
one false negative = one mistake.
But medically, the consequences may differ substantially.
So we may want a loss function that assigns different costs.
Asymmetric loss
Suppose we decide:
False positive → cost 1
False negative → cost 10.
Now the learning system has a stronger incentive to avoid false negatives.
Conceptually:
TYPE OF ERROR
↓
DIFFERENT LOSS
This is an example of an asymmetric loss function.
The asymmetry should come from the problem
There is no universal rule that false negatives should always cost more.
Consider spam filtering.
False positive
A legitimate email is hidden as spam.
False negative
A spam email reaches the inbox.
Here, a false positive might be much more damaging.
The correct loss depends on the service being designed.
Autonomous vehicles make the point obvious
Suppose an autonomous vehicle must determine whether something in front of it is:
a pedestrian
or:
not a pedestrian.
A false positive might cause unnecessary braking.
A false negative could cause a collision.
Treating both errors as equivalent would make little sense.
Electricity systems provide another example
Suppose we forecast demand.
An error of:
-1 GW
means demand was underestimated.
An error of:
+1 GW
means demand was overestimated.
Mathematically, their absolute magnitude is identical.
Operationally, they may have different consequences.
Underestimating demand during scarcity might create much greater system risk.
Context can change the cost of the same error
Suppose electricity demand is underestimated by:
2 GW.
At 03:00 on a mild spring night, the system has enormous spare capacity.
The consequence may be minor.
At 18:00 during an extreme winter peak, the same:
2 GW error
could be critical.
So the real cost of error may depend on:
system state.
State-dependent loss
Conceptually:
LOSS = function of prediction error + current state.
The same numerical prediction error can have different importance depending on:
- time,
- location,
- scarcity,
- system condition.
This is important in real cyber-physical systems.
Spatial errors can also have different consequences
Suppose we predict electricity demand across a network.
An error of:
1 MW
at one location may be harmless.
The same error at a heavily constrained part of the network may contribute to:
- congestion,
- voltage problems,
- equipment overload.
So:
where the error occurs
can matter as much as:
how large the error is.
Loss can therefore depend on time and space
Instead of treating every prediction equally:
ERROR → LOSS
we might have:
ERROR + TIME + LOCATION + SYSTEM STATE
↓
LOSS.
This creates a much richer connection between machine learning and the physical systems in which predictions are used.
Weighted loss functions
Suppose some examples are more important than others.
We can assign weights.
For example:
ordinary period → weight 1
high-demand period → weight 5
emergency period → weight 20.
Then errors during critical periods contribute more strongly to training.
Class imbalance
Suppose fraud occurs in:
0.1% of transactions.
A model that predicts:
NOT FRAUD
for every transaction would be:
99.9% accurate.
But it would detect:
zero fraud.
Clearly, accuracy alone is a terrible objective.
Weighted classification loss
We could give fraud examples more weight.
For example:
normal transaction → weight 1
fraud transaction → weight 100.
Now missing fraud has a much greater influence on the training objective.
This can help the model pay attention to rare but important cases.
Loss and evaluation metrics are not always the same
We need an important distinction.
A model might be:
trained using one loss function
but:
evaluated using several metrics.
For example:
TRAINING LOSS
Cross-entropy
but evaluate using:
- accuracy,
- precision,
- recall,
- calibration.
Why?
Because some useful evaluation metrics are difficult to optimise directly.
Training requires a useful optimisation signal
The learning algorithm needs a loss that changes smoothly enough to tell it:
Which direction should the parameters move?
A metric like simple accuracy can be too coarse.
Cross-entropy provides richer information about how predictions should change.
Loss creates a landscape
Recall parameter space.
Every possible set of parameters produces predictions.
Those predictions produce a loss.
So:
PARAMETERS
↓
PREDICTIONS
↓
LOSS.
Imagine every parameter configuration having a height corresponding to its loss.
This creates a:
LOSS LANDSCAPE.
Training searches for lower loss
The optimiser moves through parameter space.
Conceptually:
HIGH LOSS
↓
ADJUST PARAMETERS
↓
LOWER LOSS
↓
ADJUST AGAIN
↓
LOWER LOSS.
The aim is to find parameters that produce sufficiently low loss.
A valley analogy
Imagine standing somewhere in a mountainous landscape.
Your objective is:
Reach a low point.
Height represents:
loss.
Your current location represents:
parameter values.
Learning means moving through this landscape looking for lower ground.
The loss function creates the landscape
Change the loss function and the landscape changes.
The same parameter configuration may look:
good
under one objective and:
bad
under another.
So the optimiser does not search for:
the best model in some universal sense.
It searches for:
a model that performs well according to the objective we defined.
There may be many low-loss solutions
A complex model can have many parameter configurations that achieve similar loss.
Training may find one of them.
Another training run might find another.
Both may perform similarly on the training data.
But they may behave differently under:
- distribution shift,
- unusual examples,
- adversarial inputs.
Low training loss does not guarantee identical behaviour.
Local minima
A loss landscape may contain many valleys.
A learning algorithm may reach one low region without finding the absolute lowest possible point.
This is called a:
local minimum.
For modern deep learning, the geometry is much more complicated than a simple landscape with isolated valleys, but the analogy remains useful.
The lowest training loss is not necessarily the best model
Suppose:
Model A
Training loss = 0.01
Test loss = 0.50
and:
Model B
Training loss = 0.05
Test loss = 0.08.
Model A fits the training data better.
Model B generalises much better.
Our real objective is not:
minimum training loss at any cost.
It is useful performance on unseen data.
Overfitting appears through loss
During training we might observe:
TRAINING LOSS
continuing downward.
Meanwhile:
VALIDATION LOSS
first falls and then begins rising.
Conceptually:
EARLY TRAINING
Training ↓
Validation ↓
LATER TRAINING
Training ↓
Validation ↑
This is a classic signal of overfitting.
Regularisation modifies the objective
Suppose we want:
low prediction error
but also want to discourage an unnecessarily complex model.
We can add a regularisation term.
Conceptually:
TOTAL LOSS
=
PREDICTION LOSS
COMPLEXITY PENALTY.
Now the optimiser must balance:
fit the data
against:
keep the model controlled.
We are now optimising more than prediction error
This is an important conceptual shift.
The objective might no longer be:
Make predictions as accurate as possible on the training data.
Instead:
Make accurate predictions while satisfying some additional preference about the model.
That preference could involve:
- simplicity,
- robustness,
- fairness,
- energy use,
- safety.
Multi-objective learning
Suppose we care about:
accuracy
and:
fairness.
One possible objective might conceptually look like:
LOSS
=
prediction error
fairness penalty.
Now the model is asked to balance multiple objectives.
But immediately we face another question:
How much accuracy should we trade for how much fairness?
The mathematics cannot answer that by itself.
Someone must choose the relative importance.
Loss functions can encode fairness objectives
Suppose a model performs much worse for one group than another.
We might include a penalty when group performance diverges.
Conceptually:
TOTAL LOSS
=
PREDICTION LOSS
PENALTY FOR UNFAIR DIFFERENCES.
This can influence the parameters the model learns.
But "fairness" must first be defined
Should fairness mean:
- equal accuracy?
- equal false-positive rates?
- equal false-negative rates?
- equal opportunity?
- equal outcomes?
- individual fairness?
Different definitions can conflict.
So writing:
fairness penalty
does not solve the philosophical problem.
It forces us to specify it.
Loss functions expose value judgements
This is why loss functions are so important beyond mathematics.
Before the machine can optimise, somebody has to decide:
- what counts as success,
- what counts as failure,
- whether some failures matter more,
- whether some people or situations require different treatment,
- what trade-offs are acceptable.
These decisions become numbers.
The machine sees numbers, not intentions
Suppose we say:
Build a good recommendation system.
That is not an optimisation objective.
We might translate "good" into:
maximise clicks.
Now the system learns to maximise clicks.
But perhaps what we really meant was:
- provide useful information,
- improve user satisfaction,
- avoid addiction,
- expose diverse perspectives.
Clicks are only a proxy.
Proxy objectives can create unexpected behaviour
Suppose the loss rewards:
engagement.
The model discovers that:
- outrage,
- controversy,
- sensational content
generate more engagement.
The system may become extremely effective at optimising the metric while producing outcomes we did not intend.
This is not necessarily a failure of optimisation.
It may be a failure of objective design.
Goodhart's Law
A useful principle is:
When a measure becomes a target, it can stop being a good measure.
If:
engagement
is used as a proxy for:
user value,
optimising engagement aggressively can break the relationship between the two.
The system learns to maximise the metric rather than the underlying concept we cared about.
Specification problems
This creates a distinction between:
TRUE OBJECTIVE
what we actually want
and:
SPECIFIED OBJECTIVE
what the machine is mathematically told to optimise.
If these differ:
excellent optimisation
can produce:
poor real-world outcomes.
Reward hacking
In more interactive AI systems, this can lead to reward hacking.
The system discovers a way to achieve a high numerical score without accomplishing the intended goal.
For example, imagine telling a cleaning robot:
Maximise the amount of dirt collected.
Perhaps it repeatedly:
drops dirt
and:
collects it again.
The numerical objective increases.
The room does not become cleaner.
The optimisation was successful
This distinction matters.
The system did not necessarily fail to optimise.
It may have optimised exactly what we specified.
The failure occurred because:
our mathematical objective
did not perfectly represent:
our actual intention.
Loss functions are specifications
A useful way to think about a loss function is:
It is part of the specification we give the learning system.
It tells the system what kinds of behaviour should be preferred during training.
That makes loss design part of system design.
A loss function compresses priorities
Imagine a complex real-world service involving:
- accuracy,
- reliability,
- fairness,
- safety,
- cost.
We may attempt to compress those concerns into an objective such as:
L = prediction error + λ₁ safety penalty + λ₂ fairness penalty + λ₃ cost penalty.
The symbols are not important.
The idea is.
We have turned several priorities into one mathematical quantity.
The weights matter enormously
Suppose:
λ₁ = 0.001
for safety.
Safety barely affects optimisation.
Suppose:
λ₁ = 1,000,000.
Safety violations dominate the objective.
The same categories exist in both models.
Their practical priorities are completely different.
Where do the weights come from?
This is often where mathematics meets governance.
Weights might be chosen based on:
- engineering requirements,
- economic costs,
- regulation,
- social preferences,
- safety standards,
- political decisions.
There may be no objectively correct numerical answer.
Some things should be constraints, not losses
Suppose we are designing an autonomous vehicle.
Should we say:
Collisions are allowed, but they have a very large penalty?
Perhaps not.
Some requirements may be better represented as:
hard constraints.
For example:
NEVER exceed a physical safety limit.
This is different from:
try not to exceed it because doing so increases loss.
Soft objectives versus hard constraints
This distinction becomes extremely important.
Soft objective
Prefer lower energy consumption.
We might add an energy penalty to the loss.
Hard constraint
Never exceed maximum equipment temperature.
That may belong in a constraint rather than merely the loss function.
Why not put everything into one loss?
Because trade-offs imply substitutability.
Suppose:
safety violation = +100 loss
and:
profit improvement = -200 loss.
The optimiser could mathematically conclude:
The safety violation is worth it.
If safety is genuinely non-negotiable, it should not necessarily be represented as something that can simply be traded against profit.
Objectives and constraints are different
This leads directly toward our later optimisation modules.
A system might have:
OBJECTIVE
Minimise cost
subject to:
CONSTRAINTS
- maintain safety,
- respect capacity,
- satisfy legal requirements.
The system searches only among feasible solutions.
This is different from assigning every undesirable outcome a price.
Loss functions and decision-making are not the same thing
Recall:
Prediction is not decision-making.
A predictive model might minimise:
forecast error.
But the decision system using that forecast may optimise:
- cost,
- reliability,
- fairness,
- resource allocation.
The prediction loss and decision objective are different things.
A prediction can be slightly worse but more useful
Suppose:
Model A
has lower average forecast error.
But:
Model B
is better specifically during periods of scarcity.
If the forecast is being used to operate an electricity system, Model B may be more valuable.
So:
best predictive metric
does not automatically mean:
best decision-making outcome.
Decision-aware learning
Sometimes we can train models based partly on how their predictions affect downstream decisions.
Instead of asking only:
Was the prediction numerically accurate?
we ask:
Did the prediction lead to a good decision?
This is sometimes called decision-focused or decision-aware learning.
A small error can have a large consequence
Suppose predicted demand is:
99 MW
instead of:
101 MW.
Numerical error:
2 MW.
But imagine a network constraint at:
100 MW.
The two predictions fall on opposite sides of a critical threshold.
The prediction error is small.
The decision consequence may be enormous.
A large error can sometimes have little consequence
Suppose demand is predicted:
50 MW
instead of:
60 MW.
Error:
10 MW.
But available capacity is:
500 MW.
Operationally, the difference may not matter.
So:
prediction error
and:
decision cost
are not necessarily proportional.
The value of prediction depends on the decision
This is a major idea.
A forecast has value because somebody or something uses it.
So we can ask:
What decision changes because this prediction exists?
If two different predictions lead to exactly the same decision, their numerical difference may have little practical importance.
Loss functions can therefore be application-specific
There is no single best loss function for:
AI.
The appropriate objective depends on:
- what is being predicted,
- why it is being predicted,
- what decisions follow,
- who experiences the consequences,
- which failures matter most.
Loss functions for language models
Large language models provide a particularly important example.
During pre-training, a language model is commonly trained to predict:
the next token.
Suppose the text is:
The capital of France is Paris.
The model receives:
The capital of France is
and predicts a probability distribution over possible next tokens.
Next-token loss
Perhaps the model predicts:
Paris → 60%
London → 10%
Berlin → 5%
and many other possibilities.
The actual next token is:
Paris.
The loss function rewards the model for assigning greater probability to the observed token.
Repeat at enormous scale
This happens across:
- sentences,
- documents,
- books,
- websites,
- code.
Conceptually:
CONTEXT
↓
PREDICT NEXT TOKEN
↓
COMPARE WITH ACTUAL TOKEN
↓
CALCULATE LOSS
↓
UPDATE PARAMETERS
↓
REPEAT BILLIONS OR TRILLIONS OF TIMES.
This apparently simple loss drives much of language-model pre-training.
The objective is not directly "understand the world"
This is worth emphasising.
The pre-training loss does not explicitly say:
- understand physics,
- learn history,
- reason logically,
- become creative.
It says something much closer to:
Become better at predicting tokens from context.
Yet doing that extremely well requires learning enormous amounts of structure.
Capabilities can emerge indirectly
To predict text well, a model benefits from learning patterns involving:
- grammar,
- meaning,
- facts,
- relationships,
- code,
- reasoning.
So a relatively simple objective can produce surprisingly rich internal representations.
This is one of the remarkable findings behind modern AI.
But the loss does not directly optimise truth
Suppose a false statement appears frequently in training data.
The next-token objective does not contain a magical mechanism saying:
Ignore this because it is false.
It learns statistical relationships in the data.
This helps explain why language models can produce:
plausible but incorrect outputs.
Hallucination makes more sense through the loss function
A language model is trained to produce probable continuations.
It is not fundamentally trained with an objective:
Never state something unless independently verified to be true.
These are different objectives.
This does not mean hallucination is inevitable at any fixed rate.
But it helps explain why fluent generation and factual reliability are not automatically identical.
Fine-tuning changes the objective
After pre-training, models can be trained further using:
- demonstrations,
- human preferences,
- safety examples,
- specialised datasets.
The objective now changes.
The model is no longer learning solely from next-token prediction over raw text.
Reinforcement learning introduces rewards
In reinforcement learning, we often talk about:
reward
rather than:
loss.
The perspective is reversed.
Loss
smaller is better.
Reward
larger is better.
Conceptually, they can often be transformed into one another.
Reward functions have the same fundamental problem
An agent needs to know:
What behaviour should I prefer?
The reward function answers that question.
So the same issue returns:
Who defines what counts as good?
A game provides a simple reward
Suppose an AI plays chess.
We might define:
win → +1
draw → 0
loss → -1.
The objective is relatively clear.
But many real-world problems do not have such simple outcomes.
Real life has many objectives
Consider an AI managing a hospital.
Should it maximise:
- lives saved?
- average health improvement?
- number of patients treated?
- fairness?
- waiting-time reduction?
- cost efficiency?
These objectives can conflict.
There is no obvious scalar reward that perfectly represents:
run a good hospital.
Loss design becomes social design
Once AI affects shared resources, the question:
What loss should we minimise?
becomes closely related to:
What outcomes should society prioritise?
That cannot be answered by machine learning alone.
Loss functions do not eliminate politics
Suppose someone says:
Let the algorithm decide.
The algorithm still needs:
- data,
- objectives,
- constraints,
- metrics.
Those were designed by people or institutions.
Automation can hide value judgements.
It does not remove them.
A mathematically optimal answer can still be undesirable
Suppose the optimisation problem is:
Minimise average waiting time.
The model finds a policy that dramatically improves average waiting time by systematically deprioritising a small group of difficult cases.
Mathematically:
objective improved.
Socially:
perhaps unacceptable.
The problem may be the objective, not the optimiser.
Average loss can hide distribution
Suppose two systems have identical average loss.
System A
Everyone experiences moderate error.
System B
Most people experience almost no error, but a small group experiences enormous error.
Average performance is identical.
The distribution of harm is not.
Who experiences the loss?
This gives us another important question:
Whose error are we measuring?
A global average can hide:
- groups,
- locations,
- individuals,
- rare conditions.
The aggregation of loss is itself a design choice.
Individual versus aggregate objectives
Suppose ten users experience losses:
1, 1, 1, 1, 1, 1, 1, 1, 1, 20.
Average loss:
2.9.
Another system produces:
3, 3, 3, 3, 3, 3, 3, 3, 3, 3.
Average loss:
3.
The first system has slightly lower average loss.
But one user experiences a loss of:
20.
Which allocation is preferable?
That is not merely a prediction question.
It is a fairness question.
Optimising averages can sacrifice minorities
If a small group contributes little to total loss, the optimiser may have weak incentives to improve their outcomes.
This is especially important when training data is imbalanced.
A mathematically efficient solution can distribute errors very unevenly.
Minimax objectives
One alternative is to care about the:
worst-case loss.
Instead of:
Minimise average error,
we might ask:
Minimise the maximum error experienced by anyone.
This is a minimax perspective.
It prioritises protection against extreme poor outcomes.
Different social objectives produce different models
Consider:
Average loss
Optimise total performance.
Weighted loss
Give some cases more importance.
Minimax loss
Protect the worst case.
Fairness-constrained loss
Limit disparities between groups.
These are not simply technical variations.
They represent different priorities.
Loss and uncertainty
Suppose Model A predicts:
50 MW
with very high uncertainty.
Model B predicts:
51 MW
with very low uncertainty.
If we evaluate only point prediction error, we ignore this distinction.
Probabilistic loss functions can evaluate:
the entire predicted distribution.
Probabilistic forecasts
Instead of saying:
Tomorrow's demand = 50 GW,
a model might say:
10% probability: below 45 GW
60% probability: 45–55 GW
30% probability: above 55 GW.
Now we need a loss function that evaluates whether the predicted distribution was sensible.
Proper scoring rules
A proper scoring rule rewards a model for reporting probabilities that reflect its genuine beliefs.
Examples include:
- log loss,
- Brier score.
The purpose is to discourage models from gaining by deliberately misrepresenting uncertainty.
We will return to these in Module 5.
Brier score
For a binary event, suppose the model predicts:
70% chance of rain.
Rain occurs.
The Brier score considers the difference between:
0.7
and:
1.
If rain does not occur, it compares:
0.7
with:
0.
Repeated across many predictions, this evaluates probabilistic accuracy.
Loss can reward honest uncertainty
Suppose the evidence genuinely supports:
50% / 50%.
A well-designed probabilistic loss should not force the model to pretend:
99% certainty.
Representing uncertainty accurately is valuable.
This returns us to Module 3:
A good model should not merely tell us what it thinks will happen. It should help us understand how certain it is.
Robust loss functions
Sometimes we want a loss that behaves like squared error for ordinary mistakes but becomes less sensitive to enormous outliers.
One example is Huber loss.
The details are less important here than the principle:
Loss functions can be designed to reflect assumptions about the errors we expect.
There is no universally correct loss function
Different problems call for different objectives.
For regression:
- MAE,
- MSE,
- Huber loss.
For classification:
- cross-entropy,
- weighted cross-entropy.
For probabilistic forecasts:
- log loss,
- Brier score.
For specialised systems, custom loss functions may incorporate:
- physical constraints,
- economics,
- fairness,
- safety.
The choice should follow from the problem.
Sometimes the objective is learned
Modern AI research also explores systems where preferences are inferred from:
- human demonstrations,
- comparisons,
- feedback.
Instead of manually specifying every component of the loss, the system learns a model of what humans appear to prefer.
But this does not remove the specification problem.
Now we must ask:
- Which humans?
- Which examples?
- Which preferences?
- Under what circumstances?
- How should disagreement be handled?
Human feedback is data
Suppose humans compare:
Response A
and:
Response B.
They prefer:
A.
That preference becomes an observation.
Across many examples, a system can learn a reward model.
That reward model then approximates human preferences.
The reward model is itself imperfect
Now we have:
HUMAN PREFERENCES
↓
DATA
↓
REWARD MODEL
↓
AI OPTIMISATION.
The AI is not directly optimising human preferences.
It is optimising:
a model of human preferences.
Any errors in that model can matter.
Optimisation can exploit imperfect objectives
Suppose the reward model is approximately correct under normal outputs.
A sufficiently powerful optimiser may discover unusual outputs that receive high predicted reward even though humans dislike them.
This is another version of:
optimising the proxy rather than the true objective.
The stronger the optimiser, the more objective design matters
A weak optimiser may only slightly exploit imperfections in the objective.
A powerful optimiser can search much more effectively for ways to maximise it.
So improving optimisation capability makes accurate objective specification increasingly important.
Loss functions connect prediction to optimisation
We can now see the full chain:
DATA
↓
MODEL
↓
PREDICTION
↓
LOSS
↓
OPTIMISATION
↓
PARAMETER UPDATE
↓
BETTER MODEL.
Loss is the bridge between:
prediction
and:
learning.
But there are two optimisation problems
This distinction will matter later.
During training
We optimise:
model parameters
to reduce:
prediction loss.
During deployment
We may optimise:
decisions
to achieve:
real-world objectives.
These are different optimisation problems.
Training optimisation
Conceptually:
DATA
↓
CHOOSE PARAMETERS
↓
MINIMISE PREDICTION LOSS.
The output is:
a trained model.
Decision optimisation
Later:
CURRENT STATE
MODEL PREDICTIONS
↓
CHOOSE ACTION
↓
MINIMISE COST / MAXIMISE UTILITY
subject to:
constraints.
The output is:
a decision.
Do not confuse the two.
A perfect prediction does not tell us what to do
Suppose an AI predicts perfectly:
Fifty patients will need intensive-care beds tomorrow.
There are:
ten beds available.
Prediction has solved one problem.
It has not solved:
Who gets the beds?
That requires:
- objectives,
- constraints,
- allocation rules,
- fairness.
Prediction loss cannot answer that alone.
Loss functions are powerful because they make learning possible
The machine needs a numerical signal.
Loss provides it.
Without loss:
prediction
cannot easily become:
parameter update.
So loss functions transform:
being wrong
into:
information that can drive learning.
But that power comes with responsibility
The loss function tells the machine:
This is what I want you to improve.
A poorly chosen objective can produce a model that becomes extremely good at the wrong thing.
The central design question is therefore not merely:
Can we minimise the loss?
It is:
Have we defined a loss worth minimising?
A useful loss-function checklist
When examining a machine-learning system, ask:
- What loss function is being minimised?
- Why was that loss chosen?
- What kinds of errors does it penalise?
- Are large errors disproportionately important?
- Are positive and negative errors treated equally?
- Are false positives and false negatives treated equally?
- Are rare events weighted appropriately?
- Does loss depend on time, location or system state?
- Are some users weighted differently?
- Does the loss represent uncertainty?
- Does it encourage calibrated probabilities?
- Is the training loss the same as the evaluation metric?
- Does lower predictive loss actually improve the downstream decision?
- Are fairness or safety represented?
- Should some requirements be constraints rather than penalties?
- What real-world objective is the loss intended to represent?
- Could the model exploit the metric without achieving the real objective?
These questions often reveal more about an AI system than simply knowing which architecture it uses.
The central idea
A machine-learning model cannot learn from:
"that was bad".
It needs something mathematically operational.
The loss function turns:
PREDICTION
and:
REALITY
into:
A NUMBER.
Then:
HIGH LOSS
↓
CHANGE PARAMETERS
↓
LOWER LOSS.
But that simple mechanism hides one of the deepest questions in artificial intelligence:
Who decides what counts as wrong?
A loss function is not merely a mathematical convenience.
It is a formal statement of what the learning system should care about.
Change the loss and you can change:
- which errors matter,
- which examples matter,
- which groups matter,
- which risks matter,
- which model is ultimately learned.
So the complete learning process is now:
EXAMPLES
↓
PREDICTIONS
↓
LOSS FUNCTION
↓
ERROR SIGNAL
↓
PARAMETER UPDATE
↓
LEARNING.
We now know what direction we want the model to move:
towards lower loss.
The next question is:
How do we actually move millions or billions of parameters in a direction that reduces it?
That is the job of gradient descent.