Derivative of Neural Network in Pytorch

Question:

I have implemented and trained a neural network in Pytorch, however, I am interested in the derivative of the neural network parameters with respect to the input.
I have extensively searched for any procedure to that would allow evaluating the derivative of weights with respect to a given input, but I did not find anything.
I know that I can compute the gradients of a function in the following way.

external_grad = torch.tensor([1., 1.])
Q.backward(gradient=external_grad)

But How would I do that with a trained neural network instead of a function Q?

Thanks in advance.

Asked By: Magnus Moller

||

Answers:

You need to explicitly use requires_grad = True when create a tensor. And to calculate gradient you first need to apply some operation on the tensor.

Here is an example:

import torch

x = torch.rand(2, 2, requires_grad=True)
y = x + 2
z = y * y * 3
out = z.mean()

out.backward()

print(x.grad)

Output:

tensor([[3.3720, 3.4302],
        [3.4030, 3.3605]])

In this way you are using torch.autograd to calculate the gradient for tensor x. See autograd for more.

And for neural network you can simply use the network and backward it afterward.

A neural network Example:

import torch
import torch.nn as nn
import torch.nn.functional as f

x = torch.rand(2, 2)

# define a neural network
network = nn.Sequential(
nn.Linear(2,100),
nn.Linear(100,2)
)

pred = network(x)

loss = f.mae_loss(b, x) # calculating loss 

loss.backward()

# Update weights with gradients
network[0].weight = 0.1 * network[0].weight.grad
network[1].weight = 0.1 * network[1].weight.grad

Note: I didn’t put any activation function in network for the sack of simplicity.

Answered By: Rafat Hasan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.