# Is it always better to use the whole dataset to train the final model?

A common technique after training, validating and testing the Machine Learning model of preference is to use the complete dataset, including the testing subset, to train a final model to deploy it on, e.g. a product.

My question is: Is it always for the best to do so? What if the performance actually deteriorates?

For example, let us assume a case where the model scores around 65% in classifying the testing subset. This could mean that either the model is trained insufficiently OR that the testing subset consists of outliers. In the latter case, training the final model with them would decrease its performance and you find out only after deploying it.

Re-phrasing my initial question:

If you had a one-time demonstration of a model, such as deploying it on embedded electronics on-board an expensive rocket experiment, would you trust a model that has been re-trained with the test subset in the final step without being re-tested on its new performance?

Once you have obtained optimal hyperparamters for your model, after training and cross validating etc., in theory it is ok to train the model on the entire dataset to deploy to production. This will, in theory, generalise better.

HOWEVER, you can no longer make statistical / performance claims on test data since you no longer have a test dataset.

If you deploy a model to production using the entire training dataset, and you know the true values of the target variable of the new incoming data (i.e the data the production model is making predictions on), then you can calculate real time performance metrics as this new data is like test data (it was unseen to the model during training). From this process you could update the models hyperparameters to achieve better performance.

But if you knew the target values of new data, why would you train a model in the first place?

In general, I would say if you have enough data with enough variety, then shuffling and splitting the data 80:20 training:test should be sufficient to train a robust model and not have to worry about generalisation issues (assuming of course you regularize the model).

• +1'd, thanks for the answer! seems like random shuffle of the dataset before splitting into 80/20 probably makes us feel "statistically" confident (: – pcko1 Jun 12 '18 at 14:02
• The performance on your held-out test set is supposed to generalize to the entire population, so long as you've done it correctly. Although you don't have a test set after applying your method to the entire dataset, the performance on your original cross-validated train/test sets is an unbiased estimator of the performance of your training algorithm. That's the whole point of CV - not to train or parameterize a model, but to estimate the performance of the model-building process. Your performance on any test sets prior to full-data modeling is your performance estimate on the full data. – Nuclear Wang Jun 12 '18 at 19:11

Interesting question. I personally haven't seen that for products going into production, but understand the logic.

Theoretically, the more data your deployed model has seen, the better is should generalise. So if you trained the model on the full set of data you have available, it should generalise better than a model which only saw for example train/val sets (e.g. ~ 90%) from the full data set.

The problem with this (and the reason we split data into train/val/test sets in the first place!) is that we want to be able to make statistical claims as to the accuracy on unseen data. As soon as we re-train a model again on all the data, it is no longer possible to make such claims.

### 

Here is a related question on Cross-Validated, where the accepted answer makes similar points to me and mentions other ways of doing things.

We loop over:

1. train a model
2. assess performance on validation set $\rightarrow$ if satisfactory, go to step 5
3. change model
4. go to step 1
5. assess performance on test set
6. Present model with test accuracy found in step 5

Eventually, if you manage to get a great score on the test set, you can claim it generalises well. So the question as to whether re-training on the full dataset will improve performance on future unseen data is not strictly something you can test. Empirical evidence of better performance in other related problem sets would be the only source or guidance at the point in time when you must make the decision.

A sanity check would be to test the final re-trained model again on the original test set; expecting that it scores higher than it ever did when the model only saw the train/val set, because it has actually seen the test set during training. This wouldn't make me feel 100% confident that this final model is superior in all future cases, but at least it is as good as it can be with the given data.

Perhaps there are more rigorous arguments against doing what you say (probably academically motovated), however it does seem appealing for practical applications!

• +1'd, thanks for your effort and for pointing out that post, I missed it! As for your suggestion to test the model on the whole training dataset in the end, I think you don't get any valuable insight by its result. Simply because the algorithm training is usually based on optimization of cost functions, therefore the trained model is optimal given the training data. Low accuracy on train data doesn't mean it's not optimal, it just means it simply can't do better given that dataset and the selected algorithm architecture. You cannot infer anything for its external validity by that. – pcko1 Jun 12 '18 at 13:57
• You're welcome! I totally agree with your statement (although I said to test the final trained model on the origin test data, not train). In any case, I would still want to just see that the final model hasn't done something completely unexpected. Theory and practice don't always align :) – n1k31t4 Jun 12 '18 at 14:00

A point that needs to be emphasized about statistical machine learning is that there are no guarantees. When you estimate performance using a held-out set, that is just an estimate. Estimates can be wrong.

