title: Implementing accuracy for triplet loss in keras
body: <p>I want to implement an accuracy function for a triplet loss network so that I know, how does the algorithm works during the training. So far I have tried something, but I'm not sure whether it actually can work and also I have troubles implementing it in keras. My idea was to compare the predicted anchor-positive and anchor-negative distances (in <code>y_pred</code>), so that the positive distance should be low enough and the negative one large enough:</p>
  
  <pre><code>def accuracy(_, y_pred):
      pos_treshold = 0.4
      neg_treshold = 0.6
      return K.mean(y_pred[0] &lt; pos_treshold and y_pred[1] &gt; neg_treshold)
  </code></pre>
  
  <p>The problem with this is that I couldn't figure out how to implement this <code>and</code> condition in keras. </p>
  
  <p>Then I tried to find something on this topic of accuracy for triplet loss. One way of doing it is to define the accuracy as a proportion of the number of triplets in which the predicted distance between the anchor image and the positive image is less than the one between the anchor image and the negative image. With this I have even bigger problems in implementing it in keras. </p>
  
  <p>I tried this (although I don't know whether it does what I described):</p>
  
  <pre><code>K.mean(y_pred[0] &lt; y_pred[1])
  </code></pre>
  
  <p>which gives me accuracy around 0.5 all the time (probably some random stuff). So still I don't know whether the model is bad or the accuracy function is bad.  </p>
  
  <p>So my question is how to implement any reasonable accuracy function in keras? Whether it would be one of these two I don't really care.</p>