-1

I want to construct sobolev network for 3D input regression

In TensorFlow, the gradients of neural network model can be computed using tf.gradient like:

dfdx,dfdy,dfdz = tf.gradients(pred,[x,y,z])

Let M be a torch neural network with 5 layers. If X is a set of (x,y,z) (3dim data) and M.forward(X) is a 1 dim output

How can I compute like gradient of M.forward(X) with respect to X? Something like:

tf.gradient(M.forward(X),X)
Aki
  • 2,818
  • 1
  • 15
  • 23
Begiiiner
  • 57
  • 6

1 Answers1

1

If you want to compute gradient of this function for example

y_i = 5*(x_i + 1)² Create tensor of size 2x1 filled with 1's that requires gradient

x = torch.ones(2, requires_grad=True)

Simple linear equation with x tensor created

y = 5 * (x + 1) ** 2

Let take o as multiple dimension equation

o = 1/2 *sum(y_i) in python

o = (1/2) * torch.sum(y)

you can compute grad with

o.backward()
x.grad

you can get more information here https://www.deeplearningwizard.com/deep_learning/practical_pytorch/pytorch_gradients/

BIg G
  • 316
  • 1
  • 9
  • I want to get gradients of neural network model, 'https://stackoverflow.com/questions/63026358/pytorch-gradients-of-neural-network-for-3-dim-input-1-dim-output-regression' – Begiiiner Jul 22 '20 at 08:55
  • you can get more information https://stackoverflow.com/questions/43451125/pytorch-what-are-the-gradient-arguments – BIg G Jul 22 '20 at 09:07