ML Study Hub

Home / MLT / Week 9

Week 9 · Perceptron and logistic regression

Official syllabus line: Discriminative Models - Perceptron; Logistic Regression
About 80 minEnd Term material (see exam radar)BSCS2007 · Arun Rajkumar
Before you start: MLF week 7 (gradient descent), MLF week 9 (sigmoid and log-loss), MLT week 8 (generative vs discriminative).
Watch first: the animated story of this weekAbout 1-2 minutes of real 3D animation with a friendly voice. Pause any time.
Read the words of the video
  1. Imagine you have red and green marbles scattered on a table. Your goal is to find a straight stick that you can place so all the red marbles are on one side and all the green marbles are on the other. This stick helps us separate things. This is exactly what a linear classifier does!
  2. The Perceptron is like a simple teacher. It nudges the stick only when a marble is on the wrong side. Logistic Regression is gentler; it nudges the stick a little for every marble, based on how wrong its probability guess was. Both try to find the best straight stick to separate our data.
  3. Let's try it! Start with the stick anywhere. Pick a red marble on the green side. Nudge the stick just enough to bring that red marble to its side. Repeat this for all marbles that are on the wrong side. Keep going until no marbles are misplaced. The stick has learned to separate them!
  4. For your exam, remember the Perceptron updates only on mistakes, and only works if data is clearly separable. Logistic Regression always finds a solution, outputting probabilities. Both give a straight line boundary! Keep practicing, and you'll soon be an expert at separating data with your clever stick!

The one-minute story

Think of it like this

Spread red and green marbles on a table and try to lay a straight stick between them so all the reds are on one side and all the greens on the other. That stick is a linear classifier, and the whole job is to find it.

The perceptron is a very simple teacher. Start with any stick. Pick up one marble at a time; if it is on the wrong side, nudge the stick a little towards the right side for that marble. Keep going round until nobody is on the wrong side. If a straight stick can separate the marbles, this is guaranteed to work.

Logistic regression is a gentler teacher. Instead of a hard "this side / that side", it gives a probability: far on the red side means "99% red", right on the stick means "50/50". Instead of nudging only on mistakes, it nudges a little for every marble, in proportion to how wrong the probability was.

Both are discriminative: they learn the boundary directly and never model what the marbles look like.

By the end of this week you can:

  • describe a linear classifier and its decision boundary;
  • run the perceptron algorithm by hand and state its convergence theorem;
  • write the logistic regression model, loss and gradient;
  • compare the perceptron and logistic regression.

Words you need

Words you need
  • Linear classifier - predicts by the sign of wTx+b\mathbf w^T\mathbf x+b.
  • Decision boundary - the set of points where wTx+b=0\mathbf w^T\mathbf x+b=0: a line in 2-D, a plane in 3-D.
  • Linearly separable - some line separates the two classes perfectly.
  • Label convention - the perceptron uses y∈{−1,+1}y\in\{-1,+1\}; logistic regression uses y∈{0,1}y\in\{0,1\}.
  • Sigmoid σ(z)=11+e−z\sigma(z)=\dfrac1{1+e^{-z}} - squashes any number into (0,1)(0,1).
  • Log-odds (logit) - ln⁡p1−p\ln\frac p{1-p}; for logistic regression it equals wTx\mathbf w^T\mathbf x.

Idea 1: Linear classifiers

Predict y^=sign⁡(wTx+b)\hat y=\operatorname{sign}(\mathbf w^T\mathbf x+b). The weight vector w\mathbf w is perpendicular to the boundary and points towards the positive side. The value wTx+b\mathbf w^T\mathbf x+b says which side and, divided by ∥w∥\|\mathbf w\|, how far from the boundary.

A trick to avoid carrying bb around: add a constant feature 1 to every x\mathbf x and absorb bb into w\mathbf w. Then the prediction is just sign⁡(wTx)\operatorname{sign}(\mathbf w^T\mathbf x).

A point (x,y)(\mathbf x,y) with y∈{±1}y\in\{\pm1\} is classified correctly exactly when y wTx>0y\,\mathbf w^T\mathbf x>0.

Idea 2: The perceptron algorithm

The idea

Perceptron (labels y∈{−1,+1}y\in\{-1,+1\}, bias absorbed):

  1. Start with w=0\mathbf w=\mathbf 0.
  2. Go through the training examples. For each (x,y)(\mathbf x,y): if y wTx≤0y\,\mathbf w^T\mathbf x\le0 (a mistake), update
