DocsStation
PyTorch

PyTorch Autograd

By csestudy247 Updated December 2, 2026 5 min read
5 min read

If tensors are the building blocks of PyTorch, then Autograd is the engine that enables neural networks to learn.

Autograd is PyTorch’s automatic differentiation system. It records every mathematical operation performed on tensors and automatically computes gradients using backpropagation.

Without Autograd, training modern deep learning models would require manually deriving and implementing derivatives for millions of parameters an impossible task for most real-world neural networks.

Why Do We Need Autograd?

Training a neural network means adjusting parameters (weights and biases) so that the prediction error becomes smaller.

This adjustment depends on gradients.

Suppose we havey=x2y=x^2

The derivative isdydx=2xfrac{dy}{dx}=2xdxdy​=2x

Finding this derivative manually is trivial.

But a neural network is more likeLoss=f(g(h(k(x))))Loss=f(g(h(k(x))))Loss=f(g(h(k(x))))

where each layer depends on the previous one.

To compute∂Loss∂Weightfrac{partial Loss}{partial Weight}

we repeatedly apply the Chain Rule.

Doing this manually for millions of parameters is infeasible.

PyTorch solves this automatically.


How Autograd Works

Autograd builds a Dynamic Computation Graph while your program executes.

Input Tensor
      │
      ▼
 Linear Layer
      │
      ▼
 Activation
      │
      ▼
 Loss

Forward Pass

PyTorch records every operation.

x → y=x² → z=sin(y)

Instead of storing only values, PyTorch also remembers:

  • operation performed
  • parent tensors
  • gradient functions

Backward Pass

Calling

loss.backward()

makes PyTorch traverse the graph in reverse.

Loss
 ↑
Activation
 ↑
Linear Layer
 ↑
Input

The chain rule is applied automatically.


requires_grad=True

Gradient tracking is disabled by default.

Enable it using

x = torch.tensor(3.0, requires_grad=True)

Now every operation involving x becomes part of the computation graph.


Example 1 — Computing a Simple Derivative

import torch

x = torch.tensor(3.0, requires_grad=True)

y = x ** 2

y.backward()

print(x.grad)

Output

tensor(6.)

Why?

Sincey=x2y=x^2dydx=2xfrac{dy}{dx}=2xdxdy​=2x

Atx=3x=3x=3 2×3=62 times 3 = 6

Autograd computed the derivative automatically.


Example 2 — Understanding the Chain Rule

import torch

x = torch.tensor(4.0, requires_grad=True)

y = x ** 2

z = torch.sin(y)

z.backward()

print(x.grad)

Mathematically,z=sin⁡(x2)z=sin(x^2)

Using the chain rule,dzdx=cos⁡(x2)×2xfrac{dz}{dx} = cos(x^2)times2xdxdz​=cos(x2)×2x

PyTorch produces exactly this result without requiring us to derive the equation manually.

This illustrates why Autograd is so powerful for deep neural networks composed of many nested operations.


Example 3 — Training a Single Neuron

Suppose we want to predict whether a student gets placed based on CGPA.

Forward computation:

z = w*x + b

y_pred = torch.sigmoid(z)

loss = binary_cross_entropy_loss(y_pred, y)

Instead of deriving

  • ∂Loss/∂Prediction
  • ∂Prediction/∂z
  • ∂z/∂w
  • ∂z/∂b

manually,

we simply write

loss.backward()

print(w.grad)
print(b.grad)

PyTorch automatically computes

  • gradient of weight
  • gradient of bias


Working with Multiple Values

Autograd also supports vectors and matrices.

Example from the notebook:

x = torch.tensor([1.0,2.0,3.0], requires_grad=True)

y = (x**2).mean()

y.backward()

print(x.grad)

Each element receives its own gradient.

tensor([...])

This is exactly how gradients are computed for batches of data during training.


Accessing Gradients

Gradients are stored inside

tensor.grad

Example

print(x.grad)
print(w.grad)
print(b.grad)

The .grad attribute becomes available only after calling

backward()

Gradient Accumulation

An important PyTorch behavior is that gradients accumulate.

Example:

y.backward()

print(x.grad)

x.grad.zero_()

Without clearing gradients, every subsequent call to backward() adds new gradients to the existing ones.

During training, optimizers therefore perform

optimizer.zero_grad()

loss.backward()

optimizer.step()

