PyTorch
Tensors, autograd, nn.Module, Dataset/DataLoader and the explicit training loop, the most widely used framework in research and much of industry.
Intermediate · about 20 hours · difficulty 3/5
Learn first: Debugging and Running Training Experiments
PyTorch is the recommended framework: it dominates research, most new models and papers ship PyTorch code, and Hugging Face is built around it. Its Pythonic, eager style makes debugging straightforward.
What to learn
- Tensors: NumPy-like API plus
device(cpu,cuda,mps) anddtype. - Autograd:
requires_grad,loss.backward(),torch.no_grad()for inference. - nn.Module: define layers in
__init__, computation inforward;parameters(),state_dict(). - Data:
Dataset(indexing one example) andDataLoader(batching, shuffling, workers). - The training loop: forward, loss,
zero_grad,backward,step; separate evaluation loop withmodel.eval(). - Saving: save
state_dict, not the whole model object. - torch.compile for speedups once things work.
Higher-level helpers
PyTorch Lightning, Hugging Face Accelerate or fastai remove boilerplate. Learn the raw loop first so you know what they hide.
Good enough to move on when you can write a complete training and evaluation loop from memory, including checkpointing and running on GPU.
Key concepts
- Tensors and devices
- Autograd
- nn.Module
- Dataset and DataLoader
- Training and evaluation loops
- Saving state_dict checkpoints
- torch.compile
- PyTorch Lightning or Accelerate
Other options for this step
- TensorFlow and Keras: The Keras high-level API for fast model building with fit/evaluate, and the TensorFlow ecosystem for mobile and production deployment.
- JAX: Composable function transformations (grad, jit, vmap) over NumPy-style code, popular for research and large-scale training on TPUs and GPUs.
Mini project
Write a PyTorch training script for Fashion-MNIST with a custom Dataset, an MLP and a CNN, validation each epoch, best-checkpoint saving and a command-line flag for the device.
Check yourself
Why do you call optimizer.zero_grad() on every iteration, and what happens if you forget?
Free resources
- PyTorch tutorials (PyTorch)
- PyTorch documentation (PyTorch)
- Practical Deep Learning for Coders (fast.ai)