cbd:

I want layer wise non zero count value. so for “a=torch.rand(1,4,2,2)” if all 4 layer 2x2 value is non zero then i want output to be “count_a=[4 4 4 4]”. Sorry i forgot to mention that count_a is of size “torch.Size([4])” and i want output to be tensor([4., 4., 4., 4.])


Thanks for the clarification!
I misunderstood the [4, 4, 4, 4] output as the shapes, not the values.
In that case, you could flatten the last two dimensions and use the dim argument in count_nonzero:
a = torch.rand(1,4,2,2)
count_a = torch.count_nonzero(a.view(1, 4, -1), 2)
print("a value is ",a)
print("count is ",count_a)
> count is  tensor([[4, 4, 4, 4]])