Intro
PyTorch is the dominant research deep-learning framework. Dynamic graphs, Pythonic API, and a powerful ecosystem (Lightning, HuggingFace, vLLM).
PyTorch — what it is
EXAMPLE
import torch
import torch.nn as nn
# ===== The values =====
# - Eager by default (define-by-run): write Python, run Python
# - Tensor ops with autograd
# - GPU/MPS/TPU support via .to(device)
# - Massive ecosystem: torchvision, torchaudio, HuggingFace, Lightning, vLLM
# ===== Hello, tensor =====
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([10.0, 20.0, 30.0])
print(a + b)
# ===== A tiny model =====
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(4, 64)
self.fc2 = nn.Linear(64, 3)
def forward(self, x):
x = torch.relu(self.fc1(x))
return self.fc2(x)
model = Net()
optim = torch.optim.Adam(model.parameters(), lr=1e-3)
loss_fn = nn.CrossEntropyLoss()
X = torch.randn(100, 4); y = torch.randint(0, 3, (100,))
for epoch in range(5):
optim.zero_grad()
out = model(X)
loss = loss_fn(out, y)
loss.backward()
optim.step()
print('loss:', loss.item())
# ===== Save + load =====
torch.save(model.state_dict(), 'model.pt')
m2 = Net(); m2.load_state_dict(torch.load('model.pt'))
# ===== When PyTorch wins =====
# - Research and experimentation
# - HuggingFace and the entire LLM ecosystem
# - Cutting-edge papers (almost always PyTorch first)
# - When you want to debug like normal Python
# ===== When PyTorch hurts =====
# - Mobile / web deploys (PyTorch Mobile + ExecuTorch exist but less mature than TFLite)
# - Tiny edge devices (TF + Edge TPU has more polish in some cases)
# ===== Patterns to internalise =====
# - .to(device) + non_blocking + pin_memory for GPU performance
# - model.train() / model.eval() toggles BatchNorm + Dropout
# - torch.no_grad() during eval + parameter updates
# - Save state_dict, not the whole module
# ===== Pitfalls =====
# - Forgetting to zero gradients each step -> they accumulate
# - Mixing CPU + GPU tensors -> device mismatch errors
# - Training loop without scheduler / EarlyStopping
# - In-place ops on tensors with grad -> autograd surprises
Why it matters
PyTorch is research-friendly, ergonomic, and runs the lions share of the modern AI stack. Pythonic API, autograd, GPU when you want it. Master the basics (tensor, Module, optim, dataloader) and the rest of the ecosystem opens up — including pretty much every modern LLM.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
# PyTorch: dynamic graphs, Pythonic, deep-learning research standard. # Built and maintained by Meta, now under the PyTorch Foundation.Try it Yourself »
Discussion
Loading…