Autograd: Automatic Differentiation in PyTorch
Now, we will shift our focus on Autograd which is one of the most important topics in the PyTorch basics. The Autograd Module of PyTorch provides the automatic calculation of the gradients. It means that we do not need to calculate the gradients explicitly. You might be thinking what gradient is. So, the gradient represents the rate of change of functions with respect to parameters. This helps us to identify the difference between the predicted outputs and actual labels.
Let us take an example to understand this. Suppose, we create two tensors with names ‘x’ and ‘y’ and perform some computation on them. The result is stored in the variable ‘z.’ Then, we can call the backward() method to calculate the gradient of the z with respect to x and y. This is shown in the below code snippet.
Python
# Define tensors with requires_grad=True to track computation history x = torch.tensor( 2.0 , requires_grad = True ) y = torch.tensor( 3.0 , requires_grad = True ) # Perform a computation z = x * * 2 + y * * 3 print ( "Output tensor z:" , z) # Compute gradients z.backward() print ( "Gradient of x:" , x.grad) print ( "Gradient of y:" , y.grad) |
Output:
Output tensor z: tensor(31., grad_fn=<AddBackward0>)
Gradient of x: tensor(4.)
Gradient of y: tensor(27.)
Start learning PyTorch for Beginners
Machine Learning helps us to extract meaningful insights from the data. But now, it is capable of mimicking the human brain. This is done using neural networks, which contain the various interconnected layers of nodes containing the data. This data is passed to forward layers. Subsequently, the model learns from the data and predicts output for the new data.
PyTorch helps us to create and train these neural networks that act like our brains and learn from the data.
Table of Content
- What is Pytorch?
- Why use PyTorch?
- How to install Pytorch ?
- PyTorch Basics
- Autograd: Automatic Differentiation in PyTorch
- Neural Networks in PyTorch
- Working with Data in PyTorch
- Intermediate Topics in PyTorch
- Validation and Testing
- Frequently Asked Questions