Feature-Based & Relation-Based Distillation
Beyond logit matching: FitNets for intermediate layers, attention transfer, contrastive representation distillation (CRD), relational knowledge distillation (RKD), and when feature matching outperforms logit distillation.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- FitNets: Intermediate Layer Matching
- Attention Transfer
- PKT: Probabilistic Knowledge Transfer
- CRD: Contrastive Representation Distillation
- RKD: Relational Knowledge Distillation
- When to Use Feature vs Logit Distillation
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Design feature matching losses with appropriate projections.
- Derive attention transfer and its connection to feature importance.
- Explain CRD's contrastive objective and why it maximizes mutual information.
- Implement relational distillation (distance and angle losses).
- Choose between feature-based and logit-based distillation for different scenarios.
Notation
- — teacher/student features at layer
- — projection/adaptation function
- — attention map at layer
Core Intuition
Logit distillation only transfers the teacher's FINAL output. But much of the teacher's knowledge is in its INTERMEDIATE representations — how it processes input through layers, what features it attends to, how it structures its hidden space. Feature distillation transfers this richer knowledge by matching intermediate layers, attention patterns, or inter-sample relationships.
Feature / Intermediate Distillation
FitNets: Intermediate Layer Matching
Romero et al. (2015): Match student's intermediate layer to teacher's:
where is a learned linear projection (since in general).
Layer selection: Match the "hint layer" (teacher's intermediate) to the "guided layer" (student's intermediate):
- Typically: teacher's layer at 2/3 depth → student's layer at 1/2 depth.
- Deeper teacher layers contain more abstract features.
Two-stage training:
- Stage 1: Train only the student layers up to the guided layer (match intermediate).
- Stage 2: Train the full student end-to-end (logit distillation + classification loss).
Challenge: Which teacher layer should guide which student layer? Misalignment hurts performance.
Attention Transfer
Zagoruyko & Komodakis (2017): Transfer what the teacher ATTENDS to, not the full feature map.
Attention map: Summarize a feature map by channel-wise aggregation:
With : squared attention (emphasizes strong activations).
Loss:
Advantage: Resolution-independent (teacher and student can have different spatial sizes after normalization). Focuses on WHERE the model attends, not the full representation.
PKT: Probabilistic Knowledge Transfer
Passalis & Tefas (2018): Model features as probability distributions in representation space.
Teacher feature distribution: (kernel density estimate).
Student feature distribution: .
Loss: KL divergence between pairwise similarity distributions:
Intuition: The student's feature space should have the same geometry as the teacher's — similar samples should be close, dissimilar ones far.
CRD: Contrastive Representation Distillation
Tian et al. (2020): Maximize mutual information between teacher and student representations via contrastive learning.
Objective: For a sample , its teacher feature and student feature should be more similar than random pairings:
where is a learned similarity function and are negative samples.
Why contrastive beats MSE:
- MSE requires exact matching (sensitive to irrelevant details).
- Contrastive only requires RELATIVE similarity (preserves structure, ignores noise).
- Invariant to linear transformations of the feature space.
Result: State-of-the-art single-method distillation on CIFAR-100 and ImageNet.
RKD: Relational Knowledge Distillation
Park et al. (2019): Don't match individual features — match RELATIONSHIPS between samples.
Distance loss: Preserve pairwise distances:
where is the normalized pairwise distance: .
Angle loss: Preserve angular relationships (triplets):
where is the angle at vertex in the triangle formed by features of samples .
Advantage: Completely agnostic to dimensionality mismatch. No projection layer needed. Works even when teacher and student have wildly different architectures.
When to Use Feature vs Logit Distillation
Use logit distillation when:
- Teacher and student have similar architectures.
- The task is classification with many classes (rich logit signal).
- Simplicity is desired (no layer matching decisions).
Use feature distillation when:
- Large architecture gap (ResNet teacher → MobileNet student).
- Dense prediction tasks (detection, segmentation) — intermediate features matter.
- The student needs to learn specific spatial patterns.
Use relational distillation when:
- Extreme architecture differences (CNN teacher → Transformer student).
- Retrieval/embedding tasks (preserving metric structure).
- No clear layer correspondence between teacher and student.
Best practice: Combine: .
Common Pitfalls
Pitfall 1. Matching ALL intermediate layers simultaneously. Too many constraints overconstrain the student. Match 1-3 carefully chosen layers.
Pitfall 2. Forgetting the projection layer when dimensions differ. Direct MSE between and features is meaningless without a learned adapter.
Pitfall 3. Using MSE for feature matching when CRD would be better. MSE penalizes absolute values; if the teacher uses a different scale or has irrelevant noise, CRD's relative matching is more robust.
Summary
- FitNets: Match intermediate layers with projection; two-stage training.
- Attention transfer: Match spatial attention maps (WHERE the model looks).
- CRD: Contrastive learning to maximize teacher-student mutual information.
- RKD: Match pairwise distances and angles (architecture-agnostic).
- Choice depends on: architecture similarity, task type, computational budget.
- Best results: combine logit + feature + relational losses.
Exercises
Exercise 1. For a teacher with layers and student with : design the projection layers for FitNet matching.
Exercise 2. Compute the attention map (equation 2, ) for a feature map with known values.
Exercise 3. For CRD with negatives and : compute the loss for a positive pair with similarity 0.9 and negatives with similarities .
Exercise 4. Prove that RKD angle loss is invariant to uniform scaling of features.
Exercise 5. Design a combined distillation loss for distilling ViT-L (teacher) into EfficientNet-B0 (student) for ImageNet. Specify which layers to match and the loss weights.