# Can siamese model trained with euclidean distance as distance metric use cosine similarity during inference?

If I have 3 embeddings Anchor, Positive, Negative from a Siamese model trained with Euclidean distance as distance metric for triplet loss.

During inference can cosine similarity similarity be used?

I have noticed if I calculate Euclidean distance with model from A, P, N results seem somewhat consistent with matching images getting smaller distance and non-matching images getting bigger distance in most cases.

In case I use cosine similarity on above embeddings I am unable to differentiate as similarity values between (A, P) and (A, N) seem almost equal or for different images one value seem higher vice versa.

Triplets were selected at random with no online hard, semi hard mining.

Wondering if I made mistake somewhere in implementation or the distance function in inference time should be same.

• Can you elaborate what you mean by "or for different images one value seem higher vice versa."? Feb 23 at 9:56
• Both for positive and negative image the values almost all around 0.9+. For example positive example 0.995, another 0.981 and for a negative image it can be 0.997, another 0.972. But calculating Euclidean distance similarity like, 1 /(1 + euclidean_dist(embedding1, embedding2) ) the positive images get expected higher values and negatives lower in most cases. Feb 23 at 12:00

## 2 Answers

The distance function used at inference time should be the same as used for training. The distance function affects the way the embeddings are learned, and determining similarity at inference time will be using this same embedding space.

You can train your network with Cosine Embedding Loss if you think that will represent your data better than Euclidean distance.

• I will accept your answer, but need additional details. I need to know Euclidean distance and cosine distance have different embedding space. In following example, for triplet loss the distance between two embedding was calculated using Euclidean distance. Then at inference time cosine similarity was used to measure similarity which seems to have worked. keras.io/examples/vision/siamese_network Feb 21 at 15:40

Cosine distance isn't a true metric, it violates the identity of indiscernibility, since the cosine distance doesn't much care about the magnitude of the vectors in question.

Cosine distance has more interpretability than Euclidean distance, since cosine is bounded on $$[-1,1]$$, but needs to be applied with caution.