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 with weights feed one node. The class predicted is , but the node computes first
where is the activation function. For ADALINE is the identity: . (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 , not on the sign.
2. The error to minimise
With the target and ,
3. Gradient descent
Idea (the sketch). Plot as a function of one weight : 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 form a staircase that gets shorter near the bottom, because the slope there is smaller. In several variables the slope becomes the gradient (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
with 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
Component (derivative of a square, then of the inner function with respect to , which is , the -th coordinate of the -th example):
Written for , all components share the factor , the error of example , multiplied by the coordinate of the example (for it is : the bias weight moves with the plain error).
Matrix form. Let be the vector of labels, the data matrix (one row per example, first column all ones; the page writes the number of columns as ) and the weights:
Dimensions of the error vector: gives , so is ; is ; the product is , the same shape as . Row of collects the -th coordinate of all examples, which is exactly the sum over of the component formula. Therefore
(the page writes it without the constant , which is absorbed into ; with the constant it is , and the notebook uses delta_w = eta * 2 * X^T errors / N). The update rule becomes
5. The algorithm
- Given a data set and a vector of labels .
- Start with a vector (zeros or small random numbers in the notebooks).
- Apply the update rule to again and again (this is gradient descent, GD; the page marks it "GD").
- 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 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 | itself |
| uses | only misclassified points | all points |
| update | ||
| stops when | no point misclassified | stopping rule (zero errors or maximum epochs) |
Versione ripasso
- ADALINE: identity activation ; prediction , training error on the real value .
- Error: .
- Gradient descent: , learning rate; move against the slope; steps shrink near the minimum; the result depends on the start and on the step length.
- Component: , with for the bias.
- Shapes: is , is , is . (constant absorbed in ); .
- Algorithm: given ; start with ; apply the update; stop when all points are correct or at the maximum number of epochs; store the error of each epoch.
- Variants: SGD (one example at a time), mini-batch; shuffle the data at each epoch. The notebook also draws the sum of the errors of each epoch, to check that it goes down.
- Sketch of the bowl: plot against one weight ; the tangent on the left of the minimum has negative slope, on the right positive slope; the iterates form a staircase that gets shorter near the bottom. A second sketch with bumps shows that the result depends on where the steps start.
- Gradient step in matrix form: if the constant is kept; the notebook uses
eta * 2 * X^T errors / N. - Perceptron uses only misclassified points (); ADALINE uses all points.
- Typical errors: sign of the gradient (the update is ); forgetting the bias column; too large or too small; shuffling rows without their labels.
- Related: 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 → (closed form), 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 → (sigmoid activation), 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 → (perceptron).
Teoria collegata
- Halfspaces and the Perceptron Algorithm
- Introduction to Machine Learning - Program Map
- Linear Regression
- Logistic Regression
- Python Notebooks - Exercises on Perceptron, Regression and Logistic Regression
- Python Notebooks - Perceptron and ADALINE from Scratch
- Python Notebooks - Random Samples, Plots and Linear Algebra with NumPy
- The Statistical Learning Framework and Empirical Risk Minimization