How to count number of 1's in column axis

Hi
I have a tensor and i need to count number of 1’s in each column

B=torch.Tensor([[[0., 0., 0., 0., 1.]],
                [[0., 0., 0., 0., 1.]],
                [[0., 1., 0., 0., 1.]]])

and i need an output like this:

 output=tensor([[0.],
                [1.],
                [0.],
                [0.],
                [3.]])

I will be grateful if someone gives me a help

For your example specifically,

output = B.sum(0).view(-1,1)

should work.

1 Like

Yeah it worked…thank you very much