Calculating gradient of a tensor object using pytorch

[This Tutorial is under development ......Please don't use it right now]

 

import torch
x = torch.tensor([[5., 2.], [3., 4.]], requires_grad=True)
print(x)
out = (3*x+x.pow(3)).sum()
out.backward()
# If you remember your highschool mathematics
# you can understand that if we differentiate (3x + x^3)
# we will get (3 + 3x^2)
print(x.grad)

Output:

tensor([[5., 2.],
        [3., 4.]], requires_grad=True)
tensor([[78., 15.],
        [30., 51.]])