Linear Regression from Scratch: Gradient Descent vs OLS

A linear regression fitted two ways with my own NumPy code: iteratively, with a one-layer network (Dense + MSELoss) trained by gradient descent (500 epochs, learning rate 0.01), and in closed form, with an OLS class that solves the normal equation. If both are correct they reach the same coefficients. Use the GD + OLS button on each chart to overlay the closed-form fit. Data are synthetic with known true coefficients.

Code: ml-from-scratch @ 7791264

Simple linear regression

100 points from y = 3x + 2 + noise (σ = 0.5).

ParameterGradient descentOLS (closed form)|GD − OLS|
intercept1.99661.99771.1e-03
slope3.05143.05968.2e-03

Multiple linear regression (2 features)

100 points from y = 3·x1 + 1.5·x2 + 2 + noise (σ = 0.5). Drag to rotate, scroll to zoom. Gray segments are residuals: the vertical distances that least squares minimizes.

ParameterGradient descentOLS (closed form)|GD − OLS|
intercept1.98031.98046.2e-05
x12.95082.95082.1e-05
x21.49571.49591.3e-04