You can only call torch.nonzero() on a simple tensor, not a variable. It makes sens: I doubt that the counting of non-zero element would be differentiable.
but you have sum( abs( x/ (abs(x) + epsilon) )), that approximates the number of zero, and is differentiable.