Welcome to the section on error sanctioning.
In the previous section we saw how to build a simple linear regression model.
In this section, we'll see how to calculate the model's error in order to permit it make or update
its parameters such that it makes, uh, such that this error is reduced.
So let's get back here.
We had this y equals mx plus c and then we had this line.
But um, again this line could be just any other line depending on how we initialize um m um and c.
So we could have this line.
It could be this line, it could be this line.
It could be this line, or it could be maybe say even this line.
Now, supposing that it is this line right here, then for each and every point, the way we are going
to sanction, um, the error or the model's error is by, first of all, measuring it.
So we would, um, get let's, let's start with this point.
For example, if we have this point here, uh, what the model predicts is that the price if you use
the model's prediction and then you link this up, this point up with the model's prediction, that
is a straight line.
Then you would find that, uh, model model predicts is actually this value.
So the model predicts uh, the, let's say a value of um, the well this this prices go from 8 to 10.
So let's suppose that this is a value of about five, whereas it's supposed to predict ten, because
here we have the true value.
Let's take this, let's copy this and um just drag it right here.
So as we're saying here we have the true value from this point.
The true value is actually this ten.
But what the model predicts is five.
And so now this difference of five is actually what we would call the error.
So this this difference that's the error.
But it happens that this error is not always this large for all other values.
If you let's zoom in let's zoom in.
You will find that for this value.
For example, for this, um, specific value we have here, what the model predicts and its actual value
is the same.
Let's get back, copy this and paste out.
So we have that here.
So let's say the model here predicts um 5.5 or 5 point um two.
So here we have 5.2.
Well this is five here.
Let's drag this down.
So here we have this five.
And then just right beside it we have 5.2.
So this is 5.2.
And then this is five.
So what we're saying for this point what the the model predicts.
That's when you link the point with the with a line with is our function y equals mx plus c.
You will find that what it predicts is um 5.2.
And the actual value itself is 5.2.
So what this tells us now is that our error our error is equal to zero.
So that's it.
We have an error of zero.
Um, well, just above here at this, uh, at the level of this point, we have an error which is,
um, quite large of about five.
Let's let's see there we have an error of, um, about five.
Okay.
So that's it.
We see that depending on the depending on what the point, we could have different, um, error values.
Now generally what we want to do is we want to get an average of all these values.
And then, um, that will be our overall error.
So our overall error here will be for each and every point we have here we will get the errors.
So let's say uh we will take this point.
You see when you when you link this up with a, with a line, you find that what the model predicts
here is this value, whereas the actual value is right here.
And so the error is going to be this for this point.
And you could repeat the same process for all other points.
But what's important to note is the fact that, um, the error of the model is going to be um, the,
the, the mean or the sum total, um, averaged of all these different points.
Let's suppose that we had, um, this line something like this.
So let's suppose that this line was like this.
In that case the, the error for all these values, you see that the error for all these values when
you compare, well, let's get back, you would find that when you compare this model, this model here
with this other model, the error here will be much larger than the error for a.
So let's call this model A and let's call this model B.
Um our error.
Our error.
For model B will be much larger or let's say greater than the error for model A.
And this makes sense because for most of the points we have here, they are individual errors.
When we're using this method, B is going to be much larger than the individual errors.
When we're using, um model A diving into some code, we get to the documentation.
You have TensorFlow losses.
Let's reduce this.
We have TensorFlow losses Keras.
And then we have losses.
Here we have this mean squared error.
Let's click on this mean squared error.
And it's going to be defined your computer mean of squares of errors between labels and predictions.
So that's it.
Simple definition.
And then you have some examples.
So just right here we have um y true.
And so what that means is if you consider in this example here, if you consider this example it's y
true is ten.
Whereas what the model predicts is five.
You see.
So here is y true it's going to be ten while the y pred by the model is going to be five.
And then with this um mean squared error loss we'll be able to compute the the loss very easily.
So let's copy this.
Get back to our notebook, space it out.
And you see here we have y true zero one.
Let's just let's rearrange this zero one uh and zero zero and one one and one zero.
So let's run this and then see what we get.
Well let's print this out.
So let's have print print that out and see what we get.
So you see we have 0.5.
And the way we could get this is simple.
We will have for each and every position you compare the values zero and one.
That gives you zero minus one.
And the mean square error as the name goes is squared.
So we have zero minus one all of that squared.
So we have that squared.
Um plus it's a mean.
So it's an average.
So we have plus um one here and one.
So let's just copy this and paste out here.
Take that off.
We have one and 1 or 1 minus one all that squared.
Plus um for the next zero minus one that is zero minus one all that squared.
And then here we have zero -0 or that squared.
So zero -0 other square.
Take that off and we have zero.
So all this is going to give us um one this is one square one plus zero plus one plus zero.
That's two.
And if you divide this by the total number of um elements we have four.
So this gives us 0.5.
Now let's modify this slightly and say we will have one.
So if we have one then we will have one divided by four which will give us 0.25.
Here one divided by four should give us 0.25.
Let's change this from 0 to 1.
Now we have one minus.
We have zero minus one.
So here is zero minus one.
And then um this should be one.
This is one plus zero plus one one plus one.
So that's three divided by four.
Now we should have 0.75.
So let's comment that and see what we get.
There we go.
You see we have 0.75.
So essentially we have this error which is for each and every point here what the model predicts, which
in this case is um, five actually.
Because when you, when you make use of the line you see is five what the model predicts that, uh,
five minus what the model predicts, minus what um, is actually like minus the actual value.
So that's why a y actual.
And you take all this and you square, then what you do is you repeat this for all the different points,
and then you look for the average.
So simply, um, the sum of all this y minus y is um, all of that square.
So you sum all those up, and then you divide by the total number of points or total number of samples
in, in this case is, um, say thousand.
Now, the next problem we have with this mean square error is that it doesn't do well when it comes
to, um, dealing with outliers.
A simple outlier we could detect, um, by looking at this plot.
Is this one right here?
Why is this an outlier?
The answer is simple.
We have high horsepower, but we have low price.
We would expect that the the higher the horsepower.
Like most others here, we should have, um, a higher price, just like, um, as we've seen with all
these others.
So with this outliers, given that the deviate a lot from the usual pattern of the whole, um, data
set of, uh, different samples using y minus y, that's y minus y, let's say y prime square using
all this, this kind of formula where we have, uh, y minus y prime square, um, isn't the best,
uh.
Here.
The different the the the error is much larger as compared to all the others.
So here we would have an error.
Um, if we take this and paste it out here, let's try to link this up with our with our plot.
So let's, let's, let's keep dragging this.
See we link this up and then we drag this to up.
There we go.
You see that this error is much larger as compared to all the other errors.
So um, compared to this is much larger than this, much larger than maybe the error would have here,
much larger than this error and much larger than this error.
And so because of that, um, the model will pay more attention to this outlier as compared to all these
other, um, points.
And this is for the simple reason that the loss is going to be much higher, especially as we're going
to be squared.
So remember, if you have, um, a square, um, you would have something like this plot like this.
So if you have um say an error of three.
An era of three.
Well, you would have a square of nine, but if you have an error of ten, an error of ten, you have
a square of about 100.
So the, the, the gap you have here will be much larger than as compared to the gap you have here.
So this kind of outliers tend to trick the model into updating its parameters M and C based on um,
its own particular loss when um, it's actually an outlier.
So what we use in this kind of cases is we make use of the mean absolute error.
So instead of squaring we just simply say y minus y prime and then we take the absolute value.
So instead of this kind of plot we would have a plot like um this instead.
And the difference now is that although the we still have that error margin.
So although the error or the difference between the y predicted and the Y um, actual is going to still
be large, we we don't have to square it so it doesn't, um, aggravate the situation.
So that's it.
We have this mean absolute error again with TensorFlow.
You could, um, use that without writing any extra line of code.
Now, if you want the best of both worlds, that's if you want to use the mean squared error and the
mean absolute error.
Then you could go in for the Uber loss.
Now with the Uber loss spurred Uber loss with the Uber loss is actually a mixture of the two.
So what we have here is if we have um if the error is too large, then we'll use the mean absolute error.
So we just we just compare if we have um, error, if our, our error, um, error is too large.
So if error is large then we use our mean absolute error.
And then if, if we have an error that is two that is small or that is usual like um, in the case of
all these other points, if you have usual errors then our usual error margins, then you would go in
for the mean square error.
So in that case if your error, if your error, um, error is small, then you will go in for the mean
square error.
Diving back to the documentation, you have the mean absolute error just right here.
This is the mean squared error square of y minus y prime square or rather the square y minus y prime.
And then for the mean absolute error computes the mean of absolute difference between the labels and
the predictions.
Now um, remember that the absolute value, the absolute value actually is um x.
That's let's, let's, let's write that out.
In case you don't know, when you talk about absolute function, the absolute value is simply x if x
is greater than zero.
So if x is already positive then it remains the same.
But if x is negative, then you add a negative there so that it turns into a positive value.
So in that case, the absolute value of minus seven is seven and the absolute value of seven is seven.
So that's it.
We get back here.
That's that's it for the mean absolute error.
So let's get here.
We have Uber.
There we go.
We have this Uber loss.
Let's scroll up.
Compute the Uber loss between y true and y pred.
So we have this parameter delta.
And the role of this parameter delta is simple.
Um the way we know that the error is large is by comparing it with our parameter delta.
So if the error is greater than the parameter delta, then we consider that it's large and we use the
mean absolute error.
If the error is less than the parameter delta, we consider that the error is small and we use the mean
squared error.
So getting back here we have that.
We should have an example showing that okay this is it.
You see we have um x squared.
That's y minus y prime square.
If x is less than that parameter delta.
Yeah denoted as d.
And um if it's not then we're going to use um the absolute value.
Well looking at this here, this definition, it turns out that we made an error, uh, on the board.
Um, and it's actually not just absolute value.
So we have uh, with the Uber, if it's less than the delta, that is, if the error is small, then
we use the mean squared error y minus y prime um square.
But for the Uber it's still the absolute value.
But now we subtract half of the sigma and then multiply by sigma.
So or delta.
So this delta um comes in again in the formula.
So it's not just y minus y prime.
And then one uh times one over n.
Anyways the good thing with TensorFlow is you don't really need to dive into all these details before
making use of, um, the Uber loss in practice.
So here you see how to make use of it.
Um, usage with the compile API.
So after you've created your model, you now compile that model.
You specify the loss and you see you specify the optimizer, which we shall look at in the next section.
So let's just copy this and then um get back to the code.
Take this off.
We have now model compile.
We have the optimizer SGD and the loss which is the over loss.