01

Programming or learning

What changes when a machine learns instead of following rules?

12 minExecutive path

Programming or learning

With voice. You can pause and jump to any step.

Lesson complete

Subtitles

An email arrives and, in a second, you know whether it is spam. Now try explaining to a computer how you know.

Voice

No voice: the lesson advances on its own with subtitles.

The voice for this lesson is being recorded: for now it advances on its own with subtitles.

0:00 / 0:00

What you will achieve

  • Explain the difference between programming (rules + data → answers) and learning (data + answers → rules).
  • Tell training from using a model, and identify the parameters where it stores what it learned.
  • Recognize why a model is judged by how it does on new cases, not on the examples it learned from.

Key ideas

  1. Programming is writing rules: rules + data → answers. Learning flips the flow: data + answers → rules.
  2. A model is a form of rule with adjustable numbers, the parameters. Training is the search for those numbers from examples with answers.
  3. Using a trained model (inference) means applying its frozen parameters to a new case. It learns nothing at that moment.
  4. What matters is generalizing: getting new cases right. Measuring on the very examples used for training tends to give an optimistic figure.
  5. The model learns what the examples show: if they are mislabeled or biased, the rule it learns comes out bent.

What many people believe

Common belief A machine that learns does it on its own: it programs itself without anyone telling it anything.

What is true Someone decides what to measure, gathers and labels the examples, chooses the form of the model, and checks the result on new cases. The machine only searches for the numbers.

Check yourself

Four questions. No grade, no account: they just show whether the idea stuck.

ConceptWhat changes when a machine learns instead of following rules?
ConceptAn already trained spam filter classifies a new email. What happens to its parameters?
CalculationA model has w₁ = 0.5, w₂ = 0.1 and b = −4. An email has 6 links and 20% capital letters. What score does it get and what does the model answer?
ApplicationA vendor boasts that its fraud detector is 99% accurate in its demo. What is the most important question?

Under the hoodFor those who want the math and the code. You do not need it to follow the course.

Two flows of information

Programming means building a function that, given the rules and the data, produces the answers:

answers=f(rules, data)\text{answers} = f(\text{rules},\ \text{data})

Learning swaps the roles of data and rules: you start from examples with their answers and search for the rule.

rules=g(data, answers)\text{rules} = g(\text{data},\ \text{answers})

Here “rules” means a model with parameters θ\theta, chosen so that it is wrong as rarely as possible on the examples. A classic definition, by Tom Mitchell (1997): a program learns from experience EE with respect to a class of tasks TT and a performance measure PP if its performance at tasks in TT, as measured by PP, improves with experience EE. In the lesson, TT is classifying emails, EE is the thirty examples with answers, and PP is accuracy on new emails.

The lesson’s model

Each email is summarized by two measures, x1x_1 (links) and x2x_2 (% capital letters). The model computes a score and decides by its sign:

s=w1x1+w2x2+bspam if s>0s = w_1 x_1 + w_2 x_2 + b \qquad \text{spam if } s > 0

The parameters are θ=(w1,w2,b)\theta = (w_1, w_2, b) and the boundary w1x1+w2x2+b=0w_1 x_1 + w_2 x_2 + b = 0 is a straight line. To train it, the score is turned into a probability with p=σ(s)=1/(1+e−s)p = \sigma(s) = 1/(1+e^{-s}) and the error is measured with cross-entropy over the nn examples, with yi=1y_i = 1 for spam and 00 for legitimate:

L(θ)=−1n∑i=1n[ yiln⁡pi+(1−yi)ln⁡(1−pi) ]L(\theta) = -\frac{1}{n}\sum_{i=1}^{n}\Big[\,y_i \ln p_i + (1-y_i)\ln(1-p_i)\,\Big]

Each step moves the parameters against the gradient, θ←θ−η ∇L(θ)\theta \leftarrow \theta - \eta\,\nabla L(\theta) (lesson 4). In the scene: η=0.5\eta = 0.5, 300 steps, and the 30 examples, with the measures standardized, (x1−3.5)/2.5(x_1 - 3.5)/2.5 and (x2−14)/8(x_2 - 14)/8, and an arbitrary starting point. The result, in the original units, is w1≈0.69w_1 \approx 0.69, w2≈0.53w_2 \approx 0.53 and b≈−9.63b \approx -9.63. Accuracy is hits divided by total: 27 of 30 on the examples and 52 of 60 on the new emails. The data is synthetic, made up for illustration; these are not real emails.

The hand-written rule in the lesson, “spam if x1≥ax_1 \ge a or x2≥bx_2 \ge b”, is not free either: it also has parameters, the two thresholds. Learning means letting the data set them instead of tuning them by eye.

Why the figure on the examples is misleading

Accuracy measured on the very examples used for tuning is, on average, optimistic. In the lesson, the best of the 10×36=36010 \times 36 = 360 rules the sliders allow gets 28 of the 30 examples right (≈93 %\approx 93\,\%); six rules tie, and on the 60 new emails those six get between 47 and 52 right (≈78 %\approx 78\,\% to 87 %87\,\%). Among the 84 rules that get 24 or more of the 30 right, 58 drop on the new emails, 16 rise, and 10 stay the same; the average change is −3.2-3.2 percentage points. That is a tendency, not a law: with only 60 new emails each figure is noisy, and a carefully tuned rule can hold up (“links ≥5\ge 5 or capitals ≥17 %\ge 17\,\%” gets 27 of 30 and 54 of 60). The more options you try on the same data, the more you fit the luck of that data.