This takes some getting used to, but it's something you're going to have to get comfortable with. When you say "What if the performance actually deteriorates?", the answer is sure, that could happen. The actual performance could be worse than you estimated/predicted. It could also be better. Both are possible. That's unavoidable. There is some inherent, irreducible uncertainty.

When you evaluate performance using a held-out test set, you are using data from the past to try to predict future performance. As they say, past performance is no guarantee of future results. This is a fact of life that we just have to accept.

You can't let this immobilize you. The fact that it's possible to do worse than you predicted is not a reason to avoid deploying to production a model trained on the data. In particular, it's also possible to do poorly if you don't do that. It's possible that a model trained on all the data (train+validation+test) will be worse than a model trained on just the train+validation portion. It's also possible that it will be better. So, rather than looking for a guarantee, we have to ask ourselves: What gives us the best chance of success? What is most likely to be the most effective?

And in this case, when you want to deploy to production, the best you can do is use all the data available to you. In terms of expected performance, using all of the data is no worse than using some of the data, and potentially better. So, you might as well use all of the data available to you to train the model when you build the production model. Things can still go badly -- it's always possible to get unlucky, whenever you use statistical methods -- but this gives you the best possible chance for things to go well.

In particular, the standard practice is as follows:

1. Reserve some of your data into a held-out test set. There is no hard-and-fast rule about what fraction to use, but for instance, you might reserve 20% for the test set and keep the remaining 80% for training & validation. Normally, all splits should be random.

2. Next, use the training & validation data to try multiple architectures and hyperparameters, experimenting to find the best model you can. Take the 80% retained for training and validation, and split it into a training set and a validation set, and train a model using the training set and then measure its accuracy on the validation set. If you are using cross-validation, you will do this split many times and average the results on the validation set; if you are not, you will do a single split (e.g., a 70%/30% split of the 80%, or something like that) and evaluate performance on the validation set. If you have many hyperparameters to try, do this once for each candidate setting of hyperparameter. If you have many architectures to try, do this for each candidate architecture. You can iterate on this, using what you've found so far to guide your choice of future architectures.

3. Once you're happy, you freeze the choice of architecture, hyperparameters, etc. Now your experimentation is done. Once you hit this point, you can never try any other options again (without obtaining a fresh new test set) -- so don't hit this point until you're sure you're ready.

4. When you're ready, then you train a model on the full training + validation set (that 80%) using the architecture and hyperparameters you selected earlier. Then, measure its accuracy on the held-out test set. That is your estimate/prediction for how accurate this modelling approach will be. You get a single number here. That number is what it is: if you're not happy with it, you can't go back to steps 1 and 2 and do more experimentation; that would be invalid.

5. Finally, for production use, you can train a model on the entire data set, training + validation + test set, and put it into production use. Note that you never measure the accuracy of this production model, as you don't have any remaining data for doing that; you've already used all of the data. If you want an estimate of how well it will perform, you're entitled to use the estimated accuracy from step 4 as your prediction of how well this will perform in production, as that's the best available prediction of its future performance. As always, there are no guarantees -- that's just the best estimate possible, given the information available to us. It's certainly possible that it could do worse than you predicted, or better than you predicted -- that's always true.

• +1'd for the effort, even though I don't fully agree :) e.g. when you mention "In terms of expected performance, using all of the data is no worse than using some of the data, and potentially better." I don't see the reasoning behind it. On the other hand, the 2nd point that you mention seems very important, cross validation! so essentially you train/validate with all samples, thus probably you reject outliers in the chosen final model. Thanks for your answer. – pcko1 Jun 12 '18 at 21:07
• @pcko1, The principle is simple. If you have data, should you use all of it, or some of it? Why? Maybe when we get data, before we do anything, we should just take 10% of it and throw it away and never look at it. In fact, if throwing out some is good, throwing out more is even better, so maybe we should throw out all our data. That's absurd, right? Why? See if you can figure out why, and then try applying it to this situation. Hopefully this gets you thinking! – D.W. Jun 12 '18 at 21:35

One of the reasons of having a data set is to avoid overfitting. If you employ cross-validation, you essentially allow the entire dataset to act as the training set, but retraining won’t let you validate whether there is sign of overfitting. I guess that either way (cross validation or retrain with the entire data set) should not dramatically change your result (from my uneducated guess), but you won’t be able to do hyperparameter tuning or validate your model performance as you don’t have a test set. Whether it ends up being better, it is hard to say, but I guess the only way to know is to do an A/B of the two models over real data over time.