import torch x = torch.autograd.Variable(torch.Tensor([2]),requires_grad=True) y = 5*x**4 + 3*x**3 + 7*x**2 + 9*x - 5 y.backward() x.grad