Skip to content
mentoor

PyTorch basics

For engineers

Tensors, autograd, nn.Module and DataLoader, a complete training loop, and debugging shapes, devices and NaN losses, including in AI-written code

See where you stand

110 questions across four stages: Understand, Apply, Solve and Judge.

Try 5 questions

Practice items, not counted towards your mastery. No account needed.

Check yourself · 1 of 5

A tensor has shape [32, 1] and another has shape [32]. You subtract one from the other. What shape comes out?

Choose one answer

Check yourself · 2 of 5

You call loss.backward() three times in a row on freshly built graphs without zeroing. What does w.grad hold?

Choose one answer

Check yourself · 3 of 5

Which two methods must a custom torch Dataset implement?

Choose one answer

Check yourself · 4 of 5

Which order is correct inside one training step?

Choose one answer

Check yourself · 5 of 5

A traceback says mat1 and mat2 shapes cannot be multiplied (32x784 and 100x10). What does it mean?

Choose one answer

Topics

  1. Tensors, dtypes and devices4 min read · Creating, reshaping and broadcasting tensors, choosing float32 or bfloat16, and keeping data on one device across CPU, CUDA and Apple MPS
  2. Autograd and computation graphs4 min read · How requires_grad and backward() produce gradients, why .grad accumulates until zero_grad, and when to use torch.no_grad for inference
  3. Models with nn.Module and DataLoader4 min read · Defining and registering layers in an nn.Module, batching data with Dataset and DataLoader, and switching correctly between train and eval mode
  4. Writing a PyTorch training loop5 min read · The forward, loss, zero_grad, backward and step sequence, plus a no-grad validation pass and saving the best checkpoint
  5. Debugging shape errors and NaN losses5 min read · Reading tracebacks, printing shapes, overfitting one batch and chasing NaN losses, including bugs hidden in agent-written PyTorch code

Start here

110 questions · last verified 2026-12