-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) 

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

2 Comments

I want to get gradients of neural network model, 'stackoverflow.com/questions/63026358/…'
you can get more information stackoverflow.com/questions/43451125/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.