If you call autograd.grad or backward multiple times, then in the first call, you have to specify retain_graph=True.
In your sample above, that means changing the autograd.grad call to:
 dy_dx = torch.autograd.grad(loss.mean(), x_,allow_unused=True, retain_grad=True)