1
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Can you elaborate what you mean by "or for different images one value seem higher vice versa."? $\endgroup$
    – Jonathan
    Commented Feb 23, 2022 at 9:56
  • $\begingroup$ 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. $\endgroup$
    – B200011011
    Commented Feb 23, 2022 at 12:00

2 Answers 2

0
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ 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 $\endgroup$
    – B200011011
    Commented Feb 21, 2022 at 15:40
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.