torch.sum()

torch.sum()_第1张图片

在dim这个维度上,对里面的tesnor 进行加和,如果keepdim=False,返回结果会删去dim这个维度。因为在dim上加和之后,dim=1,所以可以直接删去。

import torch

a = torch.tensor([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(a.size())
c = torch.sum(a,dim=1)
print(c,c.size())
b = torch.sum(a,dim=(1,2))
print(b,b.size())
b = torch.sum(a,dim=2)
print(b,b.size())
b = torch.sum(a,dim=(2,1))
print(b,b.size())
b = torch.sum(a,dim=0)
print(b,b.size())


torch.Size([2, 2, 3])
tensor([[ 5,  7,  9],
        [17, 19, 21]]) torch.Size([2, 3])
tensor([21, 57]) torch.Size([2])
tensor([[ 6, 15],
        [24, 33]]) torch.Size([2, 2])
tensor([21, 57]) torch.Size([2])
tensor([[ 8, 10, 12],
        [14, 16, 18]]) torch.Size([2, 3])

 

你可能感兴趣的:(pytorch)