Oh sorry, I had to declare z as the nn.Parameter and make requires_grad=True for z.
That solved the problem.
But
threshold.grad
returns 0. So I am unclear if the gradient is being computed for threshold even though its requires_grad parameter is True. If not, am  I interpreting it wrong?
Is there a way we can have a learnable threshold?