Using grad to calculate derivative of two outputs w.r.t. the same input

Make sure to zero the gradients (if x.grad: x.grad.zero_()) before calculating new y.

Some more info: