# PyTorch

> Tensors, autograd, nn.Module, Dataset/DataLoader and the explicit training loop, the most widely used framework in research and much of industry. Free notes,...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/pytorch
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/machine-learning/training-debugging)

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`) and `dtype`.
- **Autograd**: `requires_grad`, `loss.backward()`, `torch.no_grad()` for inference.
- **nn.Module**: define layers in `__init__`, computation in `forward`; `parameters()`, `state_dict()`.
- **Data**: `Dataset` (indexing one example) and `DataLoader` (batching, shuffling, workers).
- **The training loop**: forward, loss, `zero_grad`, `backward`, `step`; separate evaluation loop with `model.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](https://www.internhack.in/roadmaps/tracks/machine-learning/tensorflow-keras): The Keras high-level API for fast model building with fit/evaluate, and the TensorFlow ecosystem for mobile and production deployment.
- [JAX](https://www.internhack.in/roadmaps/tracks/machine-learning/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](https://pytorch.org/tutorials/) (PyTorch)
- [PyTorch documentation](https://pytorch.org/docs/stable/index.html) (PyTorch)
- [Practical Deep Learning for Coders](https://course.fast.ai/) (fast.ai)

Previous: [Debugging and Running Training Experiments](https://www.internhack.in/roadmaps/tracks/machine-learning/training-debugging) · Next: [TensorFlow and Keras](https://www.internhack.in/roadmaps/tracks/machine-learning/tensorflow-keras)

## More in Deep Learning Frameworks and Hardware

- [PyTorch](https://www.internhack.in/roadmaps/tracks/machine-learning/pytorch)
- [TensorFlow and Keras](https://www.internhack.in/roadmaps/tracks/machine-learning/tensorflow-keras)
- [JAX](https://www.internhack.in/roadmaps/tracks/machine-learning/jax)
- [GPUs and Efficient Training](https://www.internhack.in/roadmaps/tracks/machine-learning/gpu-training)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
