A bank clerk checking the signature on a cheque is not asked to name the customer. The specimen signature is on file, and the only question is whether the two were made by the same hand. That turns out to be the right question for a machine too.
The Handwriting Forensics demo in the Y2K Lab, from my college coursework, is built around it. You register a handful of samples per writer, a Siamese-style network learns each person’s stylistic fingerprint, and a new sample is judged by comparison rather than sorted into a category. Here is the standard technique behind that kind of network.
Why not just classify?
The obvious design is a classifier: one output per writer, highest score wins. It works for a fixed group and breaks the moment the group changes. A new writer has no output, so adding one means changing the network and training it again.
It also cannot say “none of these”. Train it on five writers, show it a sample from a sixth, and it will name one of the five, often confidently, because its scores add up to one and something has to win. And five samples per person cannot teach a network what handwriting is, only those five pictures.
Comparison sidesteps all three. Train one network on a general skill, telling whether two samples share a writer, using as many writers as you can find. After that, registering someone new means storing a few samples, and a sample far from everyone on file can be reported as unknown.
An embedding is an address
The network does one job: it turns a handwriting sample into a short list of numbers, say 32 of them. That list is an embedding, an address in a 32-dimensional space, and the network is trained so that samples by the same hand land close together and samples by different hands land far apart.
Nobody tells it what the numbers mean. A few may track things you could name, like slant or the roundness of loops; most track blends with no name. It does not matter, because only the distance between two addresses is ever used.
“Siamese” describes training. Two samples go through two copies of the network that share every weight, in practice the same network run twice, so a sample gets the same address whichever side it enters on.
Teaching it with pairs and triplets
Training uses pairs. Run two samples through the network and measure the distance between them. If both are by the same writer, the error is that distance: pull them together. If they are by different writers, there is error only while they sit closer than a chosen margin: push them apart until they clear it, then stop. The margin lets the network stop worrying about writers it already separates and spend its effort on the pairs it still confuses.
A common refinement is the triplet: an anchor sample, another by the same writer, and one by someone else. The rule is relative. The same-writer sample must be closer to the anchor than the stranger’s, by at least the margin. That matches how it is used: what matters is which writer on file is nearest.
Which pairs you choose matters more than how many. The pairs that teach are the hard ones: different people with similar hands, and the same person writing slowly and in a hurry.
The comparison, in code
Once the network is trained, identifying a writer is arithmetic. Every registered sample is already an embedding; the new sample becomes one too, and is compared with each writer on file:
// Straight-line distance between two embeddings
function distance(a, b) {
let sum = 0;
for (let i = 0; i < a.length; i++) sum += (a[i] - b[i]) ** 2;
return Math.sqrt(sum);
}
// writers: { name: [embedding, embedding, ...] } from registered samples
// sample: the embedding of the sample to identify
function identify(sample, writers, maxDistance) {
let best = null, bestDist = Infinity;
for (const [name, embeddings] of Object.entries(writers)) {
const d = Math.min(...embeddings.map(e => distance(sample, e)));
if (d < bestDist) { best = name; bestDist = d; }
}
// Too far from everyone on file: say so instead of guessing
return bestDist <= maxDistance ? { writer: best, distance: bestDist }
: { writer: null, distance: bestDist };
}Two decisions hide in there. Measuring against each writer’s nearest sample, as above, copes with a hand that varies; measuring against the average of their embeddings is steadier when one registered sample is odd. And maxDistance is a policy, not a piece of maths. Lower it and the system wrongly accepts fewer samples and wrongly rejects more. Someone has to decide which mistake costs more.
What it cannot tell you
Forgery. A forger copies exactly what an embedding of a scanned sample measures: the shape on the page. Document examiners look for what a copy struggles to hide, such as tremor, hesitation and pen lifts in odd places. A network reading a finished image sees little of that. Systems that record the pen as it moves, its speed and pressure, have more to go on, because they capture how the shape was made.
Small samples. A handful of registered samples shows how someone wrote that day, with that pen. Handwriting shifts with speed, tiredness and the surface underneath. A note scribbled standing at a counter can sit further from its writer’s samples than from a stranger’s, and with three samples on file there is no way to know how much variation is normal for that person.
So read the answer for what it is. Not “this person wrote it”, but “of the writers on file, this one is closest, by this much”. Honest systems built on the idea report it that way, a distance and a threshold, with a person making the final call. The demo is an early take on biometric handwriting analysis, not evidence, and it is most instructive when you try to fool it. Register yourself as one of the writers, then imitate another writer’s style and see how close it lets you get.