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

## Module Interface {#module-interface}

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

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

Aggregate a stream of value into their sum.

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:

*   **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 SumMetric
>>> metric = SumMetric()
>>> metric.update(1)
>>> metric.update(tensor([2, 3]))
>>> metric.compute()
tensor(6.)
```

#### `plot(val=None, ax=None)` {#torchmetrics.aggregation.SumMetric.plot}

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

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
>>> from torchmetrics.aggregation import SumMetric
>>> metric = SumMetric()
>>> metric.update([1, 2, 3])
>>> fig_, ax_ = metric.plot()
```

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

```python
>>> # Example plotting multiple values
>>> from torch import rand, randint
>>> from torchmetrics.aggregation import SumMetric
>>> metric = SumMetric()
>>> values = [ ]
>>> for i in range(10):
...     values.append(metric([i, i+1]))
>>> fig_, ax_ = metric.plot(values)
```

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

---

**Navigation**

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