2-Architecture

Understanding Siamese Networks in NLP (with Example Architecture

Sections

What is a Siamese Network?
Example: Siamese Network for Sentence Similarity
How Cosine Similarity Works Here
Step-by-Step Process
Why Use Identical Subnetworks?
What’s Next?

What is a Siamese Network?

A Siamese network is a special type of neural network architecture designed to compare two inputs and determine how similar they are.

Instead of using two completely different models, a Siamese network has two identical subnetworks that share the same weights and parameters. You can think of them as twin networks or sister networks — both process different inputs, but in exactly the same way.

The outputs of these subnetworks are then compared to produce a similarity score.

Example: Siamese Network for Sentence Similarity

Let’s look at an example where we want to check if two questions mean the same thing.

  1. Inputs: Question 1 and Question 2.

  2. Embedding: Each question is transformed into an embedding (a numeric vector).

  3. LSTM Processing: Each embedding is passed through an LSTM layer to capture the meaning of the question.

  4. Output Vectors: Each LSTM produces a vector representation.

  5. Cosine Similarity: The two output vectors are compared using cosine similarity.

Siamese networks have a special type of architecture. They have two identical subnetworks which are merged together to produce a final output or a similarity score. I like to think of these two subnetworks as sister networks which come together to produce a similarity score. >> Speaker 2: This is a model architecture for a Siamese network. Note that the architecture presented here is just an example. Not all Siamese networks will be designed to contain LSTMs. On the left, you have two inputs which represent question 1 and question 2. You'll take each question, transform it into an embedding, and then you'll run the embedding through an LSTM layer to model the question's meaning. Each LSTM outputs a vector. So in this architecture, you have two identical subnetworks, one for question 1 and a second for question 2. An important note here is that the subnetworks share identical parameters. That is, the learned parameters of each subnetwork are exactly the same. So you actually only need to train one set of weights, not two.

How Cosine Similarity Works Here

Cosine similarity is a measure of how close two vectors are in direction:

  • If the vectors point in the same direction, cosine similarity is close to 1 → questions are similar.

  • If they point in opposite directions, the similarity is close to -1 → questions are different.

  • A threshold (τ) is chosen to decide when two questions should be considered "the same."

👉 Example: If cosine similarity > τ → label as similar. Otherwise → label as different.

Then, given the two outputs vectors, one corresponding to each question, find their cosine similarity. Recall that the cosine similarity is a measure of similarity between two vectors. When two vectors point generally in the same direction, the cosine of the angle between them is near 1. And for vectors that point in opposite directions, the cosine of the angle between them is -1. If that sounds unfamiliar, don't worry. Right now you just need to know that the cosine similarity tells you how similar two vectors are. And in this case, it tells you how similar the two questions are. So the cosine similarity gives the Siamese network's prediction, denoted here by the variable y hat, which will be a value between -1 and positive 1. If y hat is less than or equal to some threshold tau, then you will say that the input questions are different. And if y hat is greater than tau, then you will say that they are the same. The threshold tau is a parameter, that you'll choose based on how often you want to interpret cosine similarity to indicate that two questions are similar or not. A higher threshold means that only very similar sentences will be considered similar.

Step-by-Step Process

To summarize, a Siamese network for question similarity works like this:

  1. Feed each question into its own subnetwork.

  2. Transform each into embeddings.

  3. Pass embeddings through an LSTM.

  4. Get output vectors.

  5. Compare vectors using cosine similarity.

  6. Apply a threshold τ to decide if they’re similar or different.

If you think of this process as a series of steps you take to get from your input to your output, it would go something like this. You start with the model architecture for a Siamese network, made up of two identical subnetworks. In this case, your inputs are questions that you feed into each subnetwork. And each question gets transformed into an embedding, and passed through an LSTM layer. Then you take the outputs of each of the subnetworks and compare them using cosine similarity to get your y hat. >> Speaker 1: After seeing the model architecture, I'll start talking about different cost functions you can use for this type of architecture.

Why Use Identical Subnetworks?

The key idea is parameter sharing. Since both subnetworks are identical:

  • You only train one set of weights.

  • The model processes both inputs in the same way.

  • This makes training more efficient and ensures fairness when comparing inputs.

What’s Next?

Once the architecture is set, the next step is choosing the right loss function. Common choices include:

  • Contrastive loss (used for pairwise similarity).

  • Triplet loss (used when comparing anchor, positive, and negative examples).

We’ll cover these cost functions in a follow-up post.

Last modified: Sunday, 28 September 2025, 10:05 AM