# Deep ensembles

The core idea behind ensembling is that by having a committee of models, different strengths will complement one another, and many weaknesses will cancel each other out. This is the guiding intuition behind 18th century French mathematician Nicolas de Condorcet’s famous jury theorem (Estlund 1994): If each juror has a probability that's greater than 50% of arriving at the true verdict, and if the jurors make independent decisions, the probability of a correct group verdict increases to 100% as the number of jurors increases.

Moving to recent history, the process of ensembling ML models includes two steps: training
different models and combining the predictions. You can obtain different models by using
different feature subsets, training data, training regimes, and model architectures. You can
combine predictions by averaging them, training a new model on top of the predictions
(*model stacking*), or using custom voting rules that you can tune to a
specific context (see the case study for one such example). Two
of the initial ensembling techniques for machine learning are *boosting*
(Freund and Schapire 1996) and *random forests*
(Breiman 2001). These are two complementary approaches.

The idea behind boosting is to sequentially train weak learners. Each subsequent model focuses on a subset of the data and is boosted by the errors previously observed during training. In this way each sequential tree is trained on a new training set that was previously unseen. At the end of training, predictions are averaged across the weak learners.

The idea behind random forests is training multiple decision tree models without pruning, on bootstrapped samples of the data and by selecting random feature subsets. Breiman showed that the generalization error has an upper bound that is a function of the number and decorrelation of the individual trees.

In deep learning, dropout is designed as a regularization technique and can also be interpreted as an ensemble of multiple models (Srivastava et al. 2014). The realization that dropout could be used to effectively quantify uncertainty (Gal and Ghahramani 2016) motivated a further exploration of ensembles in deep learning models for the same purpose. Deep ensembles have been shown to outperform MC dropout in quantifying uncertainty in a variety of datasets and tasks in regression and classification (Lakshminarayanan, Pritzel, and Blundell 2017). Additionally, deep ensembles have been shown to be state-of-the-art in out-of-distribution settings (such as perturbations of the data or the introduction of new classes unseen during training). They outperform MC dropout and other methods (Ovadia et al. 2019). The reason why deep ensembles perform so well in out-of-distribution settings is that their weight values and loss trajectories are very different from one another, and, as a result, they lead to diverse predictions (Fort, Hu, and Lakshminarayanan 2019).

Neural networks often have hundreds of millions more parameters than training data points.
This means that they include a large space of possible functions that might sufficiently
approximate the data generating function. Consequently, there are many low-loss valleys and
regions that all correspond to good, but different, functions. Viewed from a Bayesian perspective
(Wilson and Izmailov 2020), these candidate functions correspond
to different hypotheses that identify the true underlying function. As such, the more candidate
functions you ensemble, the more likely you are to represent the truth, and therefore achieve a
robust model that shows low confidence when you extend inference out of distribution. Ensembles
essentially settle in many distant low-loss valleys, yielding a *distribution of diverse
functions* (Fort, Hu, and Lakshminarayanan 2019). On the
other hand, alternative methods such as MC dropout and alternative Bayesian approaches will hone
in to just one valley, yielding a *distribution of similar functions*.
Therefore, just a few independently trained neural networks from the ensemble—(Lakshminarayanan, Pritzel, and Blundell 2017) and (Ovadia et al. 2019) suggest that five models are sufficient—will more accurately
recover the true marginal likelihood (predictive distribution), when compared with sampling
around a single low-loss region, which will host a lot of redundancy (because functions will all
be similar).

In summary, to improve your accuracy and to maximize the reliability of your uncertainties, ensemble your models.