CS-4125 ML
Lecture 07: Regularization
An in-depth guide to regularization in machine learning, covering overfitting, underfitting, regularized cost functions, and regularized linear and logistic regression.
The Problem of Overfitting
- So far we've seen a few algorithms - work well for many applications, but can suffer from the problem of overfitting
- What is overfitting?
- What is regularization and how does it help
Overfitting with Linear Regression
- Using our house pricing example again
- Fit a linear function to the data - not a great model
- This is underfitting - also known as high bias
- Bias is a historic/technical one - if we're fitting a straight line to the data we have a strong preconception that there should be a linear fit
- In this case, this is not correct, but a straight line can't help being straight!
- Fit a quadratic function
- Works well
- Fit a 4th order polynomial
- Now curve fits through all five examples
- Seems to do a good job fitting the training set
- But, despite fitting the data we've provided very well, this is actually not such a good model
- This is overfitting - also known as high variance
- Now curve fits through all five examples
- Algorithm has high variance
- High variance - if fitting high order polynomial then the hypothesis can basically fit any data
- Space of hypothesis is too large
- Fit a linear function to the data - not a great model

- To recap, if we have too many features then the learned hypothesis may give a cost function of exactly zero
- But this tries too hard to fit the training set
- Fails to provide a general solution - unable to generalize (apply to new examples)
Overfitting with Logistic Regression
- Same thing can happen to logistic regression
- Sigmoidal function is an underfit
- But a high order polynomial gives an overfitting (high variance hypothesis)

Addressing Overfitting
- Later we'll look at identifying when overfitting and underfitting is occurring
- Earlier we just plotted a higher order function - saw that it looks "too curvy"
- Plotting hypothesis is one way to decide, but doesn't always work
- Often have lots of features - here it's not just a case of selecting a degree polynomial, but also harder to plot the data and visualize to decide what features to keep and which to drop
- If you have lots of features and little data - overfitting can be a problem
- How do we deal with this?
-
- Reduce number of features
- Manually select which features to keep
- Model selection algorithms are discussed later (good for reducing number of features)
- But, in reducing the number of features we lose some information
- Ideally select those features which minimize data loss, but even so, some info is lost
-
- Regularization
- Keep all features, but reduce magnitude of parameters
- Works well when we have a lot of features, each of which contributes a bit to predicting
-
Cost Function Optimization for Regularization
- Penalize and make some of the parameters really small
- e.g. here and

- The addition in blue is a modification of our cost function to help penalize and
- So here we end up with and being close to zero (because the constants are massive)
- So we're basically left with a quadratic function

- In this example, we penalized two of the parameter values
- More generally, regularization is as follows:
- Regularization
- Small values for parameters corresponds to a simpler hypothesis (you effectively get rid of some of the terms)
- A simpler hypothesis is less prone to overfitting
- Another example
- Have 100 features
- Unlike the polynomial example, we don't know what are the high order terms
- How do we pick the ones to shrink?
- With regularization, take cost function and modify it to shrink all the parameters
- Add a term at the end
- This regularization term shrinks every parameter
- By convention you don't penalize - minimization is from onwards
- Add a term at the end

- In practice, if you include it has little impact
- is the regularization parameter
- Controls a trade off between our two goals:
-
- Want to fit the training set well
-
- Want to keep parameters small
-
- Controls a trade off between our two goals:
- With our example, using the regularized objective (i.e. the cost function with the regularization term) you get a much smoother curve which fits the data and gives a much better hypothesis
- If is very large we end up penalizing ALL the parameters ( etc.) so all the parameters end up being close to zero
- If this happens, it's like we got rid of all the terms in the hypothesis
- This results here is then underfitting
- So this hypothesis is too biased because of the absence of any parameters (effectively)
- If this happens, it's like we got rid of all the terms in the hypothesis
- If is very large we end up penalizing ALL the parameters ( etc.) so all the parameters end up being close to zero
- So, should be chosen carefully - not too big...
- We look at some automatic ways to select later in the course
Regularized Linear Regression
- Previously, we looked at two algorithms for linear regression
- Gradient descent
- Normal equation
- Our linear regression with regularization is shown below

- Previously, gradient descent would repeatedly update the parameters , where simultaneously
- Shown below:

- We've got the update here shown explicitly
- This is because for regularization we don't penalize so treat it slightly differently
- How do we regularize these two rules?
- Take the term and add
- Sum for every (i.e. to )
- This gives regularization for gradient descent
- Take the term and add
- We can show using calculus that the equation given below is the partial derivative of the regularized

- The update for :
- gets updated to
- So if you group the terms together:

- The term:
![]()
- Is going to be a number less than 1 usually
- Usually learning rate is small and is large
- So this typically evaluates to
- So the term is often around 0.99 to 0.95
- Usually learning rate is small and is large
- This in effect means gets multiplied by 0.99
- Means the squared norm of a little smaller
- The second term is exactly the same as the original gradient descent
Regularization with the Normal Equation
- Normal equation is the other linear regression model
- Minimize the using the normal equation
- To use regularization we add a term () to the equation
- is the identity matrix

Regularization for Logistic Regression
- We saw earlier that logistic regression can be prone to overfitting with lots of features
- Logistic regression cost function is as follows:

- To modify it we have to add an extra term:

- This has the effect of penalizing the parameters up to
- Means, like with linear regression, we can get what appears to be a better fitting lower order hypothesis
- How do we implement this?
- Original logistic regression with gradient descent function was as follows:

- Again, to modify the algorithm we simply need to modify the update rule for , onwards
- Looks cosmetically the same as linear regression, except obviously the hypothesis is very different

Advanced Optimization of Regularized Linear Regression
- As before, define a
costFunctionwhich takes a parameter and givesjValandgradientback

- use
fminunc- Pass it an
@costfunctionargument - Minimizes in an optimized manner using the cost function
- Pass it an
jVal- Need code to compute
- Need to include regularization term
- Need code to compute
Gradient- Needs to be the partial derivative of with respect to
- Adding the appropriate term here is also necessary

- Ensure summation doesn't extend to the lambda term!
- It doesn't, but, you know, don't be daft!
Lecture 06: Logistic Regression
An in-depth explanation of logistic regression for binary and multiclass classification, including hypothesis representation, decision boundaries, cost function formulation, gradient descent, advanced optimization, and the one-vs-all strategy.
Lecture 08: Neural Networks - Representation
An introduction to neural network representation, covering non-linear hypotheses, biological and artificial neuron models, forward propagation, complex non-linear function evaluation, and multiclass classification.