<br />

<button value="stable" default="">See Stable</button> <button value="nightly">See Nightly</button>

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/distillation/distillation_loss.py#L260-L390) |

Distillation loss that transfers knowledge from final model outputs.

Inherits From: [`DistillationLoss`](https://www.tensorflow.org/api_docs/python/tf/keras/distillation/DistillationLoss.md.txt)

```python
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.

<br />

<br />

<br />

| ## Arguments ||
|---|---|
| `temperature` | Temperature for softmax scaling. Higher values produce softer probability distributions that are easier for the student to learn. Typical values range from 3-5. Defaults to 3.0. |
| `loss` | Loss function to use for distillation. Can be: <br /> - String identifier (e.g., 'kl_divergence', 'categorical_crossentropy') - Keras loss instance - Nested structure of losses matching the model output structure - `None` to skip distillation for that output (useful for multi-output models where you only want to distill some outputs) At least one loss must be non-`None`. Defaults to 'kl_divergence'. |

Examlpe(s):

```python
# 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`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/distillation/distillation_loss.py#L333-L376)

```python
compute_loss(
    teacher_outputs, student_outputs, **kwargs
)
```

Compute distillation loss using the configured loss function.

<br />

<br />

<br />

| 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). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| Distillation loss tensor. ||

<br />

### `from_config`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/distillation/distillation_loss.py#L385-L390)

```python
@classmethod
from_config(
    config
)
```

Create instance from configuration.

### `get_config`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/distillation/distillation_loss.py#L378-L383)

```python
get_config()
```

Get configuration for serialization.

### `validate_model_compatibility`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/distillation/distillation_loss.py#L73-L83)

```python
validate_model_compatibility(
    teacher, student
)
```

Validate that teacher and student models are compatible.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `teacher` | The teacher model. |
| `student` | The student model. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If models are not compatible with this distillation loss. |

<br />

### `validate_outputs`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/distillation/distillation_loss.py#L62-L71)

```python
validate_outputs(
    teacher_outputs, student_outputs
)
```

Validate that teacher and student outputs are compatible.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `teacher_outputs` | Outputs from the teacher model. |
| `student_outputs` | Outputs from the student model. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If outputs are not compatible. |

<br />