Grade 10 Computer Science · How a machine learns

Examples instead of rules

When the rule cannot be written, and what is done then.

You know how to do it, and cannot write it down

You recognize a friend’s handwriting in a quarter of a second. Try writing a condition that tells the letter a from the letter o in handwriting, in every shape people write them. After half an hour you will still have no function that works. So how does a computer learn to do it anyway?

Everything you have written until now was an explicit rule: if this then that, repeat this, return that. There are tasks at which you know how to perform and cannot state the rule. You recognise handwriting in a quarter of a second, and cannot write a condition distinguishing the letters. That is where this approach enters.

The examples go in, and from them a rule is derived that guesses on new cases.
# ordinary: the rule is written, the answers follow
def is_spam(text):
    return "free money" in text.lower()

# learned: the answers are given, the rule follows
examples = [
    ("free money now", True),
    ("meeting at four", False),
    ("claim your prize", True),
]
# a procedure reads these and produces a rule nobody wrote

What is needed for this to work

Three conditions, and when one of them is missing the approach does not fit. Many examples — a rule derived from three examples is not a rule but a coincidence. Labelled examples — each example needs the right answer, and usually a person has to write it. And a resemblance between past and future — the rule is derived from what was, and if reality changes it stops fitting without warning.

The approachWhat is givenWhat comes out
Ordinary programmingA ruleAnswers
Learning from examplesExamples and answersA rule

Example 1 — why handwriting is not a rule

  1. You recognise a letter at once.
  2. And cannot state the condition.
  3. Examples are plentiful.

Example 2 — why three examples do not suffice

  1. Every possible rule fits three.
  2. There is no way to choose between them.
  3. The rule that comes out will be a coincidence.
And learning needs many labeled examples, and a past that resembles the future.