# Error functions

### Learning Rate

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lin8yVJj3QCuxfGjdhc%2F-LitjoXEet62foJAjgDb%2Fimage.png?alt=media\&token=efd79da3-d985-4451-9b41-b4fdab2a1692)

To find the best line we want to take small steps. This is the learning rate Alpha. We add Alpha to w1 and w2 to get our line closer and closer to the best fit.&#x20;

###

### Mean Absolute Error

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LeRWV75IJvPXtiaJVGM%2F-LeRWnHStwSMFQ7AkTi7%2Fimage.png?alt=media\&token=32ed4bf7-378b-4aff-a98d-676a50785d19)

We're using the average of the error, 1/m.&#x20;

We want the error to always be positive so we have an absolute value in the error.&#x20;

### Mean Squared Error

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LeRWV75IJvPXtiaJVGM%2F-LeRWxY671oTTvtqGe-F%2Fimage.png?alt=media\&token=0806ab8b-41c7-416f-aa4a-f6703ac03939)

We take the average of all the squared differences, and we have this extra factor of 1/2 for convenience we'll talk about it later (we'll be taking the derivative of this error, it'll cancel out). We can multiply the error by any constant and the process of minimizing will be the exact same thing.&#x20;

The difference is always positive because we are taking the square.&#x20;

Graph:

We plot all the errors in the graph on the right.&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf5wibHUl1oytwfqrEV%2Fimage.png?alt=media\&token=ce3168ab-ee18-4d98-b60e-3c5125957949)

As we descend from the function we  use gradient descent, we're minimizing the error, we get a better and better line until we find the best possible fit with the smallest possible Mean Squared Error.&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf5wpW8Plr3n_yVOkAv%2Fimage.png?alt=media\&token=f77f30d0-ac2a-499c-b5fd-faaa49359137)

Minimizing the error is minimizing the average of the area's squares.&#x20;

Minimizing Error Functions

When we are minimizing the absolute error, we are using a gradient descent step.&#x20;

### Gradient descent

To minimize the error with the line, we use gradient descent.&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf5rV6zpmbmqsamLkBQ%2Fimage.png?alt=media\&token=4f08eb60-b86b-457f-bb06-09a3cb1fea6e)

**The way to descend is to take the gradient of the error function with respect to the weights.** This gradient is going to point to a direction where the gradient increases the most. Therefore the negative of this gradient is going to point down in the direction where the function decreases the most. So we take a step down in that direction.&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf5sNnRpYn8H3uD5eis%2Fimage.png?alt=media\&token=ad470d0a-b7a3-4042-87d1-1fd4c3322470)

We'll be multiplying this derivative by the learning rate because we want to make small steps. \
So the error function is decreasing and we're going towards a minimum. If we do this several times, we'll get to a minimum, which is the solution.&#x20;

### Minimizing error functions

The gradient descent step uses these two derivatives: the derivative with respect to the slope w1, and the derivative to the y intercept w2. They are as follows:&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf5z0Ckz88hDkVo7wK_%2Fimage.png?alt=media\&token=f4ec579e-234a-4e05-8c65-83e48b23f207)

This means that we have to increase the slope w1 by - (y - y\_hat) \* x \* Alpha (the learning rate) and upgrade the y incercept by adding - (y - y\_hat) \* learning rate Alpha. This is exactly what the gradient step is doing.&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf5zMpVdorXidKFPvcP%2Fimage.png?alt=media\&token=0770cb32-69cb-4978-b49d-22734cbbf30d)

Same thing can be applied with the absolute error:

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf6-KnqxSE5aGcMlqZ-%2Fimage.png?alt=media\&token=f30660ac-8edc-486c-a773-0e8032909d2b)

### Development of the derivative of the error function

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf61tDJBLkRXS6Yyhk-%2Fimage.png?alt=media\&token=619d1f5a-c650-4204-a626-d6cbc82440dd)

## Mean vs Total Squared (or Absolute) Error <a href="#mean-vs-total-squared-or-absolute-error" id="mean-vs-total-squared-or-absolute-error"></a>

A potential confusion is the following: How do we know if we should use the mean or the total squared (or absolute) error?

