Wei_Deng:

diagonal of the Hessian matrix


Simply try backward() function two times and you get the diagonal of Hessian matrix.
x = torch.ones(2, requires_grad=True, )
y = torch.pow(x,3)
out = torch.mean(y)
print(y)
print(out)
out.backward( retain_graph=True, create_graph=True)
print(x.grad)
print(out)
out.backward()
x.grad