Machine Learning in 15 Minutes

Generative & Agentic AI · AI Foundations · Basics · 15 min

Before GenAI: how a computer 'learns' from examples.

A line on a graph slowly turning toward a row of dots. After every nudge the red gaps between the dots and the line get shorter: that shrinking gap is the loss.

The idea in everyday words

Teaching a kid to spot ripe mangoes. You don't write rules: you show 100 mangoes and say 'ripe' or 'not ripe'. The kid slowly learns which colour and softness matter. Machine learning is exactly that: show examples, let the model adjust itself until its guesses match the answers.

The learning loop

  1. Model makes a prediction.
  2. Measure how wrong it is: the loss.
  3. Nudge the model's numbers (weights) to be a bit less wrong: gradient descent.
  4. Repeat thousands of times.

Watch a 1-number model learn y = 2x from examples:

data = [(1, 2), (2, 4), (3, 6), (4, 8)]
w = 0.0                      # the model: y = w * x
lr = 0.05                    # learning rate (step size)
for epoch in range(8):
    grad = sum(2 * (w * x - y) * x for x, y in data) / len(data)
    w -= lr * grad
    loss = sum((w * x - y) ** 2 for x, y in data) / len(data)
    print("epoch", epoch, "w =", round(w, 3), "loss =", round(loss, 3))

Try changing lr to 0.2: it overshoots and explodes!

Words you'll hear

  • Training / test split: learn on some data, check on unseen data.
  • Overfitting: memorising the training examples instead of learning the pattern.
  • Supervised (labelled examples), unsupervised (find groups), reinforcement (rewards).
  • Neural network: many layers of weights; deep learning = many layers.
  • An LLM is a huge neural network trained to predict the next token.

Regression vs classification

The challenge is regression: predict a number (a price, a temperature) and measure the miss with MSE.

Classification predicts a label (spam / not spam, ripe / not ripe). The model outputs a probability for each class, and the loss is log loss (cross-entropy): it punishes being confidently wrong. If the model says "99% ripe" for an unripe mango, the loss is huge; "55% ripe" costs much less.

Task Output Typical loss Example
Regression a number MSE house price
Classification class probabilities cross-entropy spam filter
Next-token prediction (LLMs) probability for every token cross-entropy ChatGPT, Claude

An LLM is a giant classifier: at every step it picks one class (the next token) out of ~100,000.

Step by step

  1. Guess:

    w = 0 → predicts 0 for every x

  2. Loss:

    big error, gradient says 'increase w'

  3. Update:

    w moves toward 2

  4. Repeat:

    loss shrinks each epoch → w ≈ 2

Code example

# Overfitting in one picture: memorise vs generalise
train = {1: 2, 2: 4, 3: 6}
memoriser = lambda x: train.get(x, 0)
pattern = lambda x: 2 * x
for x in [2, 10]:
    print("x =", x, "memoriser:", memoriser(x), "pattern:", pattern(x))

Interview tips

  • Explain overfitting and how to fight it (more data, regularisation, early stopping).

  • Know train/validation/test splits.

  • Gradient descent in one sentence.

Challenge: Mean Squared Error

Return the mean squared error between predictions and targets (same length, non-empty), rounded to 2 decimals.

Example: mse([2, 4], [3, 4]) → 0.5

Solve it in the browser with tests, hints and an AI coach.

Practice problems

More in AI Foundations

Next: Neural Networks from Scratch