# Machine learning: overfitting phenomena

Please explain why we should avoid "over fitting phenomena" in training a learning model and how to detect it?

• Welcome to math.SE! This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level. – user37238 Oct 6 '15 at 12:52
• Read a book. This one is free from the authors. statweb.stanford.edu/~tibs/ElemStatLearn – Mark L. Stone Oct 6 '15 at 12:56
• Do you mean what many people call "the overfitting phenomenon"? It's a problem in many activities, not just machine learning. Presumably, if you are asking this question, you know what the overfitting phenomenon is, which should give you a clue why we want to avoid it. If you don't know what it is, or are unsure of your understanding, perhaps your question should be, "What is it?" (But in that case be sure to say what you have heard about it and explain what appears to be missing or incomplete in those descriptions.) – David K Oct 6 '15 at 13:00

While i fully agree with the previous comments, for example that book on statistical learning is great and if you google around you will find some really nice accompanying videos from the authors as well...I would just add this short thought to get you started - ideally one wants to train a model which would be good in predicting general cases - the generality is what makes it so useful right? Now, having a model which is overfitted or over-trained basically means that it is good in predicting only particular cases. Now detecting it, depends strongly on what are you trying to do, there is no simple general look up table.

You can detect over-fitting by comparing training and test performance. For instance, you can divide the data $X$ into training and test sets and compute a loss function as the number of training samples increases. The distance between training and test loss will be proportional to the amount of over-fitting.

In order to avoid over-fitting (and do well on out-of-sample or test data), it is common to use cross-validation for model parameter tuning. Cross-validation divides the data into K folds and uses K-1 for training and 1 for testing averaging results over K iterations. This helps avoid over-fitting and selects the right parameters for the model.

What is overfitting?

It's when your model has learned from the data it was given (and very well, usually), yet does very poorly on new data.

Example: imagine you have a noisy line $$\{(x_i,y_i)\}_i$$, where $$x\in[0,1]$$ and $$y_i\sim \mathcal{N}(x_i,\varepsilon)$$ for some small $$\varepsilon$$. Consider fitting the model $$f_\ell(x)=ax$$ versus the model $$f_p(x)=\sum_{n=0}^{1000} a_nx^n$$. Suppose you have only a few points, say, 10. Basically, in the former case you will get a nicely generalizing line, while in the latter case, you can perfectly fit the data - but what happens in between the points from the training data set will usually be terrible. Another example is to have a model that memorizes the training set, and if it does not know the input, outputs a random value; i.e., it works as follows: given an input $$x_i$$, if $$x_i$$ is in the models' training set, it outputs y_i; otherwise, it outputs $$0$$. Clearly, it will do perfectly on the training data, but fail miserably on any other data you give it.

More formally, in ML we want to do empirical risk minimization, meaning find a function $$h^*: D\rightarrow \mathcal{Y}$$ from data space to target space such that $$h^* = \arg\min_{h\in\mathfrak{H}} \int_D L(y,h(x)) P(x,y)\, dxdy$$ where $$h^*$$ is out of some hypothesis (function) space $$\mathfrak{H}$$. The problem is that in practice we only cover a small portion of $$D$$ with our training data; hence, our minimization of the expectation above may be rather poor for the areas of the data space that our model has not seen.

Why should we avoid it?

Generally, in ML, we train a model and then want to use it on different data than what we trained with. E.g., if I train a model to predict drug efficacy, I don't want it to run on chemicals I trained with (for which I already have the answer); I want to run it on new ligands, for which I do not. As noted above, overfitting implies (by definition really) doing poorly on data outside of the training sample (but well on in-sample data) - but out-of-training-sample data are exactly the ones we care about in practice the most!

How can we detect it?

Usually, the standard practice in ML is to have three data sets: a training set, a validation set, and a testing set. In cross-validation, the first two are intertwined. The use of the validation set is to prevent overfitting on hyper-parameters; the use of the test set is to test generalization capabilities.

In other words, you train the model on one set, then test it on another (that it has never seen before): this gives an estimate of the generalization error (i.e., the out-of-sample error or loss). If the training error is much smaller than the validation or test error, then you are overfitting the model. If the validation error is much smaller than the test error, then the hyper-parameters of the algorithm may be overfitting.

How can we fix it?

If you see that the model does very well on samples it knows, but poorly on samples that it doesn't, then the standard approach is to regularize the model. Mathematically, this usually involves restricting $$\mathfrak{H}$$, meaning we only consider functions that we think are good candidates for the task. (In a Bayesian sense, we essentially place a prior on the hypothesis space). For example, when we place an $$L_2$$ weight decay penalty on the parameters of a neural network, we are regularizing it by forcing $$\mathfrak{H}$$ (or at least the part of it we can feasibly reach) to shrink to the set of functions parameterized with low-value weights (a little like putting a zero-centered Gaussian prior on the weights ;) ). Adding noise, like dropout, also regularizes the network. One can think of it as artificially expanding the parts of the data space seen by the network or lowering the capacity of the network by forcing it to "spend" part of its representational power handling the noise. Either way, it essentially "smooths" the behaviour of the network on unseen parts of $$D$$.

Lastly, I'll note the connection of overfitting to the famous bias-variance tradeoff. Note that here the variance refers to the variance in the model output as the training set changes, not the variance of the outputs within a particular training set, assuming a deterministic model anyway. Weaker models tend to have high bias (meaning they underfit; they do not even learn the training set that well), but low variance (meaning they behave "reasonably" on unseen data, and hence do not overfit). Powerful models (e.g., so-called "deep" ones) tend to have high variance: they essentially "learned" the noise and useless idiosyncrasies of the training data (meaning they overfit). In the latter case, if the training set changed, the predictions of the model on the same test data might change drastically, since it had overfit to the noise of the training set. A weaker (e.g, linear) model is less likely to have such unpredictable behaviour - but may do worse on the training data.

Since we are on the math SE, I'll just mention statistical learning theory as a good place to learn more from a theoretical point of view. For practical advice, any book on ML will talk about it.