Mikhail_RC
(Mikhail )
November 20, 2022, 6:40pm
#1
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]
```

KFrank
(K. Frank)
November 22, 2022, 6:46pm
#2
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__)
1.13.0
>>>
>>> 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
tensor(686.)
>>> T_d_xx_B
tensor(686., grad_fn=<MulBackward0>)
>>> T_d_yy
tensor(1050.)
>>> T_d_yy_B
tensor(1050., grad_fn=<MulBackward0>)
```

Best.

K. Frank