🌏 中文版
Video status: Videos included. This article is written from the official MOOC handout 02_handout.pdf and transcripts of this lecture's four official videos, with formulas following the handout. The transcripts are machine transcripts made for this article (Whisper), not official captions, and no human has proofread them. The videos are taught in Mandarin; the slides are in English. Each section opens with its official video and the approximate time ranges. Page numbers refer to the number at the lower right of each slide (0/22 to 22/22), and wherever the text says "in the lecture" it is paraphrasing what is said in the video.
This is Foundations Lecture 2 in Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques. The Roadmap on page 1 of the handout divides Foundations into four questions, and this lecture is still under the first, "When Can Machines Learn?". The previous lecture (Foundations Lecture 1: The Learning Problem) is left with a single line on that page: the algorithm takes the data and the hypothesis set to get . In that flowchart and were still empty boxes; this lecture fills them with their first concrete content, on the simplest kind of problem: answering yes or no.
The prerequisites are the five symbols from the previous lecture (, , , , ), the vector inner product, and the relation between vector lengths and the angle between them (). After reading, you should be able to write PLA yourself, work out every correction by hand on a small data set, and derive from scratch the conclusion that "when the data is linearly separable, PLA makes at most corrections"; you should also know why to switch to pocket when the data cannot be separated.
What the hypothesis set looks like: writing the credit approval rule as a line
(Video 00:00–03:00 reviews the previous lecture, 03:00–07:47 covers the score and the threshold, 07:47–10:26 the vector form, 10:26–14:15 the two-dimensional picture, and Fun Time follows.)
A weighted score and a threshold
Page 2 of the handout shows the credit card flowchart from the previous lecture again, with a single question underneath: what hypothesis set can we use? The answer on page 3 is a rule for computing a score. Each applicant is represented by a vector ; the dimensions the handout lists are age, annual salary, year in job, and current debt. Each dimension is multiplied by a weight , and the sum is the applicant's score:
In the lecture, Lin compares this to an exam: every question carries some points, a total above 60 passes, and anything else fails. The weights are the points per question. A dimension that favors approval gets a positive weight, larger the more important it is; an unfavorable dimension, for which the example in the lecture is owing a lot of money, may get a negative weight, or a smaller one.
There are only two outputs, and the handout encodes them as numbers: . In the lecture, Lin explains that any symbols would do, and and are chosen for mathematical convenience: with them, "output good above the threshold and bad below it" is just taking the sign of "score minus threshold".
When the score equals the threshold exactly, the sign is 0, and the handout only writes "0 ignored". The lecture adds a little more: this case rarely happens and does not matter in the discussion that follows, and if it does come up, treat it as decided by a coin flip.
Note that this is a lowercase . One set of weights plus one threshold determines one , a different set gives a different , and all such together form the hypothesis set . The handout says this kind of hypothesis is historically called a perceptron. In the lecture, Lin gives the origin of the name: it comes from very early neural network research and can be seen as a mathematical model of a single neuron, which this course does not go into for now.
Folding the threshold into the vector
Writing a sum and then subtracting a threshold every time is tedious. Page 4 tidies this up in a way that changes only the notation, not the meaning: treat "minus the threshold" as one more product term.
The trick is to give every example a zeroth coordinate that always equals , and to call by the name . Once the sum starts at 0 instead of 1, the threshold is absorbed. Both and are then vectors of dimension , which the handout calls the 'tall' versions (the lecture uses the same image in Mandarin), and score minus threshold is the inner product of the two vectors. For the rest of the lecture and always mean these versions with one extra dimension, and one represents one hypothesis .
On the plane, a perceptron is a line
Page 5 draws the case . Here , and the picture maps to the symbols as follows:
| Symbol | What it looks like in the picture |
|---|---|
| Customer features | a point on the plane (a point in in higher dimensions) |
| Label | the shape of the point: a blue circle ○ is , a red cross × is |
| Hypothesis | a line (a hyperplane in higher dimensions), predicting on one side and on the other |
Why a line? The explanation in the lecture is to look at where the sign flips: where the quantity in the parentheses equals 0. is a first-degree equation in and , which is a straight line on the plane. Points on one side of the line give a positive value and points on the other side a negative one. The handout's figure puts two different lines through the same set of points, each painting the plane into a blue region and a red region, to show that different lines classify the customers differently.
An addition in this article, a geometric fact that will be used again and again: is the normal vector of the line and points toward the side predicted ; the distance from the line to the origin is divided by the length of . Multiplying the whole of by a positive number changes neither the position of the line nor its positive and negative sides, so one line corresponds to infinitely many that point in the same direction.
The handout's conclusion is perceptrons ⇔ linear (binary) classifiers: a perceptron is a linear binary classifier.
Fun Time: the weights of a spam detector
Question (page 6): Consider using a perceptron to detect spam messages. Assume that each email is represented by the frequency of keyword occurrence, and output indicates a spam. Which keywords below shall have large positive weights in a good perceptron for the task?
- coffee, tea, hamburger, steak
- free, drug, fantastic, deal
- machine, learning, statistics, textbook
- national, Taiwan, university, coursera
Answer: 2. The handout's explanation: the occurrence of keywords with positive weights increases the 'spam score', and hence those keywords should often appear in spams. The lecture says the same: this perceptron is in effect computing a spam score for an email, and free, drug, fantastic, and deal are words spam commonly uses, so they get the largest weights.
The words in the other choices appear about equally often in spam and in normal mail, or more often in normal mail (for people taking this course, choices 3 and 4 are the words a course announcement would use), and giving them large positive weights would only push normal mail over the threshold. One small difference: the handout writes the "frequency" of keyword occurrence, while the lecture describes it orally as "record yes if the word appears and no if it does not". The reasoning is the same under either representation.
How to pick one line out of infinitely many: correct a mistake when you see one
(Video 00:00–04:45 covers what kind of line to pick and why it is hard, 04:45–08:38 the rule of PLA, 08:38–10:04 cyclic PLA, 10:04–15:49 goes through the figures step by step, 15:49–17:41 lists the questions not yet answered, and Fun Time follows.)
The goal: get everything right on the data already seen
is all lines; which one should be? Page 7 reasons in four steps:
- What we want is . But is unknown, so this cannot be done directly. In the lecture, Lin says that if we knew we could call it a day and go home, with no need for machine learning.
- The almost necessary condition is that is close to on , ideally right on every example: . As the lecture puts it, first make sure the data we have seen is fine, and worry about the unseen data later.
- The difficulty is that is of infinite size. In the lecture, Lin explains that even in two dimensions a line can be rotated into infinitely many positions, and the brute-force way of trying them one by one never finishes.
- So take a different approach: start with some line in hand, and 'correct' its mistakes on . The lecture likens this to how people learn: not very good at first, and better the more they learn.
The figure on the right of the same page shows exactly this last step: a line puts one circle at the lower right on the red side, and a few small arrows drawn on the line point toward that circle. In the lecture, Lin says to move the line a little and see whether that fixes the mistake, or at least makes it less wrong.
is represented by its weight vector . The subscript of this means "round 0" and it is a whole vector, not the same thing as the component (not bold) that stands for the threshold in "Folding the threshold into the vector" above.
The two steps of PLA
Page 8 is the whole algorithm. Start from some (the handout says will do), and for repeat:
- Find an example that gets wrong, and call it . Getting it wrong means
- (Try to) correct the mistake by
Keep going until there are no more mistakes, and return the last (called ) as . The notation only means "the index of the example picked in round ".
This is the Perceptron Learning Algorithm (PLA). At the bottom of the page the handout writes "A fault confessed is half redressed"; in the lecture, Lin explains that this corresponds to a Chinese saying to the effect that nothing is better than recognizing a mistake and correcting it, and so often refers to PLA by that saying. As for starting from , the explanation in the lecture is that it stands for knowing nothing.
Why adding yₙxₙ counts as a correction
The right side of page 8 has two small figures, one for each kind of mistake. First recall that the sign of an inner product is determined by the angle: means the angle between and is less than 90 degrees, and less than 0 means the angle is more than 90 degrees.
| Case | What it looks like in the figure | Correction | Effect |
|---|---|---|---|
| , but | the angle between and is too large | the new vector falls between and , rotating toward | |
| , but | the angle between and is too small | the new vector rotates away from |
The way the lecture puts it: when the answer should be positive, rotate a little closer to , and when it should be negative, rotate it a little away from . The two cases need only one formula, because carries the sign itself: .
The figures only say the direction is right; algebra can say it more precisely. Take the inner product of both sides of the correction rule with (writing as for short):
The last step uses . The quantity is worth remembering: when it is positive, the sign of the score agrees with and the example is classified correctly; when it is negative or 0, the example is wrong. The equation above says that after the correction this quantity has increased by . Because , is at least 1, so the push is always in the right direction. Whether one push is enough to make the quantity positive, this equation does not guarantee, which is why the handout puts "(try to)" in front of "correct". This paragraph is the content of this section's Fun Time.
Implementation: cyclic PLA
"Find a mistake" on page 8 does not say how to find it, and "no more mistakes" does not say how to tell. Page 9 gives a practical version called cyclic PLA: arrange the data in a cycle, find the "next" mistake each time, and stop only after a full cycle of not encountering mistakes.
The lecture illustrates with 100 examples: go from number 1 to number 100; if this one is fine, look at the next, and if it is wrong, correct it; once all 100 have been seen and the cycle comes back around without another mistake, you are done. The order can follow the original numbering of the data (the handout calls this the naïve cycle), or you can fix a random order in advance and cycle through that (the precomputed random cycle). The benefit of writing it this way is that "is anyone still making a mistake" becomes easy to check.
The handout's demonstration: halting after nine corrections
Page 10 is titled Seeing is Believing: a run of 11 figures that draw every step of PLA on a two-dimensional data set. The figure has 6 blue circles and 9 red crosses, with the circles toward the lower right and the crosses toward the upper left. The figures show the following:
| Figure | Point used for the correction | What is drawn |
|---|---|---|
| initially | only the data, no line yet | |
| update: 1 | (circle) | is the vector from the origin to |
| update: 2 | (circle) | the line from the previous step is perpendicular to ; falls on the red side, and rotates toward it |
| update: 3 | (cross) | it has rotated too far, a distant cross ends up on the blue side, and rotates away from it |
| update: 4 | (cross) | another cross is misclassified, so rotate away again |
| update: 5 to 9 | , , , , | the line swings back and forth between these two points: after a correction with the line gets flatter, and after a correction with it gets steeper again |
| finally | separates the circles and crosses completely |
Why is there always a mistake at the first step? In the lecture, Lin explains that at the start , which amounts to having no line, so whatever you look at counts as wrong. For the steps in the middle that swing back and forth, the explanation in the lecture is that PLA looks at only one point at a time, and fixing this one may send another over to the wrong side; here Lin uses a Taiwanese proverb about going after the tortoise and having the soft-shelled turtle get away, meaning that you catch this point and another one escapes. The nine corrections use only 4 distinct points, and alone is used 4 times.
There are two lines of text at the bottom of the page. The first, worked like a charm with < 20 lines: in the lecture, Lin says the program behind these figures, not counting the plotting, is under 20 lines. The second is a reminder for anyone who wants to redraw this figure: note: made for visual purpose. The lecture explains that every feature value was deliberately made much larger than so the picture looks better; without doing so the algorithm still finds a line.
A numerical example: four points, from w = 0 until it halts
The following is an example made up for this article, not from the slides. Four two-dimensional examples, with added the way the handout does:
| Original point | |||
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 |
Start from and run cyclic PLA in the order 1, 2, 3, 4. One detail has to be settled first: does a score of exactly 0 count as a mistake? Page 15 of the handout writes "mistake" as , with equality included, and that convention is followed here: a score of 0 is treated as wrong. Without this rule, every score is 0 when starting from and the algorithm cannot take a single step.
First step: look at example 1; , which counts as wrong. .
Next, example 2: and , correct. Example 3: , but , wrong. .
Every later step is the same calculation, collected in the table. "Points still wrong after the correction" is the result of plugging the new back into all four examples.
| Point used | Points still wrong after the correction | |||||
|---|---|---|---|---|---|---|
| 0 | 1 | 3, 4 | ||||
| 1 | 3 | 2, 3, 4 | ||||
| 2 | 4 | 2, 3 | ||||
| 3 | 2 | 3, 4 | ||||
| 4 | 3 | 3, 4 | ||||
| 5 | 4 | none |
After , go around one more full cycle: for the four examples is , , , and , all positive, and the algorithm halts. There were 6 corrections in total.
What this result means. corresponds to the line , that is, . The two positive examples have and lie on one side of the line; the two negative examples have and lie on the other. Translated back into credit card terms: each of the two features is worth 1 point, and the card is approved only when the total exceeds 2.
Three things in this run can be checked against the earlier explanation:
- Step corrects with example 3. Before the correction , and afterwards it is . It increased by , in the right direction, but example 3 is still wrong. "Try to correct" does not guarantee a fix in one go.
- After that same correction, example 2, which had been correct, becomes and lands on the line. Correcting one point moves the others, which is why the line swings back and forth in the handout's figures.
- After step , the score of example 3 is , still wrong under the rule above; but the next example cyclic PLA looks at is example 4, which it corrects first, and as a result example 3 becomes correct as well.
Written as a program, this takes under 20 lines of plain Python:
def pla(data):
w = [0, 0, 0]
updates, clean, i = 0, 0, 0
while clean < len(data): # stop only after a full cycle with no mistakes
x, y = data[i]
score = sum(wj * xj for wj, xj in zip(w, x))
if y * score <= 0: # misclassified, or exactly on the line
w = [wj + y * xj for wj, xj in zip(w, x)]
updates, clean = updates + 1, 0
else:
clean += 1
i = (i + 1) % len(data)
return w, updates
data = [((1, 3, 0), +1), ((1, 0, 3), +1), ((1, 1, 0), -1), ((1, 0, 1), -1)]
print(pla(data)) # ([-2, 1, 1], 6)
The output is ([-2, 1, 1], 6), which agrees with the last row of the table above and the total of 6 corrections. This program does not stop on data that cannot be separated; the reason is in the section "What to do when the data cannot be separated".
Two kinds of questions not yet answered
Page 11 sorts the questions PLA leaves open into two kinds, all still marked with question marks:
- Algorithmic: does it halt? For cycling in the original order, cycling in a random order, and other variants, the handout writes "??" for each. The lecture stresses that what was just shown is a single example, with no claim that PLA always halts.
- Learning: is ? If it halts, yes on , because there are no mistakes. What about outside ? What if it does not halt? Both are "??".
The bottom of the page previews what the section "Does PLA always halt" below will prove: if (...), after 'enough' corrections, any PLA variant halts. The condition in the parentheses is left blank for now. As for the question outside , the lecture says it will be about two lectures before it is discussed, which is Foundations Lecture 4: Feasibility of Learning.
Fun Time: what the correction rule guarantees
Question (page 12): Let's try to think about why PLA may work. Let , according to the rule of PLA below,
which formula is true?
Answer: 3. The handout's hint is to multiply the second part of the rule by , and the derivation is the one in "Why adding yₙxₙ counts as a correction" above: the right side gains , so the left side cannot be smaller. The handout says the result shows that the rule somewhat 'tries to correct the mistake'. The lecture reads it this way: is the score, and its getting larger after being multiplied by means the sign of the score agrees with a little better; that may make the example fully right, or it may not yet.
The other choices can be ruled out with the numerical example above. Choice 1: after the first correction, , which is not equal to ; the correction rule never aims to make the score exactly . Choice 2: after the correction at step , the score of example 3 is , whose sign still differs from . Choice 4 points the opposite way from choice 3, and since , it cannot hold.
Does PLA always halt: the guarantee under linear separability
(Video 00:00–01:40 defines linear separability, 01:40–06:17 is the first fact, 06:17–09:20 the second fact, 09:20–10:58 puts the two together, and Fun Time follows.)
The precondition: linear separability
When PLA halts, the line in hand makes no mistakes on . So page 13 says the necessary condition for PLA to halt is that allows some to make no mistake. The handout calls such a linear separable. The lecture is blunt about it: if no line can cut the data apart at all, PLA can run forever and never halt.
The handout illustrates with three figures:
| Figure | What the data looks like | Linear separable? |
|---|---|---|
| Left | circles on the right, crosses on the left, separable by one line | Yes |
| Middle | split roughly along the diagonal into two sides, but with a few points of each kind mixed into the other side | No |
| Right | circles clustered in the middle with crosses surrounding them, separable only by a circle | No |
In the lecture, Lin mentions in passing that the two figures on the right are the two patterns in this course's logo, and that later lectures will introduce ways of handling them one by one, with part of these cases already handled later in this lecture.
With the necessary condition settled, the question is turned around: assuming is linear separable, does PLA always halt?
Fact one: each correction increases the inner product of w and w_f by at least a fixed amount
Linear separable means there exists a perfect such that for every example (page 14). In the lecture, Lin explains that the subscript is deliberate: think of this line as the target function. We do not know what is, but the proof only needs it to exist.
classifies every example correctly, so is positive for every example, and no point lies on the line or on the wrong side. There are only examples, so the smallest of these positive numbers is also positive. The example PLA picks in round is one of them, so
In the lecture, Lin calls the example that attains the minimum "the person closest to the line".
Now look at how the inner product of and changes after one correction. Substitute the correction rule and expand the inner product:
The third line uses the inequality just above. The conclusion is that each correction increases the inner product by at least . Note that this part uses only "correct with some example"; it never uses the fact that the example is misclassified.
With the inner product growing, the handout says appears more aligned with , followed by a "(really?)". In the lecture, Lin raises the objection on behalf of the students who are good at math: the inner product is , and it may grow because the angle gets smaller, or merely because gets longer. Take two vectors with a positive inner product, multiply one of them by two or three, and the inner product grows too. So only half has been proved at this point, and the length of still has to be kept under control.
Fact two: the squared length of w grows by at most R² each time
Page 15 uses the property that fact one did not, which the lecture calls the most important property of PLA: it corrects only when there is a mistake. The handout rewrites "misclassified" as a convenient inequality:
When the score and have different signs, their product cannot be positive. Now expand the squared length after a correction. ; take and :
Third line: the middle term is 2 times a number that is at most 0, and replacing it with 0 can only make the right side larger. Fourth line: the example used in this round is no longer than the longest example in the data. is and does not affect the length, so ; in the lecture, Lin explains that this is why the slide shades the here in gray.
The handout sums up this part as: mistake 'limits' growth, even when updating with 'longest' . Even if every correction used the longest example, the squared length would grow by only at a time. As the lecture puts it, the only thing that makes grow is the last term; the middle term gives it no growth at all.
Putting the two facts together: T ≤ R²/ρ²
The bottom of page 15 gives only the shape of the conclusion: start from , and after mistake corrections,
The lecture leaves the calculation in between to the students, saying that writing down the results of the two pages on a sheet of paper is enough to get it. Here it is carried through. First give two quantities names, using the notation of the Fun Time on page 16:
is the length of the longest example, and the lecture mentions that some people call the squared radius. is the minimum from fact one divided by , so , and when the data is linearly separable.
Step one: accumulate fact one times. , so . Each correction adds at least :
Step two: accumulate fact two times. , and each correction adds at most :
Step three: divide. When , step one says , so is not the zero vector and we can divide by its length. Use the lower bound from step one for the numerator and the upper bound from step two for the denominator (replacing the denominator with a larger number makes the whole fraction smaller):
The constant on the handout is .
Step four: the left side cannot exceed 1. The left side is the inner product of two vectors of length 1, that is, for the angle between and , which is at most 1. This time it really does measure how close the directions are, since the effect of length has been divided out, and that answers the handout's "(really?)". Therefore
Read the inequality this way: whenever PLA has made corrections, cannot exceed . PLA keeps correcting only while there are mistakes, and since the number of corrections is bounded, at some moment it must fail to find a mistake, which is to say it halts, and when it halts there are no mistakes on . The way the lecture puts it: each correction brings the two a little closer, and the closeness grows with the square root of the number of corrections, but the inner product of the two vectors is at most 1 and cannot keep rising without limit.
Nothing in the derivation uses "how the mistake is picked". Cycling in the original order, cycling in a random order, and picking any wrong example each time are all covered, which is why the preview on page 11 says any PLA variant, and the condition left blank in the parentheses is linear separable.
Three additions in this article. First, has a geometric meaning: for a point , is the length of its projection onto the direction of , so measures "how far the closest example is from the separating hyperplane determined by , in the space of dimension with added". When presenting fact one, the lecture already describes , not yet divided by , as a signed distance; that is a looser way of speaking, and strictly it is a distance only after dividing by the length. The farther the data is from the boundary (large ) and the shorter the examples themselves (small ), the lower the bound. Second, there is more than one . Any that classifies the data perfectly can be plugged in, each gives its own bound, all of them hold, and the one with the largest gives the tightest bound. Third, the condition is actually used: both accumulations start from 0.
Checking the bound against the four-point example
Go back to the made-up data from "A numerical example: four points" above. Its classifies the data perfectly, so it can be used directly as .
- : for the four examples is , , , and , so the maximum is .
- : is for all four examples (computed at the end of the PLA example), and , so and .
- The bound: .
The run actually made 6 corrections, and . The two facts can also be checked step by step. In the table below, is the number of corrections made so far, and is taken from the table in that example:
| Lower bound | Upper bound | Lower bound | |||||
|---|---|---|---|---|---|---|---|
| 1 | |||||||
| 2 | |||||||
| 3 | |||||||
| 4 | |||||||
| 5 | |||||||
| 6 |
What this table means. The inner product column goes up by exactly 1 each time, because for this data every example's equals the minimum 1, and the inequality in fact one is an equality here. The squared length column is far below its upper bound, and it can even decrease: it drops from to because the middle term at that step is , and adding gives a net change of . Fact two throws this negative term away entirely and also treats every example as the longest one, so its upper bound is loose, and that is where 60 being ten times 6 comes from. The column climbs all the way to 1, meaning the direction of ends up exactly the same as that of ; at every step it is no lower than the lower bound in the rightmost column.
Try a different . also classifies all four correctly: is , , , and , the minimum is , , , and the bound is . It holds just the same, only more loosely. For this data no has a larger than (the argument is an addition in this article): take any vector of length 1 and average the four values with weights , , , ; the result is the inner product of with , which cannot exceed the length of the latter, . The minimum of four numbers is no larger than their weighted average, so . 60 is therefore the best number this theorem can give for this data.
The bound holds for every variant, and this can be verified too: run cyclic PLA once for each of the 24 orderings of the four examples, and the number of corrections is 6, 12, or 14; pick an arbitrary example among the misclassified ones each time, enumerate every possible way of picking, and the number of corrections is at least 6 and at most 15. None of them exceeds 60.
Fun Time: the bound in terms of R and ρ
Question (page 16): Let's upper-bound , the number of mistakes that PLA 'corrects'. Define
We want to show that . Express the upper bound by the two terms above.
Answer: 2. The handout's explanation: the maximum value of is 1, and since mistake corrections increase the inner product by , the maximum number of corrected mistakes is . The constant was computed above as , so .
The other choices can be ruled out from two angles. One is the square root: the inequality is and has to be squared to get ; choice 1 is missing the square and choice 3 squares only half. The other is the direction: data farther from the separating line should be easier to separate, so a larger should give a smaller bound, and choice 4 has in the numerator, the wrong way around. Check with the four-point example: choice 1 gives , choice 3 gives , and choice 4 gives . The run actually made 6 corrections, which refutes choice 4 outright; other orderings produced 12 and 14 corrections, so choice 1 fails too. Choice 3 has the wrong units: scale all the data up by a factor of 10 ( included), and both and become 10 times larger, every step of PLA is simply scaled along with them and the number of corrections does not change, yet becomes one tenth, about , smaller than the actual 6 corrections.
What to do when the data cannot be separated: the pocket algorithm
(Video 00:00–02:48 sums up the pros and cons of PLA, 02:48–06:56 discusses noise and NP-hardness, 06:56–10:02 is the pocket algorithm, 10:02–11:34 is Fun Time, and the summary follows.)
Two practical problems with PLA's guarantee
Page 17 first organizes the result of the convergence guarantee into three blocks.
Guarantee. As long as the data is linear separable and corrections are made only by mistake: the inner product of and grows fast, the length of grows slowly, and the PLA 'lines' are more and more aligned with , so it halts.
Pros. Simple to implement, fast, works in any dimension . In the lecture, Lin explains that the whole algorithm consists of vector operations only and uses no geometric property specific to two dimensions, so two dimensions and a hundred dimensions are about the same to the program, apart from taking 50 times the computation time.
Cons. The handout lists two:
- It 'assumes' linear separable to halt, and this property is unknown in advance. The handout adds in parentheses: no need for PLA if we know . The lecture calls this a circular argument: confirming that the assumption holds requires finding a first, but finding is exactly what PLA is for.
- It is not fully sure how long halting takes, because depends on . can be computed directly from the data; cannot. The handout adds one remark: though practically fast.
The lecture combines the two into the situation you face in real use: all you can do is let PLA keep running, and be glad if it halts; if it does not, it may not have run long enough yet, or the data may not be separable at all, and you cannot tell the two cases apart.
Noisy data: step back and look for the line with the fewest mistakes
Page 18 changes one place in the previous lecture's flowchart: the box for the target function now has "+ noise" added. The sources of noise mentioned in the lecture are a bank having issued cards wrongly in the past, with bad results afterwards, or its financial advisors getting some things wrong. As a result, even if really is a line, the we get is not necessarily linearly separable. The question becomes: how to at least get on noisy ?
The reasoning on page 19 has three steps:
- Assume 'little' noise, so most of the data still has . The reason given in the lecture is that if the noise were large enough to drown out , what we would be learning is the noise.
- If so, on means that most of the data has .
- Since no line makes no mistakes at all, look for the line that makes the fewest:
The double square brackets are the handout's notation: the value is 1 when the condition inside holds and 0 when it does not, so this sum is the number of examples that gets wrong on . The figure on the same page is the middle one from page 13: a diagonal line classifies most of the points correctly, with a few points left on each side that are mixed into the other.
The handout follows immediately with: NP-hard to solve, unfortunately. For those who have not heard the term, the lecture explains it as a very hard problem for which no efficient algorithm is currently known; Lin also jokes that anyone with an efficient solution should come and collaborate right away. So the practical question is: can we modify PLA to get an 'approximately good' ?
Pocket: keep the best line in your pocket
The pocket algorithm on page 20 is PLA plus a few lines (the handout prints the original parts of PLA in black and the added parts in orange):
- Initialize the pocket weights .
- For :
- Find an example that gets wrong; the handout notes that it is found in a "random" way.
- (Try to) correct the mistake: .
- If makes fewer mistakes than , replace by .
- Run until "enough" iterations, and return (called ) as .
There are three differences from PLA. First, there is a pocket, which holds the line with the fewest mistakes seen so far; keeps being corrected as usual, unaffected by the pocket. The analogy in the lecture is a greedy child clutching the most fun toy in hand, who throws it away for a new one on seeing something more fun; Lin adds that foreigners are more reserved, which is why they describe it as keeping it in a pocket. Second, the mistake is picked at random. The reason given in the lecture is the hope of seeing more variety among the lines along the way, which gives a better chance of running into a good one. Third, the stopping condition is different. PLA stops when there are no mistakes, a condition that never arrives when the data cannot be separated, so it is changed to running until you feel it has been enough iterations.
The handout's wording about the result is cautious: (somewhat) 'best' weights. It does not guarantee finding the line with the fewest mistakes. In the lecture, Lin says there is theory showing that the line it finds is fairly good, but this course does not go into it.
A numerical example: after adding one noisy example
The following is also an example made up for this article. To the four points above, add a fifth example , labeled . According to the line it should be positive, so think of it as a record that was entered wrongly.
These five cannot be separated. The midpoint of the two positive examples and is ; the weighted average of the three negative examples in the proportions , , , , is also . Any line that predicts both positive examples as positive predicts their midpoint as positive; any line that predicts all three negative examples as negative predicts their weighted average as negative. The same point cannot be both positive and negative, so a perfect line does not exist, and the best line gets at least one example wrong.
To make the run reproducible, no randomness is used here; the next mistake is found in the order 1 to 5 (the same as cyclic PLA). At the start , all five scores are 0 and all are wrong, and the pocket starts with (5 mistakes).
| Correction number | Point used | Mistakes of (which examples) | ||
|---|---|---|---|---|
| 1 | 1 | 3 (3, 4, 5) | replaced by , 3 mistakes | |
| 2 | 3 | 4 (2, 3, 4, 5) | not replaced | |
| 3 | 4 | 3 (2, 3, 5) | not replaced, not fewer than 3 | |
| 4 | 5 | 2 (1, 2) | replaced by , 2 mistakes | |
| 5 | 1 | 2 (2, 3) | not replaced | |
| 6 | 2 | 4 (2, 3, 4, 5) | not replaced | |
| 7 | 3 | 3 (2, 3, 5) | not replaced | |
| 8 | 5 | 2 (1, 2) | not replaced |
Keep running, and correction 25 gives , which gets only example 5 wrong, and the pocket is replaced by it. It was proved above that at least one example must be wrong, so this is already the line with the fewest mistakes.
What this example means. First, the number of mistakes of itself is 3, 4, 3, 2, 2, 4, 3, 2, up and down, with no trend toward getting better. Stop at an arbitrary point midway and take the of that moment, and you may get a very bad line; the pocket exists for exactly this. Second, if you stop at correction 8, what is returned is , which gets both positive examples wrong and is still some way from the best line. There is no standard answer to how many is "enough", and this is where the handout writes somewhat. Third, after every correction all five examples have to be recomputed to know how many are wrong, and this cost is the point of the Fun Time below. Running the earlier PLA program on the same data, it still has not stopped after 10,000 corrections.
Fun Time: pocket or PLA
Question (page 21): Should we use pocket or PLA? Since we do not know whether is linear separable in advance, we may decide to just go with pocket instead of PLA. If is actually linear separable, what's the difference between the two?
- pocket on is slower than PLA
- pocket on is faster than PLA
- pocket on returns a better in approximating than PLA
- pocket on returns a worse in approximating than PLA
Answer: 1. The handout's explanation: because pocket needs to check whether is better than in each iteration, it is slower than PLA; on linear separable , is the same as , both making no mistakes.
The lecture gives two reasons for the slowdown, and the handout writes only the second. One is the effort of storing what is in the pocket. The other is that comparing which of two lines is better requires going through all the data; PLA only needs to find one mistake to move on. This article puts the second reason in numbers: a PLA correction, at its cheapest, computes a single inner product; pocket has to compute more inner products after every correction to count the mistakes.
Choice 2 points the wrong way. Choices 3 and 4 are wrong because, when the data is linearly separable, eventually reaches a line that makes no mistakes; at that moment its number of mistakes is 0, necessarily fewer than what is in the pocket (unless the pocket already holds it), so the pocket ends up holding the very line PLA would return, and the two are equally good.
Summary of this lecture
Page 22 condenses each of the four sections into a line: the perceptron hypothesis set is hyperplanes in , that is, linear classifiers; PLA corrects mistakes and improves iteratively; there is no mistake eventually if the data is linear separable; for non-separable data, hold the (somewhat) 'best' weights in the pocket. The handout's preview of the next lecture is the zoo of learning problems, and the lecture says machine learning can do more than yes/no questions.
Of the two kinds of questions on page 11, this lecture has answered half: PLA halts when the data is linearly separable, and once it halts it is entirely right on . "Outside , is still close to " still has no answer.
Self-check
1. Currently . An example arrives whose original point is , with . Is it misclassified? If so, compute and confirm how much this example's score changes after the correction.
Answer: with added, and the score is , whose sign differs from , so it is misclassified. . The new score is , and this time it is correct. The score goes from to , an increase of , exactly .
2. There are only two examples: labeled and labeled . How many corrections does PLA make starting from ? Compute with and with .
Answer: and . At the first step the score is 0, which counts as wrong, and . Check example 2: , the same sign as ; example 1: . It halts after 1 correction. . With : is for both examples, , , and the bound is . With : the two values are and , the minimum is , , and the bound is . Both bounds hold; is an integer, so means at most 1 correction, and for this data the bound is tight.
3. Where in the proof is "correct only when there is a mistake" used? If someone changed the algorithm to add every example whether it is classified correctly or not, would each of the two facts still hold?
Criteria: the answer should point out that it is used only in fact two, to say that and can be dropped. Fact one holds for any example and is unaffected. If correctly classified examples are added, the middle term is positive, and the amount by which grows at a time is no longer limited by ; in the extreme case of adding the same correctly classified example over and over, grows in proportion to , the lower bound on no longer grows with , and no upper bound on follows.
4. Multiply every (including ) by 10. How does change? What about the number of PLA corrections?
Answer: becomes 100 times larger. is the minimum of the projections of the onto a unit vector, so it becomes 10 times larger and becomes 100 times larger. Dividing one by the other, the bound is unchanged. When PLA starts from , the at every step is just 10 times the original, the signs of the scores do not change, and the mistakes picked and the number of corrections are exactly the same. Actually running it on the four-point example: after scaling it is still 6 corrections, and the final is .
5. While running pocket, six consecutive corrections produce that make 4, 2, 3, 2, 1, 3 mistakes on , and the initial pocket makes 5 mistakes. How many mistakes does the line in the pocket make after each correction? If you stop at this point, which correction's is returned?
Answer: the pocket is 4, 2, 2, 2, 1, 1 in turn. The 2 mistakes at the fourth correction are not "fewer" than the 2 in the pocket, so it is not replaced. On stopping, the from the fifth correction is returned, not the last one (the last one makes 3 mistakes).
Exercises and corresponding reading
The corresponding textbook sections. The Fall 2026 course page maps this lecture to 1.1.2 and 3.1 of Learning from Data (LFD), and puts this lecture's four videos on the W2 list of videos to watch before class.
What the Fall 2026 extended slides (02e) add. 02e_handout.pdf on the same course page has four things beyond the MOOC handout:
- How to treat sign(0). Starting from , the first step necessarily runs into sign(0); the extended slides list four conventions, , , , and random, and say that the choice matters little as long as usually becomes non-zero. The numerical examples in this article use "0 counts as wrong", which corresponds to the third of these (sign(0) = 0, correct whenever it comes up).
- The role of : every correction changes the zeroth component of by .
- The full derivation that joins the two facts into , which the extended slides call the Magic Chain; its content is the four steps in "Putting the two facts together" in this article.
- A comparison of pocket's pattern of "generate candidates, keep the better one" with contemporary approaches to aligning generative AI, citing a paper by Lu, Lin, and Wang at the ICML 2026 Workshop on Generative and Agentic AI for Biology.
Homework. The first homework of both semesters has problems on this lecture; there are no official solutions, and grading is limited to enrolled students.
- Q3–4, Q7–8: how scaling the data and the value of change PLA's bound and behavior; the problems cite this lecture's page on the convergence bound. Self-check question 4 is a warm-up.
- Q9: a derivation about using a perceptron to detect hate posts.
- Q10–12: run randomly picking PLA 1000 times on the first 200 examples of LIBSVM's rcv1_train.binary, plot the histogram of the number of corrections and how changes, then compare with the variant that "keeps correcting the same example until it is right". Q13 is a bonus.
- Q2–4: the minimum number of corrections needed to guarantee fixing one mistake, how the bound changes after normalizing the inputs, and the relation between multiclass PLA with and binary PLA.
- Q11–16 (programming problems): run random PLA 1000 times on hw1_train.dat (, ), measure the fraction of runs with , the number of corrections, and the median of , then see how the number of corrections changes when is shrunk by half and when is changed to 0.5 or 0.
Without official solutions, you can check a programming problem yourself like this: on small data you generate yourself and know to be linearly separable, confirm that the error on the training data is 0 when the program halts, and check whether the number of corrections exceeds . The guide to the whole assignment is in Foundations Homework.
Hands-on. Two things you can do tonight:
- After watching the second video, without looking at the handout, write PLA in Python in 20 lines or fewer, and use this article's four-point example to check whether the output is and 6 corrections.
- Generate 50 linearly separable points on the two-dimensional plane yourself, run your PLA on them, draw the line after every correction, and compare with the figures on page 10 of the handout. As the handout reminds you, the feature values have to be much larger than for the picture to look good.
Further reading. The Stanford CS229 guide introduces perceptrons and linear classification in a different way. Caltech Learning from Data is the course Abu-Mostafa teaches in English from the same textbook.
Next lecture. Foundations Lecture 3: Types of Learning classifies learning problems by output, labels, the way data is obtained, and input; yes/no questions are only one cell of the grid.
Course video sources
Original videos: Perceptron Hypothesis Set, Perceptron Learning Algorithm, Guarantee of PLA, Non-Separable Data
Checked: 2026-10-13. The video IDs, titles, and lengths were taken from the official playlist on that day; the videos have no caption track on YouTube, and what this article reports from the lecture comes from transcripts made for it.
Update Log
- 2026-10-13: The series now has one article per lecture; this lecture is a newly split-out article, written from the handout and the video transcripts.
References
- Foundations Lecture 2 handout, 02_handout.pdf: the main source for this article. The content of the four sections, the derivations of the two PLA facts, the four Fun Time questions, and the demonstration figures on page 10 all come from this handout.
- Machine Learning Foundations YouTube playlist: the IDs and titles of the four videos; what the text reports as said "in the lecture" is paraphrased from machine transcripts of these four videos.
- Hsuan-Tien Lin > MOOCs: the official entry for handouts and recordings.
- Machine Learning, Fall 2026 course page: the W2 list of videos to watch before class and the LFD sections.
- Foundations Lecture 2 extended slides (02e_handout.pdf): the conventions for sign(0), the role of , the Magic Chain, and the contemporary comparison for pocket.
- Fall 2024 Homework 1 (hw1_red.pdf): the content of Q3–4 and Q7–13.
- Fall 2026 Homework 1 (hw1.pdf) and hw1_train.dat: the content of Q2–4 and Q11–16 and the data for the programming problems.
- LIBSVM Data: rcv1_train.binary: the data used by the programming problems of Fall 2024 HW1.
- Learning from Data textbook website: the source of 1.1.2 and 3.1.
- Caltech Learning from Data: further reading, an English-language course from the same textbook.
- Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques: Course Overview: where this lecture sits in the whole course and the reading routes.
Loading...