---
title: Multi-output Wrapper
description: Wrap a base metric to enable it to support multiple outputs.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/wrappers/multi_output_wrapper'
---
# Multi-output Wrapper {#multi-output-wrapper}

## Module Interface {#module-interface}

### `class torchmetrics.wrappers.MultioutputWrapper(base_metric, num_outputs, output_dim=-1, remove_nans=True, squeeze_outputs=True)` {#torchmetrics.wrappers.MultioutputWrapper}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/multioutput.py#L44-L203)

Wrap a base metric to enable it to support multiple outputs.

Several torchmetrics metrics, such as [`SpearmanCorrCoef`](/docs/torchmetrics/1.9.0/regression/spearman_corr_coef.md#torchmetrics.SpearmanCorrCoef "torchmetrics.regression.spearman.SpearmanCorrCoef") lack support for multioutput mode. This class wraps such metrics to support computing one metric per output. Unlike specific torchmetric metrics, it doesn’t support any aggregation across outputs. This means if you set `num_outputs` to 2, `.compute()` will return a Tensor of dimension `(2, ...)` where `...` represents the dimensions the metric returns when not wrapped.

In addition to enabling multioutput support for metrics that lack it, this class also supports, albeit in a crude fashion, dealing with missing labels (or other data). When `remove_nans` is passed, the class will remove the intersection of NaN containing “rows” upon each update for each output. For example, suppose a user uses MultioutputWrapper to wrap [`torchmetrics.regression.r2.R2Score`](/docs/torchmetrics/1.9.0/regression/r2_score.md#torchmetrics.R2Score "torchmetrics.regression.r2.R2Score") with 2 outputs, one of which occasionally has missing labels for classes like `R2Score` is that this class supports removing `NaN` values (parameter `remove_nans`) on a per-output basis. When `remove_nans` is passed the wrapper will remove all rows

Parameters:

*   **base\_metric** ([`Metric`](/docs/torchmetrics/1.9.0/api-reference/metric.md#torchmetrics.Metric "torchmetrics.metric.Metric")) – Metric being wrapped.
    
*   **num\_outputs** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Expected dimensionality of the output dimension. This parameter is used to determine the number of distinct metrics we need to track.
    
*   **output\_dim** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Dimension on which output is expected. Note that while this provides some flexibility, the output dimension must be the same for all inputs to update. This applies even for metrics such as Accuracy where the labels can have a different number of dimensions than the predictions. This can be worked around if the output dimension can be set to -1 for both, even if -1 corresponds to different dimensions in different inputs.
    
*   **remove\_nans** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Whether to remove the intersection of rows containing NaNs from the values passed through to each underlying metric. Proper operation requires all tensors passed to update to have dimension `(N, ...)` where N represents the length of the batch or dataset being passed in.
    
*   **squeeze\_outputs** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – If `True`, will squeeze the 1-item dimensions left after `index_select` is applied. This is sometimes unnecessary but harmless for metrics such as R2Score but useful for certain classification metrics that can’t handle additional 1-item dimensions.
    

Example

```python
>>> # Mimic R2Score in `multioutput`, `raw_values` mode:
>>> import torch
>>> from torchmetrics.wrappers import MultioutputWrapper
>>> from torchmetrics.regression import R2Score
>>> target = torch.tensor([[0.5, 1], [-1, 1], [7, -6]])
>>> preds = torch.tensor([[0, 2], [-1, 2], [8, -5]])
>>> r2score = MultioutputWrapper(R2Score(), 2)
>>> r2score(preds, target)
tensor([0.9654, 0.9082])
```

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

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

Compute metrics.

Return type:

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

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/multioutput.py#L139-L153)

Call underlying forward methods and aggregate the results if they’re non-null.

We override this method to ensure that state variables get copied over on the underlying metrics.

Return type:

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

#### `plot(val=None, ax=None)` {#torchmetrics.wrappers.MultioutputWrapper.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/multioutput.py#L161-L203)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.
    
*   **ax** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

```python
>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.wrappers import MultioutputWrapper
>>> from torchmetrics.regression import R2Score
>>> metric = MultioutputWrapper(R2Score(), 2)
>>> metric.update(torch.randn(20, 2), torch.randn(20, 2))
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/multi\_output\_wrapper-1.png](/docs/torchmetrics/1.9.0/static/_images/multi_output_wrapper-1.png)](/docs/torchmetrics/1.9.0/static/_images/multi_output_wrapper-1.png)

```python
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.wrappers import MultioutputWrapper
>>> from torchmetrics.regression import R2Score
>>> metric = MultioutputWrapper(R2Score(), 2)
>>> values = [ ]
>>> for _ in range(3):
...     values.append(metric(torch.randn(20, 2), torch.randn(20, 2)))
>>> fig_, ax_ = metric.plot(values)
```

[![../../\_images/multi\_output\_wrapper-2.png](/docs/torchmetrics/1.9.0/static/_images/multi_output_wrapper-2.png)](/docs/torchmetrics/1.9.0/static/_images/multi_output_wrapper-2.png)

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/multioutput.py#L155-L159)

Reset all underlying metrics.

Return type:

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

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/multioutput.py#L129-L133)

Update each underlying metric with the corresponding output.

Return type:

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

---

**Navigation**

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