POLA: learn a pseudo-metric from "similar" and "different" pairs¶
POLA (Shalev-Shwartz, Singer and Ng, 2004) learns from the simplest feedback: two customers and a
label, similar or different. It keeps a matrix A and a threshold b, predicts that two
customers are similar when their distance is below the threshold, and corrects itself when it is
wrong. The correction has two steps, and the second one, which keeps the matrix a valid distance, is
what makes POLA the most expensive of the three. The other two learners are OASIS and
LEGO; the overview is on Metric learning.
Gamestory¶
London, late at night. The mysterious thief strikes again, but this time he has left confusing traces all over the city.
While analyzing the files on his desk, the young detective realizes that the thief never acts alone: he has a network of accomplices hidden among the citizens. To unmask them, he cannot use a normal yardstick of judgment, because the flashiest details are just traps to throw him off the scent. So he decides to apply the POLA method, a rapid training based on pairwise comparison:
- The "Similar or Different" game: The police informant puts two files on the table at a time. He doesn't reveal who the culprits are, but gives a single blunt clue: "These two suspects work together" or "These two have nothing to do with each other".
- Uncovering the decoy clues: Each file has four pieces of data: the money in the bank account, museum visits, store spending, and age. At first, the detective looks at everything the same way, but he realizes that the money or spending change at random and only cause confusion.
- Adjusting aim with every mistake: Every time the detective is wrong about whether two suspects are accomplices, POLA makes him recalibrate his "investigation ruler". Slowly, he zeroes out the weight of useless details and focuses all his attention on the only clue that really matters: age (the thief only recruits the very young!).
- The capture threshold: At the end of the training, the detective has built a perfect ruler and a safety threshold. When he crosses paths with two new suspects on the street, he measures their "distance" with this new ruler: if they are closer than the threshold, the handcuffs snap shut!

Pair after pair, POLA moves the matrix and the threshold. Left: the squared distance of 200 fresh pairs, on average, over 10 runs (line: median, band: quartiles). The threshold b starts at 1 and rises to about 1.64; the pairs of the same kind stay below it (a median of 1.10 at the end) and the pairs of different kinds climb well above it (5.34). Right: how many times, in 2000 pairs, the correction would have made A lose positive semi-definiteness (a negative eigenvalue), and the projection cut it back to zero: about 500 times. That projection is what makes POLA more expensive than the other two learners.
A first example: similar or different¶
A bank has two kinds of customers, young and older. For each customer it knows four numbers, already put on the same scale: the age, which tells the two kinds apart, and the spending, the balance and the number of visits, which tell nothing about the group. With the plain Euclidean distance the three useless columns weigh three times as much as the useful one. POLA sees 2000 pairs, half of customers of the same kind (+1) and half of different kinds
(-1).
import numpy as np
from dense_armor.core.online.learn.metric_learning import POLA
rng = np.random.default_rng(0)
def customer(young):
return {
"age": rng.normal(-1.0 if young else 1.0, 0.5),
"spend": rng.normal(0, 1),
"balance": rng.normal(0, 1),
"visits": rng.normal(0, 1),
}
def euclid(u, v):
return sum((u[k] - v[k]) ** 2 for k in u) ** 0.5
pola = POLA()
for _ in range(2000):
young = rng.random() < 0.5
if rng.random() < 0.5:
pola.learn_pair(customer(young), customer(young), +1)
else:
pola.learn_pair(customer(young), customer(not young), -1)
print(np.round(np.diag(pola.A), 2), round(pola.b, 2))
hits = {"Euclidean": 0, "POLA": 0}
for _ in range(600):
young = rng.random() < 0.5
a, same, other = customer(young), customer(young), customer(not young)
hits["Euclidean"] += euclid(a, same) < euclid(a, other)
hits["POLA"] += pola.distance(a, same) < pola.distance(a, other)
print({k: round(v / 600, 3) for k, v in hits.items()})
The first line is the diagonal of the matrix A (columns in alphabetical order, the age first: it weighs the most, 0.58) and the threshold
b = 1.78: two customers are called similar when their squared distance is at most b. The second
line is the share of 600 test triplets in which the anchor is closer to the customer of its own kind:
75.8 % with the Euclidean distance, 86.0 % with POLA: with the same 2000 pairs it gets less far than
OASIS and LEGO in this example. POLA moves only when a pair is on the wrong side of the threshold, or
closer to it than the margin of 1.

