Salta al contenuto
Note per Studenti Halfspaces and the Perceptron Algorithm Report

Halfspaces and the Perceptron Algorithm

In questa pagina 4

The first learning algorithm of the course. Hypotheses are halfspaces: the input space is cut by a hyperplane and one side is labelled +1+1, the other −1-1. The convergence guarantee is in Perceptron Convergence and the Margin BoundThe distance of a point x from the hyperplane w^T x + b = 0 is |w^T x + b| / sqrt(w^T w), obtained by writing x = x_p + gamma with gamma parallel to w. If the data are linearly separable with margin gamma (a unit vector w* with y_i w*^T x_i >= gamma for all i) and every point lies in a ball of radius R, the Perceptron Learning Algorithm makes at most K <= R^2 / gamma^2 updates. Proof: after K updates w_{K+1}^T w* >= K gamma, hence norm of w_{K+1} >= K gamma; and norm of w_{K+1} squared <= K R^2; combine.Perceptron Convergence and the Margin Bound →; the same unit trained by gradient descent is in ADALINE and Gradient DescentADALINE is a perceptron whose activation function is the identity: during training the unit outputs w^T x and the in-sample error E_in = (1/N) sum (y_i - w^T x_i)^2 is minimised by gradient descent, w_new = w_old - eta * grad E_in, with partial derivatives dE_in/dw_j = (2/N) sum (y_i - w^T x_i)(-x_i^j) and matrix form grad E_in proportional to -X^T (Y - X w). The prediction is still the sign of w^T x. Learning rate eta sets the step size and a stopping rule ends the iteration.ADALINE and Gradient Descent →; code in Python Notebooks - Perceptron and ADALINE from ScratchCode of the course notebooks that implement the Perceptron Learning Algorithm first with Python lists, then with plots of the successive lines, then with NumPy arrays (data matrix X with a column of ones, update w + eta * y * x on mistakes, epoch counter), and finally ADALINE: batch gradient descent with delta_w = eta * 2 * X^T (y - X w) / N and the stochastic version that shuffles the data and updates after every example. Differences between the versions (condition for a mistake, learning rate, stopping rule) are pointed out.Python Notebooks - Perceptron and ADALINE from Scratch →.

1. The neuron model

The handwritten page draws a node (cell, neuron) that receives four inputs x1,…,x4x_1,\dots,x_4 through four weights w1,…,w4w_1,\dots,w_4 and either fires or does not (output yy). The hypothesis is

h(x)=w1x1+⋯+w4x4=∑i=14wixi,h(\mathbf{x})=w_1x_1+\dots+w_4x_4=\sum_{i=1}^{4}w_ix_i ,

and the rule is

  • ∑iwixi>threshold\sum_i w_ix_i>\text{threshold} ⇒\Rightarrow it fires;
  • ∑iwixi≤threshold\sum_i w_ix_i\le\text{threshold} ⇒\Rightarrow it does not fire.

Encoding "fires" as +1+1 and "does not fire" as −1-1:

h(x)=sign⁡(∑i=1dwixi−threshold)∈{+1,−1}.h(\mathbf{x})=\operatorname{sign}\Big(\sum_{i=1}^{d}w_ix_i-\text{threshold}\Big)\in\{+1,-1\}.

Bias trick. The part wixiw_ix_i and the term −threshold-\text{threshold} are circled in the page and then merged: add a constant input x0=1x_0=1 ("bias =1=1") with weight w0=−thresholdw_0=-\text{threshold}. Then

h(x)=sign⁡(∑i=0dwixi)=sign⁡(wTx),w=[w0w1⋮wd],  x=[x0x1⋮xd].h(\mathbf{x})=\operatorname{sign}\Big(\sum_{i=0}^{d}w_ix_i\Big)=\operatorname{sign}(\mathbf{w}^T\mathbf{x}),\qquad \mathbf{w}=\begin{bmatrix}w_0\\w_1\\\vdots\\w_d\end{bmatrix},\; \mathbf{x}=\begin{bmatrix}x_0\\x_1\\\vdots\\x_d\end{bmatrix}.

A point of the plane (x1,x2)(x_1,x_2) becomes the vector [1,x1,x2]T[1,x_1,x_2]^T: the page lifts a point with x1=1x_1=1, x2=3x_2=3 into the vector x=[1, 1, 3]T\mathbf{x}=[1,\,1,\,3]^T (coordinates x0,x1,x2x_0,x_1,x_2) and draws a third axis x0x_0 for the constant. Every example of the course (and the data matrix XX of the notebooks) carries this column of ones.

2. The boundary

The decision depends on the sign of wTx\mathbf{w}^T\mathbf{x}: positive side +1+1, negative side −1-1; wTx=0\mathbf{w}^T\mathbf{x}=0 is the boundary. In the plane write a generic line as h(x)=ax+by+ch(x)=ax+by+c; with the course symbols the boundary is

w1x1+w2x2+w0=0  ⟹  w2x2=−w1x1−w0  ⟹  x2=−w1w2 x1−w0w2.w_1x_1+w_2x_2+w_0=0\;\Longrightarrow\;w_2x_2=-w_1x_1-w_0\;\Longrightarrow\;x_2=-\frac{w_1}{w_2}\,x_1-\frac{w_0}{w_2}.

