PyTorch Interview Questions (2026)

Practice 45 PyTorch interview questions on tensors, autograd, nn.Module, DataLoader, training loops, optimizers, checkpoints, TorchScript, and deployment.

45 questions with answers

What Is PyTorch?

Key Takeaways

  • PyTorch is an open-source machine learning framework known for eager execution, dynamic computation graphs, and flexible training loops.
  • Interviews test tensors, autograd, nn.Module, Dataset, DataLoader, optimizers, loss functions, checkpoints, device placement, and deployment options.
  • Strong answers can write the training loop and explain what zero_grad, backward, and step do.
  • Experienced candidates discuss reproducibility, mixed precision, distributed training, model export, and production monitoring.

PyTorch is an open-source framework for building, training, and deploying machine learning models. It is popular because tensor operations feel close to Python, autograd tracks gradients dynamically, and training loops are explicit. In interviews, PyTorch questions test whether you understand tensors, modules, data loading, loss calculation, backpropagation, optimization, checkpointing, and deployment trade-offs.

45Questions with direct answers
3Groups: PyTorch basics, coding tasks, senior scenarios
7+Code, diagrams, tables, videos, and quiz
45-90 minTypical PyTorch interview round length

Watch: Introduction to PyTorch

Video: Introduction to PyTorch (PyTorch, YouTube)

Test yourself and earn a certificate

6 quick questions. Score 70%+ to download your PyTorch certificate.

Jump to quiz

All Questions on This Page

45 questions
PyTorch Advanced Scenarios
  1. 31. Loss behaves oddly because gradients accumulate. What was missed?
  2. 32. Validation results change unexpectedly. What do you check?
  3. 33. Runtime says tensors are on different devices. What is wrong?
  4. 34. GPU utilization is low. What do you inspect?
  5. 35. A checkpoint fails to load. What do you inspect?
  6. 36. Training returns NaN loss. What do you check?
  7. 37. Loss does not change. What might be wrong?
  8. 38. Training accuracy is high but validation is poor. What do you do?
  9. 39. BatchNorm gives poor inference results. What do you check?
  10. 40. CUDA runs out of memory. What do you change?
  11. 41. A candidate uses retain_graph=True everywhere. What is the concern?
  12. 42. A classifier looks good by accuracy but misses rare positives. What changes?
  13. 43. Production predictions differ from notebook predictions. What do you inspect?
  14. 44. When would you use DistributedDataParallel?
  15. 45. What do you ask before approving PyTorch training code?

PyTorch Fundamentals

Foundational15 questions

Start here. These are the definitions and first-principle checks that open most rounds.

Q1. What is a tensor in PyTorch?

A tensor is a multi-dimensional array with dtype, shape, and device information.

Tensors are the core data structure for inputs, weights, activations, and gradients.

Tensor changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Watch a deeper explanation

Video: Introduction to PyTorch (PyTorch, YouTube)

Q2. What is autograd?

Autograd is PyTorch's automatic differentiation system. It tracks operations on tensors and computes gradients during backward.

It builds a dynamic graph during the forward pass.

Autograd changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q3. What does requires_grad mean?

requires_grad tells PyTorch to track operations on a tensor so gradients can be calculated.

Model parameters usually require gradients.

requires_grad changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q4. What is nn.Module?

nn.Module is the base class for PyTorch models and layers.

It tracks parameters, submodules, and mode changes.

nn.Module changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Answer partWhat to sayEvidence to mention
Definitionnn.Module in one direct sentence.Official docs or course material
Use caseThe work where it changes a decision.Dataset, model, query, dashboard, or pipeline
RiskWhat breaks when it is misunderstood.Metric, log, test result, or review note

Q5. What does forward do?

forward defines how input tensors pass through the model to produce outputs.

Calling model(x) invokes forward through module hooks.

forward method changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Watch a deeper explanation

Video: PyTorch for deep learning and machine learning (freeCodeCamp.org, YouTube)

Q6. What is a Dataset?

A Dataset defines how to access individual samples and labels.

