Cheat sheet · Specialise
PyTorch cheat sheet
Opens with creditsA quick-revision book on PyTorch for engineers who train and ship neural networks. It follows the order you meet things in a real project: tensors, autograd, models, data, the training loop, then speed, scale, debugging and export.
Chapters
9 chapters · 59 concepts
1TensorsThe n-dimensional array every PyTorch program is built on: how to create one, what it holds, how it is shaped and where it lives.7 concepts
- 1Creating tensorsA tensor is a typed, n-dimensional array.
- 2Data typesEvery tensor has one dtype.
- 3Shapes and reshaping
shapelists the size of each dimension. - 4BroadcastingWhen two tensors of different shapes meet in an operation, PyTorch lines up their shapes from the right and stretches size-one dimensions to match, without…
- 5Indexing and maskingTensors support NumPy-style slicing, integer index lists and boolean masks.
- 6DevicesA tensor lives on one device: the CPU, an NVIDIA GPU (
cuda), Apple silicon (mps) or others. - 7Reductions and matrix mathsReductions such as
sum,mean,maxandargmaxtake adimto reduce along.
7 in this chapterOpen this chapter2AutogradHow PyTorch computes gradients for you: it records operations on tensors that need gradients, then walks that record backwards.6 concepts
- 1requires_gradA flag on a tensor that tells autograd to track operations on it.
- 2The computation graphAs tracked operations run, autograd builds a graph linking each result to its inputs through a
grad_fn. - 3backward and .gradCalling
.backward()on a scalar computes the gradient of it with respect to every leaf tensor that requires grad, and adds the result into each tensor's… - 4no_grad and inference_mode
torch.no_grad()turns off graph recording inside its block. - 5detach
tensor.detach()returns a tensor that shares the data but is cut off from the graph, so no gradient flows through it. - 6In-place operationsMethods ending in an underscore, such as
add_andzero_, change a tensor in place instead of returning a new one.
6 in this chapterOpen this chapter- Models with nn.ModuleHow to define a network: a class that owns its layers and says how data flows through them, plus the common building blocks.7 conceptsOpens with credits
- Losses, optimisers and schedulersWhat the model minimises, the algorithm that updates the weights, and how the learning rate changes over time.7 conceptsOpens with credits
- Datasets and DataLoadersHow data reaches the model: a Dataset knows how to fetch one example, and a DataLoader batches, shuffles and loads them in parallel.6 conceptsOpens with credits
- Training, evaluation and checkpointsThe loop you write in every project, the evaluation loop beside it, and how to save and restore work.7 conceptsOpens with credits
- Speed: GPUs, mixed precision and torch.compileThe switches that make the same model train faster and fit in less memory.7 conceptsOpens with credits
- Debugging trainingHow to find out why a model will not run, will not learn or learns the wrong thing.6 conceptsOpens with credits
- Distributed training and exportTraining on several GPUs, and turning a trained model into something another program can run.6 conceptsOpens with credits
- 10Glossary, A to ZEvery concept in the sheet and the smaller terms it uses, in one list.83 terms
About this sheet
Tensors, autograd, nn.Module, training loops, data loading, GPUs, mixed precision, torch.compile, DDP and export in PyTorch.
Related on Sefism
Picked from the other libraries by shared topics.
Roadmaps
See allBest practices
See allChecklists
See allProject structures
See all- Machine learning project project structureFor students and analysts whose model works in a notebook and now needs to be reproducible, reviewable, served and watched. It starts from the Cookiecutter Data Science v2 layout (immutable raw data, numbered notebooks, a real Python package) and grows into versioned data with DVC, typed parameters, data validation with pandera, experiment tracking and a model registry with MLflow, a FastAPI prediction service, drift monitoring and, for a team, scheduled retraining, model cards, fairness checks and shadow deployment.
- MLOps pipeline project structureFor engineers who can train a model and now need the training to be a pipeline anyone can rerun, review and release. A uv workspace holds the pipeline package (prepare, validate, featurize, train, evaluate, register steps run by DVC), a small shared feature package, and a separate FastAPI prediction service that loads models from the MLflow registry by alias. It grows into CI that reproduces the pipeline on pull requests, scheduled retraining, approved promotion and monitoring.
Career questions
See all- My machine learning projects never leave a Jupyter notebook. How do I learn to deploy a model?Move the notebook code into a small Python package, save the whole preprocessing and model pipeline, put it behind a FastAPI endpoint in a Docker container and deploy it. Then add what makes it production: tracked runs, a registry, a test in CI, prediction logging and a drift check. Start with batch predictions if you can.
- What is the difference between a data analyst, a data scientist and a machine learning engineer?Analysts answer business questions with SQL, spreadsheets, dashboards and reports. Data scientists add statistics, experiments and predictive models. ML engineers build and run models inside production software. Titles vary by company, so read the work in the job advert, and pick by the kind of problem you enjoy, not the title that sounds most senior.
- How do I become an MLOps engineer?MLOps engineers keep models working in production: packaging, versioning, pipelines, serving, CI/CD and monitoring. Come in from either software or DevOps with ML basics, or from ML with engineering habits. Learn the lifecycle in order, then prove it with one model you deploy, monitor and roll back yourself.
Work with me
Stuck on something specific?
Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.
- 1:1 Mentorship
- Fresh Graduate Mentorship
- Career Guidance
Brand deals, promos and partnerships: tauseeffayyaz@gmail.com