How to count the number of zero weights in a pytorch model

You could also go for :

zeros += param.numel() - param.nonzero().size(0)

I am not sure which one is the fastest, but I find this way a bit clearer.

3 Likes