How do i count the list of nested list?

Hi, I would like to ask how can i read a list of list and count the numbers of True inside?

For example,

tensor([[ True, False, True, False, True],
[ True, True, True, True, True],
[False, False, False, True, True]])

How do I iterate and count the numbers of True inside the whole list?

You could use x.sum() to get the number of True values inside the tensor.

1 Like

Thank you! it seem to work when i tried it on a random list. Gonna try it on my actual function after its done training! thank you!

sry, what if I want to calculate the entire amount in the entire list? this x.sum() surprisingly sums the total number of True. Awesome!


Hi, I tried using len(), but does not work like how sum() works… Is there other way possible?

Maybe, you could use x.numel() to get the number of values inside the tensor.

torch.numel() link