title: How to write a conditional custom loss function for a Keras model
body: <p>I am trying to build a model that can do landmark detection for Dog Faces - if a dog face is present, extract the x/y coordinates of the eyes and the nose, otherwise tell me that there's no dog face.</p>
  
  <p>I would like my output tensor to be of size 7:</p>
  
  <ul>
  <li>output[0] - be the probability that a dog face is present or not</li>
  <li>output[1]/output[2] - x/y of the left eye</li>
  <li>output[3]/output[4] - x/y of the right eye</li>
  <li>output[5]/output[6] - x/y of the nose</li>
  </ul>
  
  <p>I can't figure out how to write a custom loss function that would go something like this:</p>
  
  <pre class="lang-py prettyprint-override"><code># This is largely pseudo-code!
  def custom_l1_loss(y_true, y_pred):
    if y_true[0] == 1:
      # Probability that there's a dog face = 1
      return K.sum(K.abs(y_pred[1:] - y_true[1:]), axis=-1)
    else:
      # Only return the difference in probabilities that a dog face is present
      return y_true[0] - y_pred[0]
  </code></pre>
  
  <p><strong>Question</strong>: Has anybody had experience with writing a similar conditional loss function in Keras?</p>