---
title: Bootstrapper
description: Using Turn a Metric into a Bootstrapped.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/wrappers/bootstrapper'
---
# Bootstrapper {#bootstrapper}

## Module Interface {#module-interface}

### `class torchmetrics.wrappers.BootStrapper(base_metric, num_bootstraps=10, mean=True, std=True, quantile=None, raw=False, sampling_strategy='poisson', **kwargs)` {#torchmetrics.wrappers.BootStrapper}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/bootstrapping.py#L55-L221)

Using [Turn a Metric into a Bootstrapped](https://en.wikipedia.org/wiki/Bootstrapping_\(statistics\)).

That can automate the process of getting confidence intervals for metric values. This wrapper class basically keeps multiple copies of the same base metric in memory and whenever `update` or `forward` is called, all input tensors are resampled (with replacement) along the first dimension.

Parameters:

*   **base\_metric** ([`Metric`](/docs/torchmetrics/1.9.0/api-reference/metric.md#torchmetrics.Metric "torchmetrics.metric.Metric")) – base metric class to wrap
    
*   **num\_bootstraps** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – number of copies to make of the base metric for bootstrapping
    
*   **mean** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – if `True` return the mean of the bootstraps
    
*   **std** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – if `True` return the standard deviation of the bootstraps
    
*   **quantile** (`Union`\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(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)")\]) – if given, returns the quantile of the bootstraps. Can only be used with pytorch version 1.6 or higher
    
*   **raw** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – if `True`, return all bootstrapped values
    
*   **sampling\_strategy** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")) – Determines how to produce bootstrapped samplings. Either `'poisson'` or `multinomial`. If `'possion'` is chosen, the number of times each sample will be included in the bootstrap will be given by $`n\sim Poisson(\lambda=1)`$, which approximates the true bootstrap distribution when the number of samples is large. If `'multinomial'` is chosen, we will apply true bootstrapping at the batch level to approximate bootstrapping over the hole dataset.
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.
    

Example::

```python
>>> from pprint import pprint
>>> from torch import randint
>>> from torchmetrics.wrappers import BootStrapper
>>> from torchmetrics.classification import MulticlassAccuracy
>>> base_metric = MulticlassAccuracy(num_classes=5, average='micro')
>>> bootstrap = BootStrapper(base_metric, num_bootstraps=20)
>>> bootstrap.update(randint(5, (20,)), randint(5, (20,)))
>>> output = bootstrap.compute()
>>> pprint(output)
{'mean': tensor(0.2089), 'std': tensor(0.0772)}
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/bootstrapping.py#L149-L166)

Compute the bootstrapped metric values.

Always returns a dict of tensors, which can contain the following keys: `mean`, `std`, `quantile` and `raw` depending on how the class was initialized.

Return type:

[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\]

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/bootstrapping.py#L168-L170)

Use the original forward method of the base metric class.

Return type:

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

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/bootstrapping.py#L179-L221)

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 BootStrapper
>>> from torchmetrics.regression import MeanSquaredError
>>> metric = BootStrapper(MeanSquaredError(), num_bootstraps=20)
>>> metric.update(torch.randn(100,), torch.randn(100,))
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/bootstrapper-1.png](/docs/torchmetrics/1.9.0/static/_images/bootstrapper-1.png)](/docs/torchmetrics/1.9.0/static/_images/bootstrapper-1.png)

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

[![../../\_images/bootstrapper-2.png](/docs/torchmetrics/1.9.0/static/_images/bootstrapper-2.png)](/docs/torchmetrics/1.9.0/static/_images/bootstrapper-2.png)

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/bootstrapping.py#L172-L177)

Reset the state of the base metric.

Return type:

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

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/bootstrapping.py#L126-L147)

Update the state of the base metric.

Any tensor passed in will be bootstrapped along dimension 0.

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)
