Using Pytorch to compute gradient

Computing gradient using pytorch

Computing gradient is one of the most fundamental operation in deep learning. It is used in back propagation step while we are updating weights of network

Next is a simple example of how pytorch do gradient computaion

$$ y = 3x^2 + 2x + 1 $$

Compute gradient for x=2

import torch
x = torch.tensor([[2.]], requires_grad=True)
y = 3*(x**2) + 2*x +1
y.backward()
x.grad

Output: tensor([[14.]])

links

social