keras-team/keras · error · NotImplementedError
Subclasses must implement compute_loss
Error message
Subclasses must implement compute_loss
What it means
Raised by the base DistillationLoss in keras/src/distillation/distillation_loss.py: compute_loss is the abstract hook that each distillation strategy must provide, and the base class intentionally raises to signal that a subclass forgot to override it. Calling a DistillationLoss subclass (or the base) that lacks its own compute_loss triggers this error.
Solutions
- Subclass DistillationLoss and implement compute_loss(self, teacher_outputs, student_outputs, **kwargs)
- Use a provided concrete subclass (e.g. a built-in distillation loss) instead of instantiating the base
- Check for a misspelled override name (e.g. compute_losses) that fails to override
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/distillation/distillation_loss.py:60 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/e910568fe9c48ad6.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/distillation/distillation_loss.py:60
override the `compute_loss` method.
"""
def compute_loss(self, teacher_outputs, student_outputs, **kwargs):
"""Compute distillation loss between teacher and student outputs.
This method should implement the specific distillation logic for
transferring knowledge from teacher to student.
Arguments:
teacher_outputs: Outputs from the teacher model. Can be a single
tensor or a list/tuple of tensors for multi-output models.
student_outputs: Outputs from the student model. Can be a single
tensor or a list/tuple of tensors for multi-output models.
**kwargs: Additional arguments for custom distillation_loss.
Returns:
Distillation loss tensor.
"""
raise NotImplementedError("Subclasses must implement compute_loss")
def validate_outputs(self, 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.
"""
keras.tree.assert_same_structure(teacher_outputs, student_outputs)
def validate_model_compatibility(self, teacher, student):
"""Validate that teacher and student models are compatible.
Arguments:
teacher: The teacher model.
student: The student model.View on GitHub (pinned to 7a34a03db6)