Distill an Ensemble Into a Student With Temperature-Softened Targets
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `soften_probabilities(probs, temperature)` and `distill_ensemble(members, X_train, y_train, X_test, y_test, temperature=2.0, student=None, seed=0)`. Fit the ensemble members, soften their averaged training probabilities, train a regressor student on the log of those softened probabilities, and report test-set accuracy and agreement.
Requirements:
- `soften_probabilities` clips probabilities to `[1e-12, 1]` and returns the row-normalized `p ** (1/T)`, computed in a numerically stable way. Both functions raise `ValueError` when `T <= 0`.
- Fit a `clone` of each member on the training data and leave the passed members unfitted. The ensemble probability is the mean of the members' `predict_proba`.
- Fit the student on `(X_train, log(softened_train_proba))`. The default student is `MLPRegressor(hidden_layer_sizes=(32,), max_iter=2000, random_state=seed)`.
- Predicted labels are `classes[argmax]` over the student outputs and over the ensemble's test probabilities, with `classes = np.unique(y_train)`.
- Return a dict with float `student_accuracy`, `ensemble_accuracy` and `agreement` (student predictions compared with ensemble predictions), plus the fitted `student` object.
Example:
```python
soften_probabilities(np.array([[0.5, 0.25, 0.25]]), 2.0)
# -> [[0.4142, 0.2929, 0.2929]] (sqrt(p) / sum(sqrt(p)))
soften_probabilities(p, 1.0) # -> p unchanged (rows that already sum to 1)
```
```hint Stable softening
Work in log space. Raising small probabilities to a power directly can underflow, and a softmax over scaled log-probabilities has a standard stabilizing step.
```
```hint Mapping outputs to labels
The student's output columns and the ensemble's probability columns are indexed 0 to K-1, while the labels may not be. Check which order the `predict_proba` columns follow.
```
### Constraints & Assumptions
- Each member is a scikit-learn classifier that supports `clone` and `predict_proba`.
- A custom `student` exposes only `fit` and `predict` and is used as-is (not cloned).
- Labels may be non-contiguous (for example `[0, 2, 5]`).
### What a Strong Answer Covers
- Numerically stable softening, including the clipping, the `T <= 0` check in both functions and the behavior at T = 1.
- Fitting clones so the caller's members stay unfitted, and averaging `predict_proba` with columns aligned to the sorted class labels.
- Training the student on log-softened targets and mapping its outputs to labels through `np.unique(y_train)`, including a student whose `predict` returns a 1-D array.
- Computing student accuracy, ensemble accuracy and agreement as floats and returning the fitted student.
- Why regressing on log-probabilities passes on more of the teacher's knowledge than hard labels do.
### Follow-up Questions
- How would you benchmark the latency win of the student over the ensemble?
- Why regress on log-probabilities instead of training a classifier on hard ensemble labels?
- How would you pick the temperature?
Overview: Implement knowledge distillation from a scikit-learn ensemble: soften the averaged class probabilities with a temperature, train a regressor student on their logs, and report test accuracy and agreement with the ensemble. Tests numerically stable softmax, label mapping and why soft targets carry more signal than hard labels.