Is @cbd’s custom function a sound approach? As seems to be mentioned by OP, torch.count_nonzero is non-differentiable. If I understand correctly, is this essentially ignoring the gradient of torch.count_nonzero?