every iteration.


Disabling Gradient Tracking

Gradients are needed only during training.

During inference, gradient computation wastes memory.

Your notebook demonstrates three common approaches.

1. requires_grad_(False)

x.requires_grad_(False)

2. detach()

z = x.detach()

The detached tensor shares data but no longer belongs to the computation graph.


3. torch.no_grad()

with torch.no_grad():
    prediction = model(x)

This is the preferred approach during inference because it reduces memory usage and improves execution speed.


Complete Training Workflow

A simplified training iteration looks like this:

# Forward Pass
prediction = model(x)

# Compute Loss
loss = criterion(prediction, target)

# Clear Previous Gradients
optimizer.zero_grad()

# Backpropagation
loss.backward()

# Update Parameters
optimizer.step()

This sequence is repeated for every batch throughout training.


Best Practices

✔ Enable requires_grad=True only for trainable parameters.

✔ Always clear gradients before backpropagation.

✔ Use torch.no_grad() during inference.

✔ Inspect .grad for debugging and understanding optimization behavior.

✔ Remember that PyTorch builds computation graphs dynamically during execution.


Key Takeaways

  • Autograd is PyTorch’s automatic differentiation engine.
  • It constructs a dynamic computation graph during the forward pass.
  • Calling backward() automatically applies the chain rule to compute gradients.
  • Computed gradients are stored in the .grad attribute.
  • Gradients accumulate unless they are explicitly cleared.
  • Disable gradient tracking during inference using torch.no_grad() or .detach().
  • The examples in this notebook—from simple derivatives to training a perceptron—demonstrate the exact workflow used in real-world deep learning models.

PyTorch Autograd Quiz

Q.1 What is the primary function of the PyTorch Autograd module?



  • To automatically optimize hyperparameters like learning rate.


  • To visualize the neural network architecture during training.


  • To convert Python code into low-level C++ for faster execution.


  • To provide automatic differentiation for all operations on tensors.

Q.2 Which attribute must be set to True when defining a tensor to track its operations for gradient calculation?



  • track_history


  • compute_derivative


  • requires_grad


  • enable_grad

Q.3 In the context of Autograd, what does the 'Backward' pass achieve?



  • It calculates the final loss value based on predictions and targets.


  • It resets the weights of the neural network to their initial values.


  • It computes the gradients of a scalar value with respect to all leaf tensors.


  • It reverses the order of the input data to prevent overfitting.

Q.4 If you have a tensor x=3 with requires_grad=True and y=x², what will x.grad be after calling y.backward()?



  • 2.0


  • 3.0


  • 9.0


  • 6.0

Q.5 PyTorch represents the computation history as a Directed Acyclic Graph (DAG). In this graph, what do the 'Leaf' nodes represent?



  • The optimized gradients after the weights have been updated.


  • The final loss value calculated at the end of the network.


  • Input tensors for which the user has requested gradient tracking.


  • Intermediate activation functions like Sigmoid or ReLU.

Q.6 Why is it necessary to call x.grad.zero_() when performing multiple gradient calculations in a loop?



  • To prevent the model from overfitting by regularizing the gradients.


  • To release the GPU memory used by the tensor during the backward pass.


  • Because Autograd only works if the initial gradient value is zero.


  • Because PyTorch accumulates gradients by adding them to the existing values in .grad.

Q.7 Which method would you use to temporarily disable gradient tracking during model evaluation to save memory?



  • tensor.stop_tracking()


  • x.requires_grad_(True)


  • with torch.no_grad():


  • y.backward(track=False)

Q.8 What is the result of applying .detach() to a tensor that is currently part of a computation graph?



  • It calculates the second-order derivative (Hessian) of the tensor.


  • It forces the tensor to only accept integer values.


  • It removes the tensor from the computer's RAM entirely.


  • It returns a new tensor that shares the same data but does not require gradients.

Q.9 Which function is commonly used to compute gradients for a scalar loss value in PyTorch?



  • loss.forward()


  • loss.backward()


  • loss.compute_grad()


  • torch.gradient(loss)

Q.10 What is the primary purpose of the computation graph built by Autograd during the forward pass?



  • To permanently store the neural network architecture on disk.


  • To record operations so gradients can be computed efficiently during backpropagation.


  • To convert tensors into NumPy arrays automatically.


  • To optimize GPU memory allocation before training starts.

Scroll to Top