Salta al contenuto
Note per Studenti ADALINE and Gradient Descent Report

ADALINE and Gradient Descent

In questa pagina 6

The perceptron of Halfspaces and the Perceptron AlgorithmA perceptron computes h(x) = sign(w^T x) where x includes a constant coordinate x0 = 1 that absorbs the threshold into the bias weight w0; its decision boundary is the hyperplane w^T x = 0 (a line in the plane, x2 = -(w1/w2) x1 - w0/w2). The Perceptron Learning Algorithm scans the data and, on every misclassified point, replaces w by w + y x. A single perceptron cannot separate the exclusive-or pattern; two perceptrons feeding a third give a multilayer network.Halfspaces and the Perceptron Algorithm → learns only from misclassified points, and has no notion of "how wrong" a point is. ADALINE (the handwritten title reads "Adaline, Adaptive Neural Network") defines a smooth error on all the points and reduces it step by step. The error function is the one introduced in The Statistical Learning Framework and Empirical Risk MinimizationA hypothesis is judged by an error function computed pointwise on the data and averaged: the in-sample (empirical) error E_in(h) = (1/N) sum of e(f(x_i), h(x_i)); with squared differences it is the sum of squared errors. The learning algorithms of the course all search the hypothesis set for the h with small in-sample error, but what matters is the error on new inputs drawn from the same input distribution, so the data must cover the regions where the inputs live, and part of the data must be held back to check the result.The Statistical Learning Framework and Empirical Risk Minimization →; the same minimisation in closed form is Linear RegressionIn linear regression the hypothesis is h(x) = w^T x and the in-sample error is E_in(w) = (1/N) sum (w^T x_i - y_i)^2 = (1/N) times the squared norm of (Xw - Y), with X the N-row data matrix. Setting the gradient (2/N) X^T (Xw - Y) to zero gives the normal equations X^T X w = X^T Y and the closed-form solution w = (X^T X)^{-1} X^T Y, where (X^T X)^{-1} X^T is the pseudo-inverse of X; only a small square matrix (number of features) has to be inverted. Polynomial regression is the same formula on a data matrix with columns 1, x, x^2, ...Linear Regression →. Code: 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 unit

Inputs x0=1,x1,…,xdx_0=1,x_1,\dots,x_d with weights w0,…,wdw_0,\dots,w_d feed one node. The class predicted is h(x)=sign⁡(wTx)h(\mathbf{x})=\operatorname{sign}(\mathbf{w}^T\mathbf{x}), but the node computes first

ϕ(wTx),\phi(\mathbf{w}^T\mathbf{x}),

where ϕ\phi is the activation function. For ADALINE ϕ\phi is the identity: ϕ(wTx)=wTx\phi(\mathbf{w}^T\mathbf{x})=\mathbf{w}^T\mathbf{x}. (For the perceptron the output after the activation is the sign; for Logistic RegressionLogistic regression models the probability p of the positive class: the logit (log of the odds p/(1-p)) is assumed linear in the input, logit(p) = w^T x = z. Inverting it gives p = 1/(1 + e^(-z)), the logistic sigmoid; an object goes to class 1 when the sigmoid exceeds 0.5, i.e. when z > 0. The parameters are learned by maximising the Bernoulli log-likelihood, equivalently minimising the cost J = -y log(yhat) - (1 - y) log(1 - yhat), by gradient descent with gradient (1/m) X^T (h(x) - y).Logistic Regression → the activation is the sigmoid.) During training the error is computed on the real number wTx\mathbf{w}^T\mathbf{x}, not on the sign.

2. The error to minimise

With the target f(xi)=yif(\mathbf{x}_i)=y_i and h(xi)=wTxih(\mathbf{x}_i)=\mathbf{w}^T\mathbf{x}_i,

Ein(h)=1N∑i=1N(f(xi)−h(xi))2=1N∑i=1N(yi−wTxi)2(minimise!)E_{\text{in}}(h)=\frac1N\sum_{i=1}^{N}\big(f(\mathbf{x}_i)-h(\mathbf{x}_i)\big)^2=\frac1N\sum_{i=1}^{N}\big(y_i-\mathbf{w}^T\mathbf{x}_i\big)^2\qquad\text{(minimise!)}

3. Gradient descent

Idea (the sketch). Plot EinE_{\text{in}} as a function of one weight ww: a bowl. At a point on the left of the minimum the tangent has negative slope, on the right positive slope (the page writes the sign next to each tangent). To go down, move against the slope: to the right when the slope is negative, to the left when it is positive. Repeating, the successive points w1,w2,…w_1,w_2,\dots form a staircase that gets shorter near the bottom, because the slope there is smaller. In several variables the slope becomes the gradient ∇Ein\nabla E_{\text{in}} (see Differentiability and the Gradientf is differentiable at x0 if f(x) = f(x0) + grad f(x0)·(x - x0) + o(‖x - x0‖), i.e. near x0 the graph is indistinguishable from the tangent plane z = f(x0) + grad f(x0)·(x - x0). Differentiable implies continuous, with partial derivatives and D_v f = grad f · v; continuous partial derivatives imply differentiable. The gradient gives the direction of steepest increase (slope ‖grad f‖) and is orthogonal to level curves.Differentiability and the Gradient →) and the update rule is

  wnew=wold−η ∇Ein  \boxed{\;\mathbf{w}_{\text{new}}=\mathbf{w}_{\text{old}}-\eta\,\nabla E_{\text{in}}\;}

