Hi,
The only problem here is that you change the python variable “w” to point to the new squeezed Tensor. So it doesn’t point to the Tensor that you created with requires_grad=True anymore and so the Tensor it now points to has a .grad field that is not populated.
You just need to do w_new = w.squeeze() and use w_new in your computations and the w.grad will still be populated as you expect.