Something similar happens with few examples. With 4 examples, the model gets the 4 it saw right and 48 of the 60 new ones (80 %80\,\%); with all 30, it gets 27 of 30 (90 %90\,\%) and 52 of 60 (87 %87\,\%): the two figures move closer. Splitting training, validation, and test data, and measuring well, is the topic of lesson 10.

Samuel and checkers (1959)

The name machine learning is associated with Arthur L. Samuel, who uses it in the title of “Some Studies in Machine Learning Using the Game of Checkers” (IBM Journal of Research and Development, vol. 3, no. 3, July 1959). His program, on an IBM 704, chose moves by looking several moves ahead and scored each board position with a “linear polynomial,” in his words: a weighted sum of board features. In one of his two forms of learning (“generalization”), the program determined the sign and magnitude of the coefficients that multiply those features and chose which terms to use: the same form of model as in this lesson, though with a different purpose (scoring positions, not classifying). In the version he describes there were 38 terms, besides the piece-advantage term, 16 of them active at any one time. The abstract states that a computer can be programmed to learn to play a better game of checkers than the person who wrote the program, in 8 or 10 hours of machine-playing time, given only the rules of the game, a sense of direction, and a redundant and incomplete list of parameters. And the paper itself states the goal: programming computers to learn from experience “should eventually eliminate the need for much of this detailed programming effort.” The oft-repeated phrase “the field of study that gives computers the ability to learn without being explicitly programmed” is frequently attributed to him, but it does not appear in that paper.

A real case: filtering junk mail

In 1998, Sahami, Dumais, Heckerman, and Horvitz studied how to build junk-mail filters automatically. They point out that hand-written rules assume users who can build them and must be tuned constantly as junk mail changes, and they propose learning from the user’s own messages instead. Their probabilistic classifier (naive Bayes) combined the text of the message with problem-specific features, such as the percentage of non-alphanumeric characters in the subject. They also stress that classifying a legitimate email as junk usually costs far more than letting a junk one through; so they only marked a message as junk if its probability exceeded 99.9%.

In code

import math

# (links, % capital letters, is_spam)
data = [(0, 5, 0), (1, 8, 0), (2, 10, 0), (3, 7, 0),
        (4, 20, 1), (6, 25, 1), (7, 15, 1), (9, 30, 1)]

def score(w1, w2, b, links, caps):
    return w1 * links + w2 * caps + b

w1 = w2 = b = 0.0            # parameters: start from zeros
eta = 0.1                    # step size
for _ in range(2000):        # training: many passes over the examples
    g1 = g2 = gb = 0.0
    for links, caps, y in data:
        z = score(w1, w2, b, links / 10, caps / 100)   # measures on a 0-1 scale
        p = 1 / (1 + math.exp(-z))                     # probability of spam
        e = p - y                                      # how wrong it is
        g1 += e * links / 10
        g2 += e * caps / 100
        gb += e
    n = len(data)
    w1 -= eta * g1 / n
    w2 -= eta * g2 / n
    b -= eta * gb / n

print(round(w1, 1), round(w2, 1), round(b, 1))   # weights on the 0-1 scale of the measures
# using it: frozen parameters, just arithmetic
for links, caps in [(1, 6), (8, 12), (5, 30)]:
    s = score(w1, w2, b, links / 10, caps / 100)
    print((links, caps), round(s, 1), 'spam' if s > 0 else 'legit')
# 7.7 2.8 -3.3
# (1, 6) -2.3 legit
# (8, 12) 3.3 spam
# (5, 30) 1.5 spam
# These 8 examples are perfectly separable: with more steps the weights would keep growing;
# 2000 are enough to classify them correctly.

What comes next

In lesson 2 we will see how any data (an image, a sound, a text) is turned into numbers so it can be used as x1,x2,…x_1, x_2, \dots; in lesson 3, how a model’s error is measured; and in lesson 4, how the parameters are adjusted step by step (the gradient descent we only saw at work here). This lesson’s model, a weighted sum that decides by its sign, is in essence an artificial neuron (lesson 5).

Decision lens

What this idea means if your job is to decide, not to code.

  • Learning from data makes sense when the rule is hard to write, there are many examples with answers, and some errors are tolerable. If a law or a procedure fixes the rule and an error is unacceptable, write the rule.
  • It all starts with examples that have answers: ask who labeled them, how many there are, and whether they resemble real cases. If they are mislabeled or biased, the model learns that error, as we saw with four bent labels.
  • Demand the accuracy figure measured on cases the model never saw, not on the training data. A demo on the same data used for tuning does not tell you how it will work in real operation.
  • Separate costs and errors: training is a large, infrequent expense; using the model is a small expense repeated on every query. And not all errors cost the same: flagging a legitimate email as spam usually costs more than letting a spam email through, as Sahami and colleagues already argued in 1998.

Terms in this lesson

Full transcript
CommentsThis lesson’s public conversation: questions, facts to check, ideas.

Comments live in GitHub Discussions, in the course’s public repository; writing one requires a GitHub account. They load from giscus.app when you open this section. The course reviewers read them, and if a change is accepted, it is applied in both languages. Open the thread on GitHub

Sources