Using your code I get the following output:
X shape torch.Size([50, 2])
y shape torch.Size([50, 1])
w shape torch.Size([2, 1])
Analytical gradient [ 4.254795  -3.4482186]
PyTorch’s gradient [ 4.2547956 -3.4482183]
tensor([[ 4.2548],
        [-3.4482]], grad_fn=<DivBackward0>)