arrow_back All articles

AI Loss Functions: How a Model Knows It Is Wrong

William Golovlev · 2 min read AI/ML
AI Loss Functions: How a Model Knows It Is Wrong

In my last post I covered tensors, the “memory” of a machine learning model. This one is about the scorecard that tells the model whether it’s getting better or just guessing. That scorecard is the loss function.

The dartboard analogy

You’re throwing darts. You remember exactly how you threw each one (angle, speed, release), but there’s a catch: you can’t see where the bullseye is.

You’ll never improve that way. You hit the wall, but you don’t know how far off you were, so you can’t adjust the next throw. The loss fixes that. It’s a single number for how far your throw landed from the bullseye.

Watching it learn

To see this in action I used TensorFlow Playground, which shows a model classifying points in real time.

A fresh model is neutral. It doesn’t know which areas are “blue” and which are “orange,” so it starts with a failing grade of around 0.5 loss. As it runs through epochs (loops over the training data), the colored regions on screen get sharper. That’s the loss dropping as the model gets more confident.

Learning too fast

You might think you should crank the learning rate to the max so the model learns instantly. Don’t. If it learns too fast, the math blows up: the numbers get huge and stop making sense.

A moderate learning rate lets the model settle into the right “shape” for the data instead of overshooting and crashing. If your loss suddenly jumps or turns into NaN, a too-high learning rate is the first thing to check.

How it all collapses into one number

Data passes through the network’s layers, and each layer has weights. I think of those weights as radio knobs.

The input (say, coordinates on a graph) gets multiplied and shifted by those knobs. At the end, all of that gets boiled down to a single number, the loss. In my example:

  1. Take the model’s prediction.
  2. Subtract the correct answer.
  3. Take the absolute value, because we only care how far off we were, not whether we were too high or too low.
  4. Average those errors across all the data points.

That recipe has a name: mean absolute error (MAE). Other loss functions square the error instead, but the idea is the same: one number, lower is better.

The rewind button

Because tensors remember their history, we can hit a rewind button called backward(). It carries the error back through every operation and works out how much each knob (weight) contributed to the mistake.

Next up: how the model actually turns those knobs to fix itself.


Check out the interactive tool mentioned in the video: TensorFlow Playground

Let me know what you think on LinkedIn, and stay tuned for the next video in this series!

#Machine Learning#Loss Functions#AI Tutorial#Neural Networks#Model Training