---
title: Running Sum
description: Aggregate a stream of value into their sum over a running window.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/aggregation/running_sum'
---
# Running Sum {#running-sum}

## Module Interface {#module-interface}

### `class torchmetrics.aggregation.RunningSum(window=5, nan_strategy='warn', **kwargs)` {#torchmetrics.aggregation.RunningSum}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/aggregation.py#L685-L740)

Aggregate a stream of value into their sum over a running window.

Using this metric compared to SumMetric 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.

As input to `forward` and `update` the metric accepts the following input

*   `value` ([`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)") or [`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): a single float or an tensor of float values with arbitrary shape `(...,)`.
    

As output of forward and compute the metric returns the following output

*   `agg` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): scalar float tensor with aggregated sum over all inputs received
    

Parameters:

*   **window** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – The size of the running window.
    
*   **nan\_strategy** (`Union`\[[`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'error'`, `'warn'`, `'ignore'`, `'disable'`\], [`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]) – options: - `'error'`: if any nan values are encountered will give a RuntimeError - `'warn'`: if any nan values are encountered will give a warning and continue - `'ignore'`: all nan values are silently removed - `'disable'`: disable all nan checks - a float: if a float is provided will impute any nan values with this value
    
*   **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.
    

Raises:

[**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `nan_strategy` is not one of `error`, `warn`, `ignore`, `disable` or a float

Example

```python
>>> from torch import tensor
>>> from torchmetrics.aggregation import RunningSum
>>> metric = RunningSum(window=3)
>>> for i in range(6):
...     current_val = metric(tensor([i]))
...     running_val = metric.compute()
...     total_val = tensor(sum(list(range(i+1))))  # total sum over all samples
...     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)
```

---

**Navigation**

- Parent: [Aggregation](/docs/torchmetrics/1.9.0/aggregation.md)