w←w+y x\mathbf w\leftarrow\mathbf w+y\,\mathbf x
  1. Repeat passes over the data until a full pass makes no mistakes.

Why does the update help? After the update the new score for that same example is y(w+yx)Tx=ywTx+∥x∥2y(\mathbf w+y\mathbf x)^T\mathbf x=y\mathbf w^T\mathbf x+\|\mathbf x\|^2, which is larger by ∥x∥2>0\|\mathbf x\|^2>0. So the mistake gets less wrong (it may still need more nudges).

Convergence theorem. If the data is linearly separable with margin γ\gamma (every point is at least γ\gamma from some separating boundary, with ∥w∗∥=1\|\mathbf w^*\|=1) and every ∥x∥≤R\|\mathbf x\|\le R, then the perceptron makes at most (R/γ)2(R/\gamma)^2 mistakes and stops. If the data is not linearly separable (the classic example: XOR), it never converges, cycling forever. Also, the solution it finds is not unique: it depends on the order of the data.

Solved example: 1. A perceptron run

Question. Data: (1,2)(1,2) with y=+1y=+1; (−1,−1)(-1,-1) with y=−1y=-1; (2,−1)(2,-1) with y=+1y=+1 (bias already absorbed). Run the perceptron from w=(0,0)\mathbf w=(0,0), going through the points in this order.

Solution.

Pass 1.

  • (1,2),+1(1,2),+1: y wTx=0≤0y\,\mathbf w^T\mathbf x=0\le0 mistake. w←(0,0)+(1,2)=(1,2)\mathbf w\leftarrow(0,0)+(1,2)=(1,2).
  • (−1,−1),−1(-1,-1),-1: wTx=−3\mathbf w^T\mathbf x=-3, y⋅(−3)=3>0y\cdot(-3)=3>0 correct.
  • (2,−1),+1(2,-1),+1: wTx=2−2=0\mathbf w^T\mathbf x=2-2=0, y⋅0≤0y\cdot0\le0 mistake. w←(1,2)+(2,−1)=(3,1)\mathbf w\leftarrow(1,2)+(2,-1)=(3,1).

Pass 2.

  • (1,2)(1,2): 3+2=5>03+2=5>0 ✓. (−1,−1)(-1,-1): −3−1=−4-3-1=-4, times −1-1 is 4>04>0 ✓. (2,−1)(2,-1): 6−1=5>06-1=5>0 ✓.

No mistakes: stop with w=(3,1)\mathbf w=(3,1) after two updates. The boundary is 3x1+x2=03x_1+x_2=0.

Idea 3: Logistic regression

Instead of a hard sign, model the probability of class 1:

P(y=1∣x)=σ(wTx)=11+e−wTxP(y=1\mid\mathbf x)=\sigma(\mathbf w^T\mathbf x)=\frac1{1+e^{-\mathbf w^T\mathbf x}}

Equivalently, the log-odds are linear: ln⁡P(y=1∣x)P(y=0∣x)=wTx\ln\dfrac{P(y=1\mid\mathbf x)}{P(y=0\mid\mathbf x)}=\mathbf w^T\mathbf x. The class is predicted as 1 when P>0.5P>0.5, i.e. wTx>0\mathbf w^T\mathbf x>0, so the decision boundary is still the straight line wTx=0\mathbf w^T\mathbf x=0.

Try it yourself

Slide ww and bb to bend and shift the S-curve through the data. The dashed vertical line is the decision boundary, where the probability is 0.5.

Training by maximum likelihood. With labels yi∈{0,1}y_i\in\{0,1\} the likelihood of the data is ∏piyi(1−pi)1−yi\prod p_i^{y_i}(1-p_i)^{1-y_i} with pi=σ(wTxi)p_i=\sigma(\mathbf w^T\mathbf x_i). Taking the negative log gives the cross-entropy (log) loss:

L(w)=−∑i=1n[yiln⁡pi+(1−yi)ln⁡(1−pi)]\mathcal L(\mathbf w)=-\sum_{i=1}^n\Big[y_i\ln p_i+(1-y_i)\ln(1-p_i)\Big]

It is convex with a lovely gradient:

∇L(w)=∑i=1n(pi−yi) xi\nabla\mathcal L(\mathbf w)=\sum_{i=1}^n(p_i-y_i)\,\mathbf x_i

("prediction minus truth, times the input"). There is no closed-form solution, so use gradient descent: w←w−η∑i(pi−yi)xi\mathbf w\leftarrow\mathbf w-\eta\sum_i(p_i-y_i)\mathbf x_i.

