Computing Library › Neural Architectures
Neural Architectures

Siamese Networks

Siamese networks run two inputs through identical weight-sharing towers and compare their embeddings to judge similarity.

Twin towers, shared weights

A Siamese network processes two inputs through two identical subnetworks that share the same weights, producing an embedding for each. A distance or similarity between the two embeddings then indicates how alike the inputs are. Because the towers share weights, both inputs are mapped by the same function into a common space, so their embeddings are directly comparable. The architecture is built to answer 'are these two things the same?' rather than 'what is this?'

Why weight sharing matters

Sharing weights guarantees symmetry: swapping the two inputs must not change the judged similarity, and both inputs are embedded consistently. It also halves the parameters relative to two independent networks and ensures that whatever features one tower learns, the other applies identically. This shared mapping is the defining property that makes the comparison meaningful.

Training objectives

Siamese networks are trained with objectives that shape distances. Contrastive loss operates on pairs: minimize distance for similar pairs, and push dissimilar pairs apart beyond a margin. Triplet loss operates on an anchor, a positive, and a negative, requiring the anchor-positive distance to be smaller than the anchor-negative distance by a margin. Mining informative (hard) triplets is important for effective training.

python

# triplet loss with margin m
# d_pos = dist(f(anchor), f(positive))
# d_neg = dist(f(anchor), f(negative))
# loss = max(0, d_pos - d_neg + m)

One-shot and few-shot recognition

A key strength is recognizing classes with very few examples. Once trained to judge similarity, a Siamese network can classify a new item by comparing it to a single stored reference per class, no retraining needed. This suits face verification, signature matching, and any setting where classes are numerous, change often, or have scarce examples, situations where a fixed-output classifier would need constant retraining.

Applications and relation to contrastive learning

Siamese networks power face verification, signature and fingerprint matching, duplicate detection, and similarity search. The architecture is closely related to contrastive learning: both learn embeddings where distance reflects similarity, and both often use paired or triplet supervision. Contrastive self-supervised methods can be seen as Siamese networks trained on augmented views without labels. In technical settings, similarity models can match new measurements against a library of known reference cases.