Most loss functions calculate the difference between an expected target value and a computed predicted value. For example, if a machine learning regression model predicts a person’s age to be 27.0 years but the person’s actual age is 25.0 years, then mean squared error loss is (25 – 27)^2 = 4.0.
Triplet loss is an unusual loss function. Instead of supplying two values and then computing some sort of difference between them, you supply three values: an “anchor”, a “positive” that’s supposed to be the same as the anchor, and a “negative” that’s supposed to be much different from the anchor. There’s also a constant called a margin. Most neural network libraries have a built-in triplet loss function.
You compute the distance between anchor and positive — d(a,p) — and the distance between the anchor and the negative — d(n,p) — and specify a margin, typically 1.0. The triplet loss is:
triplet_loss = d(a,p) – d(a,n) + margin
If this value is 0.0 or larger then you’re done, but if the equation gives a negative value you return 0.0.
The d(a,p) is the main term and corresponds to a normal loss function. The d(a,n) is like reverse error because the larger it is, the lower the error. The margin adds a small value to the error to counteract very large values of d(a,n) that would lower the loss. And because loss should never be less than 0, you ensure that.
An example where triplet loss might be used is people’s handwritten signature verification. A bank might have one reference/anchor signature for each customer. If you want to train a system to verify that a customer’s signature is valid, you could use triplet loss where you use the refence/anchor signature, a second signature from the customer, and a third signature from a completely different customer.
The triplet loss is used mostly with Siamese neural networks and one-shot neural learning. Those are topics for another post.

The use of the triplet loss function is quite rare. Identical triplet actor and actresses are very, very rare. A search of the Internet turned up only three sets of identical triplets. Left: Nicole, Erica, and Jaclyn Dahm were born in 1977. Center: Joelle, Jade, and Mariah Tang were born in 1990, Right: Leanna, Joy, and Monica Creel were born in 1970.


.NET Test Automation Recipes
Software Testing
SciPy Programming Succinctly
Keras Succinctly
R Programming
Visual Studio Live
Microsoft MLADS Conference
DevIntersection Conference
Machine Learning Week
Ai4 Conference
G2E Conference
iSC West Conference
You must be logged in to post a comment.