Siamese Networks Architecture
You'll learn: How Siamese networks use twin neural networks with shared weights to learn whether two inputs are similar or different.
Core Idea
Imagine two identical twins evaluating two items—they use the exact same criteria because they think alike. A Siamese network works similarly: it consists of two (or more) identical neural networks that share the same weights. Each network processes a different input, and the outputs are compared to determine if the inputs are similar.
Instead of learning to classify inputs directly, Siamese networks learn a similarity function. You feed in pairs of examples—like two images or two sentences—and the network learns to map them into an embedding space where similar items are close together and dissimilar items are far apart.
How It Works
The architecture has three key components:
- Twin Networks: Two (or more) identical networks with shared weights
- Embedding Function: Each network transforms its input into a feature vector
- Comparison Function: A distance metric (like Euclidean distance or cosine similarity) measures how close the embeddings are
During training, you show the network pairs labeled as "similar" or "different." The network adjusts its shared weights to minimize distance for similar pairs and maximize it for dissimilar ones.
Why Shared Weights Matter
The shared weights are crucial—they ensure both networks learn the same representation function. If the networks had different weights, they'd be learning incompatible embedding spaces, making comparison meaningless.
Key Takeaway: Siamese networks learn similarity by processing paired inputs through twin networks with shared weights, mapping similar examples to nearby points in embedding space—perfect for few-shot learning where you need to compare new examples to limited reference samples.