How do I implement the Triplet Loss in Keras?
<p>I'm trying to implement Google's Facenet paper:</p>

<p><img src="http://i.stack.imgur.com/RT3TZ.png" alt="enter image description here"></p>

<p>First of all, is it possible to implement this paper using the Sequential API of Keras or should I go for the Graph API? </p>

<p>In either case, could you please tell me how do I pass the custom loss function <code>tripletLoss</code> to the model compile and how do I receive the <code>anchor embedding</code>, <code>positive embedding</code> and the <code>negative embedding</code> as parameters to calculate the loss? </p>

<p>Also, what should be the second parameter Y in model.fit(), I do not have any in this case...</p>

<p><a href="https://github.com/fchollet/keras/issues/369" rel="nofollow">This issue</a> explains how to create a custom objective (loss) in Keras:</p>

<pre><code>def dummy_objective(y_true, y_pred):
    return 0.5  # your implem of tripletLoss here

model.compile(loss=dummy_objective, optimizer='adadelta')
</code></pre>

<p>Regarding the <code>y</code> parameter of <code>.fit()</code>, since you are the one handling it in the end (the <code>y_true</code> parameter of the objective function is taken from it), I would say you can pass whatever you need that can fit through Keras plumbing. And maybe a dummy vector to pass dimension checks if your really don't need any supervision.</p>

<p>Eventually, as to how to implement this particular paper, looking for <code>triplet</code> or <code>facenet</code> in <a href="https://keras.io/" rel="nofollow">Keras doc</a> didn't return anything. So you'll probably have to either implement it yourself or find someone who has.</p>

python keras