CS-4125 ML
Lecture 10: Advice for Applying Machine Learning
Practical advice and diagnostic techniques for evaluating and improving machine learning algorithms, including train/validation/test splits, bias vs. variance analysis, regularization tuning, and learning curves.
Deciding What to Try Next
Debugging a Learning Algorithm
- We now know many techniques.
- But, there is a big difference between someone who knows an algorithm vs. someone less familiar and doesn't understand how to apply them.
- Make sure you know how to choose the best avenues to explore the various techniques.
- Here we focus on deciding what avenues to try.
- So, say you've implemented regularized linear regression to predict housing prices:

- Trained it.
- But, when you test on new data you find it makes unacceptably large errors in its predictions.
:-(
- What should you try next?
- There are many things you can do:
- Get more training data: Sometimes more data doesn't help. Often it does though, although you should always do some preliminary testing to make sure more data will actually make a difference (discussed later).
- Try a smaller set of features: Carefully select a small subset. You can do this by hand, or use some dimensionality reduction technique (e.g., PCA — we'll get to this later).
- Try getting additional features: Sometimes this isn't helpful. LOOK at the data. Can be very time consuming.
- Adding polynomial features: You're grasping at straws, aren't you...
- Building your own, new, better features based on your knowledge of the problem: Can be risky if you accidentally overfit your data by creating new features which are inherently specific/relevant to your training data.
- Try decreasing or increasing : Change how important the regularization term is in your calculations.
- These changes can become MAJOR projects/headaches (6 months+).
- Sadly, the most common method for choosing one of these examples is to go by gut feeling (randomly).
- Many times, you see people spend huge amounts of time only to discover that the avenue is fruitless (no apples, pears, or any other fruit. Nada.).
- There are some simple techniques which can let you rule out half the things on the list — saving you a lot of time!
- There are many things you can do:
- Machine learning diagnostics:
- Tests you can run to see what is/what isn't working for an algorithm.
- See what you can change to improve an algorithm's performance.
- These can take time to implement and understand (a week).
- But, they can also save you spending months going down an avenue which will never work.
Evaluating a Hypothesis
- When we fit parameters to training data, we try to minimize the error.
- We might think a low error is good — it doesn't necessarily mean a good parameter set.
- Could, in fact, be indicative of overfitting.
- This means your model will fail to generalize.
- How do you tell if a hypothesis is overfitting?
- Could plot , but with lots of features it may be impossible to plot.
- We might think a low error is good — it doesn't necessarily mean a good parameter set.
- Standard way to evaluate a hypothesis is:
- Split data into two portions:
- 1st portion is training set
- 2nd portion is test set
- Typical split might be 70:30 (training:test).
- Split data into two portions:

- Note: If data is ordered, send a random percentage (or randomly order, then split data). Data is typically ordered in some way anyway.
- So a typical train and test scheme would be:
- Learn parameters from training data, minimizing using 70% of the training data.
- Compute the test error:
- average square error as measured on the test set.

- This is the definition of the test set error.
- What about if we were using logistic regression?
- The same: learn using 70% of the data, test with the remaining 30%:

- Sometimes there is a better way — misclassification error (0/1 misclassification):
- We define the error as follows:

- Then the test error is:

- i.e., it's the fraction in the test set that the hypothesis mislabels.
- These are the standard techniques for evaluating a learned hypothesis.
Model Selection and Training / Validation / Test Sets
- How to choose regularization parameter or degree of polynomial (model selection problems).
- We've already seen the problem of overfitting.
- More generally, this is why training set error is a poor predictor of hypothesis accuracy for new data (generalization).
- Model selection problem:
- Try to choose the degree for a polynomial to fit data:

- what degree of polynomial do you want to pick.
- An additional parameter to try and determine for your training set:
- (linear)
- (quadratic)
- Choose a model, fit that model and get an estimate of how well your hypothesis will generalize.
- An additional parameter to try and determine for your training set:
- You could:
- Take model 1, minimize with training data which generates a parameter vector (where ).
- Take model 2, do the same, get a different (where ).
- And so on...
- Take these parameters and look at the test set error for each using the previous formula:
- You could then:
- See which model has the lowest test set error. Say, for example, is the lowest.
- Now take the model and say, how well does it generalize?
- You could use , BUT this is going to be an optimistic estimate of generalization error, because our parameter () was fit to that test set (i.e., we specifically chose it because the test set error was small).
- So not a good way to evaluate if it will generalize!
- Improved model selection:
- Given a dataset, instead split into three pieces:
- Training set (60%) — values
- Cross validation (CV) set (20%) —
- Test set (20%) —
- As before, we can calculate:
- Training error
- Cross validation error
- Test error
- Given a dataset, instead split into three pieces:

- So:
- Minimize cost function for each of the models as before (on training set).
- Test these hypotheses on the cross validation set to generate the cross validation error.
- Pick the hypothesis with the lowest cross validation error (e.g., pick ).
- Finally, estimate generalization error of the model using the test set ().
- Final note:
- In machine learning as practiced today — many people will select the model using the test set and then check the model is OK for generalization using the test error (which we've said is bad because it gives a biased analysis).
- With a MASSIVE test set this is maybe OK.
- But considered much better practice to have separate training, validation, and test sets.
- In machine learning as practiced today — many people will select the model using the test set and then check the model is OK for generalization using the test error (which we've said is bad because it gives a biased analysis).
Diagnosis — Bias vs. Variance
- If you get bad results, it's usually because of one of two main issues:
- High bias — underfitting problem
- High variance — overfitting problem
- Important to work out which is the problem:
- Knowing which will help let you improve the algorithm.
- Bias/variance shown graphically below:

- The degree of a model will increase as you move towards overfitting.
- Let's define training and cross validation error as before.
- Now plot:
- CV error and test set error will be very similar:

- This plot helps us understand the error.
- We want to minimize both errors — which is why that model is the sweet spot.
- How do we apply this for diagnostics?
- If CV error is high, we're either at the high or the low end of :

- If is too small this probably corresponds to a high bias problem.
- If is too large this probably corresponds to a high variance problem.
- For the high bias case, we find both cross validation and training error are high:
- Doesn't fit training data well.
- Doesn't generalize either.
- For high variance, we find the cross validation error is high but training error is low:
- So we suffer from overfitting (training error is low, cross validation error is high).
- i.e., training set fits well, but generalizes poorly.
Regularization and Bias/Variance
- How is bias and variance affected by regularization?

- The equation above describes fitting a high order polynomial with regularization (used to keep parameter values small).
- Consider three cases:
- :
- All values are heavily penalized.
- So most parameters end up being close to zero.
- So hypothesis ends up being close to 0.
- So high bias underfitting data.
- :
- Only this value gives the fitting which is reasonable.
- :
- , so we make the regularization term 0.
- So high variance overfitting (minimal regularization means it obviously doesn't do what it's meant to).
- :

- How can we automatically choose a good value for ?
- To do this we define another function which is the optimization function without the regularization term (average squared errors).
- Define cross validation error and test set errors as before (i.e. without regularization term). So they are average squared error of various sets.

- Choosing :
- Have a set or range of values to use. Often increment by factors of 2:
- This gives a number of models which have different .
- With these models:
- Take each one (-th) and minimize the cost function (with regularization ). This will generate some parameter vector, call this .
- So now we have a set of parameter vectors corresponding to models with different values.
- Take all of the hypotheses and use the cross validation set to validate them.
- Measure average squared error on cross validation set.
- Pick the model which gives the lowest error (say we pick ).
- Finally, take the one we've selected () and test it with the test set.
- Have a set or range of values to use. Often increment by factors of 2:
- Bias/variance as a function of :
- Plot vs. :
- When is small you get a small value (regularization basically goes to 0).
- When is large you get a large value corresponding to high bias.
- :
- When is small we see high variance (too small a value means we overfit the data).
- When is large we end up underfitting, so this is high bias.
- So cross validation error is high.
- Such a plot can help show you you're picking a good value for .
- Plot vs. :
Learning Curves
- A learning curve is often useful to plot for algorithmic sanity checking or improving performance.
- What is a learning curve?
- Plot (average squared error on training set) or (average squared error on cross validation set) against (number of training examples).
- is a constant. So artificially reduce and recalculate errors with the smaller training set sizes.
- : Error on smaller sample sizes is smaller (as less variance to accommodate). So as grows, error grows.
- : Error on cross validation set. When you have a tiny training set your hypothesis generalizes badly. But as training set grows your hypothesis generalizes better. So CV error will decrease as increases.
- What do these curves look like if you have High Bias?
- e.g., setting a straight line to quadratic data.

- : Training error is small at first and grows. Training error becomes close to cross validation. So the performance of the cross validation and training set end up being similar (but very poor).
- : Straight line fit is similar for a few vs. a lot of data. So it doesn't generalize any better with lots of data because the function just doesn't fit the data. No increase in data will help it fit.
- The problem with high bias is because cross validation and training error are both high.
- Also implies that if a learning algorithm has high bias, as we get more examples the cross validation error doesn't decrease.
- So if an algorithm is already suffering from high bias, more data does NOT help!
- So knowing if you're suffering from high bias is good!
- In other words, high bias is a problem with the underlying way you're modeling your data.
- So more data won't improve that model; it's too simplistic.
- What do these curves look like if you have High Variance?
- e.g., high order polynomial.

- : When set size is small, training error is small too. As training set size increases, value is still small but slowly increases (in a near linear fashion). Error is still low.
- : Error remains high, even when you have a moderate number of examples, because the problem with high variance (overfitting) is your model doesn't generalize.
- An indicative diagnostic that you have high variance is that there's a big gap between training error and cross validation error.
- If a learning algorithm is suffering from high variance, more data IS probably going to help!
- These are clean curves. In reality the curves you get are far dirtier, but learning curve plotting can help diagnose the problems your algorithm will be suffering from.
What to Do Next (Revisited)
- How do these ideas help us choose how we approach a problem?
- Original example:
- Trained a learning algorithm (regularized linear regression).
- But, when you test on new data you find it makes unacceptably large errors in its predictions.
- What should you try next? How do we decide what to do?
- Get more examples fixes high variance (not good if you have high bias).
- Smaller set of features fixes high variance (overfitting) (not good if you have high bias).
- Try adding additional features fixes high bias (because hypothesis is too simple; make hypothesis more specific).
- Add polynomial terms fixes high bias problem.
- Decreasing fixes high bias.
- Increasing fixes high variance.
- Relating it all back to Neural Networks — Selecting a network architecture:
- One option is to use a small neural network:
- Few (maybe one) hidden layer and few hidden units.
- Such networks are prone to underfitting, but they are computationally cheaper.
- Larger network:
- More hidden layers.
- How do you decide that a larger network is good?
- Using a single hidden layer is a good default.
- Also try with 1, 2, 3 hidden layers; see which performs best on cross validation set.
- So like before, take three sets (training, cross validation, test).
- More units: This is computationally expensive, prone to overfitting.
- Use regularization (like weight decay) to address overfitting.
- One option is to use a small neural network:
Lecture 09: Neural Networks - Learning
An in-depth guide to learning parameters in Neural Networks, covering the cost function, backpropagation algorithm, parameter unrolling, gradient checking, random weight initialization, and complete workflow implementation.
Lecture 11: Machine Learning System Design
A comprehensive guide to machine learning system design, including prioritization, error analysis, handling skewed classes with precision/recall and F1 score, and analyzing data requirements.