Extras. If the classes are perfectly separable the likelihood keeps improving as ∥w∥→∞\|\mathbf w\|\to\infty, so add a regulariser λ∥w∥2\lambda\|\mathbf w\|^2 (as in ridge) to keep w\mathbf w finite. For more than two classes, replace the sigmoid with softmax (week 12).

Solved example: 2. One gradient step of logistic regression

Question. Two examples (no bias): x1=(1,2)\mathbf x_1=(1,2) with y1=1y_1=1 and x2=(−1,−1)\mathbf x_2=(-1,-1) with y2=0y_2=0. Start with w=(0,0)\mathbf w=(0,0) and use η=0.1\eta=0.1. Do one step.

Solution. With w=0\mathbf w=0, every z=0z=0, so p1=p2=σ(0)=0.5p_1=p_2=\sigma(0)=0.5.

∇L=(0.5−1)(1,2)+(0.5−0)(−1,−1)=(−0.5,−1)+(−0.5,−0.5)=(−1, −1.5)\nabla\mathcal L=(0.5-1)(1,2)+(0.5-0)(-1,-1)=(-0.5,-1)+(-0.5,-0.5)=(-1,\,-1.5)
w←(0,0)−0.1(−1,−1.5)=(0.1, 0.15)\mathbf w\leftarrow(0,0)-0.1(-1,-1.5)=(\mathbf{0.1},\,\mathbf{0.15})

The weights moved so that x1\mathbf x_1 gets a higher score and x2\mathbf x_2 a lower one. ✓

Solved example: 3. From score to probability

Question. w=(0.5, 1)\mathbf w=(0.5,\,1), bias b=−2b=-2. What is P(y=1∣x)P(y=1\mid\mathbf x) at x=(2,3)\mathbf x=(2,3)?

Solution. z=0.5⋅2+1⋅3−2=2z=0.5\cdot2+1\cdot3-2=2. p=σ(2)=11+e−2=11+0.135=0.881p=\sigma(2)=\frac1{1+e^{-2}}=\frac1{1+0.135}=\mathbf{0.881}. Predict class 1.

Idea 4: Perceptron versus logistic regression

PerceptronLogistic regression
Outputhard label ±1\pm1probability in (0,1)(0,1)
Updatesonly on mistakeson every example, scaled by the error
Lossmistake-driven (0-1 flavoured)log-loss (smooth, convex)
Convergenceonly if linearly separablealways fine (with regularisation)
Boundarya linea line

Your turn

Your turn: 1. Perceptron updates

Start from w=(0,0)\mathbf w=(0,0). Examples in order: (2,1)(2,1) with y=−1y=-1, then (1,3)(1,3) with y=+1y=+1. Apply one pass of the perceptron.

Need a hint?

Update only when y wTx≤0y\,\mathbf w^T\mathbf x\le0.

Show the full solution

First: wTx=0\mathbf w^T\mathbf x=0, mistake: w=(0,0)+(−1)(2,1)=(−2,−1)\mathbf w=(0,0)+(-1)(2,1)=(-2,-1). Second: wTx=−2−3=−5\mathbf w^T\mathbf x=-2-3=-5, y⋅(−5)=−5≤0y\cdot(-5)=-5\le0, mistake: w=(−2,−1)+(1,3)=(−1, 2)\mathbf w=(-2,-1)+(1,3)=(\mathbf{-1},\,\mathbf2).

Your turn: 2. Probabilities

A model has w=(2,−1)\mathbf w=(2,-1) and b=−3b=-3. Compute the class-1 probability at x=(3,1)\mathbf x=(3,1) and say which side of the boundary it is on.

Need a hint?

z=wTx+bz=\mathbf w^T\mathbf x+b.

Show the full solution

z=6−1−3=2>0z=6-1-3=2>0, so the positive side. p=σ(2)=0.881p=\sigma(2)=0.881.

Your turn: 3. The boundary

For w=(1,1)\mathbf w=(1,1) and b=−4b=-4, write the decision boundary and give two points on it.

Need a hint?

Set wTx+b=0\mathbf w^T\mathbf x+b=0.

Show the full solution

x1+x2=4x_1+x_2=4. E.g. (4,0)(4,0) and (2,2)(2,2). w\mathbf w is perpendicular to this line.

Your turn: 4. True or false

