3-Cost Function
Understanding Triplet Loss in Siamese Networks: A Beginner-Friendly Guide
🔍 What Is a Siamese Network?
A Siamese network is a special type of neural network designed to compare two or more inputs — for example, checking if two questions mean the same thing, or if two faces belong to the same person.
Instead of predicting a label directly (like “spam” or “not spam”), a Siamese network outputs feature embeddings — numerical representations of each input — and then measures how similar or different they are.
💡 The Idea Behind Triplet Loss
To train a Siamese network effectively, we use a triplet loss function.
This loss helps the model learn that:
-
Similar items (like “How old are you?” and “What is your age?”) should have high similarity scores.
-
Different items (like “How old are you?” and “Where are you from?”) should have low similarity scores.

I'm going to show you how the cost function for a Siamese network works. This is known as the triplet loss. Let's dive in, I'll now show you a simple loss function you can use in your Siamese network. Just as a recap, this is the overall structure of the Siamese network which enables you to predict whether two questions are similar or different. With the outputs of the network, you are able to calculate Y hat, which is the similarity between the two questions.
🧩 Anchor, Positive, and Negative Samples
Each training example in triplet loss consists of three inputs:
-
Anchor (A): The main reference input (e.g., “How old are you?”)
-
Positive (P): A similar input (e.g., “What is your age?”)
-
Negative (N): A dissimilar input (e.g., “Where are you from?”)
The goal is simple:
The network should learn to bring A and P closer together in feature space, and push A and N farther apart.
Now I'll show you a loss function for a Siamese network. I'll start by looking at this first question, which is how old are you? I'll call this first question the anchor, which I'm going to use to compare against two other questions relative to the anchor. Other questions that have the same meaning as the anchor are called positive questions. Whereas questions that do not have the same meaning as the anchor are called negative questions. Note that the meaning of positive and negative in the context of finding question duplicate is referring to whether a question is similar to the anchor or not. Or not whether it has a positive or negative sentiment. The question, what is your age is considered a positive question relative to the anchor because how old are you and what is your age mean the same thing. This other question, where are you from, is considered a negative question because it does not have the same meaning as the anchor question.
🔢 Cosine Similarity in Siamese Networks
The similarity between two vectors is often measured using cosine similarity, defined as:
Cosine similarity ranges from -1 to +1:
-
+1→ the vectors are very similar -
-1→ the vectors are very different

Here's a definition of cosine similarity between two vectors. That will be the similarity of function S. To train your model, you'll be comparing the vectors that are output by each sub network using similarity. For this example, you're going to take the similarity between A and P, where a refers to the anchor question and P refers to the positive question. Similarity is bounded between negative one and one. For vectors that are completely different, the similarity is near negative one. For vectors that are nearly identical, their similarity is close to positive one. For a well trained model, you'd like to see a similarity close to one when comparing the anchor and a positive example. Similarly, when comparing the anchor to the negative example, a successful model should yield a similarity close to negative one. To begin building a loss function, you start with the similarity of A and N and subtract the similarity of A and B to calculate the difference. What you have here is a loss function that allows you to determine whether your model is roughly doing what you hope it will do. Namely, finding that the anchor and the positive example are similar, and that the anchor and the negative example are different. As the difference gets bigger or smaller along the X axis, the loss gets bigger or smaller along the Y axis. When minimizing the loss in training, you are, in effect, minimizing the difference. In the next video, I'll introduce triplets.
⚙️ Triplet Loss Function
The triplet loss is typically written as:
Here’s what it does:
-
Encourages A and P to be close (high similarity)
-
Encourages A and N to be apart (low similarity)
-
The margin prevents trivial solutions where all vectors collapse together
In short, minimizing this loss helps the network understand what “similar” actually means in your data.
🧭 Key Takeaways
-
Triplet loss helps models learn relative similarity.
-
It uses anchor, positive, and negative samples to guide training.
-
Cosine similarity measures how close two embeddings are.
-
Lower loss means the network is learning meaningful representations.
