Rate this Page

torch.sum#

torch.sum(input,*,dtype=None)Tensor#

Returns the sum of all elements in theinput tensor.

Parameters

input (Tensor) – the input tensor.

Keyword Arguments

dtype (torch.dtype, optional) – the desired data type of returned tensor.If specified, the input tensor is casted todtype before the operationis performed. This is useful for preventing data type overflows. Default: None.

Note

Use thedtype argument if you need the result in a specific tensor type.Otherwise, the result type may be automatically promoted (e.g., fromtorch.int32 totorch.int64).

Example:

>>>a=torch.randn(1,3)>>>atensor([[ 0.1133, -0.9567,  0.2958]])>>>torch.sum(a)tensor(-0.5475)
torch.sum(input,dim,keepdim=False,*,dtype=None)Tensor

Returns the sum of each row of theinput tensor in the givendimensiondim. Ifdim is a list of dimensions,reduce over all of them.

Ifkeepdim isTrue, the output tensor is of the same sizeasinput except in the dimension(s)dim where it is of size 1.Otherwise,dim is squeezed (seetorch.squeeze()), resulting in theoutput tensor having 1 (orlen(dim)) fewer dimension(s).

Parameters
  • input (Tensor) – the input tensor.

  • dim (int ortuple ofints,optional) – the dimension or dimensions to reduce.IfNone, all dimensions are reduced.

  • keepdim (bool,optional) – whether the output tensor hasdim retained or not. Default:False.

Keyword Arguments

dtype (torch.dtype, optional) – the desired data type of returned tensor.If specified, the input tensor is casted todtype before the operationis performed. This is useful for preventing data type overflows. Default: None.

Example:

>>>a=torch.randn(4,4)>>>atensor([[ 0.0569, -0.2475,  0.0737, -0.3429],        [-0.2993,  0.9138,  0.9337, -1.6864],        [ 0.1132,  0.7892, -0.1003,  0.5688],        [ 0.3637, -0.9906, -0.4752, -1.5197]])>>>torch.sum(a,1)tensor([-0.4598, -0.1381,  1.3708, -2.6217])>>>b=torch.arange(4*5*6).view(4,5,6)>>>torch.sum(b,(2,1))tensor([  435.,  1335.,  2235.,  3135.])