Машинное обучение (7)

машинное обучение

Это 25-й день моего участия в августовском испытании обновлений. Узнайте подробности события:Испытание августовского обновления

Заметки о машинном обучении Эндрю Нг — (7) переобучение

The Problem of Overfitting

Consider the problem of predicting yy from xеRx \in \R. The leftmost figure below shows the result of fitting a y=θ0+θ1xy=\theta_0+\theta_1x to a dataset. We can see that the data doesn't really lie on a straight line, and so the fit is not very good.

img

Instead, if we had added an extra feature x2x^2, and fit y=θ0+θ1+θ2x2y=\theta_0+\theta_1+\theta_2x^2, then we obtain a slightly better fit to the data (See middle figure).

Naively, it might seem that the more features we add, the better. However, there is also a danger in adding too many features: The rightmost figure is the result of fitting a 5th5^{th} order polynomial y=j=05θjxjy = \sum_{j=0} ^5 \theta_j x^j. We see that even though the fitted curve passes through the data perfectly, we would not expect this to be a very good predictor for unseen examples.

The figure on the left shows an instance of underfitting— в которых данные ясно показывают структуру, не захваченную моделью

Underfitting, or high bias, is when the form of our hypothesis function h maps poorly to the trend of the data. It is usually caused by a function that is too simple or uses too few features.

The figure on the right is an example of overfitting.

Overfitting, or high variance, is caused by a hypothesis function that makes accurate predictions for examples in the training set, but it does not generalize well to make accurate predictions on new, previously unseen examples. It is usually caused by a complicated function that creates a lot of unnecessary curves and angles unrelated to the data.

Overfitting is applied to both linear and logistic regression. There are two main options to address the issue of overfitting:

  1. Reduce the number of features:
  • Manually select which features to keep.
  • Use a model selection algorithm (studied later in the course).
  1. Regularization
  • Keep all the features, but reduce the magnitude of parameters θj\theta_j.
  • Regularization works well when we have a lot of slightly useful features.

Cost Function

If we have overfitting from our hypothesis function, we can reduce the weight that some of the terms in our function carry by increasing their cost.

Say we wanted to make the following function more quadratic:

θ0+θ1x+θ2x2+θ3x3+θ4x4\theta_0 + \theta_1x + \theta_2x^2 + \theta_3x^3 + \theta_4x^4

We'll want to eliminate the influence of θ3x3\theta_3x^3 and θ4x4\theta_4x^4 . Without actually getting rid of these features or changing the form of our hypothesis, we can instead modify our cost function:

minθ 12mi=1m(hθ(x(i))y(i))2+1000θ32+1000θ42min_\theta\ \dfrac{1}{2m}\sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)})^2 + 1000\cdot\theta_3^2 + 1000\cdot\theta_4^2

We've added two extra terms at the end to inflate the cost of θ3\theta_3 and θ4\theta_4. Now, in order for the cost function to get close to zero, we will have to reduce the values of θ3\theta_3 and θ4\theta_4 to near zero. This will in turn greatly reduce the values of θ3x3\theta_3x^3 and θ4x4\theta_4x^4 in our hypothesis function. As a result, we see that the new hypothesis (depicted by the pink curve) looks like a quadratic function but fits the data better due to the extra small terms θ3x3\theta_3x^3 and θ4x4\theta_4x^4.

img

We could also regularize all of our theta parameters in a single summation as:

minθ12m i=1m(hθ(x(i))y(i))2+λ j=1nθj2\mathop{min}\limits_{\theta} \dfrac{1}{2m}\ \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)})^2 + \lambda\ \sum_{j=1}^n \theta_j^2

The λ\lambda, or lambda, is the regularization parameter. It determines how much the costs of our theta parameters are inflated.

Using the above cost function with the extra summation, we can smooth the output of our hypothesis function to reduce overfitting.

Note. If lambda is chosen to be too large, it may smooth out the function too much and cause underfitting.

Regularized Linear Regression

We can apply regularization to both linear regression and logistic regression.

Gradient Descent

We will modify our gradient descent function to separate out θ0\theta_0 from the rest of the parameters because we do not want to penalize θ0\theta_0.

