Python Pytorch

Compute differentiation easily using Pytorch Autograd

Pytorch comes with a super easy method to compute differentiation – autograd (automatic gradient). To illustrate the method, consider the following differentiation

$$ \begin{align} z &= 2y^2\\ y &= x^3 \end{align} $$ Compute $\frac{\partial z}{\partial x}$ for x=1. The code is shown below:
import torch

# Creating the graph
x = torch.tensor(1.0, requires_grad = True)
y = 2*x**2
z = y**3

z.backward() #Computes the gradient 
print( #Print dz/dx 


Relevant Courses

June 13, 2021