In PyTorch, what does calling model.eval() before validation do?
- Exam domain
- Core Machine Learning and AI Knowledge20% of the exam
- Difficulty
- Medium
- In this mock
- 10 questions on this domainPractise this domain
Why the exam asks this
Core Machine Learning and AI Knowledge (20% of the exam)
Objective in the exam guide: Work with deep learning frameworks such as TensorFlow or PyTorch.
Framework basics come up because they cause silent bugs.
Why D is the answer
It switches layers like dropout and batch norm to inference behaviour
model.eval() sets the module to evaluation mode, so dropout is disabled and batch norm uses running statistics. Use torch.no_grad() as well to skip gradient tracking.
Remember: eval() switches dropout and batch norm to inference mode.
How it works
Deep learning frameworks such as PyTorch have train and eval modes that change how some layers behave.
In detail
- eval() changes layer behaviour.
- no_grad() saves memory and time.
Every option, checked
Knowing why each wrong option fails is how you rule it out on exam day.
A. It moves the model to the GPUWrong
Device placement uses .to().
B. It computes the validation accuracy automatically and stops training when it fallsWrong
Metrics are computed by your code.
C. It freezes all weights permanentlyWrongMost tempting
Weights can still be trained later.
Why it tempts: "Eval" sounds like locking the model.
D. It switches layers like dropout and batch norm to inference behaviourRight
eval() switches dropout and batch norm to inference mode.
Use case
An illustrative scenario, not a real incident.
Illustrative: validation scores stop jumping around once dropout is turned off with eval().
See it in different ways
stateDiagram-v2 state "Train mode" as s0 state "Eval mode" as s1 [*] --> s0 s0 --> s1: model.eval s1 --> s0: model.train
How to read this chart
- It starts at "Train mode".
- From "Train mode", "model.eval" moves it to "Eval mode".
- From "Eval mode", "model.train" moves it to "Train mode".
mindmap
root(("Validation in PyTorch"))
n1["Mode"]
n2["model.eval"]
n3["Gradients"]
n4["torch.no_grad"]
n5["After"]
n6["model.train"]How to read this chart
- The centre is "Validation in PyTorch".
- "Mode" covers "model.eval".
- "Gradients" covers "torch.no_grad".
- "After" covers "model.train".
Related questions
Learn more
- Our lesson: Deep learning foundations for multimodal models
- Practise Core Machine Learning and AI Knowledge questions in the mock exam
- Source: PyTorch documentation
- Fundamentals of Deep Learning (NVIDIA DLI)
- PyTorch documentation
- Deep Residual Learning for Image Recognition (arXiv)
- Transfer learning tutorial (PyTorch)