How do we find the derivatives of the function in PyTorch?

The derivatives of the function are calculated with the help of the Gradient. There are four simple steps through which we can calculate derivative easily.
 
These steps are as follows :
 
* Initialization of the function for which we will calculate the derivatives.
* Set the value of the variable which is used in the function.
* Compute the derivative of the function by using the backward () method.
* Print the value of the derivative using grad.