---
title: Running
description: Running wrapper for metrics.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/wrappers/running'
---
# Running {#running}

## Module Interface {#module-interface}

### `class torchmetrics.wrappers.Running(base_metric, window=5)` {#torchmetrics.wrappers.Running}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/running.py#L28-L183)

Running wrapper for metrics.

Using this wrapper allows for calculating metrics over a running window of values, instead of the whole history of values. This is beneficial when you want to get a better estimate of the metric during training and don’t want to wait for the whole training to finish to get epoch level estimates.

The running window is defined by the window argument. The window is a fixed size and this wrapper will store a duplicate of the underlying metric state for each value in the window. Thus memory usage will increase linearly with window size. Use accordingly. Also note that the running only works with metrics that have the full\_state\_update set to False.

Importantly, the wrapper does not alter the value of the forward method of the underlying metric. Thus, forward will still return the value on the current batch. To get the running value call compute instead.

Parameters:

*   **base\_metric** ([`Metric`](/docs/torchmetrics/1.9.0/api-reference/metric.md#torchmetrics.Metric "torchmetrics.metric.Metric")) – The metric to wrap.
    
*   **window** ([`int`](https://docs.python.org/3/library/functions.html#int "(in Python v3.14)")) – The size of the running window.
    

Example (single metric):

```python
>>> from torch import tensor
>>> from torchmetrics.wrappers import Running
>>> from torchmetrics.aggregation import SumMetric
>>> metric = Running(SumMetric(), window=3)
>>> for i in range(6):
...     current_val = metric(tensor([i]))
...     running_val = metric.compute()
...     total_val = tensor(sum(list(range(i+1))))  # value we would get from `compute` without running
...     print(f"{current_val=}, {running_val=}, {total_val=}")
current_val=tensor(0.), running_val=tensor(0.), total_val=tensor(0)
current_val=tensor(1.), running_val=tensor(1.), total_val=tensor(1)
current_val=tensor(2.), running_val=tensor(3.), total_val=tensor(3)
current_val=tensor(3.), running_val=tensor(6.), total_val=tensor(6)
current_val=tensor(4.), running_val=tensor(9.), total_val=tensor(10)
current_val=tensor(5.), running_val=tensor(12.), total_val=tensor(15)
```

Example (metric collection):

```python
>>> from torch import tensor
>>> from torchmetrics.wrappers import Running
>>> from torchmetrics import MetricCollection
>>> from torchmetrics.aggregation import SumMetric, MeanMetric
>>> # note that running is input to collection, not the other way
>>> metric = MetricCollection({"sum": Running(SumMetric(), 3), "mean": Running(MeanMetric(), 3)})
>>> for i in range(6):
...     current_val = metric(tensor([i]))
...     running_val = metric.compute()
...     print(f"{current_val=}, {running_val=}")
current_val={'mean': tensor(0.), 'sum': tensor(0.)}, running_val={'mean': tensor(0.), 'sum': tensor(0.)}
current_val={'mean': tensor(1.), 'sum': tensor(1.)}, running_val={'mean': tensor(0.5000), 'sum': tensor(1.)}
current_val={'mean': tensor(2.), 'sum': tensor(2.)}, running_val={'mean': tensor(1.), 'sum': tensor(3.)}
current_val={'mean': tensor(3.), 'sum': tensor(3.)}, running_val={'mean': tensor(2.), 'sum': tensor(6.)}
current_val={'mean': tensor(4.), 'sum': tensor(4.)}, running_val={'mean': tensor(3.), 'sum': tensor(9.)}
current_val={'mean': tensor(5.), 'sum': tensor(5.)}, running_val={'mean': tensor(4.), 'sum': tensor(12.)}
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/running.py#L116-L125)

Forward input to the underlying metric and save state afterwards.

Return type:

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

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/wrappers/running.py#L141-L183)

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.13)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.14)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")\], [`None`](https://docs.python.org/3/library/constants.html#None "(in Python v3.14)")\]) – 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.14)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.1)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple "(in Python v3.14)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.1)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.1)"), [`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/library/exceptions.html#ModuleNotFoundError "(in Python v3.14)") – If matplotlib is not installed

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

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

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

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

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

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

Reset metric.

Return type:

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

---

**Navigation**

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