It usually implements __len__ and __getitem__.

Dataset changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q7. What does DataLoader do?

DataLoader batches, shuffles, and loads Dataset samples, often with worker processes.

Poor DataLoader settings can slow GPU training.

DataLoader changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q8. What is an optimizer?

An optimizer updates model parameters using gradients.

SGD, Adam, and AdamW are common choices.

Optimizer changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q9. What does the loss function do?

The loss function measures how far predictions are from targets and provides a signal for gradient updates.

Loss must match the task and output shape.

Loss function changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q10. Why call zero_grad?

zero_grad clears old gradients because PyTorch accumulates gradients by default.

Forgetting it can make updates wrong.

zero_grad changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

python
optimizer.zero_grad()
outputs = model(batch_x)
loss = loss_fn(outputs, batch_y)
loss.backward()
optimizer.step()

Q11. What does loss.backward do?

loss.backward computes gradients for tensors that require gradients.

The optimizer uses those gradients in step.

backward changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q12. Why switch between train and eval mode?

train and eval change behavior of layers such as dropout and batch normalization.

Use eval during validation and inference.

model.train and model.eval changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q13. Why use torch.no_grad?

torch.no_grad disables gradient tracking during inference or validation.

It saves memory and compute.

torch.no_grad changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Watch a deeper explanation

Video: Learn PyTorch for deep learning (Daniel Bourke, YouTube)

Q14. What is state_dict?

state_dict is a dictionary containing model parameters and persistent buffers.

It is the common way to save and load PyTorch models.

state_dict changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q15. What does device placement mean?

Device placement controls whether tensors and models live on CPU, CUDA GPU, MPS, or another backend.

Inputs and model parameters must be on compatible devices.

Device changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Back to question list

PyTorch Practical Interview Questions

Intermediate15 questions

These questions test whether you can apply the topic to real data, real code, and messy constraints.

Q16. Walk through a PyTorch training loop.

Set model.train, move data to device, run forward pass, compute loss, zero gradients, call backward, step optimizer, and record metrics.

Mention validation separately with eval and no_grad.

write training loop changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q17. How do you build a custom Dataset?

Implement __len__ and __getitem__, load or index samples, return tensors and labels, and keep heavy transforms controlled.

Do not load the whole dataset in memory unless it fits.

build Dataset changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q18. How do you speed up DataLoader?

Tune batch size, num_workers, pin_memory for CUDA, prefetching, transforms, and storage format.

Measure GPU utilization to confirm the bottleneck.

speed DataLoader changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q19. What do you save in a checkpoint?

Save model state_dict, optimizer state, epoch, best metric, config, and random seed state when needed.

Saving only weights may not support exact resume.

save checkpoint changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q20. How do you load a PyTorch model for inference?

Create the model class, load state_dict, move to device, call eval, and run inside no_grad.

The architecture must match the saved weights.

load model changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q21. How do you debug a shape mismatch?

Print tensor shapes at each layer, check batch dimension, flattening, output size, and label shape.

Most shape bugs are visible with one batch.

fix shape error changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q22. How do you use a GPU safely?

Move model and tensors to the same device, avoid unnecessary CPU-GPU copies, and monitor memory.

Device mismatch is a common runtime error.

handle GPU changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q23. How do you write a validation loop?

Set eval mode, use no_grad, compute loss and metrics without optimizer steps, then return aggregate metrics.

Do not train during validation.

validate model changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q24. How do you choose Adam versus SGD?

Adam or AdamW often trains quickly with less tuning, while SGD with momentum can work well for some vision tasks after tuning.

The right answer depends on data, model, and metric.

choose optimizer changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q25. How do you reduce overfitting in PyTorch?

Use validation, augmentation, dropout, weight decay, early stopping, smaller models, or more data.

Track train and validation curves.

prevent overfitting changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Watch a deeper explanation

Video: PyTorch 101 crash course (Daniel Bourke, YouTube)

Q26. How do you use mixed precision?

