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