(a) The perceptron converges on any data set. (b) Logistic regression has a closed-form solution. (c) The logistic regression boundary is linear. (d) The log-loss is convex in w\mathbf w.

Need a hint?

Think about XOR and the gradient formula.

Show the full solution

(a) False, only if separable. (b) False, use gradient descent. (c) True. (d) True.

Your turn: 5. Margin and mistakes

The data have ∥x∥≤5\|\mathbf x\|\le5 and are separable with margin γ=0.5\gamma=0.5. Give the perceptron's mistake bound.

Need a hint?

(R/γ)2(R/\gamma)^2.

Show the full solution

(5/0.5)2=102=100(5/0.5)^2=10^2=\mathbf{100} mistakes at most. A bigger margin means fewer.

Exam radar

Exam radar

In the End Term, summaries mention logistic regression in 23 papers, perceptron in 13 and perceptron algorithm in 15, weight update in 13, decision boundary in 19, sigmoid in 10.

Typical: "perform one update of the perceptron", "when does the perceptron converge?", the gradient of the log-loss (p−yp-y times x\mathbf x), "what is the decision boundary of logistic regression?" (linear), the probability computed from given weights, and a comparison table like the one above.

Watch out - real exam trap
  • Perceptron labels are ±1\pm1; logistic labels are 0/10/1. The gradient formula (pi−yi)xi(p_i-y_i)\mathbf x_i assumes 0/10/1.
  • Update w+yx\mathbf w+y\mathbf x only on a mistake (including the "score =0=0" case).
  • Don't forget the bias: add a feature of 1.
  • The perceptron solution is not unique; logistic regression's is (when regularised).
  • Linearly separable is required for perceptron convergence, not for logistic regression to run.

Quick quiz

Quick quiz

The perceptron update on a mistake is...

  • w minus y x
  • w plus y x
  • w times y
  • w plus x
The weights are moved towards the correct side: w <- w + y x.

The decision boundary of logistic regression is...

  • a circle
  • a straight line (hyperplane)
  • a parabola
  • not defined
Probability 0.5 occurs where w transpose x = 0, a hyperplane.

The gradient of the logistic log-loss for labels in {0,1} is...

  • sum of (y - p) squared
  • sum of (p - y) x
  • sum of p x
  • sum of y x
It is the prediction error times the input.

Select all TRUE statements. (select all that apply)

  • The perceptron stops only if the data is linearly separable.
  • Logistic regression outputs a probability.
  • Logistic regression has a closed-form solution.
  • The perceptron minimises the log-loss.
Logistic regression needs gradient descent, and the perceptron is a mistake-driven rule, not a log-loss minimiser.

Recap

Remember this
  • Linear classifier: sign⁡(wTx+b)\operatorname{sign}(\mathbf w^T\mathbf x+b); w⊥\mathbf w\perp boundary; correct iff ywTx>0y\mathbf w^T\mathbf x>0.
  • Perceptron: on a mistake w←w+yx\mathbf w\leftarrow\mathbf w+y\mathbf x; converges iff separable; mistake bound (R/γ)2(R/\gamma)^2; solution not unique.
  • Logistic regression: P(y=1∣x)=σ(wTx)P(y=1\mid\mathbf x)=\sigma(\mathbf w^T\mathbf x); log-odds linear; boundary linear; cross-entropy loss, gradient ∑(pi−yi)xi\sum(p_i-y_i)\mathbf x_i; gradient descent; regularise if separable.
Formulas to keep
  • Perceptron: mistake if ywTx≤0y\mathbf w^T\mathbf x\le0; update w←w+yx\mathbf w\leftarrow\mathbf w+y\mathbf x; mistakes ≤(R/γ)2\le(R/\gamma)^2
  • Logistic: p=σ(wTx)=11+e−wTxp=\sigma(\mathbf w^T\mathbf x)=\frac1{1+e^{-\mathbf w^T\mathbf x}}; ln⁡p1−p=wTx\ln\frac p{1-p}=\mathbf w^T\mathbf x
  • Loss −∑[yln⁡p+(1−y)ln⁡(1−p)]-\sum[y\ln p+(1-y)\ln(1-p)]; gradient ∑(pi−yi)xi\sum(p_i-y_i)\mathbf x_i; step w←w−η∇L\mathbf w\leftarrow\mathbf w-\eta\nabla\mathcal L
Connected weeks (learn them together)

Practise with real exam questions

These come straight from past papers (with the real figures and the official answer key). Open 39 real questions on this week   All End Term papers