View source on GitHub
|
Distillation loss that transfers knowledge from final model outputs.
Inherits From: DistillationLoss
tf.keras.distillation.LogitsDistillation(
temperature=3.0, loss='kl_divergence'
)
This distillation loss applies temperature scaling to the teacher's logits before computing the loss between teacher and student predictions. It's the most common approach for knowledge distillation.
Examlpe(s):
# Basic logits distillation with KL divergence
distillation_loss = LogitsDistillation(temperature=3.0)
# With categorical crossentropy loss
distillation_loss = LogitsDistillation(
temperature=4.0,
loss="categorical_crossentropy"
)
# With custom loss instance
distillation_loss = LogitsDistillation(
temperature=4.0,
loss=keras.losses.CategoricalCrossentropy(from_logits=True)
)
# For multi-output models
distillation_loss = LogitsDistillation(
temperature=3.0,
loss=["kl_divergence", "categorical_crossentropy"]
)
# For multi-output models, only distill some outputs
distillation_loss = LogitsDistillation(
temperature=3.0,
loss=["kl_divergence", None] # Skip second output
)
Methods
compute_loss
compute_loss(
teacher_outputs, student_outputs, **kwargs
)
Compute distillation loss using the configured loss function.
| Arguments | |
|---|---|
teacher_outputs
|
Logits from teacher model. Can be a single tensor, list/tuple of tensors, or dict of tensors. |
student_outputs
|
Logits from student model. Can be a single tensor, list/tuple of tensors, or dict of tensors. |
**kwargs
|
Additional arguments (ignored). |
| Returns | |
|---|---|
| Distillation loss tensor. |
from_config
@classmethodfrom_config( config )
Create instance from configuration.
get_config
get_config()
Get configuration for serialization.
validate_model_compatibility
validate_model_compatibility(
teacher, student
)
Validate that teacher and student models are compatible.
| Arguments | |
|---|---|
teacher
|
The teacher model. |
student
|
The student model. |
| Raises | |
|---|---|
ValueError
|
If models are not compatible with this distillation loss. |
validate_outputs
validate_outputs(
teacher_outputs, student_outputs
)
Validate that teacher and student outputs are compatible.
| Arguments | |
|---|---|
teacher_outputs
|
Outputs from the teacher model. |
student_outputs
|
Outputs from the student model. |
| Raises | |
|---|---|
ValueError
|
If outputs are not compatible. |
View source on GitHub