CSE-41XX
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:

Regularized linear regression cost function

  • 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 λ\lambda: 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!
  • 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 hθ(x)h_\theta(x), but with lots of features it may be impossible to plot.
  • 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).

Training set and test set split

  • 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:
    1. Learn parameters θ\theta from training data, minimizing J(θ)J(\theta) using 70% of the training data.
    2. Compute the test error:
      • Jtest(θ)=J_{\text{test}}(\theta) = average square error as measured on the test set.

Test set error for linear regression formula

  • 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%:

Test set error for logistic regression formula

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

0/1 misclassification error definition

  • Then the test error is:

Test error using misclassification error

  • 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:

Polynomial degree model selection examples

  • d=d = what degree of polynomial do you want to pick.
    • An additional parameter to try and determine for your training set:
      • d=1d = 1 (linear)
      • d=2d = 2 (quadratic)
      • \dots
      • d=10d = 10
    • Choose a model, fit that model and get an estimate of how well your hypothesis will generalize.
  • You could:
    • Take model 1, minimize with training data which generates a parameter vector θ(1)\theta^{(1)} (where d=1d = 1).
    • Take model 2, do the same, get a different θ(2)\theta^{(2)} (where d=2d = 2).
    • And so on...
    • Take these parameters and look at the test set error for each using the previous formula:
      • Jtest(θ(1))J_{\text{test}}(\theta^{(1)})
      • Jtest(θ(2))J_{\text{test}}(\theta^{(2)})
      • \dots
      • Jtest(θ(10))J_{\text{test}}(\theta^{(10)})
  • You could then:
    • See which model has the lowest test set error. Say, for example, d=5d=5 is the lowest.
    • Now take the d=5d=5 model and say, how well does it generalize?
    • You could use Jtest(θ(5))J_{\text{test}}(\theta^{(5)}), BUT this is going to be an optimistic estimate of generalization error, because our parameter (dd) 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:
      1. Training set (60%) — mm values
      2. Cross validation (CV) set (20%) — mcvm_{\text{cv}}
      3. Test set (20%) — mtestm_{\text{test}}
    • As before, we can calculate:
      • Training error
      • Cross validation error
      • Test error

Training, cross validation, and test error definitions

  • 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 θ(5)\theta^{(5)}).
    • Finally, estimate generalization error of the model using the test set (Jtest(θ(5))J_{\text{test}}(\theta^{(5)})).
  • 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.

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:

Underfitting, optimal, and overfitting plots for bias vs variance

  • The degree of a model will increase as you move towards overfitting.
  • Let's define training and cross validation error as before.
  • Now plot:
    • x=degree of polynomial dx = \text{degree of polynomial } d
    • y=error for both training and cross validation (two lines)y = \text{error for both training and cross validation (two lines)}
    • CV error and test set error will be very similar:

Polynomial degree vs error plot for training and cross validation

  • This plot helps us understand the error.
  • We want to minimize both errors — which is why that d=2d=2 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 dd:

High bias vs high variance on polynomial degree error plot

  • If dd is too small \to this probably corresponds to a high bias problem.
  • If dd is too large \to 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?

Regularized linear regression with high order polynomial

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

Underfitting, good fit, and overfitting with different lambda values

  • How can we automatically choose a good value for λ\lambda?
    • To do this we define another function Jtrain(θ)J_{\text{train}}(\theta) 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 12\frac{1}{2} average squared error of various sets.

Cost function without regularization term

  • Choosing λ\lambda:
    • Have a set or range of values to use. Often increment by factors of 2:
      • Model(1)    λ=0\text{Model}(1) \implies \lambda = 0
      • Model(2)    λ=0.01\text{Model}(2) \implies \lambda = 0.01
      • Model(3)    λ=0.02\text{Model}(3) \implies \lambda = 0.02
      • Model(4)    λ=0.04\text{Model}(4) \implies \lambda = 0.04
      • Model(5)    λ=0.08\text{Model}(5) \implies \lambda = 0.08
      • \dots
      • Model(p)    λ=10\text{Model}(p) \implies \lambda = 10
    • This gives a number of models which have different λ\lambda.
    • With these models:
      • Take each one (pp-th) and minimize the cost function J(θ)J(\theta) (with regularization λ(p)\lambda^{(p)}). This will generate some parameter vector, call this θ(p)\theta^{(p)}.
      • So now we have a set of parameter vectors corresponding to models with different λ\lambda 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 θ(5)\theta^{(5)}).
      • Finally, take the one we've selected (θ(5)\theta^{(5)}) and test it with the test set.
  • Bias/variance as a function of λ\lambda:
    • Plot λ\lambda vs. JtrainJ_{\text{train}}:
      • When λ\lambda is small you get a small value (regularization basically goes to 0).
      • When λ\lambda is large you get a large value corresponding to high bias.
    • JcvJ_{\text{cv}}:
      • When λ\lambda is small we see high variance (too small a value means we overfit the data).
      • When λ\lambda 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 λ\lambda.

Learning Curves

  • A learning curve is often useful to plot for algorithmic sanity checking or improving performance.
  • What is a learning curve?
    • Plot JtrainJ_{\text{train}} (average squared error on training set) or JcvJ_{\text{cv}} (average squared error on cross validation set) against mm (number of training examples).
    • mm is a constant. So artificially reduce mm and recalculate errors with the smaller training set sizes.
    • JtrainJ_{\text{train}}: Error on smaller sample sizes is smaller (as less variance to accommodate). So as mm grows, error grows.
    • JcvJ_{\text{cv}}: 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 mm increases.
  • What do these curves look like if you have High Bias?
    • e.g., setting a straight line to quadratic data.

Learning curves for high bias algorithm

  • JtrainJ_{\text{train}}: 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).
  • JcvJ_{\text{cv}}: 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.

Learning curves for high variance algorithm

  • JtrainJ_{\text{train}}: 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.
  • JcvJ_{\text{cv}}: 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 \to fixes high variance (not good if you have high bias).
      • Smaller set of features \to fixes high variance (overfitting) (not good if you have high bias).
      • Try adding additional features \to fixes high bias (because hypothesis is too simple; make hypothesis more specific).
      • Add polynomial terms \to fixes high bias problem.
      • Decreasing λ\lambda \to fixes high bias.
      • Increasing λ\lambda \to 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.

On this page