Ensemble Methods II — Stacking, Mixture of Experts, Activations
Meta-learning, gating networks, and the sigmoid/softmax distinction.
Three topics that look unrelated but are examined together: how to learn a combination rule, how to make that rule input-dependent, and how a network produces probabilities.
Stacking
> Combines multiple classification models via a meta-classifier. The individual models are trained on the complete training set; the meta-classifier is fitted on their outputs (the "meta-features").

Stacking, three steps
- Train the base classifiers on the training set.
- Generate meta-features — run the training data through the base classifiers and record their predictions as a new feature matrix.
- Train the meta-classifier on those meta-features against the true labels.

Stacking versus bagging — the comparison
| Bagging | Stacking | |
|---|---|---|
| Base models | same algorithm | usually different algorithms |
| Training data | different bootstrap subsets | the same full training set |
| Combination | fixed rule — vote / average, all weights equal | learned by a meta-model |
| What is learned | the base models only | base models and the combiner |
| Overfitting risk | low | high — the meta-model can fit the base models' training noise |
The leakage problem, and its fix. If you generate meta-features by predicting on the same rows the base models were trained on, those predictions are optimistically good (near-perfect on training data), so the meta-classifier learns to trust whichever base model overfits hardest. The standard fix is stacking with cross-validation: generate out-of-fold predictions, so each meta-feature row comes from a model that never saw that row. The deck has dedicated slides for exactly this (Stacking w/ Cross-Validation).

The notebook's mlxtend result is a nice empirical footnote: on the breast-cancer features 2–4, KNN 0.86, decision tree 0.87, SVM 0.89, and the stack 0.88 — stacking does not automatically win. It only helps when the base models are individually strong and make different errors.
Mixture of Experts
> A divide-and-conquer ensemble technique.
The contrast with stacking is the point:
- Stacking feeds all inputs through all models globally, then learns a fixed combiner.
- MoE assumes different models (experts) specialise in localised regions of the feature space, and the combiner is a function of the input.

Two components:
- Experts — a collection of base models, each $y_m(x)$
- Gating — determines the suitable expert for a specific input

The two formulas
$$\boxed{\;y(x) = \sum_{m} g_m(x)\, y_m(x)\;}$$
$$\boxed{\;g_m(x) = \frac{\exp\!\big(z_m(x)\big)}{\sum_{i}\exp\!\big(z_i(x)\big)}\;}$$

"What is the role of gating in Mixture-of-Experts? How is it calculated?" The gate decides which expert should be trusted for this particular input, and by how much. Unlike bagging or boosting, the weight of each expert is not constant — it is a function of $x$. Concretely, the gate is a small learned network that produces one logit $z_m(x)$ per expert, and the weights are the softmax over those logits, $g_m(x)=\exp(z_m(x))/\sum_i \exp(z_i(x))$, which makes them non-negative and sum to one. The final output is the gated sum $y(x)=\sum_m g_m(x)y_m(x)$. The softmax is what enforces "a probability distribution over experts", so the model can be read as softly choosing a region of feature space.
Why softmax and not a hard argmax: softmax is differentiable, so the gate can be trained end-to-end by gradient descent together with the experts. A hard argmax would have zero gradient almost everywhere.
Sigmoid and softmax
The deck detours into activation functions because the gate is a softmax.
Logistic regression, again
$$y(x) = \sigma(w^Tx + w_0), \qquad \sigma(z) = \frac{1}{1+e^{-z}}$$
Output is $p(y\mid x)$, thresholded to produce a class.

The two formulas side by side
$$\sigma(z_i) = \frac{1}{1+e^{-z_i}} \qquad\qquad \text{softmax}(z)_i = \frac{e^{z_i}}{\sum_{j=1}^{C} e^{z_j}}$$
| Sigmoid | Softmax | |
|---|---|---|
| Classes | 2 (binary) | > 2 (multiclass) |
| Output range | $(0,1)$, independent per unit | $(0,1)$, sums to 1 across $C$ |
| Interpretation | $P(y=1\mid x)$ | $P(y=c\mid x)$ for each $c$ |
| Units needed | 1 | $C$ |
| Loss that pairs with it | binary cross-entropy | categorical cross-entropy |
| Multi-label? | yes — each label independent | no — classes mutually exclusive |

For $C=2$, softmax with two logits is equivalent to sigmoid on their difference: $\text{softmax}([z_0,z_1])_1 = \sigma(z_1-z_0)$. So the choice is a parameterisation choice, not a modelling one — but in practice you use 1 output + sigmoid, or $C$ outputs + softmax, never both.
The deck's own decision prompts are the right test of understanding:
- MNIST digit (0–9) → 10 mutually exclusive classes → softmax.
- "Does this patient have COVID?" → one binary question → sigmoid.
- CIFAR-10 (10 object categories) → mutually exclusive → softmax.


The chest-X-ray case is the one people get wrong when the question says "pneumonia, COVID, normal — what is the category of this X-ray?" If the classes are exclusive, softmax; if the finding list allows several simultaneous findings (COVID and pneumonia), you need C independent sigmoids, not a softmax. Read the wording.
































