Second derivatives of a function of two variables

My model takes two inputs x and y and returns T(x, y).
How can I find the second derivatives of d_2_T / d_x_x and d_2_T / d_y_y?

Now I managed to find only the first derivatives in this way:

# g - tensor of input values
T = NN(g)
T_x_y = autograd.grad(T,g,torch.ones([g.shape[0], 1]).to(device), retain_graph=True, create_graph=True)[0]

Hi Mikhail!

Continue on with the code you have. Your call to autograd.grad() computes
the first derivative, but it also constructs the computation graph for the
computation of the first derivative. So you can simply call autograd.grad()
a second time to compute the second derivative.

Here is an illustration in the spirit of your code:

>>> import torch
>>> print (torch.__version__)
>>> g = torch.tensor ([5.0, 7.0], requires_grad = True)
>>> T = g[0]**2 * g[1]**3
>>> T_x_y = torch.autograd.grad (T, g, retain_graph=True, create_graph=True)[0]   # creates graph of first derivative
>>> T_d_xx = torch.autograd.grad (T_x_y[0], g, retain_graph = True)[0][0]         # compute second derivative (and save graph for T_d_yy computation)
>>> T_d_yy = torch.autograd.grad (T_x_y[1], g)[0][1]                              # compute second derivative
>>> T_d_xx_B = 2 * g[1]**3        # check second derivative "by hand"
>>> T_d_yy_B = g[0]**2 * 6*g[1]   # check second derivative "by hand"
>>> T_d_xx
>>> T_d_xx_B
tensor(686., grad_fn=<MulBackward0>)
>>> T_d_yy
>>> T_d_yy_B
tensor(1050., grad_fn=<MulBackward0>)


K. Frank