to know backpropagation?
In case you’re making an attempt to know how fashionable AI techniques like massive language fashions (LLMs) are skilled, backpropagation is without doubt one of the most vital ideas to know.
However in case you ask me how I felt once I encountered it, I used to be utterly misplaced by trying on the math equations. It felt like a psychological block for me.
I spotted and needed to begin 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 obtained an excellent response. Thanks for that!
Now, let’s proceed with the identical strategy. We’ll break down backpropagation step-by-step, retaining 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, every little thing else turns into a lot simpler to know.
So, let’s get began.
Welcome again!
Let’s proceed our studying journey via deep studying.
We have already got a primary understanding of neural networks, which we explored utilizing a easy dataset within the earlier weblog.
Now, let’s first recall what we realized within the earlier weblog on neural networks.
Fast Recap
We thought of this easy dataset.
After plotting the information, it appeared 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 obtained to know concerning the equation of a single neuron, and after that, we realized concerning the totally different layers in a neural community.
For simplicity, we thought of one hidden layer with two hidden neurons.
Subsequent, we noticed how the 2 hidden neurons produced two totally different linear transformations, after which we needed 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 information.
That is the place we realized the importance of activation capabilities, as they introduce non-linearity into the mannequin.
So, we handed the outputs from the hidden neurons via 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 via ahead propagation.

Earlier than we find out how backpropagation works, let’s first have a look at the values produced at every layer throughout the ahead cross 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 Have to Be taught?
After we have a look at the ultimate curve produced by our neural community, we are able to see that it isn’t an excellent 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 large 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 understand how to do 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 thought about easy linear regression, how the loss is calculated, and the way the bowl curve seems.
Possibly we are able to study one thing from it.
In easy linear regression, we have to discover the optimum values for β0 (intercept) and β1 (slope).
After all, 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 ultimately solved the ensuing equations to acquire the formulation.
In easy linear regression, we are able to use totally different loss capabilities such because the Sum of Squared Errors (SSE), Imply Squared Error (MSE), or different appropriate loss capabilities 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]
Now we have to search 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 are able to 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 now not given by
[
hat{y}=beta_0+beta_1x.
]
As an alternative, it’s produced by your complete 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 now 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]
Similar to in easy linear regression, our aim remains 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 retaining the remaining parameters fastened.
In different phrases, we have to compute partial derivatives equivalent to
[
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 mixtures of the slope and intercept, we get a bowl-shaped curve in three-dimensional house.
For our neural community, nevertheless, we can not visualize the loss floor in the identical method as a result of it now exists in eight-dimensional house.
Regardless that we are able to’t visualize it, our goal stays the identical which is to search 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 strategy 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 vital idea that we have to perceive, and that’s the chain rule as a result of it’s the basis of every little thing we’re going to do subsequent.
The chain rule is used at any time when 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 spinoff utilizing classical differentiation.
Discover that [z] is written by way of [y] not [x] Since we would like the spinoff with respect to [x] we are able to first eradicate 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 are able to differentiate it straight.
Utilizing the facility rule,
[
frac{dz}{dx}
=
frac{d}{dx}(x^6)
=
6x^5
]
This technique might be straightforward for easy issues like this one as a result of we are able to simply substitute one expression into one other.
Nonetheless, think about a a lot larger expression with a number of intermediate variables.
Rewriting your complete equation earlier than differentiating would change into troublesome and there’s a greater probability for errors.
As an alternative of mixing every little thing right into a single expression first, now we have a way more systematic strategy referred to as the chain rule.
As an alternative of eliminating the intermediate variables, with the chain rule we are able to work via them one step at a time.
Let’s see how we are able to 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 straight on [x]
As an alternative, the connection seems like this:
[
x rightarrow y rightarrow z.
]
Which means that at any time when [x] modifications, it first modifications [y] and that change in [y] then modifications [z]
Now as a substitute of making an attempt to distinguish every little thing 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 vital factor we are able to observe right here is that we by no means differentiated your complete expression at a time.
As an alternative, we broke it into smaller derivatives, solved them one after the other, after which multiplied them collectively.
We’ll use precisely the identical thought in our neural community.
The one distinction is that the chain is now a bit 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 cross works. Nonetheless, our goal is to study these values from the information.
So, as a substitute of utilizing fastened values, let’s signify 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 are able to 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 spinoff 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 spinoff with respect to
[w_1]
from such a big equation?
Let’s proceed utilizing the identical concepts from differentiation that we already know.
As an alternative of differentiating every little thing 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 spinoff.
[
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 needed.
As [frac{1}{n}] is a continuing, we all know that it may be moved exterior the spinoff.
[
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 be linear, so the spinoff can cross via it.
What can we imply by that?
It means in summation we add many phrases collectively and we are able to 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 facility 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}
]
Exchange [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 commentary) [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 capabilities.
[
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 would possibly get complicated, let’s decelerate and see what we really did right here.
We all know that the ReLU activation doesn’t depend upon straight.
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 search 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
]
Last consequence
Now we 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 might look obscure, however it’s really fairly easy.
Because it tells us how the whole loss modifications after we make a really small change to the load w1​, this worth is named 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 cross via 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 provides the common gradient throughout your complete dataset.
This common gradient tells us how ought to be adjusted to scale back the general loss, quite than simply the error for a single coaching instance.
Conclusion
In case you keep 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 spinoff.
So, what do you suppose?
Do we have to repeat this whole course of for each weight and bias?
Happily, no.
As neural networks change into bigger, manually deriving each gradient would rapidly change into laborious and inefficient.
There needs to be a greater method.
The excellent news is that we don’t want any new arithmetic.
We merely want a greater strategy to manage these calculations.
The whole lot remains 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 your complete neural community, main us to probably the most vital algorithms in deep studying: backpropagation.
I hope you realized one thing from this text. In case you’re nonetheless confused about neural networks or wish to revisit the fundamentals, you’ll be able to all the time learn my earlier article right here.
In case you discovered this beneficial, be at liberty to share it with individuals who may have it.
In case you’ve got 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!
