Once we have done some trouble shooting for errors in our predictions by:
- Getting more training examples
- Trying smaller sets of features
- Trying additional features
- Trying polynomial features
- Increasing or decreasing λ
We can move on to evaluate our new hypothesis.
A hypothesis may have a low error for the training examples but still be inaccurate (because of overfitting). Thus, to evaluate a hypothesis, given a dataset of training examples, we can split up the data into two sets: a training set and a test set. Typically, the training set consists of 70 % of your data and the test set is the remaining 30 %.
The new procedure using these two sets is then:
The test set error
1. For linear regression: Jtest(Θ)=12mtest∑mtesti=1(hΘ(x(i)test)−y(i)test)2 J t e s t ( Θ ) = 1 2 m t e s t ∑ i = 1 m t e s t ( h Θ ( x t e s t ( i ) ) − y t e s t ( i ) ) 2
err(hΘ(x),y)={1 if hΘ(x)≥0.5 and y=0 or hΘ(x)<0.5 and y=10otherwise e r r ( h Θ ( x ) , y ) = { 1 if h Θ ( x ) ≥ 0.5 and y = 0 or h Θ ( x ) < 0.5 and y = 1 0 o t h e r w i s e
This gives us a binary 0 or 1 error result based on a misclassification. The average test error for the test set is:
TestError=1mtest∑mtesti=1err(hΘ(x(i)test),y(i)test) T e s t E r r o r = 1 m t e s t ∑ i = 1 m t e s t e r r ( h Θ ( x t e s t ( i ) ) , y t e s t ( i ) )
This gives us the proportion of the test data that was misclassified.
Just because a learning algorithm fits a training set well, that does not mean it is a good hypothesis. It could over fit and as a result your predictions on the test set would be poor. The error of your hypothesis as measured on the data set with which you trained the parameters will be lower than the error on any other data set.
Given many models with different polynomial degrees, we can use a systematic approach to identify the ‘best’ function. In order to choose the model of your hypothesis, you can test each degree of polynomial and look at the error result.
One way to break down our dataset into the three sets is:
We can now calculate three separate error values for the three different sets using the following method:
This way, the degree of the polynomial d has not been trained using the test set.
In this section we examine the relationship between the degree of the polynomial d and the underfitting or overfitting of our hypothesis.
At the same time, the cross validation error will tend to decrease as we increase d up to a point, and then it will increase as d is increased, forming a convex curve.
High bias (underfitting): both Jtrain(Θ) and JCV(Θ) J t r a i n ( Θ ) and J C V ( Θ ) will be high. Also, JCV(Θ)≈Jtrain(Θ) J C V ( Θ ) ≈ J t r a i n ( Θ ) .
High variance (overfitting): Jtrain(Θ) J t r a i n ( Θ ) will be low and JCV(Θ) J C V ( Θ ) will be much greater than Jtrain(Θ) J t r a i n ( Θ ) .
The is summarized in the figure below:
In the figure above, we see that as λ λ increases, our fit becomes more rigid. On the other hand, as λ λ approaches 0, we tend to over overfit the data. So how do we choose our parameter λ λ to get it ‘just right’ ? In order to choose the model and the regularization term λ λ , we need to:
Training an algorithm on a very few number of data points (such as 1, 2 or 3) will easily have 0 errors because we can always find a quadratic curve that touches exactly those number of points. Hence:
Experiencing high bias:
Low training set size: causes Jtrain(Θ) J t r a i n ( Θ ) to be low and JCV(Θ) J C V ( Θ ) to be high.
Large training set size: caused both Jtrain(Θ) J t r a i n ( Θ ) and JCV(Θ) J C V ( Θ ) to be hight with Jtrain(Θ)≈JCV(Θ) J t r a i n ( Θ ) ≈ J C V ( Θ )
If a learning algorithm is suffering from high bias, getting more training data will not (by itself) help much.
Experiencing high variance:
Low training set size: Jtrain(Θ) J t r a i n ( Θ ) to be low and JCV(Θ) J C V ( Θ ) to be high.
Large training set size: Jtrain(Θ) J t r a i n ( Θ ) increases with training set size and JCV(Θ) J C V ( Θ ) continues to decrease without leveling off. Also, Jtrain(Θ)<JCV(Θ) J t r a i n ( Θ ) < J C V ( Θ ) but the difference between them remains significant.
If a learning algorithm is suffering from high variance, getting more training data is likely to help.
Our decision process can be broken down as follows:
Diagnosing Neural Networks
A neural network with fewer parameters is prone to underfitting. It is also computationally cheaper.
A large neural network with more parameters is prone to overfitting. It is also computationally expensive. In this case you can use regularization (increase λ) to address the overfitting.
Using a single hidden layer is a good starting default. You can train your neural network on a number of hidden layers using your cross validation set. You can then select the one that performs best.
Model Complexity Effects:
System Design Example:
Given a data set of emails, we could construct a vector for each email. Each entry in this vector represents a word. The vector normally contains 10,000 to 50,000 entries gathered by finding the most frequently used words in our data set. If a word is to be found in the email, we would assign its respective entry a 1, else if it is not found, that entry would be a 0. Once we have all our x vectors ready, we train our algorithm and finally, we could use it to classify if an email is a spam or not.
So how could you spend your time to improve the accuracy of this classifier?
It is difficult to tell which of the options will be most helpful.
The recommended approach to solving machine learning problems is to:
For example, assume that we have 500 emails and our algorithm misclassifies a 100 of them. We could manually analyze the 100 emails and categorize them based on what type of emails they are. We could then try to come up with new cues and features that would help us classify these 100 emails correctly. Hence, if most of our misclassified emails are those which try to steal passwords, then we could find some features that are particular to those emails and add them to our model. We could also see how classifying each word according to its root changes our error rate:
It is very important to get error results as a single, numerical value. Otherwise it is difficult to assess your algorithm’s performance. For example if we use stemming, which is the process of treating the same word with different forms (fail/failing/failed) as one word (fail), and get a 3% error rate instead of 5%, then we should definitely add it to our model. However, if we try to distinguish between upper case and lower case letters and end up getting a 3.2% error rate instead of 3%, then we should avoid using this new feature. Hence, we should try new things, get a numerical value for our error rate, and based on our result decide whether we want to keep the new feature or not.