Slope −w1/w2-w_1/w_2 and intercept −w0/w2-w_0/w_2 are the two circled quantities of the page; the notebooks use exactly this to draw the line from the three weights.

3. Perceptron Learning Algorithm (PLA)

Data set: {(x1,y1),…,(xm,ym)}\{(\mathbf{x}^1,y_1),\dots,(\mathbf{x}^m,y_m)\} with yi∈{+1,−1}y_i\in\{+1,-1\}. We want h(xi)=yih(\mathbf{x}^i)=y_i for every ii (the page writes h(x1)=1  ⟺  y1h(\mathbf{x}^1)=1\iff y_1, …\dots, h(xm)=−1  ⟺  ymh(\mathbf{x}^m)=-1\iff y_m). The two ways to be wrong (framed in red) are

  • h(xi)=+1h(\mathbf{x}^i)=+1 but yi=−1y_i=-1;
  • h(xi)=−1h(\mathbf{x}^i)=-1 but yi=+1y_i=+1.

Geometry of a mistake. Draw w\mathbf{w} and x\mathbf{x} as vectors from the origin.

  1. wTxj>0\mathbf{w}^T\mathbf{x}^j>0 but yj=−1y_j=-1: the weight vector points "towards" x\mathbf{x} (acute angle) when it should point away. The page draws −x-\mathbf{x} and rotates w\mathbf{w} towards it.
  2. wTxj<0\mathbf{w}^T\mathbf{x}^j<0 but yj=+1y_j=+1: the angle is obtuse; x\mathbf{x} must be added to w\mathbf{w} so that it turns towards x\mathbf{x}.

Both cases are one formula, the update rule:

  wnew=wold+xj yj  \boxed{\;\mathbf{w}_{\text{new}}=\mathbf{w}_{\text{old}}+\mathbf{x}^{j}\,y_j\;}

Why it moves the right way (computed here): after the update on xj\mathbf{x}^j, wnewTxj=woldTxj+yj∥xj∥2\mathbf{w}_{\text{new}}^T\mathbf{x}^j=\mathbf{w}_{\text{old}}^T\mathbf{x}^j+y_j\|\mathbf{x}^j\|^2. For yj=+1y_j=+1 the value goes up by ∥xj∥2\|\mathbf{x}^j\|^2, for yj=−1y_j=-1 it goes down by the same amount: always towards the correct sign (one update may not be enough, but it improves that point).

Algorithm.

start with w (the notebooks use zeros or random numbers)
repeat
    for each example (x, y) of the data set:
        if h(x) != y  (that is, y * w^T x has the wrong sign):
            w = w + y * x
until no example is misclassified   (or a maximum number of steps is reached)

The page of "the lines" shows the successive separating lines converging between a cloud of +1+1 points (triangles) and a cloud of −1-1 points (circles). The question left open at this point, "how many steps do we need to find a solution?", is answered in Perceptron Convergence and the Margin BoundThe distance of a point x from the hyperplane w^T x + b = 0 is |w^T x + b| / sqrt(w^T w), obtained by writing x = x_p + gamma with gamma parallel to w. If the data are linearly separable with margin gamma (a unit vector w* with y_i w*^T x_i >= gamma for all i) and every point lies in a ball of radius R, the Perceptron Learning Algorithm makes at most K <= R^2 / gamma^2 updates. Proof: after K updates w_{K+1}^T w* >= K gamma, hence norm of w_{K+1} >= K gamma; and norm of w_{K+1} squared <= K R^2; combine.Perceptron Convergence and the Margin Bound →. A learning rate η\eta multiplying the update (w+η yx\mathbf{w}+\eta\,y\mathbf{x}) is used in the notebooks.

4. Limits of a single perceptron

Not every data set is separable. The page with the four points (0,0),(1,1)(0,0),(1,1) (triangles) and (0,1),(1,0)(0,1),(1,0) (dots) is the exclusive-or pattern: whichever straight line is drawn, one of the points ends on the wrong side. A single halfspace cannot represent it, and PLA, which only stops when all points are correct, would never stop (the notebooks add a maximum number of iterations for this reason).

Two perceptrons. A later page solves the same pattern with two lines p1p_1 and p2p_2: the points of one class lie between the two lines (region C1C_1), the points of the other class lie outside (region C2C_2 above p1p_1 and below p2p_2). The sketch next to it is a small network: four inputs x1,…,x4x_1,\dots,x_4, a layer of two perceptrons, and one more perceptron that combines their outputs (connections drawn with signs ++ and −-) to produce the final output. It is labelled multilayer neural network, trained with the backpropagation algorithm, and its deep versions "deep learning". The notes only name these; no derivation is given.

Which separating line? When the data are separable there are infinitely many lines that work (the family of black lines in the drawing). The notes sketch a strip of maximal width between the two clouds, with the points that touch its edges circled, and name it Support Vector Machines; again only a pointer.

Versione ripasso

Teoria collegata