One-Layer NN (Linear Regression)
The goal of the following document is to show linear regression is just a neural network with a single layer with no activation function (or identity) where the final loss function is the mean squared error.
To see the above, let’s recall what is the goal of linear regression. It tries to find a linear model whose output \(\hat{y}\) is the linear combinations of inputs plus a constant term, i.e.,
When we collect data, we have \(N\) such \((x_1^{(i)}, x_2^{(i)},\dots, x_n^{(i)})\) for each \(y^{(i)}\) where \(i=1, \dots, N\).
Suppose \(N=5\), \(n=3\), then we have the following table.
\(x_1\) |
\(x_2\) |
\(x_3\) |
\(y\) |
---|---|---|---|
\(x_1^{(1)}\) |
\(x_2^{(1)}\) |
\(x_3^{(1)}\) |
\(y^{(1)}\) |
\(x_1^{(2)}\) |
\(x_2^{(2)}\) |
\(x_3^{(2)}\) |
\(y^{(2)}\) |
\(x_1^{(3)}\) |
\(x_2^{(3)}\) |
\(x_3^{(3)}\) |
\(y^{(3)}\) |
\(x_1^{(4)}\) |
\(x_2^{(4)}\) |
\(x_3^{(4)}\) |
\(y^{(4)}\) |
\(x_1^{(5)}\) |
\(x_2^{(5)}\) |
\(x_3^{(5)}\) |
\(y^{(5)}\) |
This table represents our dataset with 5 samples (\(N=5\)) and 3 features (\(n=3\)), where each row contains the input features and corresponding label value.
Then we can stack \(x\) values together and create the data matrix \(\mathbf{X}\) and take the column of \(y\) and create the label vector \(\mathbf{y}\). Thus, one needs to minimize the following to find the best \(\boldsymbol{\theta}\):
where \(\mathbf{\hat{y}}\) is the model output vector,
with \(\mathbf{1}\) as a column of ones, and \(\boldsymbol{\theta}=[\theta_0, \theta_1, \dots, \theta_n]^{\top}\).
Notice that we can write the above as follows:
where \(\text{MSE}\) is the mean squared error loss funciton.
Sounds very complicated, but if you notice, you can see that the following neural network does the same thing where \(\mathbf{w}=[\theta_1, \dots, \theta_n]\) and \(b=\theta_0\). This single-layer neural network with no activation function (or using the identity function) and MSE loss is mathematically equivalent to linear regression.
The image shows a single-layer neural network with:
Input nodes (\(x_1\), \(x_2\), …, \(x_n\)) in purple
Weights connecting the inputs to the output (\(w_1\), \(w_2\), …, \(w_n\))
A bias term \(b\)
A summation node (the green + symbol)
An output \(\hat{y}\)
In this formulation, the neural network directly computes:
This is identical to the linear regression formula:
Where:
The bias \(b\) in the neural network corresponds to \(\theta_0\) in linear regression
The weights \(w_1\), \(w_2\), …, \(w_n\) correspond to \(\theta_1\), \(\theta_2\), …, \(\theta_n\)
When using MSE (Mean Squared Error) as the loss function, we’re essentially minimizing:
Which is exactly the same optimization objective as in standard linear regression.
The key insight is that while neural networks typically use non-linear activation functions to model complex relationships, when you use:
A single layer
No activation function (or the identity function)
MSE as the loss function
You end up with precisely the mathematical formulation of linear regression. This demonstrates that linear regression is essentially the simplest possible neural network.