The answer to this question was answered on stackoverflow 4 by a user named jodag. I have modified and added it here.
SOLUTION:
What you have seems like it would work provided you did it after  loss.backward()  and before  optimizer.step()  (referring to the common usage for these variable names). That said, it seems a bit convoluted. Also, if your weights are floating-point values then comparing them to exactly zero is probably a bad idea, we could introduce an epsilon to account for this.
The modified code :
EPS = 1e-6       #Changes
for name, p in model.named_parameters():
            if 'weight' in name:
                tensor = p.data.cpu().numpy()
                grad_tensor = p.grad.data.cpu().numpy()
                grad_tensor = np.where(tensor < EPS, 0, grad_tensor)     #Changes
                p.grad.data = torch.from_numpy(grad_tensor).to(device)