Hello.
Welcome back.
So let's see how to land from data practically.
I'm gonna make a copy of our last project copy paste over here.
I'll call this Pruett force land.
And by the end of this uh this experiment you should understand how the um the model learns from data
and updates the weight accordingly.
Call this brute force learning.
Like I said open it
so there's our old project I'm going to create a new function here.
You know why.
Simple neural network that see fall in this function we shall use to demonstrate what we're talking
about.
So like we mentioned earlier learning is just it's just reducing error.
Essentially it involves learning the difference between the um the predicted value and the expected
value and adjust in our weight accordingly to reduce the error for the next prediction.
So whenever you hear Len and you can simply think of reducing error or reducing loss.
Often people say Lenin as simply reducing loss right.
So I'm going to create a new function here.
I'll see void
let me show me in a bit for you.
Brute force Lenin and this function.
Let's say it takes five parameters so that you can change the parameters.
The first parameter is an input.
The second parameter is the weight
the third parameter is the expected value.
The fourth parameter is something that we call step amount.
And we're going to pass an amount here.
And this amount is going to be the amount that we increase the weight by.
So based on the home based on the difference between the expected value and the and the predicted value
we would decide whether we want to increase the weight or reduce the weight by this variable that we
call step amount over here.
Step amount.
And the last argument is going to be number of iterations.
This is often known as number of epochs.
If you use a library such as carers or tens of flow so we're going to run the function.
This number of times for me to learn right.
Okay so we start off by computing our predicted value.
And um we can start off by creating a local variable here to hold the predicted value.
I'm gonna say double
prediction.
Hope other.
And another variable code error and a self more double up prediction.
We're gonna predict up and predict down storm prediction.
And this was just for demonstration.
Hopefully this will give you a clearer picture on the concept of learning in machine learning.
So we have a prediction on prediction.
Let's see another variable up error down.
Error
okay.
Right.
So once they start we're gonna open a full loop over here.
Oh say for int i equals zero.
I is less than I.
It's less than a number of iterations.
All right.
Plus plus open and then close and we're gonna come over here and say
prediction equals input multiplied by weight like this.
And of course we can compute the error by saying error because we already have a function for compute
an error but we can write it here.
Power of prediction
minus expected value or rich to the poor to cause we reason it to the power to so coma to
right.
So once that is done we can sort of print the arrow and prediction.
So I'm gonna put a print off over here print f
error percentage f
prediction
percentage f then
and of course we simply need to pass error and prediction here
right.
So we will print the error in prediction once that is done we're gonna do up prediction and down prediction
and we do a prediction by adding D step amount to the current weight value and then compute a new predicted
value that will be the prediction and then we compute the error we get when we increase the weight.
The reason we have this error is because we don't have access to a y you arts module in this in this
file.
So what I'm gonna do is over here you know I mean we often call the U of stone h I'm gonna cut this
and I'm going to put it in the simple neural networks that H.
Should we just call it was so pasted in here and the error should disappear.
There is no more error here right.
So we do this we do the prediction we calculate the arrow and then like I said we're going to increase
the weight by the step amount and we shall call this r prediction and then calculate the up arrow and
do the same for down and calculate it down arrow.
So I say a prediction you input multiply by weight plus step amount
and then up error
then up arrow we say equals we compute error the same way.
This time the predicted value is a prediction.
So I'm just gonna copy this actually and this variable here becomes a prediction.
Like this.
Right.
So because we reached to the poor to you.
Then if we place expected value here and predicted value on the other side.
Reason to the pope to sort of create an absolute value for us.
Right
so let's do don't prediction.
I'm going to come over here.
Don't prediction equals input times.
Wait.
Minus
step amount and down.
Error
equals
Storm Prediction
minus expected value.
Commentary like this.
Right.
So we have a prediction.
And don't predict.
So now we've got a steady direction.
Right.
What our goal or aim here is to reduce the error which is the difference between the predicted value
and the expected value.
And we've created some variables here known as a prediction and down prediction meaning we simply take
the weight and we add a small amount to it and then we compute the error for that and then we take the
weight again we add the same we subtract the same small amount from the weight and we compute the error.
Now we're going to say that if the down arrow is less than the up arrow this means then we are in the
right direction right.
Cos the arrow the difference between the expected value on the and the predicted value when we subtracted
something small from the weight is is better.
The difference is smaller compared to the difference between the expected value and the predicted value.
When we added something small to the weight so we want smaller difference meaning less error.
So if down arrow is less than up arrow then we want to subtract again we keeps subtracting something
small until we diminish the day the arrow.
Ideally we want the error to be zero but we may not be able to achieve that we want it to be as small
as possible.
So I'm gonna say if down arrow is less than up.
Error like this.
If that's the case then we simply want to say wait a course wait minus step amount cos we are in the
right direction.
Let's subtract again and see if indeed it's gonna be even smaller.
The area is gonna be smaller and we keep subtracting.
So I'm gonna say Step amount over here.
On the other hand if up if down arrow we can just keep down their own decide down arrow is greater than
up arrow then up arrow which would add more cos the down arrow when we went to the down direction by
subtracting the arrow became bigger.
So we need to add more meaning to you.
The answer is on the other side.
So we need to add.
I hope you get this.
If you do not just send me a message.
We say weight equals weight plus step hormones like this.
Right.
So we can expose the function now and try it out.
I'm gonna copy this.
Put it in the head of all over here
and I'm going to go to the main file
and I'm gonna clean everything here.
Cause this from my previous projects and we don't need any of this.
It's a very simple experiment
und let's create some data here for testing our function.
I'm gonna say double wait let's see the weight equals zero point five.
That is a realistic weight.
And when I see double input hobos you put five as well.
Three point five over here and I'm going to see double
expected value.
Let's say the expected value which is the y value is zero point eight.
And I'm going to say double step amount
and I'm gonna see this the step I'm on this the amount we're going to be adding also trucked in from
the weights.
I'm going to see it 0.01.
Sure point sure issue 1 right.
So I'm going to call our function over here.
The function is brute force learn in the first argument is the inputs.
So pass inputs over here.
The second argument is the weight past wage of here.
The third argument is the expected value pass expected value over here.
The next argument is this step amount of positive step amount Yeah.
The last argument is the number of iterations.
Let's say we want to run this for one thousand five hundred times right.
So let's build
um actually going to open to return first said I want it built and we download.
We can see it from scratch rather than a research my board actually.
Yeah.
Terra term is going to be minimized if I click this window so let's just build.
Yeah click over here to build your successfully Gary on the board.
Okay.
I'm gonna open to return.
And this is where we have it's land in to remember the expected value we want is zero point eight.
Let's see it run.
It s still land in.
And as you can see us it s last.
The arrow reduces and we get closer and closer to the expected value.
It s still learning it's still learning.
Oh sorry bother minimized that we know but I hope you're running it on your side as well
right.
Okay now the error is 0.
The expected value is the same as the predicted value but we are running it more than it's required.
We said if you run for one thousand five hundred times that is why it's still running so we can wait
for you to finish.
Okay.
It s done running now okay.
This is it.
So um oh advice you play around with it.
You can change the parameters especially the um the number of iterations on the um and the step amount
you can change it and see.
Sometimes you get close to the expected value sometimes you don't.
Sometimes you actually jump over the expected value so you can certainly try that right.
And tell me how it goes.
Okay.
So this order is for this lesson and I'll see you later.