sum是按我维度统计的,torch.sum 默认是所有求和,
import torch
if __name__ == '__main__':
data=torch.linspace(1, 27, steps=27).view(9, 3)
sum_a=sum(data>5)
print(sum_a)
print(torch.sum(data>5))
结果:
tensor([7, 7, 8])
tensor(22)
pytorch按维度统计:
import torch
if __name__ == '__main__':
data=torch.linspace(1, 27, steps=27).view(9, 3)
sum_a=sum(data>5)
print(sum_a)
print(data.shape)
print(torch.sum(data>5,dim=0))
print(torch.sum(data>5,dim=1))
结果:
tensor([7, 7, 8])
torch.Size([9, 3])
tensor([7, 7, 8])
tensor([0, 1, 3, 3, 3, 3, 3, 3, 3])
但是这个是不对的:
aa= torch.sigmoid(conf_preds[:, :, 0][pos_mask])
n_correct= int(sum(aa> 0.5)