Hi,
First of all, could you please reformat your code section using ```???
It is really hard to read.
And note that Variable has been removed from PyTorch, and now if you want to enable gradient computing on a tensor use following snippet:



 xavier:

b=Variable(torch.tensor([0.7]), requires_grad=True)


b = torch.tensor([0.7])
b.requires_grad = True

But about this part:



 xavier:

y_hat_w_grad=w.grad.data.detach().clone()


I think you first need to clone then detach. If you detach first, your w will no longer have grad because it is detached from computing graph.
So the solution would be something like this:
y_hat_w_grad=w.grad.data.clone().detach()

Bests
Nik