How Machines Learn

Computers that learn are not programmed with the answers. They are shown examples, measure their mistakes and adjust — millions of times over. Here is how that works, with no maths at all.

Think about how you learned to tell a ripe durian from a bad one. Nobody handed you a rule book. You watched your father tap, sniff and shake them at the roadside stall; you tasted a few disappointments; and slowly your judgement improved. You could probably not write down the rules even now. Machine learning works in much the same way: rather than being told the rules, the computer is shown lots of examples and left to find the pattern for itself.

Rules versus examples: the spam story

In the late 1990s, email was drowning in spam. The obvious fix was to write rules: if a message contains “FREE MONEY”, or more than five exclamation marks, or the word “lottery”, send it to junk. It worked — for about a week. Spammers simply wrote “FR3E M0NEY” instead, and the rule lists grew longer and longer while legitimate emails (“the lottery results for our office charity draw”) got caught by mistake.

In 2002 the programmer Paul Graham published an essay, “A Plan for Spam”, arguing for a different approach. Instead of writing rules, collect a pile of emails people have already sorted into “spam” and “not spam”, and let the computer count which words tend to appear in each pile. A new email is then scored by how spam-like its words are. When spammers change tactics, you do not rewrite anything: you add the new examples and let the filter learn again. Statistical filters of this kind were already being studied by researchers, and Graham’s essay helped popularise them. Today’s spam filters are far more sophisticated, but they rest on the same idea.

Key point

Traditional programming: rules + data → answers. Machine learning: data + answers → rules. The computer works out the rules from examples, and those learned “rules” are a mass of numbers rather than sentences anyone can read.

The four ingredients

Every machine-learning system, from a spam filter to a chat assistant, is built from the same few ingredients. Learn these words and most AI news becomes easier to follow.

DATATraining dataThe examples

The training data is the collection of examples the system learns from — thousands of emails labelled spam or not, or millions of photos labelled “cat”, “dog”, “kereta”. A whole collection is called a dataset. Its quality matters enormously: a system can only be as good, and as fair, as the examples it learned from.

MODModelThe learner

The model is the thing that does the learning and then makes predictions. Picture it as a machine full of adjustable knobs: put an email in one end, and a score (“87% likely spam”) comes out the other. Before training, the knobs are set randomly and its answers are rubbish.

KNOBParametersThe knobs

The knobs are called parameters (or weights). A small spam filter might have a few thousand; a large language model has billions. Learning simply means finding good settings for all of them. Nobody turns them by hand — there are far too many.

ERRLossThe score of wrongness

The loss is a single number that says how wrong the model’s answers are on the training examples. Called a legitimate email spam? The loss goes up. Got it right with confidence? The loss goes down. Training means turning the knobs to make the loss as small as possible.

Walking downhill in fog

So how do you tune billions of knobs? Imagine you are standing on a hillside in Cameron Highlands in thick morning fog. You want to reach the bottom of the valley, but you can only see the ground right at your feet. A sensible plan: feel which way the ground slopes, take a step downhill, and repeat. You will not see the whole valley, but step by step you will get lower.

That is gradient descent, the method behind almost all modern machine learning. Height is the loss — how wrong the model is. Your position is the current setting of the knobs. “Feeling the slope” means working out, for every knob, whether nudging it up or down would reduce the error. Then every knob gets nudged a little in the helpful direction, and the process repeats.

The size of each step is called the learning rate. Try it in the demo below: the ball is the model, the curve is the loss, and the dashed line marks the best possible setting.

Some experiments worth trying:

Real models have billions of knobs rather than one, so the “valley” has billions of directions, but the idea is the same. Choosing a good learning rate is one of the practical arts of training a model.

The training loop

Put the ingredients together and you get a loop that repeats, often millions of times. Step through it.

LOOPOne turn

Once training is finished, the knobs are frozen and the model is used to make predictions on new data. Note that a chat assistant does not usually keep training on your conversation as you talk; any learning from user data happens later, in separate training runs, if at all.

Three ways to learn

Machine learning comes in three main styles, depending on what kind of examples and feedback are available.

Learning with an answer key. In supervised learning, every training example comes with the right answer, like a tuition teacher marking worksheets. This email is spam; this X-ray shows pneumonia; this house in Shah Alam sold for this price.

Everyday examples: spam filters, bank fraud alerts, recognising handwriting on cheques, predicting property prices, translating sentences. Most practical machine learning is supervised. The catch: someone has to label all those examples, which is slow and expensive.

Overfitting: memorising the past-year papers

Every Malaysian student knows the trick: before SPM, drill the past-year papers. Done well, it teaches you the patterns of the subject. Done badly, you simply memorise the answers to those exact questions — and when the real exam asks something slightly different, you freeze.

Machines fall into exactly the same trap. A model with many knobs can end up memorising its training examples, quirks and all, instead of learning the general pattern. It scores brilliantly on the examples it has seen and poorly on new ones. This is called overfitting. A spam filter that overfits might learn “emails sent on Tuesdays from addresses with the letter Q are spam” simply because that happened to be true in its training pile.

The defence is simple and important. Before training, set aside a portion of the examples — perhaps a fifth — and never let the model see them. This is the test set: the “real exam”. Only the score on the test set tells you how the model will behave in the world. Consensus Measuring a model on the same data it trained on is one of the most common and serious mistakes in machine learning.

Why this matters to you

Overfitting and biased training data explain many real AI failures. A face-recognition system trained mostly on one group of faces may work worse on others; a model trained on last year’s patterns may stumble when habits change. When you hear that an AI is “95% accurate”, it is fair to ask: accurate on what data, and does that data look like mine?

Check your understanding

Quick check

What is the main difference between a rule-based spam filter and a machine-learning one?

Machine learning turns data plus answers into rules; nobody writes the rules by hand.

In the “walking downhill in fog” picture, what does height stand for?

Gradient descent walks downhill on the loss, adjusting the knobs to reduce error step by step.

Your phone groups photos of the same person without you ever naming them. Which kind of learning is that most like?

There are no labels; the system finds clusters of similar faces by itself.

A model scores 99% on its training data but 60% on new data. What has probably happened?

A big gap between training and test performance is the classic sign of overfitting.

// Pause and reflect

  1. Think of a skill you learned by examples rather than rules — cooking without a recipe, judging traffic, reading a customer’s mood. How is it similar to machine learning?
  2. If you were training a model to spot fake bank text messages, what examples would you collect, and how might your data accidentally be biased?
  3. Where in your own study or work have you “overfitted” — learned the answers but not the idea?

Your notes are saved privately in this browser.

Try this

Open your email’s spam folder (or your phone’s blocked-messages list) and look at five messages. For each, guess which words or features gave it away. Then find one legitimate message wrongly caught, or a spam message that slipped through. You have just done what machine-learning engineers do every day: error analysis.

Key terms on this page

Sources & further reading