title: Make a custom loss function in keras
body: <p>Hi I have been trying to make a custom loss function in keras for dice_error_coefficient. It has its implementations in <strong>tensorboard</strong> and I tried using the same function in keras with tensorflow but it keeps returning a <strong>NoneType</strong> when I used <strong>model.train_on_batch</strong> or <strong>model.fit</strong> where as it gives proper values when used in metrics in the model. Can please someone help me out with what should i do? I have tried following libraries like Keras-FCN by ahundt where he has used custom loss functions but none of it seems to work. The target and output in the code are y_true and y_pred respectively as used in the losses.py file in keras.</p>
  
  <pre><code>def dice_hard_coe(target, output, threshold=0.5, axis=[1,2], smooth=1e-5):
      """References
      -----------
      - `Wiki-Dice &lt;https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient&gt;`_
      """
  
      output = tf.cast(output &gt; threshold, dtype=tf.float32)
      target = tf.cast(target &gt; threshold, dtype=tf.float32)
      inse = tf.reduce_sum(tf.multiply(output, target), axis=axis)
      l = tf.reduce_sum(output, axis=axis)
      r = tf.reduce_sum(target, axis=axis)
      hard_dice = (2. * inse + smooth) / (l + r + smooth)
      hard_dice = tf.reduce_mean(hard_dice)
      return hard_dice
  </code></pre>