to know backpropagation?
In case you’re attempting to know how fashionable AI programs like giant language fashions (LLMs) are educated, backpropagation is among the most necessary ideas to know.
However when you ask me how I felt after I encountered it, I used to be fully misplaced by trying on the math equations. It felt like a psychological block for me.
I spotted and wished to start out from scratch and construct my understanding one step at a time.
That journey started with my earlier article, the place we constructed a neural community from scratch utilizing a easy dataset and understood the way it makes predictions.
The weblog acquired an important response. Thanks for that!
Now, let’s proceed with the identical method. We’ll break down backpropagation step-by-step, preserving it as easy and intuitive as earlier than.
Earlier than we start, I simply need to say one factor. We’ll take this one step at a time.
Matters like backpropagation can really feel overwhelming at first, however as soon as we construct a powerful basis, all the pieces else turns into a lot simpler to know.
So, let’s get began.
Welcome again!
Let’s proceed our studying journey by way of deep studying.
We have already got a fundamental understanding of neural networks, which we explored utilizing a easy dataset within the earlier weblog.
Now, let’s first recall what we discovered within the earlier weblog on neural networks.
Fast Recap
We thought-about this easy dataset.

After plotting the info, it regarded like this:

We noticed {that a} single line was not sufficient to suit it. So, we determined to unravel it utilizing neural networks.
Subsequent, we acquired to know in regards to the equation of a single neuron, and after that, we discovered in regards to the totally different layers in a neural community.
For simplicity, we thought-about one hidden layer with two hidden neurons.
Subsequent, we noticed how the 2 hidden neurons produced two totally different linear transformations, after which we wished to mix them within the output layer.
Nonetheless, we came upon that combining two strains produced one other line, not the curve that would match the info.
That is the place we realized the importance of activation features, as they introduce non-linearity into the mannequin.
So, we handed the outputs from the hidden neurons by way of the activation perform (ReLU) after which mixed them within the output layer.
In different phrases, we took the linear mixture of the outputs from the activation perform within the output layer, and eventually, we obtained the curve.

Within the earlier weblog we constructed the neural community structure and noticed the way it makes predictions by way of ahead propagation.

Earlier than we find out how backpropagation works, let’s first have a look at the values produced at every layer in the course of the ahead go which we mentioned in earlier weblog.
We’ll use these values all through the weblog to know how the community learns by updating its parameters.

Why Does the Community Must Be taught?
Once we have a look at the ultimate curve produced by our neural community, we will see that it isn’t match.
For instance, when the hours studied (x) is 1, the precise examination rating is 55, however our neural community predicts it as 28, which is a big distinction.
Now, we have to make our neural community carry out higher, which implies it ought to predict values which are a lot nearer to the precise examination scores.
To do this, the neural community must study. By studying, we imply determining which parameters ought to be elevated and which ought to be decreased to scale back the loss.
Studying from a Acquainted Instance
Now, how can we do that?
At this level, we don’t know the way to try this.
Let’s do one factor. Let’s proceed with what we already know.
However what can we already know?
We have already got an concept about easy linear regression, how the loss is calculated, and the way the bowl curve appears.
Perhaps we will study one thing from it.
In easy linear regression, we have to discover the optimum values for β0 (intercept) and β1 (slope).
In fact, we have already got formulation, however we additionally derived them ourselves.
What we did was plot a graph with three axes. One axis represented (β0), the second represented (β1), and the third represented the loss.
We plotted the loss values for various (β0) and (β1) values and noticed a bowl-shaped curve.
We then understood that the minimal loss happens on the backside of the curve, the place the slope of the loss floor turns into zero.

