Programming or learning
What changes when a machine learns instead of following rules?
Lesson complete
If you like:
An email arrives and, in a second, you know whether it is spam. Now try explaining to a computer how you know.
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.
Review modeComment on this stepI finished the lesson
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
- Programming is writing rules: rules + data → answers. Learning flips the flow: data + answers → rules.
- A model is a form of rule with adjustable numbers, the parameters. Training is the search for those numbers from examples with answers.
- Using a trained model (inference) means applying its frozen parameters to a new case. It learns nothing at that moment.
- What matters is generalizing: getting new cases right. Measuring on the very examples used for training tends to give an optimistic figure.
- 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.
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:
Learning swaps the roles of data and rules: you start from examples with their answers and search for the rule.
Here “rules” means a model with parameters , 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 with respect to a class of tasks and a performance measure if its performance at tasks in , as measured by , improves with experience . In the lesson, is classifying emails, is the thirty examples with answers, and is accuracy on new emails.
The lesson’s model
Each email is summarized by two measures, (links) and (% capital letters). The model computes a score and decides by its sign:
The parameters are and the boundary is a straight line. To train it, the score is turned into a probability with and the error is measured with cross-entropy over the examples, with for spam and for legitimate:
Each step moves the parameters against the gradient, (lesson 4). In the scene: , 300 steps, and the 30 examples, with the measures standardized, and , and an arbitrary starting point. The result, in the original units, is , and . 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 or ”, 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 rules the sliders allow gets 28 of the 30 examples right (); six rules tie, and on the 60 new emails those six get between 47 and 52 right ( to ). 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 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 or capitals ” 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 (); with all 30, it gets 27 of 30 () and 52 of 60 (): 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 ; 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
- Machine learning An approach in which a computer obtains the rules from examples instead of someone writing them. The term is associated with Arthur Samuel (1959).
- Training data Examples, each with its correct answer, from which a model adjusts its parameters.
- Model A form of rule with adjustable numbers. Once trained, it is the learned rule.
- Generalization Getting new cases right, ones not used for training. It is what you really want from a model.
- Parameter Each of the numbers a model adjusts while training and in which it stores what it learned.
- Inference Using an already trained model to answer a new case, with frozen parameters.
Full transcript
Sources
- Samuel, A. L. (1959). Some Studies in Machine Learning Using the Game of Checkers
- Mitchell, T. M. (1997). Machine Learning (McGraw-Hill)
- Sahami, M., Dumais, S., Heckerman, D., Horvitz, E. (1998). A Bayesian Approach to Filtering Junk E-Mail (AAAI-98 Workshop on Learning for Text Categorization, WS-98-05)
- Goodfellow, I., Bengio, Y., Courville, A. (2016). Deep Learning, capítulo 5: Machine Learning Basics
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