Use autocast and GradScaler where supported to reduce memory and speed training.

Watch numeric stability.

mixed precision changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q27. How do you freeze pretrained layers?

Set requires_grad to false for selected parameters and pass only trainable parameters to the optimizer.

Verify frozen layers do not update.

freeze layers changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q28. When do you clip gradients?

Clip gradients when training is unstable due to exploding gradients, common in some sequence models.

It is not a cure for every NaN.

clip gradients changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q29. How do you export a PyTorch model?

Choose based on serving need: state_dict for PyTorch loading, TorchScript or newer export paths for graph-style deployment, or ONNX for cross-runtime use.

Test exported output against the original model.

export model changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q30. How do you improve reproducibility?

Set seeds, version data, log config, track environment, save checkpoints, and control nondeterministic operations where possible.

Exact reproducibility can still differ across devices.

reproduce training changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Back to question list

PyTorch Advanced Scenarios

Advanced15 questions

Advanced rounds test trade-offs, failure modes, and whether the decision can hold up under production pressure.

Q31. Loss behaves oddly because gradients accumulate. What was missed?

optimizer.zero_grad was likely missing or placed incorrectly.

PyTorch accumulates gradients by default.

gradients keep growing changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q32. Validation results change unexpectedly. What do you check?

Check model.eval, no_grad, dropout, batch norm, data order, and metric reset.

Evaluation mode matters.

eval accuracy unstable changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q33. Runtime says tensors are on different devices. What is wrong?

The model and input or target tensors are not on the same device.

Move all needed tensors consistently.

cpu gpu mismatch changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q34. GPU utilization is low. What do you inspect?

Check DataLoader speed, batch size, CPU transforms, disk reads, num_workers, and GPU synchronization.

The GPU may be waiting for data.

slow gpu changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q35. A checkpoint fails to load. What do you inspect?

Check model class changes, key names, strict loading, device map, and saved state format.

Architecture and weights must match.

checkpoint cannot load changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q36. Training returns NaN loss. What do you check?

Check learning rate, input normalization, labels, loss function, exploding gradients, mixed precision, and invalid operations.

one batch comes first.

nan loss changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q37. Loss does not change. What might be wrong?

Check requires_grad, optimizer parameters, learning rate, target format, loss function, and data-label pairing.

Make sure parameters are actually updating.

no learning changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q38. Training accuracy is high but validation is poor. What do you do?

Add regularization, augmentation, early stopping, more data, or reduce model capacity.

The model is learning training noise.

overfitting changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q39. BatchNorm gives poor inference results. What do you check?

Check train/eval mode, batch size, running stats, and distribution shift.

BatchNorm behavior changes between train and eval.

bad batch norm changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q40. CUDA runs out of memory. What do you change?

Reduce batch size, use mixed precision, gradient accumulation, smaller model, or checkpoint activations.

Also clear unused tensors and avoid retaining graphs.

memory out changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q41. A candidate uses retain_graph=True everywhere. What is the concern?

It may hide graph lifecycle misunderstandings and increase memory use.

Use it only when multiple backward passes over the same graph are required.

retain graph misuse changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q42. A classifier looks good by accuracy but misses rare positives. What changes?

Use precision, recall, F1, PR AUC, threshold tuning, class weights, or sampling.

Accuracy is weak for imbalanced data.

wrong metric changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q43. Production predictions differ from notebook predictions. What do you inspect?

Check preprocessing, model mode, weights version, input dtype, shape, device, and export path.

Serving code must match training assumptions.

deployment drift changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q44. When would you use DistributedDataParallel?

Use it when training time or data size justifies multi-GPU or multi-node training.

It adds synchronization and operational cost.

distributed training changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Q45. What do you ask before approving PyTorch training code?

Ask about data split, training loop order, metrics, device handling, checkpoints, reproducibility, validation, and export tests.

These questions reveal production quality.

senior review changes PyTorch decisions by affecting the chosen method, the failure mode, or the validation step. Reliable evidence comes from a metric, test result, query output, log, or review note.