To search out that time, we used partial differentiation and finally solved the ensuing equations to acquire the formulation.
In easy linear regression, we will use totally different loss features such because the Sum of Squared Errors (SSE), Imply Squared Error (MSE), or different appropriate loss features relying on the issue.
Right here, we’ll take into account the Imply Squared Error (MSE) as our loss perform.
For easy linear regression, the loss perform is
[
L(beta_0,beta_1)=frac{1}{n}sum_{i=1}^{n}left(y_i-hat{y}_iright)^2
]
the place
[
hat{y}_i=beta_0+beta_1x_i.
]
Discover that the loss relies upon solely on two parameters, [beta_0] and [beta_1]
We now have to seek out the values of [beta_0] and [beta_1] that decrease this loss.
Now, let’s have a look at our neural community.
Since our present downside is a non-linear regression downside, we will proceed utilizing the identical Imply Squared Error (MSE).
The loss perform can now be written as
[
L(w_1,w_2,w_3,w_4,b_1,b_2,b_3)
=
frac{1}{n}
sum_{i=1}^{n}
left(y_i-hat{y}_iright)^2.
]
Nonetheless, in contrast to easy linear regression, our prediction is not given by
[
hat{y}=beta_0+beta_1x.
]
As a substitute, it’s produced by all the neural community.
For our neural community,
[
hat{y}_i
=
w_3,mathrm{ReLU}(w_1x_i+b_1)
+
w_4,mathrm{ReLU}(w_2x_i+b_2)
+
b_3.
]
Consequently, the loss not depends upon simply two parameters. It now depends upon all seven parameters of the neural community, that are [w_1,w_2,w_3,w_4,b_1,b_2,b_3]
Identical to in easy linear regression, our purpose continues to be the identical: discover the values of those parameters that decrease the loss.
To attain that, we have to perceive how the loss modifications after we change every parameter individually whereas preserving the remaining parameters fastened.
In different phrases, we have to compute partial derivatives resembling
[
frac{partial L}{partial w_1},
quad
frac{partial L}{partial w_2},
quad
frac{partial L}{partial w_3},
quad
ldots,
quad
frac{partial L}{partial b_3}.
]
These partial derivatives inform us how delicate the loss is to every parameter and assist us decide whether or not that parameter ought to be elevated or decreased to scale back the loss.
Setting the Purpose
In easy linear regression, after we plot the loss values for various combos of the slope and intercept, we get a bowl-shaped curve in three-dimensional area.
For our neural community, nonetheless, we can’t visualize the loss floor in the identical means as a result of it now exists in eight-dimensional area.
Though we will’t visualize it, our goal stays the identical which is to seek out the parameter values that decrease the loss.
Time to Perceive Chain Rule
Now, based mostly on what we already know from easy linear regression, we discovered a technique to proceed additional, which is to compute the partial derivatives of the loss with respect to every parameter.
The parameters are
[w_1,w_2,w_3,w_4,b_1,b_2,b_3]
However earlier than we proceed, there may be one necessary idea that we have to perceive, and that’s the chain rule as a result of it’s the basis of all the pieces we’re going to do subsequent.
The chain rule is used each time one amount depends upon one other amount, which in flip depends upon one other amount.
Let’s perceive this with a easy instance.
Suppose
[
y=x^2
]
and
[
z=y^3
]
Now, we need to discover
[
frac{dz}{dx}
]
First, let’s discover this by-product utilizing classical differentiation.
Discover that [z] is written by way of [y] not [x] Since we would like the by-product with respect to [x] we will first get rid of the intermediate variable by substituting [y=x^2] into the equation for [z]
Substituting,
[
z=(x^2)^3=x^6
]
Now the expression relies upon solely on [x] so we will differentiate it instantly.
Utilizing the ability rule,
[
frac{dz}{dx}
=
frac{d}{dx}(x^6)
=
6x^5
]
This technique might be simple for easy issues like this one as a result of we will simply substitute one expression into one other.
Nonetheless, think about a a lot larger expression with a number of intermediate variables.
Rewriting all the equation earlier than differentiating would change into troublesome and there’s a larger probability for errors.
As a substitute of mixing all the pieces right into a single expression first, now we have a way more systematic method referred to as the chain rule.
As a substitute of eliminating the intermediate variables, with the chain rule we will work by way of them one step at a time.
Let’s see how we will implement chain rule.
We already know that, [frac{dz}{dx}] tells us how a lot [z] modifications after we make a really small change in [x]
Right here [z] doesn’t rely instantly on [x]
As a substitute, the connection appears like this:
[
x rightarrow y rightarrow z.
]
Because of this each time [x] modifications, it first modifications [y] and that change in [y] then modifications [z]
Now as an alternative of attempting to distinguish all the pieces without delay, the chain rule tells us to interrupt the issue into smaller items.
[
frac{dz}{dx}=frac{dz}{dy}timesfrac{dy}{dx}
]
Now let’s calculate every half individually.
Since
[
z=y^3,
]
we get
[
frac{dz}{dy}=3y^2.
]
Equally, since
[
y=x^2,
]
we get
[
frac{dy}{dx}=2x.
]
Multiplying these collectively,
[
frac{dz}{dx}=3y^2times2x.
]
Lastly, we all know that
[
y=x^2,
]
so we substitute it again into the equation.
[
frac{dz}{dx}=3(x^2)^2times2x=6x^5.
]
The necessary factor we will observe right here is that we by no means differentiated all the expression at a time.
As a substitute, we broke it into smaller derivatives, solved them one after the other, after which multiplied them collectively.
We’ll use precisely the identical concept in our neural community.
The one distinction is that the chain is now somewhat longer.
Fixing step-by-step utilizing Classical Differentiation Methodology
Now that we perceive the chain rule, let’s proceed to calculate the partial derivatives with respect to every parameter.
Till now, we used particular values for the weights and biases to know how the ahead go works. Nonetheless, our goal is to study these values from the info.
So, as an alternative of utilizing fastened values, let’s symbolize them utilizing parameters first.
The output of our neural community is given by
[
hat{y}=w_3a_1+w_4a_2+b_3
]
the place
[
a_1=mathrm{ReLU}(z_1)
]
[
z_1=w_1x+b_1
]
and
[
a_2=mathrm{ReLU}(z_2)
]
[
z_2=w_2x+b_2
]
Utilizing this output, we will calculate the Imply Squared Error (MSE), which is the loss perform of our neural community.
The final MSE equation is
[
L(w_1,w_2,w_3,w_4,b_1,b_2,b_3)=frac{1}{n}sum_{i=1}^{n}(y_i-hat{y}_i)^2
]
Now, let’s substitute the prediction equation into the loss perform.
[
L(w_1,w_2,w_3,w_4,b_1,b_2,b_3)=frac{1}{n}sum_{i=1}^{n}left(y_i-left(w_3a_{1i}+w_4a_{2i}+b_3right)right)^2
]
Since
[
a_{1i}=mathrm{ReLU}(w_1x_i+b_1)
]
and
[
a_{2i}=mathrm{ReLU}(w_2x_i+b_2)
]
the entire loss perform turns into
[
L(w_1,w_2,w_3,w_4,b_1,b_2,b_3)=frac{1}{n}sum_{i=1}^{n}left(y_i-left(w_3mathrm{ReLU}(w_1x_i+b_1)+w_4mathrm{ReLU}(w_2x_i+b_2)+b_3right)right)^2
]
Now, let’s begin discovering the partial by-product with respect to any one of many parameters, let’s start with
[w_1]
So, now we have to calculate
[
frac{partial}{partial w_1}left[frac{1}{n}sum_{i=1}^{n}left(y_i-left(w_3,mathrm{ReLU}(w_1x_i+b_1)+w_4,mathrm{ReLU}(w_2x_i+b_2)+b_3right)right)^2right]
]
This equation appears troublesome to unravel. How can we discover the partial by-product with respect to
[w_1]
from such a big equation?
Let’s proceed utilizing the identical concepts from differentiation that we already know.
As a substitute of differentiating all the pieces without delay, we’ll simplify the issue step-by-step.
We need to compute
[
frac{partial L}{partial w_1}
]
Substitute the loss perform into the by-product.
[
frac{partial L}{partial w_1}
=
frac{partial}{partial w_1}
left(
frac{1}{n}
sum_{i=1}^{n}
(y_i-hat{y}_i)^2
right)
]
Discover that we don’t substitute the complete expression for
[hat{y}_i]
but. We’ll try this solely when it turns into essential.
As [frac{1}{n}] is a continuing, we all know that it may be moved exterior the by-product.
[
frac{partial L}{partial w_1}
=
frac{1}{n}
frac{partial}{partial w_1}
left(
sum_{i=1}^{n}
(y_i-hat{y}_i)^2
right)
]
The summation can also be linear, so the by-product can go by way of it.
What can we imply by that?
It means in summation we add many phrases collectively and we will differentiate every time period individually after which add the derivatives.
[
frac{partial L}{partial w_1}
=
frac{1}{n}
sum_{i=1}^{n}
frac{partial}{partial w_1}
left(
(y_i-hat{y}_i)^2
right)
]
Now Differentiate the Sq.
Let
[
A=y_i-hat{y}_i
]
Then
[
frac{partial L}{partial w_1}
=
frac{1}{n}
sum_{i=1}^{n}
frac{partial}{partial w_1}(A^2)
]
Utilizing the ability rule,
[
frac{partial}{partial w_1}(A^2)
=
2A
frac{partial A}{partial w_1}
]
Substitute this into the earlier equation.
[
frac{partial L}{partial w_1}
=
frac{2}{n}
sum_{i=1}^{n}
A
frac{partial A}{partial w_1}
]
Substitute [A] with [y_i-hat{y}_i].
[
frac{partial L}{partial w_1}
=
frac{2}{n}
sum_{i=1}^{n}
(y_i-hat{y}_i)
frac{partial}{partial w_1}
(y_i-hat{y}_i)
]
Differentiate the Expression Inside
We all know that the true goal (precise remark) [y_i] is a continuing,
[
frac{partial y_i}{partial w_1}=0
]
Due to this fact,
[
frac{partial}{partial w_1}
(y_i-hat{y}_i)
=
-frac{partialhat{y}_i}{partial w_1}
]
Substitute this again.
[
frac{partial L}{partial w_1}
=
-frac{2}{n}
sum_{i=1}^{n}
(y_i-hat{y}_i)
frac{partialhat{y}_i}{partial w_1}
]
Now Differentiating the Prediction [frac{partialhat{y}_i}{partial w_1}
]
We all know that from the output layer in our neural community
[
hat{y}_i
=
w_3a_{1i}
+
w_4a_{2i}
+
b_3
]
Substitute the equations of hidden neuron activation features.
[
hat{y}_i
=
w_3mathrm{ReLU}(w_1x_i+b_1)
+
w_4mathrm{ReLU}(w_2x_i+b_2)
+
b_3
]
Differentiate with respect to [w_1]
[
frac{partialhat{y}_i}{partial w_1}
=
frac{partial}{partial w_1}
left(
w_3mathrm{ReLU}(w_1x_i+b_1)
+
w_4mathrm{ReLU}(w_2x_i+b_2)
+
b_3
right)
]
Differentiating every time period individually.
As [w_3] is fixed,
[
frac{partial}{partial w_1}
left(
w_3mathrm{ReLU}(w_1x_i+b_1)
right)
=
w_3
frac{partial}{partial w_1}
mathrm{ReLU}(w_1x_i+b_1)
]
The second time period incorporates solely [w_2] so
[
frac{partial}{partial w_1}
left(
w_4mathrm{ReLU}(w_2x_i+b_2)
right)
=0
]
Additionally,
[
frac{partial b_3}{partial w_1}=0
]
Therefore,
[
frac{partialhat{y}_i}{partial w_1}
=
w_3
frac{partial}{partial w_1}
mathrm{ReLU}(w_1x_i+b_1)
]
Differentiate the ReLU Expression
Let
[
u=w_1x_i+b_1
]
Then
[
mathrm{ReLU}(w_1x_i+b_1)
=
mathrm{ReLU}(u)
]
Now Differentiating
[
u=w_1x_i+b_1
]
with respect to [w_1]
[
frac{du}{dw_1}=x_i
]
Now differentiate the activation.
[
frac{d,mathrm{ReLU}(u)}{du}
=
mathrm{ReLU}'(u)
]
Right here we use the chain rule,
[
frac{partial}{partial w_1}
mathrm{ReLU}(w_1x_i+b_1)
=
mathrm{ReLU}'(u)
frac{du}{dw_1}
]
Substituting [frac{du}{dw_1}=x_i]
[
frac{partial}{partial w_1}
mathrm{ReLU}(w_1x_i+b_1)
=
mathrm{ReLU}'(u)x_i
]
Changing [u]
[
frac{partial}{partial w_1}
mathrm{ReLU}(w_1x_i+b_1)
=
mathrm{ReLU}'(w_1x_i+b_1)x_i
]
This ReLU derivation may get complicated, let’s decelerate and see what we truly did right here.
We all know that the ReLU activation doesn’t rely upon instantly.
It depends upon the worth of .
On the similar time, the expression depends upon .
So when modifications, it first modifications , which in flip modifications the output of the ReLU.
That is precisely the form of scenario the place we use the chain rule.
So to seek out how the ReLU modifications with respect to , we first discover how modifications with respect to , after which how the ReLU modifications with respect to .
Lastly, we mix these two outcomes utilizing the chain rule.
Now Substitute Again
Earlier, we discovered
[
frac{partial L}{partial w_1}
=
-frac{2}{n}
sum_{i=1}^{n}
(y_i-hat{y}_i)
frac{partialhat{y}_i}{partial w_1}
]
We additionally calculated
[
frac{partialhat{y}_i}{partial w_1}
=
w_3
mathrm{ReLU}'(w_1x_i+b_1)
x_i
]
Substitute this into the earlier equation.
[
frac{partial L}{partial w_1}
=
-frac{2}{n}
sum_{i=1}^{n}
(y_i-hat{y}_i)
w_3
mathrm{ReLU}'(w_1x_i+b_1)
x_i
]
Remaining outcome
We now have derived
[
frac{partial L}{partial w_1}
=
-frac{2}{n}
sum_{i=1}^{n}
(y_i-hat{y}_i)
w_3
mathrm{ReLU}'(w_1x_i+b_1)
x_i
]
This tells us precisely how the loss modifications when the load
[w_1]
modifications.
At first, this equation could look obscure, however it’s truly fairly easy.
Because it tells us how the overall loss modifications after we make a really small change to the load w1, this worth known as the gradient, and it’s precisely what gradient descent makes use of to replace the load.
To calculate this gradient, the equation considers each coaching instance within the dataset.
For every coaching instance:
[(y_i-hat{y}_i)] tells us how far the prediction is from the precise worth.
[w_3] tells us how a lot the primary hidden neuron contributes to the ultimate prediction.
[mathrm{ReLU}'(w_1x_i+b_1)] tells us whether or not the change in [w_1] can go by way of the ReLU activation.
[x_i] tells us how a lot a small change in [w_1] impacts the neuron’s enter.
Every coaching instance contributes its personal gradient based mostly on these portions.
We add all of those particular person contributions collectively, and since we’re utilizing the Imply Squared Error (MSE) loss perform, dividing by offers the typical gradient throughout all the dataset.
This common gradient tells us how ought to be adjusted to scale back the general loss, reasonably than simply the error for a single coaching instance.
Conclusion
In case you bear in mind our dialogue on Easy Linear Regression, we calculated the partial derivatives with respect to solely two parameters.
On this weblog, now we have efficiently derived
[frac{partial L}{partial w_1}]
Though the derivation was lengthy, We used the identical concepts from calculus that we already knew in each step.
We merely utilized differentiation step-by-step and used the chain rule wherever it was required.
Now, our neural community nonetheless has six extra parameters, and every of them has its personal partial by-product.
So, what do you suppose?
Do we have to repeat this whole course of for each weight and bias?
Luckily, no.
As neural networks change into bigger, manually deriving each gradient would rapidly change into exhausting and inefficient.
There needs to be a greater means.
The excellent news is that we don’t want any new arithmetic.
We merely want a greater technique to manage these calculations.
Every little thing continues to be constructed on the identical chain rule we’ve been utilizing all through this text.
Within the subsequent half, we’ll see how the chain rule might be utilized effectively throughout all the neural community, main us to one of the vital necessary algorithms in deep studying: backpropagation.
I hope you discovered one thing from this text. In case you’re nonetheless confused about neural networks or want to revisit the fundamentals, you’ll be able to at all times learn my earlier article right here.
In case you discovered this beneficial, be happy to share it with individuals who may have it.
In case you could have any doubts or ideas, you’ll be able to touch upon LinkedIn.
“It doesn’t matter how slowly you go so long as you don’t cease.”
— Confucius
Thanks for studying, and I’ll see you in Half 2!
















