You can do this in PyTorch! Make sure requires_grad = True on x and y when you initially define them (you could directly set requires_grad = True on the variables) and then use torch.autograd.grad to compute a gradient with respect to those variables.