What Is PyTorch and Why Use It for Deep Learning

A
Author
18 Oct, 2026 4 min read 1 views

PyTorch is an open-source Python library for building and training neural networks. It gives you two core things: a fast n-dimensional array called a Tensor that can run on a CPU or a GPU, and an automatic differentiation engine called autograd that computes gradients for you. Everything else, from layers to optimisers to data loading, is built on top of those two ideas.

The two ideas behind PyTorch

Training a neural network means adjusting numbers (the weights) so that a loss value gets smaller. To do that you need gradients: how much the loss changes when each weight changes. Writing those derivatives by hand for a large network is impractical. PyTorch records the operations you run on tensors and replays them backwards to produce the gradients automatically.

The tensor part will feel familiar if you have used NumPy. The difference is that a PyTorch tensor can live on a GPU, where large matrix operations run much faster, and it can track the history of operations applied to it.

Eager execution: it feels like normal Python

PyTorch runs operations immediately, line by line. This is called eager execution. You can print a tensor in the middle of a model, set a breakpoint, use ordinary if statements and for loops, and see real values. That makes debugging far easier than in frameworks where you first build a static graph and run it later. PyTorch also offers torch.compile to speed up eager code when you need it, which later posts cover.

import torch

x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
y = (x ** 2).sum()      # y = x1^2 + x2^2 + x3^2
print(y)                # tensor(14., grad_fn=<SumBackward0>)

y.backward()            # compute dy/dx
print(x.grad)           # tensor([2., 4., 6.])

Here requires_grad=True tells PyTorch to track operations on x. After y.backward(), the gradient of y with respect to each element of x is stored in x.grad. The result 2x matches the derivative of x**2. This tiny example is the engine of all deep learning training.

What is in the library

Beyond tensors and autograd, the main parts you will use are:

  • torch.nn: layers (Linear, Conv2d, LSTM), loss functions and the Module base class
  • torch.optim: optimisers such as SGD and Adam
  • torch.utils.data: Dataset and DataLoader for feeding batches to a model
  • torchvision: a companion package with image datasets, transforms and pretrained models
  • torch.compile, torch.distributed and export tools for speed, scale and deployment

A minimal neural network

Here is a complete model that learns the function y = 2x + 1 from a few points. Do not worry about every line yet; each piece gets its own post later in this course.

import torch
from torch import nn

x = torch.linspace(-1, 1, 20).unsqueeze(1)   # shape (20, 1)
y = 2 * x + 1

model = nn.Linear(1, 1)
loss_fn = nn.MSELoss()
opt = torch.optim.SGD(model.parameters(), lr=0.1)

for step in range(200):
    pred = model(x)
    loss = loss_fn(pred, y)
    opt.zero_grad()
    loss.backward()
    opt.step()

print(model.weight.item(), model.bias.item())

After 200 steps the printed weight is very close to 2 and the bias very close to 1. The loop is the same pattern you will use for every model: predict, compute the loss, clear old gradients, backpropagate, and update the weights.

Why people choose PyTorch

  • Pythonic design: models are plain Python classes, so you can use normal tools and debuggers.
  • Flexibility: dynamic control flow and custom layers are easy, which suits research and unusual architectures.
  • Ecosystem: a large share of published model code and pretrained models is available in PyTorch form.
  • GPU support: moving a model and its data to a GPU takes a few lines.

When PyTorch is not the right tool

For small tabular problems, classic algorithms such as gradient-boosted trees or logistic regression are often faster and more accurate than a neural network. Those belong in the ML Fundamentals track. Use PyTorch when your data is images, text, audio or other signals where learned representations help, or when you need a custom model.

What to do next

Next you will install PyTorch and check that it works, then learn tensors in depth. Keep one habit from day one: always check tensor shapes. Most beginner bugs in deep learning are shape bugs, and printing .shape is the fastest fix.

Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.