TExcelTAD

Active Learning

Labelling is often the most expensive part of an ML project: a pathologist reviewing slides, a lawyer reading contracts, an engineer inspecting weld images. If you can afford only a few hundred labels, it matters a great deal which examples get them. Active learning lets the model choose: it asks for labels on the examples it expects to learn the most from.

The loop

  1. Start with a small labelled set and a large pool of unlabelled examples.
  2. Train a model on the labelled set.
  3. Use the model to score the pool and pick the most useful examples (a query strategy).
  4. Send them to a human (the oracle) for labels, add them to the labelled set.
  5. Repeat until the labelling budget is spent or the model is good enough.

Query strategies

Strategy Picks the examples where…
Uncertainty sampling the model is least sure (probability closest to 0.5 for two classes)
Margin sampling the top two class probabilities are closest to each other
Entropy sampling the predicted class distribution is most spread out
Query by committee several models disagree most
Diversity sampling the examples are most different from what is already labelled, to cover the whole space

Uncertainty-based strategies spend labels near the decision boundary, where they change the model most; examples deep inside a class add little. Diversity guards against asking about many near-identical uncertain examples.

Uncertainty sampling vs random labelling

The pool has 1500 unlabelled examples and the budget is 100 labels: 10 random ones to start, then 9 rounds of 10. The example compares choosing those labels at random with uncertainty sampling, averaged over 5 random starts, and scores each model on 500 labelled test examples.

python · try it yourself
import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression

X, y = make_classification(n_samples=2000, n_features=10, n_informative=5, class_sep=1.5, random_state=3)
X_pool, y_pool, X_test, y_test = X[:1500], y[:1500], X[1500:], y[1500:]

def run(strategy, seed=0, start=10, budget=100, batch=10):
    rng = np.random.default_rng(seed)
    labelled = list(rng.choice(len(X_pool), size=start, replace=False))
    scores = []
    while True:
        model = LogisticRegression(max_iter=1000).fit(X_pool[labelled], y_pool[labelled])
        scores.append(model.score(X_test, y_test))
        if len(labelled) >= budget:
            return scores
        rest = np.setdiff1d(np.arange(len(X_pool)), labelled)
        if strategy == "random":
            pick = rng.choice(rest, size=batch, replace=False)
        else:   # uncertainty sampling: ask about the rows closest to probability 0.5
            p = model.predict_proba(X_pool[rest])[:, 1]
            pick = rest[np.argsort(np.abs(p - 0.5), kind="stable")[:batch]]
        labelled.extend(pick)              # an expert labels these rows

print("labels:       ", list(range(10, 101, 10)))
for strategy in ["random", "uncertainty"]:
    avg = np.mean([run(strategy, seed=s) for s in range(5)], axis=0)
    print(f"{strategy:13}", [round(float(v), 3) for v in avg])
print("all 1500 labels:", round(LogisticRegression(max_iter=1000).fit(X_pool, y_pool).score(X_test, y_test), 3))
labels:        [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
random        [0.738, 0.788, 0.806, 0.802, 0.812, 0.814, 0.812, 0.816, 0.816, 0.818]
uncertainty   [0.738, 0.764, 0.81, 0.82, 0.83, 0.833, 0.834, 0.831, 0.833, 0.833]
all 1500 labels: 0.838

Reading the results:

  • With all 1500 labels the model reaches 0.838. Uncertainty sampling gets to 0.833 with only 60 labels, 4 % of the labelling cost.
  • Random labelling reaches 0.818 after 100 labels and is still behind.
  • At 20 labels random sampling is ahead (0.788 vs 0.764). With very few labels the model's uncertainty is itself unreliable, so its first questions are not the best ones: the cold-start problem. Many teams start with a random or diverse batch before switching to uncertainty sampling.

The labels the model asks for are "hard" examples near the boundary, so they are also a useful set for humans to review: they often reveal unclear labelling guidelines.

In practice

  • Batch queries. Experts label in sessions, so pick a batch (here 10) per round, not one example, and mix in diversity so the batch is not ten copies of the same hard case.
  • Labelling tools (annotation platforms) handle the queue, the guidelines and agreement checks between labellers.
  • Combine with semi-supervised learning. Ask humans about uncertain examples, and let the model pseudo-label the confident ones (previous lesson on semi-supervised learning).
  • Stopping. Stop when validation accuracy stops improving per batch of labels, or when the budget runs out.

The Training Strategies course builds a full labelling loop with several strategies.

Pitfalls

  • A biased labelled set. Actively chosen examples are not a random sample. Use a separate, randomly sampled and labelled test set to measure performance; never evaluate on the queried examples.
  • Outliers and junk. The most uncertain examples can be noise, corrupted images or out-of-scope inputs. Filter them or let the labeller mark "not applicable".
  • Model-specific choices. Examples chosen to help a logistic regression may not be the best ones for a different model trained later on the same labels.

Practice

  1. For a classifier with three classes predicting probabilities (0.48,0.47,0.05)(0.48, 0.47, 0.05) on example A and (0.60,0.20,0.20)(0.60, 0.20, 0.20) on example B, which one does margin sampling pick first?
Answer

Margin sampling compares the top two probabilities. A: 0.48−0.47=0.010.48 - 0.47 = 0.01. B: 0.60−0.20=0.400.60 - 0.20 = 0.40. A has the smaller margin, so it is picked first: the model can barely decide between its first two classes.

  1. Why does the example measure accuracy on 500 test examples that were never in the pool?
Answer

The queried examples are hard cases near the boundary, not a representative sample, so accuracy on them (or on the rest of the pool, which depends on what was queried) would be biased. A fixed, separate test set gives a fair comparison between strategies.

Test yourself

What is the main goal of active learning?

  1. To label every example
  2. To reach good performance with as few labels as possible by choosing which examples to label
  3. To avoid training a model
  4. To learn from rewards

Which examples does uncertainty sampling pick for a binary classifier?

  1. Those with predicted probability near 0 or 1
  2. Those with predicted probability closest to 0.5
  3. Random examples
  4. The oldest examples

In the example, how many labels did uncertainty sampling need to reach 0.833, close to the 0.838 of all 1500 labels?

  1. 20
  2. 60
  3. 100
  4. 1500

Why was random sampling ahead at 20 labels?

  1. Random sampling is always better
  2. With very few labels the model's uncertainty estimates are unreliable (cold start)
  3. The test set changed
  4. Uncertainty sampling used fewer labels