SoftTarget
def SoftTarget(
pred:torch.Tensor, teacher_pred:torch.Tensor, T:float=5, # Temperature for softening
**kwargs
)->torch.Tensor:Knowledge distillation with softened distributions (Hinton et al.)
This module provides loss functions for knowledge distillation. These losses enable training a smaller “student” network to mimic a larger “teacher” network.
Loss Categories: - Output-based: SoftTarget, Logits, Mutual - compare final predictions - Feature-based: Attention, FitNet, Similarity, ActivationBoundaries - compare intermediate representations
These losses compare the final output predictions between student and teacher networks.
Every output-based loss takes pred and teacher_pred, student and teacher logits (B, C).
Knowledge distillation with softened distributions (Hinton et al.)
Direct logit matching between student and teacher
KL divergence between student and teacher
def DecoupledKD(
pred:torch.Tensor, teacher_pred:torch.Tensor, T:float=4, # Temperature for softening
alpha:float=1.0, # Weight for target-class KD (TCKD)
beta:float=8.0, # Weight for non-target-class KD (NCKD)
target:torch.Tensor | None=None, # Ground-truth labels (B,)
normalize:bool=False, # NKD mode: softmax only over non-target classes
**kwargs
)->torch.Tensor:Decoupled Knowledge Distillation (Zhao et al. CVPR 2022). With normalize=True: Normalized KD (Yang et al. ICCV 2023).
These losses compare intermediate feature representations, enabling the student to learn internal representations similar to the teacher.
Every feature-based loss takes fm_s and fm_t, dicts {layer_name: feature map} for student and teacher, zipped in order.
Attention transfer loss (Zagoruyko & Komodakis)
Boundary-based knowledge distillation (Heo et al.)
FitNets: direct feature map matching (Romero et al.)
Similarity-preserving knowledge distillation (Tung & Mori)
| Loss | Type | Best For | Complexity |
|---|---|---|---|
| SoftTarget | Output | General distillation, logit matching | Low |
| DecoupledKD | Output | Fine-grained logit distillation, dark knowledge emphasis | Medium |
DecoupledKD normalize=True |
Output | Improved dark knowledge transfer (NKD, ICCV 2023) | Medium |
| Logits | Output | Direct logit regression | Low |
| Mutual | Output | KL divergence matching | Low |
| Attention | Feature | When attention patterns matter | Low |
| FitNet | Feature | Intermediate feature matching | Medium |
| Similarity | Feature | Relational knowledge transfer | Medium |
| ActivationBoundaries | Feature | Boundary-aware matching | Medium |
Tests live in nbs/tests/test_losses.ipynb.