Repeat{θ0:=θ0α1mi=1m(hθ(x(i))y(i))x0(i)θj:=θjα[(1mi=1m(hθ(x(i))y(i))xji)+λmθj]jе{1,2,,n}}\begin{array}{l} Repeat\quad\{\\ \qquad \theta_0:=\theta_0-\alpha\frac{1}{m}\sum_{i=1}^m\big(h_\theta(x^{(i)})-y^{(i)}\big)x_0^{(i)}\\ \qquad \theta_j:=\theta_j-\alpha\Big[\Big(\frac{1}{m}\sum_{i=1}^m\big(h_\theta(x^{(i)})-y^{(i)}\big)x_j^{i}\Big)+\frac{\lambda}{m}\theta_j\Big]\qquad j\in\{1,2,\cdots,n\}\\ \} \end{array}

The term λmθj\frac{\lambda}{m}\theta_j performs our regularization. With some manipulation our update rule can also be represented as:

θj:=θj(1αλm)α1mi=1m(hθ(x(i))y(i))xj(i)\theta_j:=\theta_j(1-\alpha\frac{\lambda}{m})-\alpha\frac{1}{m}\sum_{i=1}^{m}\Big(h_\theta(x^{(i)})-y^{(i)}\Big)x_j^{(i)}

The first term in the above equation, (1αλm)(1-\alpha\frac{\lambda}{m}) will always be less than 11. Intuitively we can see it as reducing the value of θj\theta_j by some amount on every update. Notice that the second term is now exactly the same as it was before.

Normal Equation

Now let's approach regularization using the alternate method of the non-iterative normal equation.

To add in regularization, the equation is the same as our original, except that we add another term inside the parentheses:

θ=(XTX+λL)1XTywhereL=[0111]\begin{array}{l} \theta=(X^TX+\lambda \cdot L)^{-1}X^Ty\\ \textrm{where}\quad L=\left[\begin{array}{cccc} 0\\ &1\\ &&1\\ &&&\ddots\\ &&&&1 \end{array}\right] \end{array}

LL is a matrix with 00 at the top left and 11's down the diagonal, with 00's everywhere else. It should have dimension (n+1)×(n+1)(n+1)×(n+1). Intuitively, this is the identity matrix (though we are not including x0x_0), multiplied with a single real number λ.

Recall that if m<nm<n, then XTXX^TX is non-invertible. However, when we add the term λL\lambda\cdot L, then XTX+λLX^TX+\lambda\cdot L becomes invertible.

Regularized Logistic Regression

We can regularize logistic regression in a similar way that we regularize linear regression. As a result, we can avoid overfitting. The following image shows how the regularized function, displayed by the pink line, is less likely to overfit than the non-regularized function represented by the blue line:

img

Recall that our cost function for logistic regression was:

J(θ)=1mi=1m[y(i) log(hθ(x(i)))+(1y(i)) log(1hθ(x(i)))]J(\theta) = - \frac{1}{m} \sum_{i=1}^m \Big[ y^{(i)}\ \log \big(h_\theta (x^{(i)})\big) + (1 - y^{(i)})\ \log \big(1 - h_\theta(x^{(i)})\big) \Big]

We can regularize this equation by adding a term to the end:

J(θ)=1mi=1m[y(i) log(hθ(x(i)))+(1y(i)) log(1hθ(x(i)))]+λ2mj=1nθj2J(\theta) = - \frac{1}{m} \sum_{i=1}^m \large[ y^{(i)}\ \log (h_\theta (x^{(i)})) + (1 - y^{(i)})\ \log (1 - h_\theta(x^{(i)}))\large] + \frac{\lambda}{2m}\sum_{j=1}^n \theta_j^2

The second sum, j=1nθj2\sum_{j=1}^n \theta_j^2 means to explicitly exclude the bias term, θ0\theta_0. I.e. the θ\theta vector is indexed from 00 to nn (holding n+1n+1 values, θ0\theta_0 through θn\theta_n), and this sum explicitly skips θ0\theta_0, by running from 11 to nn, skipping 00. Thus, when computing the equation, we should continuously update the two following equations:

img