Bhagavad Gita · 6.5
उद्धरेदात्मनात्मानं नात्मानमवसादयेत्
“Elevate yourself through the power of your mind, and not degrade yourself, for the mind can be the friend and also the enemy of the self.”
Bhagavad Gita · 6.5
उद्धरेदात्मनात्मानं नात्मानमवसादयेत्
“Elevate yourself through the power of your mind, and not degrade yourself, for the mind can be the friend and also the enemy of the self.”
Building neural networks from scratch used to be a nightmare of manual calculus. You often spent more time solving derivatives on a whiteboard than actually designing your model.
PyTorch changed the game with Autograd, a tool that automates the complex math so you can focus entirely on the architecture. In this post, we will break down exactly what Autograd is, why it is the engine behind modern AI, and walk through a simple example of how it automates the "learning" process.
In simple terms, Autograd is PyTorch’s built-in "Automatic Differentiation Engine."
Think of it as a smart recorder running in the background. When you define a variable and tell PyTorch to watch it, Autograd records every single mathematical step you take in a "history" file (technically called a Computational Graph).
It doesn't just remember the answer; it remembers how you got there, effectively building a directed acyclic graph (DAG) of operations.
Here is what that "History File" (Computational Graph) looks like visually:

Training a neural network requires adjusting weights to minimize error, which demands calculating Gradients (derivatives) for every parameter. While this is easy for a simple equation, manually deriving calculus for deep learning models with billions of parameters is effectively impossible.
Autograd automates this entire process. Instead of writing derivative formulas by hand, you run your data forward, and PyTorch automatically traverses the computational graph backwards to calculate the necessary gradients using the Chain Rule.
To see the value of Autograd, let's look at how we calculate the gradient (slope) for the equation with and without it. We want to find the slope when .
Without Autograd, you act as the mathematician. You must perform the calculus manually on paper before writing code.
x = 4.0
# You must manually calculate and type the derivative formula: 6 * x
grad = 6 * x
print(grad) # Output: 24.0The Problem: If you change the equation to , your code is instantly broken. You must re-do the math and re-write the code.
With Autograd, you skip the manual calculus. You simply write the equation, and PyTorch handles the derivation.
import torch
# 1. THE SETUP
# `requires_grad=True` turns on the "Recorder" for this variable.
x = torch.tensor(4.0, requires_grad=True)
# 2. THE FORWARD PASS
# We just write the equation. PyTorch builds the graph silently.
y = 3 * (x**2) + 5
# 3. THE BACKWARD PASS
# PyTorch checks the history and computes the gradient automatically.
y.backward()
# 4. THE RESULT
print(x.grad) # Output: 24.0

Autograd decouples the model architecture from the mathematical derivation.
You can build complex, dynamic loops with control flow (like if statements and while loops), and you never have to derive the gradient formula manually. You focus on the architecture, and Autograd handles the calculus.