The pink lines join the points at the same distance from the white customer, in a version of the example with two columns only (the age and the spending): the circles become stripes that follow the age.
How it learns¶
POLA learns a pseudo-metric from a stream of pairs labelled y = +1 (similar) or
y = −1 (different). It keeps a matrix A and a threshold b; a pair is predicted
similar when the squared Mahalanobis distance d = zᵀ A z is below b.
The formula, symbol by symbol¶
For a pair with z = u − v and label y ∈ {+1, −1}:
Then A is projected back onto the PSD cone. If A still has a negative eigenvalue,
it is removed (the paper shows only a single eigenvalue can go negative, so removing one
rank-one direction is enough).
Hand case¶
Start A = I, b = 1, z = (1, 1), label y = +1.
d = [1, 1] · I · [1, 1]ᵀ = 2
ℓ = max(0, 1 · (2 − 1) + 1) = 2
‖z‖⁴ = (√2)⁴ = 4
α = 2 / (1 + 4) = 0.4
A ← I − 1 · 0.4 · [[1, 1], [1, 1]] = [[0.6, −0.4], [−0.4, 0.6]]
b ← 1 + 0.4 = 1.4
The eigenvalues of the new A are 0.2 and 1.0 — both positive, no projection needed.
The pair is similar (y = +1), but its distance, 2, was larger than b − 1 = 0 allows.
The update shrinks A along z: the new distance of the same pair is zᵀ A z = 0.4,
exactly the new b − 1 = 0.4, so the pair now just meets the constraint (a loss of zero).
import numpy as np
from dense_armor.core.online.learn.metric_learning import POLA
learner = POLA()
learner.A = np.eye(2)
u, v = {"x0": 1.0, "x1": 1.0}, {"x0": 0.0, "x1": 0.0}
print(round(learner.distance(u, v) ** 2, 4))
learner.learn_pair(u, v, +1)
print(np.round(learner.A, 4).tolist(), round(float(learner.b), 4))
print(round(learner.distance(u, v) ** 2, 4))
The three lines are the distance before the update, the new A and b, and the distance after it:
the same numbers as the calculation by hand. POLA starts with A = 0; the code sets A = I only
to reproduce this case.
What the paper says¶
These points are from Shalev-Shwartz, Singer and Ng (2004); the section and the page of the PDF are given for each. The hand case above was recomputed with the code of the library:
- The loss is an adaptation of the hinge loss,
ℓ(A, b) = max(0, y (d_A(x, x')² − b) + 1): a pair is predicted dissimilar whend² > b, and the loss is zero only with a margin of 1 on the right side (Section 2, equation 2, PDF page 2). - The algorithm starts with
A = 0, andbany number (the library starts fromb = 1) (Figure 1, PDF page 4). - The update is two projections. First the pair
(A, b)is projected onto the set of pairs with zero loss on the current example: this gives the step = A − y α v vᵀ,b̂ = b + y α, withα = ℓ / (1 + ‖v‖⁴), wherev = x − x'(Section 3.1, equation 4, PDF page 3). Then the result is projected onto the admissible pairs,Apositive semi-definite andb ≥ 1(Section 3.2, PDF page 3). - The projection of
b̂ismax(1, b̂). For a dissimilar pair (y = −1) the step adds a positive semi-definite term, soAstays positive semi-definite; for a similar pair it subtracts one, and the projection ofAonto the positive semi-definite cone removes its negative eigenvalue, of which there is at most one (Section 3.2, PDF pages 3 and 4). The library finds it with an eigendecomposition. - The paper gives a worst-case bound for the online algorithm (Section 4, from PDF page 5) and a way to turn it into a batch one (Section 6, PDF page 6).
Results¶
On small dense data (30 % train, 70 % test, standardised features, k = 3; numbers taken from the page published in Dense-Evolution-Discovery, not re-run here):
| Dataset | Features | Euclidean | POLA |
|---|---|---|---|
| Iris | 4 | 0.9333 | 0.9524 (+1.9) |
| Wine | 13 | 0.9200 | 0.9520 (+3.2) |
| Digits | 64 | 0.9459 | 0.9658 (+2.0) |
POLA is best or tied for best on Wine and Digits and is never below the baseline in these three datasets, at the cost of an eigendecomposition for every similar pair (Digits was trained on 20,000 pairs for POLA, against 50,000 for the others, for that reason).
The parameter¶
| Parameter | Meaning | Default |
|---|---|---|
b_init |
the starting threshold; a pair is called similar when its squared distance is at most b |
1.0 |
A whole table: there is no fit¶
POLA has no fit. fit reads a table row by row through learn_one, and POLA learns from pairs with a label, not from rows: calling POLA().fit(X, y) raises TypeError: POLA has no learn_one. A table is learned with a loop over learn_pair, one line of the table at a time. Here a robot has logged runs, stable or shaky, with a speed, a load and two columns of noise, and each line of the table is a run, a second one and a label: +1 for two runs of the same kind, -1 for two of different kinds.
import numpy as np
from dense_armor.core.online.learn.metric_learning import POLA
rng = np.random.default_rng(0)
def run(stable):
return {
"speed": rng.normal(1.0 if stable else -1.0, 0.5),
"load": rng.normal(-1.0 if stable else 1.0, 0.5),
"noise1": rng.normal(0, 3),
"noise2": rng.normal(0, 3),
}
table = []
for _ in range(2000):
sa, sb = rng.random(2) < 0.5
a, b = run(sa), run(sb)
table.append((a, b, 1 if sa == sb else -1))
m = POLA()
for row in table:
m.learn_pair(*row)
plain = learned = 0
for _ in range(600):
s = rng.random() < 0.5
a, same, other = run(s), run(s), run(not s)
plain += sum((a[k] - same[k]) ** 2 for k in a) < sum((a[k] - other[k]) ** 2 for k in a)
learned += m.distance(a, same) < m.distance(a, other)
print(round(plain / 600, 3), round(learned / 600, 3))
The two numbers are the share of 600 new triplets in which a run is closer to one of its own kind than to one of the other kind: 62.2 % with the plain distance, 98.5 % after the 2000 lines of the table.
API reference¶
POLA ¶
Bases: MetricLearner
Online pseudo-metric learning with PSD projection.
Learns (A, b) from labelled pairs (x, x2, y) with y = +1 for similar
and y = -1 for dissimilar. After every update, A is projected onto the
positive semi-definite cone and b onto [1, +inf).
The update is the online algorithm POLA of Shalev-Shwartz, Singer and Ng
(2004), Figure 1, PDF page 4: the loss of Section 2 (PDF page 2), the step
of Section 3.1, equation 4 (PDF page 3), and the projection of Section 3.2
(PDF pages 3 and 4). Differences of this implementation: the threshold
starts at b_init (default 1.0), where the paper allows any b; the
negative eigenvalue of A is found with a full eigendecomposition
(numpy.linalg.eigh), where the paper uses the Lanczos method; a label
y > 0 is read as +1 and any other as -1; distance returns the
square root of max(d², 1e-12).
Examples:
>>> from dense_armor.core.online.learn.metric_learning import POLA
>>> pola = POLA()
>>> a = {"x": 0.0}
>>> b = {"x": 1.0}
>>> _ = pola.learn_pair(a, b, +1)
>>> _ = pola.learn_pair(a, b, -1)
>>> pola.b >= 1.0
True
References
[^1]: Shalev-Shwartz, S., Singer, Y. and Ng, A.Y., 2004. Online and batch learning of pseudo-metrics. In ICML. Algorithm: Figure 1, PDF page 4; equation 2, PDF page 2; equation 4, PDF page 3.
Source code in dense_armor/core/online/learn/metric_learning.py
Details¶
learn_pair(x, x2, y)takes two dicts andy: any value above 0 is read as+1, any other as−1.- Because
Astarts at 0, the distance of every pair is 0 until the first corrections;distancereturns the square root ofmax(zᵀ A z, 1e-12). - Source: Shalev-Shwartz, S., Singer, Y., Ng, A. Y. (2004), "Online and batch learning of pseudo-metrics", ICML. Equation 2: PDF page 2; equation 4: PDF page 3; Figure 1: PDF page 4.