with η\eta the learning rate, a positive number that scales the step. A second sketch of the page draws a curve with several bumps and a few steps of different length, to say that the result depends on where the steps start and how long they are; the handwriting does not allow more to be read. Convexity (why for this error the bowl has a single bottom) is in the course only as a textbook pointer; the related prerequisites are in Convex Functions in Several VariablesA set C is convex if it contains the segment between any two of its points; f on C is convex if f(tx + (1-t)y) <= t f(x) + (1-t) f(y) for t in [0,1], i.e. the graph lies below chords. If f is differentiable it is convex if and only if f(y) >= f(x) + grad f(x)·(y - x) (graph above every tangent plane). If f is C^2 on an open convex set it is convex if and only if the Hessian is positive semidefinite at every point; positive definite everywhere implies strictly convex (not conversely: x^4). For a convex function every critical point is a global minimum.Convex Functions in Several Variables →.

4. The gradient of EinE_{\text{in}}

Component jj (derivative of a square, then of the inner function yi−wTxiy_i-\mathbf{w}^T\mathbf{x}_i with respect to wjw_j, which is −xij-x_i^j, the jj-th coordinate of the ii-th example):

∂Ein∂wj=∂∂wj1N∑i=1N(yi−wTxi)2=2N∑i=1N(yi−wTxi)(−xi j),j=0,1,…,d.\frac{\partial E_{\text{in}}}{\partial w_j}=\frac{\partial}{\partial w_j}\frac1N\sum_{i=1}^{N}\big(y_i-\mathbf{w}^T\mathbf{x}_i\big)^2=\frac2N\sum_{i=1}^{N}\big(y_i-\mathbf{w}^T\mathbf{x}_i\big)\big(-x_i^{\,j}\big),\qquad j=0,1,\dots,d.

Written for j=0,1,…,dj=0,1,\dots,d, all components share the factor (yi−wTxi)(y_i-\mathbf{w}^T\mathbf{x}_i), the error of example ii, multiplied by the coordinate of the example (for j=0j=0 it is −xi0=−1-x_i^0=-1: the bias weight moves with the plain error).

Matrix form. Let Y∈RN×1Y\in\mathbb R^{N\times1} be the vector of labels, XX the N×(d+1)N\times(d+1) data matrix (one row per example, first column all ones; the page writes the number of columns as dd) and W∈R(d+1)×1W\in\mathbb R^{(d+1)\times1} the weights:

Y=[y1⋮yN],X=[x10x11⋯x1d⋮⋮xN0xN1⋯xNd],W=[w0⋮wd].Y=\begin{bmatrix}y_1\\\vdots\\y_N\end{bmatrix},\quad X=\begin{bmatrix}x_1^0&x_1^1&\cdots&x_1^d\\\vdots&&&\vdots\\x_N^0&x_N^1&\cdots&x_N^d\end{bmatrix},\quad W=\begin{bmatrix}w_0\\\vdots\\w_d\end{bmatrix}.

Dimensions of the error vector: (N×(d+1)) ((d+1)×1)(N\times(d+1))\,((d+1)\times1) gives N×1N\times1, so Y−XWY-XW is N×1N\times1; XTX^T is (d+1)×N(d+1)\times N; the product XT(Y−XW)X^T(Y-XW) is (d+1)×1(d+1)\times1, the same shape as WW. Row jj of XTX^T collects the jj-th coordinate of all examples, which is exactly the sum over ii of the component formula. Therefore

∇Ein=−XT(Y−XW)\nabla E_{\text{in}}=-X^T(Y-XW)

(the page writes it without the constant 2/N2/N, which is absorbed into η\eta; with the constant it is −2NXT(Y−XW)-\tfrac2NX^T(Y-XW), and the notebook uses delta_w = eta * 2 * X^T errors / N). The update rule becomes

  Wnew=Wold+η XT (Y−XWold)  \boxed{\;\mathbf{W}_{\text{new}}=\mathbf{W}_{\text{old}}+\eta\,X^T\,(Y-X\mathbf{W}_{\text{old}})\;}

5. The algorithm

  1. Given a data set XX and a vector of labels YY.
  2. Start with a vector WW (zeros or small random numbers in the notebooks).
  3. Apply the update rule to WW again and again (this is gradient descent, GD; the page marks it "GD").
  4. Stop by a stopping rule. The page only writes the title next to a sketch of the error curve with the minimum circled. In the notebook the loop ends when every point is classified correctly (all(adaline_output * y > 0)) or when the maximum number of epochs is reached, and the error of each epoch is stored in a vector to be plotted.

The notebook also shows the stochastic variants: the data are shuffled at each epoch and w\mathbf{w} is updated after each example (stochastic gradient descent, SGD) or after each block of examples (mini-batch). The update is then w + eta * 2 * x_i * error_i, one example at a time; the mini-batch versions of the exercises sum the errors of the block, and one of them divides by the block size (see Python Notebooks - Exercises on Perceptron, Regression and Logistic RegressionThe exercise notebooks of the course: generating two- and three-class data with NumPy distributions, training an ADALINE/perceptron by mini-batch stochastic gradient descent and plotting the errors per epoch, polynomial regression by the closed form w = (X^T X)^{-1} X^T y on a data matrix with columns 1, x, x^2, and logistic regression trained by mini-batch gradient descent on the log loss. Statement and solution code are given, with the inconsistencies between statement and code pointed out.Python Notebooks - Exercises on Perceptron, Regression and Logistic Regression →).

6. Perceptron and ADALINE compared

perceptron ADALINE
quantity compared with the label sign of wTx\mathbf{w}^T\mathbf{x} wTx\mathbf{w}^T\mathbf{x} itself
uses only misclassified points all points
update w+yx\mathbf{w}+y\mathbf{x} w−η∇Ein\mathbf{w}-\eta\nabla E_{\text{in}}
stops when no point misclassified stopping rule (zero errors or maximum epochs)

Versione ripasso

Teoria collegata