title: Implementing Triplet Loss inside Keras Layers
body: <p>In this blog <a href="https://towardsdatascience.com/lossless-triplet-loss-7e932f990b24" rel="nofollow noreferrer">post</a>, he implements the triple loss outside the Kears layers. He gets the <code>anchor_out</code>, <code>pos_out</code> and <code>neg_out</code> from the network and then passes them to the <code>triplet_loss()</code> function he defined.</p>
  
  <p>I wonder if I can calculate the triplet_loss within the Keras layers by defining my own <code>Lambda</code> layers.</p>
  
  <p>Here's my network design:</p>
  
  <pre><code>margin=1
  
  anchor_input = Input((600, ), name='anchor')
  positive_input = Input((600, ), name='positive_input')
  negative_input = Input((600, ), name='negative_input')
  
  # Shared embedding layer for positive and negative items
  Shared_DNN = Dense(300)
  
  encoded_anchor = Shared_DNN(anchor_input)
  encoded_positive = Shared_DNN(positive_input)
  encoded_negative = Shared_DNN(negative_input)
  
  DAP = Lambda(lambda tensors:K.sum(K.square(tensors[0] - tensors[1]),axis=1,keepdims=True),name='DAP_loss') #Distance for Anchor-Positive pair
  DAN = Lambda(lambda tensors:K.sum(K.square(tensors[0] - tensors[1]),axis=1,keepdims=True),name='DAN_loss') #Distance for Anchor-Negative pair
  Triplet_loss = Lambda(lambda loss:K.max([(loss[0] - loss[1] + margin),0],axis=0),name='Triplet_loss') #Distance for Anchor-Negative pair
  
  DAP_loss = DAP([encoded_anchor,encoded_positive])
  DAN_loss = DAN([encoded_anchor,encoded_negative])
  
  #call this layer on list of two input tensors.
  
  Final_loss = Triplet_loss([DAP_loss,DAN_loss])
  
  model = Model(inputs=[anchor_input,positive_input, negative_input], outputs=Final_loss)
  </code></pre>
  
  <p>However, it gives me the error:</p>
  
  <pre><code>Tried to convert 'input' to a tensor and failed. Error: Shapes must be equal rank, but are 2 and 0
      From merging shape 0 with other shapes. for 'Triplet_loss_4/Max/packed' (op: 'Pack') with input shapes: [?,1], []
  </code></pre>
  
  <p>The error is from the <code>Triplet_loss</code> layer. In the <code>K.max()</code> function, the first number <code>loss[0] - loss[1] + margin</code> has the shape <code>(None,1)</code>. Yet the second number 0 has the shape <code>(1)</code>. The two number are not of the same shape and therefore the <code>K.max()</code> function gives out an error.</p>
  
  <p>My problem is, how to solve this error? 
  I have tried replacing the <code>0</code> with <code>K.constant(0,shape=(1,))</code> and <code>K.constant(0,shape=(None,1))</code>, but they doesn't work.</p>