I am a newbie to pytorch. I recently tried to use pytorch to implement the PINN framework. The following is a simplified network layer I designed to verify the correctness of the second-order guide implementation of the program, but obviously, the output results are not very ideal. I also tried to use this form to test the Linear() and Sigmoid() layers separately, and the result was no problem. But I don’t know why there is a problem when combining them to find the second derivative. I hope you can help me find the problem.
import torch
from torch import nn
x=torch.tensor([[2.,1]],dtype=torch.float32).t()
y=torch.tensor([[1,0]],dtype=torch.float32).t()
net=nn.Sequential(nn.Linear(2,2),nn.Sigmoid())
def init_ones(m):
if type(m)==nn.Linear:
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
net.apply(init_ones)
net[0].weight.data[1][0]=0.0
net[0].weight,net[0].bias
(Parameter containing:
 tensor([[1., 1.],
         [0., 1.]], requires_grad=True),
 Parameter containing:
 tensor([0., 0.], requires_grad=True))

X=torch.cat((x,y),1)
X.requires_grad_(True)
y=net(X)
z = torch.ones_like(y)
dy = torch.autograd.grad(y, X, grad_outputs=z,retain_graph=True, create_graph=True)[0]
print(dy)
d2y = torch.autograd.grad(dy, X, grad_outputs=z, create_graph=False)[0]
print(d2y)