I want to get the gradients of model(neural network).
In tensorflow, there is a ‘tf.gradient’
so the following way is possible.
x = tf.placeholder(shape=[None,1],dtype=tf.float32)
y = tf.placeholder(shape=[None,1],dtype=tf.float32)
inp = tf.concat([x,y],axis=-1)

def network(x):
	x = tf.layers.dense(x,128,activation=tf.nn.relu)
	x = tf.layers.dense(x,128,activation=tf.nn.relu)
	x = tf.layers.dense(x,128,activation=tf.nn.relu)
	x = tf.layers.dense(x,128,activation=tf.nn.relu)
	x = tf.layers.dense(x,128,activation=tf.nn.relu)
	x = tf.layers.dense(x,1)
	return x

pred = network(inp)
dfdx,dfdy = tf.gradients(pred,[x,y])

But how to I get similar results with above code using pytorch?
class net(torch.nn.Module):
  def __init__(self):
    super(net,self).__init__()
    self.linear={}
    self.wei={}
    self.bias={}
    self.para=[]
    self.linear[0] = torch.nn.Linear(3,128)
    self.linear[1] = torch.nn.Linear(128,128)
    self.linear[2] = torch.nn.Linear(128,128)
    self.linear[3] = torch.nn.Linear(128,128)
    self.linear[4] = torch.nn.Linear(128,1)
  def forward(self,x):
    for i in range(4):
      x=F.elu(self.linear[i](x))
    x=self.linear[4](x)
    return x

M=net()

for i in range(5):
  M.para+=list(M.linear[i].parameters())


L=nn.MSELoss()
opt=torch.optim.Adam(M.para,0.00035)

######################################

X=torch.Tensor(data[:,0:3])   ### 3 dim input
X.requires_grad_(True)
Y=torch.Tensor(data[:,3])
Y=Y.reshape(-1,1)

for epoch in range(10000):
    opt.zero_grad()
    idx=torch.randint(0,X.shape[0],(24,))
    M_dev=torch.autograd.grad(M.forward(X),X,torch.ones_like(M.forward(X)))   ????


I tried ‘torch.autograd.grad(M.forward(X),X,torch.ones_like(M.forward(X)))’
but it gives wrong answer…
How I can get like ‘tf.gradient( M.forward(X), X)’  ???