The triplet loss is defined as follows:
$$ L(A, P, N) = max(‖f(A) - f(P)‖² - ‖f(A) - f(N)‖² + margin, 0) $$
where $A$=anchor, $P$=positive, and $N$=negative are the data samples in the loss, and $margin$ is the minimum distance between the anchor and positive/negative samples.
I read somewhere that $(1 - cosine\_similarity)$ may be used instead of the $L2\ distance$.
Note that I am using Tensorflow
- and the cosine similarity loss is defined that When it is a negative number between -1 and 0, 0 indicates orthogonality and values closer to -1 indicate greater similarity. The values closer to 1 indicate greater dissimilarity. So, it is the opposite of cosine similarity metric.
Another resource I found is the cosine similarity layer here, but it is not a triplet loss.
Any suggestions on how to write my triplet loss with cosine similarity?
Edit
I am having some luck with this where I see the loss function go down
loss = (1 - an_distance) + tf.maximum(ap_distance + self.margin, 0.0)
where ap_distance
and an_distance
are the cosine similarity loss (not metric - so the measure is reversed). So I wonder if the terms should be flipped.