Back to question list

PyTorch vs TensorFlow

Both frameworks can train and deploy deep learning models. The interview answer should focus on workflow and ecosystem fit, not brand preference.

AreaPyTorchTensorFlowInterview point
Execution styleEager and Pythonic by defaultEager with graph toolsDebugging style differs
Model APInn.Module and explicit loopsKeras plus lower-level APIsTraining control differs
ResearchCommon in experiments and papersAlso used, often with KerasFast iteration matters
DeploymentTorchScript, torch.export, serving stacksSavedModel and TensorFlow ServingExport contract matters

PyTorch answer signals

Training-loop clarity is the highest-value signal.

Tensors
86 signal
Autograd
92 signal
Training loop
94 signal
Production
80 signal
  • Tensors: shape, dtype, device
  • Autograd: backward and graph tracking
  • Training loop: zero_grad, loss, backward, step
  • Production: checkpoint, export, monitor

How to Prepare for a PyTorch Interview

Prepare by writing a minimal training loop and explaining each line. Then add data loading, checkpointing, metrics, and deployment concerns.

  • Review tensor shapes, dtypes, devices, broadcasting, autograd, and no_grad.
  • Practice nn.Module, Dataset, DataLoader, optimizer, loss, training loop, validation loop, and checkpoints.
  • Know GPU placement, mixed precision, reproducibility, and model.eval versus model.train.
  • One debugging story involving shape mismatch, NaN loss, overfitting, slow DataLoader, or bad checkpoint restore is useful.

PyTorch interview prep flow

1Batch
Dataset and DataLoader
2Forward
model outputs
3Loss
compare with labels
4Backward
gradients
5Update
optimizer step

Most rounds reward clear reasoning more than memorized phrasing.

How Strong PyTorch Answers Sound

A strong PyTorch answer walks through the training loop in order: move tensors to device, run forward pass, compute loss, clear old gradients, call backward, step the optimizer, and track metrics. production-ready answers mention model.train and model.eval, torch.no_grad, DataLoader bottlenecks, checkpoint contents, reproducibility, mixed precision, and how the model will be exported or served.

Explain gradient lifecycle

PyTorch accumulates gradients by default, so zero_grad must happen before backward in the usual loop.

Track device placement

Model and tensors must be on the same device. CPU-GPU mismatch is a common error.

Separate train and eval mode

Dropout and batch normalization behave differently in training and evaluation.

Save enough state

A useful checkpoint includes model state, optimizer state, epoch, metric, and config.

Test Yourself: PyTorch Quiz

Ready to test your PyTorch knowledge?

6 questions, about 4 minutes. Score 70% or higher to earn a shareable certificate.

6 questions Instant feedback Free certificate on 70%+

Frequently  Asked  Questions

What do PyTorch interviews usually ask?

They ask about tensors, autograd, nn.Module, Dataset, DataLoader, training loops, loss functions, optimizers, checkpoints, devices, and deployment. Complete coverage has one concrete example, one failure case, and one validation signal beyond the definition.

What details make PyTorch coverage complete?

Complete coverage includes the direct fact, example, trade-off, and evidence. Memorized wording is weaker than knowing what works, what fails, and how the result is verified.

What PyTorch project should I discuss?

Discuss a model where you handled data loading, training, validation, checkpointing, metrics, device placement, and an actual debugging issue.

What separates experienced PyTorch candidates?

Experienced candidates explain gradient lifecycle, train versus eval mode, DataLoader bottlenecks, checkpoint state, reproducibility, and deployment checks. Complete coverage has one concrete example, one failure case, and one validation signal beyond the definition.

Practice PyTorch answers before ML interviews

Use this PyTorch bank for model and training-loop reasoning, then practice Python and debugging tasks with Hyring's AI Coding Interviewer.

See Hyring AI Coding Interviewer

Sources

Adithyan RKWritten by Adithyan RK
Surya N
Fact-checked by Surya N
Published on: 13 Apr 2026Last updated: 4 Jul 2026
Share: