Sub topic 14
Linear Probes and Evaluating Learned Features
Assume you know basic Python, PyTorch tensors, embeddings, softmax, and cross-entropy loss. You should also understand that a CLIP image encoder maps an image to a vector.
A linear probe is a small classifier trained on frozen embeddings. “Linear” means the classifier computes a weighted sum of the input features before producing class scores. “Frozen” means the CLIP encoder does not change during training.
A linear probe answers this question:
> Do the learned CLIP features already contain information that separates these classes?
It tests the representation rather than the full model's ability to learn new features.
Zero-shot classification versus a linear probe
In zero-shot classification, you create text prompts such as “a photo of a cat” and compare their text embeddings with an image embedding. The class names and prompt wording affect the result.
With a linear probe, you collect image embeddings for labeled examples and train a classifier on them. The classifier learns a boundary for the particular dataset.
| Protocol | What is trained? | Needs labeled examples? | Main question |
|---|---|---|---|
| Zero-shot | Nothing after pretraining | No | Do prompts match the task? |
| Linear probe | A linear classifier | Yes | Are the features useful for this task? |
| Fine-tuning | Some or all model weights | Yes | Can the model adapt its features? |
A linear probe is cheaper and less likely to overfit than full fine-tuning, but it cannot fix information that the encoder discarded.
Training a probe
First, run every image through the CLIP image encoder. Store the resulting embeddings and labels. Then split the data into training and test sets before fitting the probe. The test set must remain untouched until final evaluation.
The following example trains a probe on synthetic embeddings. Replace make_dataset with embeddings produced by a frozen CLIP image encoder when using real images.
import torch
from torch import nn
torch.manual_seed(7)
def make_dataset(n, dim, classes):
centers = torch.randn(classes, dim) * 3
labels = torch.randint(0, classes, (n,))
embeddings = centers[labels] + 0.8 * torch.randn(n, dim)
return embeddings, labels
train_x, train_y = make_dataset(800, 128, 4)
test_x, test_y = make_dataset(300, 128, 4)
probe = nn.Linear(train_x.size(1), 4)
optimizer = torch.optim.AdamW(probe.parameters(), lr=1e-2)
loss_fn = nn.CrossEntropyLoss()
for _ in range(100):
logits = probe(train_x)
loss = loss_fn(logits, train_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
with torch.no_grad():
predictions = probe(test_x).argmax(dim=1)
accuracy = (predictions == test_y).float().mean()
print(f"test accuracy: {accuracy.item():.3f}")The nn.Linear layer is the probe. The embedding tensors are never updated. For a real experiment, normalize embeddings consistently, use a validation set to select learning rate and training length, and report the final test score only once.
Reading the result correctly
A high linear-probe score means the representation makes the task linearly separable. It does not prove that CLIP understands every detail of the images. A low score can mean the feature lacks the needed information, the labels are noisy, or the probe was poorly configured.
Compare against simple baselines, such as random guessing and a classifier trained on raw pixels. For imbalanced classes, accuracy can hide poor performance on rare classes, so also report macro-averaged F1 or per-class recall.
Keep the evaluation fair:
- Use the same image preprocessing as during CLIP inference.
- Prevent duplicate or near-duplicate images from crossing dataset splits.
- Fit normalization and other preprocessing only on the training split.
- Keep the encoder and probe settings fixed when comparing datasets.
Exercise
Why must the CLIP encoder remain frozen during a linear-probe evaluation?
Show answer
Freezing it isolates the quality of the learned representation. If the encoder changes, the experiment becomes fine-tuning rather than a linear-probe test.