liveBook · Manning

Evaluation mode

As explained in Deep Learning with PyTorch, Second Edition

A model setting that switches layers such as dropout or batch normalization into inference behavior.

Evaluation mode switches a trained model from training behavior to inference behavior. It matters because layers such as dropout and batch normalization can produce unreliable results when left in training mode. section 2.1.5

Image classification inference

Before running a pretrained Vision Transformer on a new image, call `vit.eval()`. The model then produces class scores that can be matched to ImageNet labels; the highest-scoring example is “golden retriever” at 86.44%. section 2.1.5

Validation uses the same mode change: call `model.eval()` before processing validation batches. Pair it with `torch.no_grad()` so validation computes no gradients, reducing memory use and improving performance. Training switches back with `model.train()`. section 15.7.3

# In[11]:
vit.eval()

# Out[11]:
VisionTransformer(
  ...
)

See the minimal PyTorch call that places a Vision Transformer in evaluation mode. section 2.1.5

model.eval()
total_val_loss = 0
num_val_batches = 0
with torch.no_grad():
    for batch in val_dataloader:
        pixel_values, labels = encode_inputs_for_model(batch[
        "ct_image_path"], batch["mask_image_path"])
        outputs = model(pixel_values=pixel_values, labels=labels)
        val_loss = outputs.loss
        total_val_loss += val_loss.item()
        num_val_batches += 1

average_train_loss = total_train_loss / num_train_batches
average_val_loss = total_val_loss / num_val_batches
print(f"Training Loss: {average_train_loss:.4f}, Validation Loss: {
average_val_loss:.4f}")

See evaluation mode combined with `torch.no_grad()` inside a validation loop. section 15.7.3

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains why dropout and batch normalization require inference behavior, and demonstrates the mode change without detailing their internals.
Examples
Demonstrates `vit.eval()` for ImageNet classification and `model.eval()` with `torch.no_grad()` for segmentation validation.
What you can do after
Apply it via PyTorch before running inference or validation, and restore training with `model.train()`.
How it connects
Connects inference and validation with dropout, batch normalization, training mode, and gradient computation.

Also explained in

Compare how 3 books explain evaluation mode →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.