The total squared error is the sum of errors at each point, given by the following equation:

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf63J7WfRBQ4PiYC6Cy%2Fimage.png?alt=media\&token=25db5d13-461f-402a-990d-96f460bf5dea)

whereas the mean squared error is the average of these errors, given by the equation, where m m is the number of points:

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf63Mg4QExV7pNg5Axt%2Fimage.png?alt=media\&token=80f4a9f9-fef6-4d32-829a-dedeadc40677)

The good news is, it doesn't really matter. As we can see, the total squared error is just a multiple of the mean squared error, since

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lf5rBYUgULTMsC3P3gv%2F-Lf63SLua1So062dz7dp%2Fimage.png?alt=media\&token=2750769b-87d1-4e5e-9231-a0caef039b3a)

Therefore, since derivatives are linear functions, the gradient of T is also mm times the gradient of M.

However, the gradient descent step consists of subtracting the gradient of the error times the learning rate alpha-α. Therefore, choosing between the mean squared error and the total squared error really just amounts to picking a different learning rate.

In real life, we'll have algorithms that will help us determine a good learning rate to work with. Therefore, if we use the mean error or the total error, the algorithm will just end up picking a different learning rate.

## Batch vs Stochastic Gradient Descent <a href="#batch-vs-stochastic-gradient-descent" id="batch-vs-stochastic-gradient-descent"></a>

At this point, it seems that we've seen two ways of doing linear regression.

* By applying the squared (or absolute) trick at every point in our data *one by one*, and repeating this process many times.
* By applying the squared (or absolute) trick at every point in our data *all at the same time*, and repeating this process many times.

More specifically, the squared (or absolute) trick, when applied to a point, gives us some values to add to the weights of the model. We can add these values, update our weights, and then apply the squared (or absolute) trick on the next point. Or we can calculate these values for all the points, add them, and then update the weights with the sum of these values.

The latter is called *batch gradient descent*. The former is called *stochastic gradient descent*.[<br>](https://classroom.udacity.com/nanodegrees/nd025/parts/c6a7e66b-64f6-41eb-9b3e-e068e0ed962b/modules/aea22017-a8fe-4902-ba79-0d79f161a6bb/lessons/859b60ff-7303-45ce-a4cb-2dce0c0e6272/concepts/1b0ed370-9d37-4b2f-a8e9-d4082ec73b77)

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lin8yVJj3QCuxfGjdhc%2F-LixM9JE3omE58osZPQq%2Fimage.png?alt=media\&token=759c5e3d-b032-46cf-b52f-52c7f1152df4)

The question is, which one is used in practice?

Actually, in most cases, neither. Think about this: If your data is huge, both are a bit slow, computationally. The best way to do linear regression, is to split your data into many small batches. Each batch, with roughly the same number of points. Then, use each batch to update your weights. This is still called *mini-batch gradient descent*.[<br>](https://classroom.udacity.com/nanodegrees/nd025/parts/c6a7e66b-64f6-41eb-9b3e-e068e0ed962b/modules/aea22017-a8fe-4902-ba79-0d79f161a6bb/lessons/859b60ff-7303-45ce-a4cb-2dce0c0e6272/concepts/1b0ed370-9d37-4b2f-a8e9-d4082ec73b77)

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lin8yVJj3QCuxfGjdhc%2F-LixMMWNagSfYZkPDzhz%2Fimage.png?alt=media\&token=6b8fd60c-aa56-4f01-bdc9-6ff8930c5e33)

### Closed Form Solution

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lin8yVJj3QCuxfGjdhc%2F-LiyFa8rJEEA7TLQ30N_%2Fimage.png?alt=media\&token=49ec96d7-13b0-4a7d-910d-bd9a7a7045ed)

Another way to calculate the solution is with system of equations. But with big matrices, n\*n (unknowns) it takes a lot of computing power. This is why we use Gradient descent.&#x20;

### Gradient descent vs normal equations.&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Lj8wmBJXGLim0dPl1a2%2F-Lj8wtgLqiYvVAG6fn8H%2Fimage.png?alt=media\&token=9d833274-92ec-441a-a294-7f85d61168f5)

Normal equation doesn't work for classification algos, so it's important to know gradient descent.&#x20;
