Hi,
A few points:

Is that expected that the experiment types you use do not match the string tested in the if statement in the sympy code?
Could you provide the code for the two different pytorch runs you do?
I would avoid using .item() in pytorch as it unpacks the content into a regular python number and thus it breaks gradient computation. If you want to have a new Tensor such that no gradients will flow back, you should use .detach().
Note that when you call .backward(), it accumulates into the .grad field. So here you accumulate both the gradients for loss and J into w.grad in the code sample you provided.