Linear regression via gradient descent
Experiment 01 · supervised learning · mean-squared-error minimisation
Aim
To fit a straight line y = w·x + b to a set of scattered 2-D points by minimising the mean-squared error using batch gradient descent, and to observe how the learning rate controls convergence of the loss curve.
Theory
Linear regression assumes the target is an affine function of the input plus noise. Training searches for the parameters (w, b) that minimise the mean-squared error (MSE) over all n samples. Because MSE is convex and differentiable, we can descend its gradient: at every step we move the parameters a small amount (the learning rate) in the direction of steepest decrease.
The coefficient of determination R² = 1 - SS_res/SS_tot reports the fraction of variance explained; R² = 1 is a perfect fit.
Procedure
- Press Sample data to load a noisy linear cloud, or click anywhere on the plot to drop your own (x, y) points.
- Set the learning rate with the slider (log scale).
- Press Train to run gradient descent continuously, or Step to advance a few epochs at a time.
- Watch the purple line rotate into place while the amber MSE curve falls each epoch.
- Read the converged weight, bias and R²; try a learning rate that is too high to see the loss diverge.
Gradient descent · MSE loss
(x, y) points. Gradient descent fits y = w·x + b by minimising mean-squared error; the line and loss curve update every epoch.Controls
R² goodness of fit
References
- Bishop, C. M. Pattern Recognition and Machine Learning, Springer 2006 — Ch. 3, Linear Models for Regression.
- Hastie, Tibshirani & Friedman. The Elements of Statistical Learning, 2nd ed., Springer 2009 — Ch. 3.
- Goodfellow, Bengio & Courville. Deep Learning, MIT Press 2016 — Ch. 4-5 on gradient-based optimisation.
Aim
To train a binary classifier by logistic regression — pushing a linear score through the sigmoid function and minimising binary cross-entropy with gradient descent — and to visualise the learned linear decision boundary.
Theory
Logistic regression models the probability that a point belongs to class 1 as the sigmoid of a linear score. The sigmoid squashes any real number into (0, 1). We fit the weights by minimising binary cross-entropy (log-loss); a convenient property is that its gradient has the same simple form as linear regression — the error (prediction minus label) times the input.
The decision boundary is the line z = 0, i.e. where p = 0.5. It is always straight (linear) for this model.
Procedure
- Choose the active class (blue or amber) and click the canvas to place training points, or press Sample data.
- Set the learning rate and press Train; the sigmoid is fit by gradient descent on cross-entropy.
- Watch the white decision line settle between the two clouds while the loss curve drops.
- Read the converged weights, loss and training accuracy. Add an outlier and re-train to see the boundary shift.
Sigmoid + cross-entropy · gradient descent
Controls
References
- Bishop, C. M. Pattern Recognition and Machine Learning, Springer 2006 — Sec. 4.3, Probabilistic Discriminative Models.
- Cox, D. R. "The regression analysis of binary sequences", J. Royal Statistical Society B, 1958.
- Murphy, K. P. Machine Learning: A Probabilistic Perspective, MIT Press 2012 — Ch. 8.
Aim
To classify points by the majority vote of their k nearest neighbours under Euclidean distance, and to visualise how the decision regions and their smoothness change with k.
Theory
k-Nearest-Neighbours is a non-parametric, "lazy" learner: there is no training phase. To classify a query point we measure its distance to every stored example, take the k closest, and assign the label held by the majority of them. Small k gives jagged, high-variance boundaries that hug noise; large k smooths the regions but can blur genuine class structure (bias-variance trade-off).
Cost is O(n) per query because every training point is examined; this is why k-NN scales poorly to huge datasets without spatial indexes.
Procedure
- Select the active class and click to place training points, or press Sample data.
- Adjust k with the slider and watch the shaded decision regions reclassify live.
- Set k = 1 to see the Voronoi-like jagged boundary, then raise k to smooth it.
- Place an isolated point and observe how a larger k makes the model ignore it.
k-Nearest-Neighbours decision regions
Controls
k nearest training points.References
- Cover & Hart. "Nearest neighbor pattern classification", IEEE Trans. Information Theory, 1967.
- Hastie, Tibshirani & Friedman. The Elements of Statistical Learning, 2nd ed. — Sec. 13.3.
- Duda, Hart & Stork. Pattern Classification, 2nd ed., Wiley 2001 — Ch. 4.
Aim
To partition unlabelled 2-D points into k clusters using Lloyd's algorithm with k-means++ seeding, and to confirm that the within-cluster inertia decreases monotonically to convergence.
Theory
k-means is unsupervised: it seeks k centroids that minimise the total squared distance from each point to its nearest centroid (the inertia). Lloyd's algorithm alternates two steps — assign every point to its closest centroid, then update each centroid to the mean of its assigned points. Each step can only lower the inertia, so it converges, though only to a local optimum. k-means++ seeding spreads the initial centroids by distance weighting, which greatly improves the result.
Procedure
- Press Re-seed blobs to generate fresh Gaussian clusters.
- Set the number of clusters k and the point count.
- Press Run Lloyd to animate, or Step to watch one assign-update iteration at a time.
- Observe the inertia and "moved" distance drop each iteration until "converged" appears.
- Re-run a few times with the same k to see different local optima from different seeds.
Lloyd's algorithm
Controls
References
- Lloyd, S. P. "Least squares quantization in PCM", IEEE Trans. Information Theory, 1982.
- Arthur & Vassilvitskii. "k-means++: The advantages of careful seeding", SODA, 2007.
- MacQueen, J. "Some methods for classification and analysis of multivariate observations", Berkeley Symp., 1967.
Aim
To build a binary decision tree on 2-D labelled points by recursively choosing axis-aligned splits that maximise the impurity decrease (Gini or entropy), and to draw the resulting rectangular decision regions.
Theory
A decision tree partitions feature space with a sequence of axis-aligned thresholds. At each node the algorithm scans every feature and every candidate threshold and picks the split that most reduces impurity. Impurity measures how mixed the labels are in a node; both Gini and entropy are zero for a pure node and maximal for a 50/50 mix. The information gain is the parent impurity minus the size-weighted average impurity of the two children. Splitting stops at a maximum depth or when a node is pure.
Procedure
- Select the active class and click to place points, or press Sample data for an XOR-like layout.
- Choose the impurity criterion (Gini or entropy) and the maximum depth.
- Press Build tree; the algorithm recursively finds the best split and shades each leaf rectangle by its majority class.
- Increase the depth to watch the boxy boundary carve finer regions and the training accuracy rise.
Recursive Gini / entropy splits
Controls
References
- Breiman, Friedman, Olshen & Stone. Classification and Regression Trees (CART), Wadsworth 1984.
- Quinlan, J. R. "Induction of decision trees", Machine Learning, 1986 (ID3).
- Hastie, Tibshirani & Friedman. The Elements of Statistical Learning — Sec. 9.2.
Aim
To compute the principal components of a 2-D point cloud from its covariance matrix using power iteration, and to draw the principal axes together with the projection of every point onto the first component.
Theory
Principal Component Analysis finds the orthogonal directions of greatest variance in the data. After centering the points, we form the 2x2 covariance matrix; its eigenvectors are the principal axes and its eigenvalues are the variance captured along each. We extract the top eigenvector by power iteration — repeatedly multiplying a random vector by the covariance matrix and renormalising — then deflate to obtain the second. Projecting each point onto the first component reduces it from two dimensions to one while preserving as much spread as possible.
Procedure
- Press Sample data for an elongated tilted cloud, or click to add your own points.
- Press Run PCA; the covariance matrix is formed and power iteration converges on the principal axes.
- The long cyan arrow is PC1 (most variance), the short purple arrow is PC2; faint lines drop each point onto PC1.
- Read the eigenvalues and the variance explained by PC1. Stretch the cloud along one direction to drive that ratio toward 1.
Covariance · power-iteration eigenvectors
Controls
--. The two axes are orthogonal by construction.References
- Pearson, K. "On lines and planes of closest fit to systems of points in space", Phil. Mag., 1901.
- Jolliffe, I. T. Principal Component Analysis, 2nd ed., Springer 2002.
- Golub & Van Loan. Matrix Computations, 4th ed., JHU Press 2013 — power iteration.
Aim
To train a multilayer perceptron (2 → 8 → 8 → 1) with two tanh hidden layers and a sigmoid output by full backpropagation, and to watch it learn a non-linear decision boundary on XOR, circles, moons or spiral data.
Theory
A multilayer perceptron stacks linear maps and non-linear activations so it can represent decision boundaries no single line could. The forward pass computes activations layer by layer; backpropagation then applies the chain rule backwards to obtain the gradient of the loss with respect to every weight. For the sigmoid output with binary cross-entropy the output error simplifies neatly to (prediction - target). Xavier initialisation keeps the initial signal variance stable across layers.
Procedure
- Pick a dataset (XOR, two circles, two moons or spiral).
- Set the learning rate and press Train to run backprop with full-batch gradient descent.
- Watch the coloured prediction surface bend to separate the classes while the BCE loss falls and accuracy climbs.
- Press Reset to re-initialise the weights; try the spiral, which needs many epochs and the right learning rate.
2 → 8 → 8 → 1 MLP · backpropagation
Controls
References
- Rumelhart, Hinton & Williams. "Learning representations by back-propagating errors", Nature, 1986.
- Glorot & Bengio. "Understanding the difficulty of training deep feedforward neural networks", AISTATS, 2010 (Xavier init).
- Goodfellow, Bengio & Courville. Deep Learning, MIT Press 2016 — Ch. 6.
Aim
To compare three gradient-descent optimizers — plain SGD, SGD with momentum, and Adam — by watching their paths descend a 2-D loss surface from the same start, and to compare their loss-versus-iteration curves.
Theory
All three optimizers follow the negative gradient, but they differ in how they smooth and scale the step. Plain SGD takes a fixed multiple of the gradient and oscillates in narrow valleys. Momentum accumulates an exponentially-decayed velocity, damping oscillation and accelerating along consistent directions. Adam keeps running averages of both the gradient (first moment) and its square (second moment), bias-corrects them, and divides the step by the root of the second moment — giving an adaptive per-parameter learning rate.
Procedure
- Choose a loss surface (a stretched bowl, a saddle, or Rosenbrock's banana valley).
- Click anywhere on the contour map to set a common starting point for all three optimizers.
- Set the base learning rate and press Run; the three paths descend simultaneously.
- Compare the trajectories on the contours and the loss curves below — note how Adam and momentum usually reach the minimum first.
SGD vs Momentum vs Adam on a 2-D surface
Controls
References
- Kingma & Ba. "Adam: A method for stochastic optimization", ICLR, 2015.
- Polyak, B. T. "Some methods of speeding up the convergence of iteration methods", USSR Comput. Math., 1964 (momentum).
- Ruder, S. "An overview of gradient descent optimization algorithms", arXiv:1609.04747, 2016.
Aim
To apply 3x3 convolution kernels to a small hand-drawn grayscale image, to follow the resulting feature map through a ReLU non-linearity and 2x2 max-pooling, and to see how a convolutional layer builds a translation-tolerant feature hierarchy.
Theory
A convolutional layer slides a small learnable kernel over the image, computing at every position the weighted sum of the local patch. This shares weights across the image and detects the same local pattern (an edge, a blob) wherever it occurs. A ReLU then discards negative responses, keeping only evidence for the feature, and max-pooling downsamples each 2x2 block to its maximum — shrinking the map while keeping the strongest activations, which gives a little translation invariance. Stacking these stages forms a hierarchy: edges, then parts, then objects.
The output map of a valid (no-padding) convolution shrinks by one pixel on every side; each pooling stage halves the resolution.
Procedure
- Draw a digit or shape on the left grid by dragging the mouse, or press Sample digit to load a built-in numeral.
- Pick a kernel (Sobel edge, Gaussian blur, sharpen, or a custom 3x3 you type in).
- Watch the feature map update live as the kernel convolves the input.
- Toggle ReLU and max-pool to see the rectified and downsampled stages of the hierarchy.
- Read the output map size; note how valid convolution and pooling both shrink it.
Convolution feature hierarchy
Controls
References
- LeCun, Bottou, Bengio & Haffner. "Gradient-based learning applied to document recognition", Proc. IEEE, 1998 (LeNet).
- Krizhevsky, Sutskever & Hinton. "ImageNet classification with deep convolutional neural networks", NeurIPS, 2012 (AlexNet).
- Goodfellow, Bengio & Courville. Deep Learning, MIT Press 2016 — Ch. 9, Convolutional Networks.
Aim
To fit a polynomial of user-chosen degree to noisy data using ridge (L2-regularized) least squares solved in closed form by the normal equations, to observe under- and over-fitting through train versus validation error, and to estimate the true generalization error by k-fold cross-validation.
Theory
Polynomial regression builds a design matrix whose columns are powers of x, then solves a linear least-squares problem for the coefficients. Ridge regression adds an L2 penalty lambda on the coefficient sizes; this shrinks them, trading a little extra bias for a large drop in variance and taming the wild oscillations of high-degree fits. The solution has a closed form — the regularized normal equations — which we solve here by hand-rolled Gaussian elimination. A low-degree model underfits (high bias); a high-degree, unregularized model overfits (high variance). k-fold cross-validation rotates each fold as the validation set and averages the held-out error to estimate generalization without a separate test set.
The penalty is conventionally not applied to the intercept (the first I entry is set to 0).
Procedure
- Press Sample data to draw noisy points from a hidden smooth curve, or click the plot to add your own.
- Set the polynomial degree and the ridge penalty lambda with the sliders.
- The fit, the train MSE and a held-out validation MSE update live; raise the degree with lambda = 0 to watch the curve oscillate and the validation error climb (overfitting).
- Increase lambda to shrink the coefficients and smooth the fit back down.
- Press Run k-fold CV to report the cross-validated error for the current settings.
Polynomial ridge fit · normal equations
Controls
--References
- Hoerl & Kennard. "Ridge regression: biased estimation for nonorthogonal problems", Technometrics, 1970.
- Hastie, Tibshirani & Friedman. The Elements of Statistical Learning, 2nd ed., Springer 2009 — Sec. 3.4 (ridge) & Ch. 7 (model assessment, CV).
- Bishop, C. M. Pattern Recognition and Machine Learning, Springer 2006 — Sec. 1.3 & 3.2, the bias-variance decomposition.
Aim
To train a softmax (multinomial logistic regression) classifier on the real 150-row Iris dataset with a train/test split, to report test accuracy, a confusion matrix and per-class precision and recall, and to visualize the learned decision regions over any two chosen features.
Theory
Softmax regression generalizes logistic regression to several classes: each class has its own weight vector, the scores are exponentiated and normalized into a probability distribution, and training minimizes the multi-class cross-entropy by gradient descent. We standardize the four features (zero mean, unit variance) so the gradient steps are well scaled. The data is split into train and test partitions; we fit on train only and report metrics on the unseen test set. The confusion matrix C has C[t][p] equal to the number of test samples whose true class is t and predicted class is p; from it precision and recall follow per class.
Procedure
- Choose the two features to plot for the decision-region view (e.g. petal length vs petal width).
- Set the train/test split fraction and the learning rate.
- Press Train; softmax regression fits all four features by gradient descent on the training split.
- Read the test accuracy, the 3x3 confusion matrix and the per-class precision and recall.
- Inspect the shaded decision regions over the two selected features; petal measurements separate the species almost perfectly, sepal measurements much less so.
Iris · softmax classifier · decision regions
Controls
Confusion matrix (test)
References
- Fisher, R. A. "The use of multiple measurements in taxonomic problems", Annals of Eugenics, 1936 (the Iris dataset).
- Bishop, C. M. Pattern Recognition and Machine Learning, Springer 2006 — Sec. 4.3.4, multiclass logistic regression (softmax).
- Powers, D. M. W. "Evaluation: from precision, recall and F-measure to ROC, informedness, markedness and correlation", J. Machine Learning Tech., 2011.