netaglazer:

temp_p = torch.tensor(p[i])
temp_y = torch.tensor(y[i])


I would say there :
temp_p = torch.tensor(p[i], requires_grad=True)
temp_y = torch.tensor(y[i], requires_grad=True)

By default, the requires_grad are set to False. You need to activate them yourself. More on this here : https://pytorch.org/docs/stable/notes/autograd.html