Skip to content

About

Interactive knowledge distillation playground, running entirely in the browser

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Distillation Playground

Live demo: https://andreas-hum.github.io/distillation-playground/

The playground on the "few labels" preset: the teacher, the hard-label student and the distilled student trace the spiral, the student from scratch does not

An interactive playground for knowledge distillation (Hinton et al., 2015). It runs entirely in the browser: a small MLP written from scratch in TypeScript, plain canvas, no ML library, no backend, no tracking.

What it shows

A teacher MLP (2 hidden layers, 64 units by default) is trained on 2D toy data. Then three students of the same small size (1 hidden layer, 2 to 32 units) train side by side from the same initial weights; only their targets differ:

  1. from scratch: cross-entropy on the labels;
  2. hard labels: α · CE(teacher's argmax) + (1 − α) · CE(labels);
  3. distilled: α · T² · KL(softmax(teacher/T) ‖ softmax(student/T)) + (1 − α) · CE(labels).

The students can be limited to a fraction of the labels. The two students that learn from the teacher also see the unlabelled points (the transfer set), through the teacher; the scratch student only has the labelled ones. The teacher term is averaged over all training points, the CE term over the labelled ones. The hard-label loss is the distillation loss with one-hot targets at T = 1, so the gap between those two students is what the soft targets add beyond the teacher's answers.

On a desktop screen everything fits on one page:

  • Settings on the left: data, teacher, students, temperature T and α, training, presets.
  • Model cards: the decision regions of the teacher and the three students, with test accuracy, how often each student agrees with the teacher on the test set, and a small training-loss curve.
  • Inside a student: pick a student and see each input, hidden and output neuron as a tile of its output over the plane, joined by its weights (thickness and colour by sign and size), updated live as it trains.
  • At a point: hover over any plane (or focus one and use the arrow keys) to compare the class probabilities of the teacher and every student there, with the teacher's soft targets at the current T (dark knowledge) against T = 1.
  • Charts: test accuracy and test loss of the students over epochs; the soft targets of the selected point and the mean entropy of the teacher's soft targets against T.
  • How it works: what distillation is, what each student isolates, why soft targets carry information, why T².

The teacher epochs setting is a quality knob: a teacher trained briefly is wrong in places, and the students that learn from it inherit its mistakes. Training runs several full-batch Adam steps per animation frame (time-boxed), and the toolbar shows the epochs per second.

Data: moons, circles, spirals, Gaussian blobs and an XOR checkerboard, with 2 to 5 classes, adjustable noise and size, a seeded RNG, and a draw mode for adding your own points. Classes have a colour-blind-safe colour and a shape each; the students have a colour and a line style each. Every setting is stored in the URL hash, so Copy link reproduces the exact run.

Presets

Test accuracy at the default seed (120 test points), measured by node scripts/presets.ts:

Preset Labels the students see Teacher Scratch Hard labels Distilled
Few labels: distillation helps 12 98% 48% 92% 91%
Hard against soft labels (α = 0.5) 12 98% 48% 88% 92%
Temperature too low (T=1 behaves like hard labels) 280 (all) 98% 87% 87% 87%
Temperature too high (targets turn uniform) (T = 20) 12 98% 48% 92% 61%
Weak teacher: students inherit its mistakes (100 teacher epochs) 12 75% 48% 78% 78%
Student too small (2 hidden units) 280 (all) 98% 47% 47% 47%

With few labels, most of the gain over the scratch student comes from the teacher's answers on the unlabelled points: the hard-label student gets nearly as far as the distilled one. In the "hard against soft" preset the soft targets add four points of accuracy and lower the test cross-entropy from 0.50 to 0.35. At T = 20 the targets turn almost uniform and the distilled student drops to 61%, while the hard-label student, which T does not affect, stays at 92%. A weak teacher caps both students near its own accuracy, and no teacher helps a student too small for a spiral: distillation transfers knowledge, not capacity.

Keyboard: Space play/pause, S one epoch, R reset, arrow keys on a focused plane to step through the training points. All controls are native inputs. With prefers-reduced-motion, training does not start by itself.

Run locally

npm install
npm run dev        # http://localhost:5173
npm test           # Vitest
npm run typecheck
npm run build      # static site in dist/

The tests cover softmax with temperature, KL and cross-entropy against hand-computed values, finite-difference gradient checks of the MLP backward pass, of the distillation loss and of the hard-label loss, deterministic data generators, a student step that trains all three students from identical weights (T moves only the distilled one; α = 0 makes all three alike), URL-state validation including the teacher epochs, and a training run on the "few labels" preset in which both students that learn from the teacher beat the scratch student.

How it is built

  • src/mlp.ts: tanh MLP on flat Float32Arrays, full-batch forward and backward, Adam.
  • src/loss.ts: softmax with temperature, cross-entropy, KL, the distillation loss with its gradient, and the hard-label loss.
  • src/train.ts: teacher first, then the three students in lockstep (one epoch = one full-batch Adam step).
  • src/data.ts, src/rng.ts: seeded toy datasets (mulberry32).
  • src/state.ts: settings, presets, URL-hash encoding (validated on load).
  • src/plot.ts, src/main.ts: canvas drawing (regions, charts, the network view) and the UI.
  • scripts/presets.ts: trains every preset and prints the table above.

CI (GitHub Actions) typechecks, tests and builds every push and pull request, and deploys main to GitHub Pages.

References

Licence

MIT

About

Interactive knowledge distillation playground, running entirely in the browser

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages