The architecture in context
The system we are building
Feature distillation asks the student to reconstruct an internal representation rather than merely imitate the teacher’s winning class. The compact network uses three convolutional stages and a projected feature vector, with a classifier available as a separate head. The training function supports feature MSE, classification cross-entropy and a combination.
Who does what in the stack
- PyTorch CNN
- Defines the compact student and feature projection.
- Cached teacher arrays
- Supply fixed targets with explicit sample identities.
- MSE / cross-entropy
- Separate representation imitation from label learning.
The custom loop aligns teacher features with student images and chooses which loss reaches which parameters. PyTorch supplies the network and optimizer; cached teacher arrays remove the need to execute the teacher at every student update. They do not remove the cost of producing or storing the teacher representation from the whole system.
From module map to executable structure
Inside Compact distillation student
CIFAR-100 images 3×32×32; teacher feature width 512; student classifier 100 classes.
| Layer or branch | Output shape | Implementation detail |
|---|---|---|
| Conv 3→32 + ReLU + pool 2 | B × 32 × 16 × 16 | 3×3, stride 1, padding 1. |
| Conv 32→64 + ReLU + pool 2 | B × 64 × 8 × 8 | 3×3, stride 1, padding 1. |
| Conv 64→128 + ReLU | B × 128 × 8 × 8 | Adaptive average pool 1×1 → flatten 128. |
| Feature projection | B × 512 | Linear 128→512; raw output is the feature-MSE target space. |
| Optional classifier | B × 100 | ReLU(features) → Linear 512→100. Unused by feature-only distillation. |
The teacher features are cached targets, so the teacher is not in the student’s backward graph. The feature projection is linear; applying its later classifier ReLU before computing MSE would change the experiment. Downstream Gram evaluation uses frozen features and must be reported separately from the student’s direct classifier accuracy.
The equation and the update
Adam 1e-3, batch 256; default 8 epochs, two seeds. Feature-only loss is MSE; hybrid adds supervised cross-entropy; scratch uses cross-entropy alone. These objectives update different parts of the same allocated model.
Implementation card / no invented benchmarks
Capacity, budget and execution evidence
- Parameters / retained state
- 210,596 allocated scalars; 51,300 classifier scalars receive no gradient in feature-only mode. The feature path has 159,296.
- Duration and hardware evidence
- The training function returns elapsed time, but E32’s JSON does not retain it; no duration is supplied as if measured.
- Source coordinates
- E31 lines 34–42, 71–91; E32 saved metrics
- Current reproduction context
- Current workstation, supplied by the author: Apple M4, 128 GB unified RAM, 40 GPU cores and 16 CPU cores. This is context for prospective reproduction, not attribution of every archived run. Python and framework versions are not fully locked for these historical sources; declarations, when available, are identified separately.
Counts above are calculated from the stated layer shapes unless identified as saved measurements. They exclude optimizer state and nontrainable buffers. No archived training was rerun for this revision.
What these design choices change
Global pooling makes the feature projection much smaller than flattening the last spatial map. A512-wide representation is chosen to match the teacher target, not because every student needs 512 features. Reducing it requires a new teacher projection or a different distillation objective.
Reproduction and measurement protocol
Assert teacher/student row identity before comparing features; cached targets in a different shuffle order create a valid-shaped but meaningless loss. Check classifier gradients are absent in feature-only mode. The saved teacher-to-student accuracy gap shows that parameter reduction did not preserve the teacher’s usefulness.
For a new run, save the resolved Python/framework versions, backend, dtype, seed, input shapes, batch size and exact source revision. Start with one batch and one update. Log training steps separately from epochs or environment steps. Do not equate the configured maximum with a completed budget or convergence.
Measure initialization/compilation, data preparation, warmed forward pass, training updates and evaluation separately. Synchronize accelerator work around timed regions using the chosen framework’s supported mechanism. Report peak process memory and framework allocation separately; parameter bytes exclude activations, gradients, optimizer state and input buffers. On a shared machine, begin with a single CPU worker and a small batch rather than claiming all available resources.
A closer look at the implementation
The code that carries the idea
The excerpt distinguishes feature regression from label supervision. Matching feature dimensions is only a shape check: normalization, scale and sample ordering also determine what MSE means. A classifier trained on those features and a separately fitted Gram readout are different deployment heads.
def train_student(Xtr, ytr, Ftr, mode, epochs, seed):
torch.manual_seed(seed); net = StudentCNN(); opt = torch.optim.Adam(net.parameters(), 1e-3)
n = len(Xtr); t0 = time.time()
for ep in range(epochs):
perm = torch.randperm(n)
for i in range(0, n, 256):
bi = perm[i:i + 256]; opt.zero_grad()
if mode == "distill":
loss = ((net.features(Xtr[bi]) - Ftr[bi]) ** 2).mean() # feature distillation only
elif mode == "distillsup": # hybrid: feature-distill + supervised (best shot for compact)
loss = ((net.features(Xtr[bi]) - Ftr[bi]) ** 2).mean() + nn.functional.cross_entropy(net(Xtr[bi]), ytr[bi])
else:
loss = nn.functional.cross_entropy(net(Xtr[bi]), ytr[bi]) # from scratch supervised
loss.backward(); opt.step()
return net, sum(p.numel() for p in net.parameters()), time.time() - t0Verbatim archive excerpt from closed_form_distill_v3_compact.py. Context-dependent historical code, not a standalone runnable program. Comments retain their original wording; the article distinguishes implemented behavior from stale or overbroad comments.
The boundary that matters
A small parameter count is not proof of successful knowledge transfer. The saved student falls substantially short of its teacher-feature comparator; E32 reports that outcome. The archive does not establish a teacher-equivalent edge model.