PyTorch deep dive 15 questions 25 min

PyTorch MCQ · test your framework knowledge

From tensors to training loops – 15 questions covering core PyTorch, autograd, nn.Module, CUDA, and best practices.

Easy: 5 Medium: 6 Hard: 4
Tensors
Autograd
nn.Module
CUDA

PyTorch: the flexible deep learning framework

PyTorch is an open‑source machine learning framework based on the Torch library. It provides tensor computation with GPU acceleration and automatic differentiation for building and training neural networks. This MCQ test covers essential PyTorch components: tensors, autograd, modules, optimizers, data handling, and GPU usage.

Why PyTorch?

Dynamic computation graphs, intuitive debugging, Pythonic style, and extensive ecosystem make PyTorch the go‑to for research and production.

PyTorch glossary – key concepts

Tensor

Multi‑dimensional array similar to numpy.ndarray, but can run on GPU. torch.tensor().

Autograd

Automatic differentiation engine that records operations on tensors and computes gradients via .backward().

nn.Module

Base class for all neural network modules. Layers are instances of nn.Module.

CUDA

PyTorch supports NVIDIA GPUs via .cuda() or .to('cuda') for accelerated computing.

Optimizer (torch.optim)

Implementations of SGD, Adam, etc. that update model parameters using gradients.

DataLoader & Dataset

Abstractions for efficient data loading, batching, shuffling, and parallelisation.

Loss functions (nn.functional)

Common losses: MSELoss, CrossEntropyLoss, L1Loss, available in torch.nn.

# Minimal PyTorch training loop
model = nn.Linear(10,1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
for x,y in dataloader:
    pred = model(x)
    loss = nn.MSELoss()(pred, y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
Interview tip: Understand the difference between torch.Tensor and torch.autograd.Variable (historical), know when gradients are accumulated, and explain how with torch.no_grad() affects evaluation.

Common PyTorch interview questions

  • How does autograd record operations on tensors?
  • What is the purpose of model.train() and model.eval()?
  • Explain the difference between nn.Module and nn.functional.
  • How do you move a model to GPU?
  • Why call optimizer.zero_grad() before loss.backward()?
  • What is the role of torch.no_grad() during inference?