CS 460G Midterm Hub / Lectures / Week 7 Lecture 3
Week 7 · Lecture 3

Ensemble Methods II — Stacking, Mixture of Experts, Activations

Meta-learning, gating networks, and the sigmoid/softmax distinction.

short answermath

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 — base models plus a meta-classifier
Stacking — base models plus a meta-classifier · slide 17

Stacking, three steps

  1. Train the base classifiers on the training set.
  2. Generate meta-features — run the training data through the base classifiers and record their predictions as a new feature matrix.
  3. Train the meta-classifier on those meta-features against the true labels.
The three stacking steps
The three stacking steps · slide 18

Stacking versus bagging — the comparison

BaggingStacking
Base modelssame algorithmusually different algorithms
Training datadifferent bootstrap subsetsthe same full training set
Combinationfixed rule — vote / average, all weights equallearned by a meta-model
What is learnedthe base models onlybase models and the combiner
Overfitting risklowhigh — 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).

Stacking with cross-validation — the leak-free construction
Stacking with cross-validation — the leak-free construction · slide 25

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.
MoE — experts specialise by region
MoE — experts specialise by region · slide 29

Two components:

  • Experts — a collection of base models, each $y_m(x)$
  • Gating — determines the suitable expert for a specific input
Experts and gate
Experts and gate · slide 30

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)}\;}$$

The mixture output and the softmax gate
The mixture output and the softmax gate · slide 31

"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.

Sigmoid as a probability output
Sigmoid as a probability output · slide 32

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}}$$

SigmoidSoftmax
Classes2 (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 needed1$C$
Loss that pairs with itbinary cross-entropycategorical cross-entropy
Multi-label?yes — each label independentno — classes mutually exclusive
Sigmoid for binary, softmax for multiclass
Sigmoid for binary, softmax for multiclass · slide 35

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.
MNIST — softmax
MNIST — softmax · slide 36
Chest X-ray, COVID — sigmoid
Chest X-ray, COVID — sigmoid · slide 37

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.