Now, what we will do is we will take the, um, well, this example and we will apply it using PyTorch.
We will see how simple it is to do so.
So we first start with this, um.
Just, uh, Jupyter Notebook and we call it, let's say basic.
Three.
This is the third or I don't know, we can call it actually back.
For back propagation.
And main.
So we start with X.
X and y.
Of course x or not actually import import.
Torch.
And we start with defining x x equals.
Torch.
Dot tensor.
Of the value.
Of the value of x, which is.
Which is one.
And why is going to be.
Equals.
The tensor of value of two.
On the other hand, w w is a little bit.
A different.
Let's let's see how we will identify.
It will be torch dot.
Tensor.
And then we will give it an initial value.
In our case we give it one.
And then we will say require requires gradient.
Gradient equals true.
And we shift enter if you print these values.
So x print y print.
W we see how they look.
X and y is just a tensor and w is a tensor, but it says required gradient.
So in the operations we will do to the w it will compute the d w over.
You know, the thing we are connecting it with in our case is the loss.
Now what we need to do is to do the operations.
We said that.
Operations is.
The first one is w multiplied x will equal y hat y hat minus y will equal s, and s squared will equal
loss.
So we do the same thing.
Y hat y hat equals w multiplied x s equals w or y hat.
Minus y and loss will equal s.
Squared.
So these are just the operations we have done.
And then we put shift enter and then we have the actual loss.
So we said the first we go forward uh operation and then we can calculate the loss.
So how much is the loss in this case.
Well actually we already did the computation and the already computed this value.
So we just, uh, print and we just print the loss.
What do we have?
We have the loss equals one.
Of course, if we want to just show the value item and we just put like this and we will get it.
Uh, like this.
Of course the uh, loss is also a tensor.
So here is we put it an item.
And now we are having the forward pass value.
How about the backward pass.
Well, in PyTorch is very simple.
We just one command.
We just say loss dot back forward and that's it.
And shift enter.
That's it.
But what does it what did it do it actually?
Or how did the.
Loss and W got calculated.
Actually, we use the loss to make a backward, uh, propagation.
And then he used these operations to go back to uh w which has the gradient equals to true.
If we have more gradients, then it will calculate the loss and how everything is, uh, how the loss
will change with every W we have.
So print let's, let's see the results w.
But this time we need to say gradient.
And then we shift into.
Of course we can just also print w like this, but we will see only the value of w.
In order to get the gradient value we just put dot gradient.
So with this way of course we have this value.
Of course as a tensor we can also item.
And do like this, but I prefer.
Okay, let's let's do.
The Or, let's put it.
Two print statements because usually it's better just this way.
So minus two, which is d loss over d w is what we calculated by hand which is this one minus two.