Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch sum over a list of tensors along an axis

Tags:

pytorch

I have a list of tensors of the same shape. I would like to sum the entire list of tensors along an axis. Does torch.cumsum perform this op along a dim? If so it requires the list to be converted to a single tensor and summed over?

like image 657
Andrew Avatar asked Mar 14 '19 10:03

Andrew


People also ask

How do you sum all elements of a tensor?

The tf. sum() function is used to calculate sum of the elements of a specified Tensor across its dimension. It reduces the given input elements along the dimensions of axes.

How do you sum two tensors?

Two tensors of the same size can be added together by using the + operator or the add function to get an output tensor of the same shape.

What is Item () PyTorch?

item () → number. Returns the value of this tensor as a standard Python number. This only works for tensors with one element. For other cases, see tolist() .


1 Answers

you don't need cumsum, sum is your friend and yes you should first convert them into a single tensor with stack or cat based on your needs, something like this:

import torch
my_list = [torch.randn(3, 5), torch.randn(3, 5)]
result = torch.stack(my_list, dim=0).sum(dim=0).sum(dim=0)
print(result.shape) #torch.Size([5])
like image 140
Separius Avatar answered Dec 17 '22 18:12

Separius