# Element wise sum of batched tensors

**URL:** <https://discuss.pytorch.org/t/element-wise-sum-of-batched-tensors/100988>\
**Category:** Uncategorized\
**Created:** [October 29, 2020, 11:26am UTC](https://discuss.pytorch.org/t/element-wise-sum-of-batched-tensors/100988 "2020-10-29T11:26:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ed-fish](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ed-fish/32/20229_2.png) [@ed-fish](https://discuss.pytorch.org/u/ed-fish)\
**Post date:** [October 29, 2020, 11:26am UTC](https://discuss.pytorch.org/t/element-wise-sum-of-batched-tensors/100988/1 "2020-10-29T11:26:02Z")

</div>

I have a list of tensors `t_list` and I need to get the element wise sum. I am concerned that because the tensors are in a batch that my method is incorrect.

**Code example**

```auto
t_list = [t1, t2, t3, t4] #where ti is a tensor 32 x 1 x 128
t_list = torch.stack(t_list) # giving 4 x 32 x 1 x 128
sum_list = sum(t_list) # result is 1 x 32 x 1 x 128

```

Is this the correct way to sum each list within the batch or would I need to specify dimensions for sum?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 30, 2020, 4:20am UTC](https://discuss.pytorch.org/t/element-wise-sum-of-batched-tensors/100988/2 "2020-10-30T04:20:56Z")

</div>

Your approach might work, but if you want to set a `dim` argument you could use `torch.sum(t_list, dim=0)`.
