---
title: Transformations
description: >-
  Transformations allow for modifications to the input a metric receives by
  wrapping its pred and target arguments.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/wrappers/transformations'
---
# Transformations {#transformations}

Transformations allow for modifications to the input a metric receives by wrapping its `pred` and `target` arguments.
Transformations can be implemented by either subclassing the `MetricInputTransformer` base class and overriding the `.transform_pred()` and/or `transform_target()` functions, or by supplying a lambda function via the `LambdaInputTransformer`.
A `BinaryTargetTransformer` which casts target labels to 0/1 given a threshold is provided for convenience.

## Module Interface {#module-interface}

### `class torchmetrics.wrappers.MetricInputTransformer(wrapped_metric, **kwargs)` {#torchmetrics.wrappers.MetricInputTransformer}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L23-L81)

Abstract base class for metric input transformations.

Input transformations are characterized by them applying a transformation to the input data of a metric, and then forwarding all calls to the wrapped metric with modifications applied.

#### `compute()` {#torchmetrics.wrappers.MetricInputTransformer.compute}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L69-L71)

Wrap the compute call of the underlying metric.

Return type:

[`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")

#### `forward(*args, **kwargs)` {#torchmetrics.wrappers.MetricInputTransformer.forward}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L73-L76)

Wrap the forward call of the underlying metric.

Return type:

[`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")

#### `reset()` {#torchmetrics.wrappers.MetricInputTransformer.reset}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L78-L81)

Wrap the reset call of the underlying metric.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `transform_pred(pred)` {#torchmetrics.wrappers.MetricInputTransformer.transform_pred}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L40-L46)

Define transform operations on the prediction data.

Overridden by subclasses. Identity by default.

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

#### `transform_target(target)` {#torchmetrics.wrappers.MetricInputTransformer.transform_target}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L48-L54)

Define transform operations on the target data.

Overridden by subclasses. Identity by default.

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

#### `update(*args, **kwargs)` {#torchmetrics.wrappers.MetricInputTransformer.update}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L64-L67)

Wrap the update call of the underlying metric.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

### `class torchmetrics.wrappers.LambdaInputTransformer(wrapped_metric, transform_pred=None, transform_target=None, **kwargs)` {#torchmetrics.wrappers.LambdaInputTransformer}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L84-L134)

Wrapper class for transforming a metrics’ inputs given a user-defined lambda function.

Parameters:

*   **wrapped\_metric** ([`Metric`](/docs/torchmetrics/1.9.0/api-reference/metric.md#torchmetrics.Metric "torchmetrics.metric.Metric")) – The underlying Metric or MetricCollection.
    
*   **transform\_pred** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable "(in Python v3.15)")\[\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\]\]) – The function to apply to the predictions before computing the metric.
    
*   **transform\_target** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable "(in Python v3.15)")\[\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\]\]) – The function to apply to the target before computing the metric.
    

Raises:

*   [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError "(in Python v3.15)") – If transform\_pred is not a Callable.
    
*   [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError "(in Python v3.15)") – If transform\_target is not a Callable.
    

Example

```python
>>> import torch
>>> from torchmetrics.classification import BinaryAccuracy
>>> from torchmetrics.wrappers import LambdaInputTransformer
>>>
>>> preds = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5, 0.6, 0.7, 0.8, 0.5, 0.4])
>>> targets = torch.tensor([1,0,0,0,0,1,1,0,0,0])
>>>
>>> metric = LambdaInputTransformer(BinaryAccuracy(), lambda preds: 1 - preds)
>>> metric.update(preds, targets)
>>> metric.compute()
tensor(0.6000)
```

### `class torchmetrics.wrappers.BinaryTargetTransformer(wrapped_metric, threshold=0, **kwargs)` {#torchmetrics.wrappers.BinaryTargetTransformer}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L137-L180)

Wrapper class for computing a metric on binarized targets.

Useful when the given ground-truth targets are continuous, but the metric requires binary targets.

Parameters:

*   **wrapped\_metric** (`Union`\[[`Metric`](/docs/torchmetrics/1.9.0/api-reference/metric.md#torchmetrics.Metric "torchmetrics.metric.Metric"), [`MetricCollection`](/docs/torchmetrics/1.9.0/user-guide/overview.md#torchmetrics.MetricCollection "torchmetrics.collections.MetricCollection")\]) – The underlying Metric or MetricCollection.
    
*   **threshold** ([`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")) – The binarization threshold for the targets. Targets values t are cast to binary with t > threshold.
    

Raises:

[**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError "(in Python v3.15)") – If threshold is not an int or float.

Example

```python
>>> import torch
>>> from torchmetrics.retrieval import RetrievalMRR
>>> from torchmetrics.wrappers import BinaryTargetTransformer
>>>
>>> preds = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5, 0.6, 0.7, 0.8, 0.5, 0.4])
>>> targets = torch.tensor([1,0,0,0,0,2,1,0,0,0])
>>> topics = torch.tensor([0,0,0,0,0,1,1,1,1,1])
>>>
>>> metric = BinaryTargetTransformer(RetrievalMRR())
>>> metric.update(preds, targets, indexes=topics)
>>> metric.compute()
tensor(0.7500)
```

#### `transform_target(target)` {#torchmetrics.wrappers.BinaryTargetTransformer.transform_target}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/transformations.py#L174-L180)

Cast the target tensor to binary values according to the threshold.

Output assumes same type as input.

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

---

**Navigation**

- Parent: [Wrappers](/docs/torchmetrics/1.9.0/wrappers.md)
