A collection of notebooks and scripts demonstrating the fundamentals of a Simple Single-Neuron Neural Network, focusing on Linear Regression and Gradient Descent from scratch.
No deep learning frameworks — just NumPy and a clean implementation of forward pass and gradient descent.
Followed on by MLP Digits Classifier, extending gradient descent from a single-output linear model to a multi-class neural network.
-
Gradient Descent from Scratch - Jupyter Notebook exploring the implementation step-by-step.
-
Linear Regression Explanation - Detailed explanation of the underlying gradient descent code and math.
-
Linear Regression Gradient Descent - Basic Python implementation using raw inputs.
-
Gradient Descent Animation - Animated visualisation of the regression line converging over training epochs.
-
Linear Regression Normalised - Implementation with normalized inputs for more stable and faster convergence.
-
Linear Regression - Ames Housing - Real-life example applying linear regression with gradient descent on the Ames Housing dataset (predicting sale price from living area).
- Data — Generates 30 points following
y = 2x + 50usingnp.arange. - Initialisation — Weight
Wand biasbstart at 0; learning ratelearning_rate = 0.001. - Forward pass — Computes predictions:
y_pred = W * X + b. - Gradients — Calculates partial derivatives of Mean Squared Error (MSE) loss with respect to
Wandb. - Update — Subtracts
learning_rate * gradientfrom each parameter, nudging them toward lower loss. - Result — After 10,000 epochs, the model recovers
w ≈ 2andb ≈ 50.
See linreg_notes.md for a detailed breakdown of the math and code.
Note 1: The
30data points were found empirically to be enough for the model to reliably recoverw ≈ 2andb ≈ 50.Note 2: Removing the learning rate (
learning_rate) causesdWanddbto explode, resulting iny_predbecomingNaNand training failing completely.Note 3: For the Linear Regression Gradient Descent example, themv Gradient_Descent.gif linreg_gd_anim.gifmv Gradient_Descent.gif linreg_gd_anim.gif learning rate (
0.001) and number of epochs (10,000) were found empirically — small changes to either can cause the model to converge too slowly or diverge entirely.Note 4: The Linear Regression Normalised example normalises the inputs for more stable convergence, reducing sensitivity to the choice of learning rate. This allows a much lower learning rate of
0.1and only20epochs to converge.
matplotlib- For histogram and scatter plot visualizations.
- Boston Housing - Practical real-life example applying linear regression with gradient descent on a well-known dataset.
- Regression from Scratch Example (Medium)
- Gradient Descent in Python (GeeksforGeeks)
- Gradient Derivative Calculation (Towards Data Science)
